-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
drivers:adc-dac:ad5592r: Driver Enhancement #2414
Open
D-Disha
wants to merge
1
commit into
analogdevicesinc:main
Choose a base branch
from
D-Disha:ad559xr-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+353
−54
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* @brief Implementation of AD5592R Base Driver. | ||
* @author Mircea Caprioru ([email protected]) | ||
******************************************************************************** | ||
* Copyright 2018, 2020(c) Analog Devices, Inc. | ||
* Copyright 2018, 2020, 2025(c) Analog Devices, Inc. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
|
@@ -320,3 +320,174 @@ int32_t ad5592r_reset_channel_modes(struct ad5592r_dev *dev) | |
|
||
return ad5592r_set_channel_modes(dev); | ||
} | ||
|
||
/** | ||
* Register update | ||
* | ||
* @param dev - The device structure. | ||
* @param reg_addr - The Register address | ||
* @param data - The data to be written | ||
* @param mask - The mask | ||
* @return 0 in case of success, negative error code otherwise | ||
*/ | ||
int32_t ad5592r_base_reg_update(struct ad5592r_dev *dev, uint16_t reg_addr, | ||
uint16_t data, uint16_t mask) | ||
{ | ||
|
||
uint16_t temp_reg_val; | ||
int32_t ret; | ||
|
||
if (!dev) | ||
return -1; | ||
|
||
ret = ad5592r_base_reg_read(dev, reg_addr, &temp_reg_val); | ||
if (ret < 0) | ||
return ret; | ||
|
||
temp_reg_val &= ~mask; | ||
temp_reg_val |= data; | ||
|
||
ret = ad5592r_base_reg_write(dev, reg_addr, temp_reg_val); | ||
if (ret < 0) | ||
return ret; | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Set ADC Range of the device | ||
* | ||
* @param dev - The device structure. | ||
* @param adc_range - ADC Range | ||
* @return 0 in case of success, negative error code otherwise | ||
*/ | ||
int32_t ad5592r_set_adc_range(struct ad5592r_dev *dev, | ||
enum ad559xr_range adc_range) | ||
{ | ||
int32_t ret; | ||
uint16_t status = 0; | ||
|
||
if (!dev) | ||
return -1; | ||
|
||
status = adc_range ? AD5592R_REG_CTRL_ADC_RANGE : 0; | ||
|
||
ret = ad5592r_base_reg_update(dev, AD5592R_REG_CTRL, status, | ||
AD5592R_REG_CTRL_ADC_RANGE); | ||
if (ret < 0) | ||
return ret; | ||
|
||
dev->adc_range = adc_range; | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Set DAC Range of the device | ||
* | ||
* @param dev - The device structure. | ||
* @param dac_range - DAC Range | ||
* @return 0 in case of success, negative error code otherwise | ||
*/ | ||
int32_t ad5592r_set_dac_range(struct ad5592r_dev *dev, | ||
enum ad559xr_range dac_range) | ||
{ | ||
int32_t ret; | ||
uint16_t status = 0; | ||
|
||
if (!dev) | ||
return -1; | ||
|
||
status = dac_range ? AD5592R_REG_CTRL_DAC_RANGE : 0; | ||
|
||
ret = ad5592r_base_reg_update(dev, AD5592R_REG_CTRL, status, | ||
AD5592R_REG_CTRL_DAC_RANGE); | ||
if (ret < 0) | ||
return ret; | ||
|
||
dev->dac_range = dac_range; | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Set Power Down DAC Channel of the device | ||
* | ||
* @param dev - The device structure. | ||
* @param chan - The channel number. | ||
* @param enable - Status to enable/disable power down. | ||
* @return 0 in case of success, negative error code otherwise | ||
*/ | ||
int32_t ad5592r_power_down(struct ad5592r_dev *dev, uint8_t chan, bool enable) | ||
{ | ||
int ret; | ||
uint16_t temp_reg_val = 0; | ||
|
||
if (!dev) | ||
return -1; | ||
|
||
temp_reg_val = enable ? NO_OS_BIT(chan) : 0; | ||
|
||
ret = ad5592r_base_reg_update(dev, AD5592R_REG_PD, temp_reg_val, | ||
NO_OS_BIT(chan)); | ||
if (ret < 0) | ||
return ret; | ||
|
||
dev->power_down[chan] = enable; | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Set Reference Select option for the device | ||
* | ||
* @param dev - The device structure. | ||
* @param enable - Status to enable/disable internal reference. | ||
* @return 0 in case of success, negative error code otherwise | ||
*/ | ||
int32_t ad5592r_set_int_ref(struct ad5592r_dev *dev, bool enable) | ||
{ | ||
uint16_t temp_reg_val = 0; | ||
int ret; | ||
|
||
if (!dev) | ||
return -1; | ||
|
||
temp_reg_val = enable ? AD5592R_REG_PD_EN_REF : 0; | ||
|
||
ret = ad5592r_base_reg_update(dev, AD5592R_REG_PD, temp_reg_val, | ||
AD5592R_REG_PD_EN_REF); | ||
if (ret < 0) | ||
return ret; | ||
|
||
dev->int_ref = enable; | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Set ADC Buffer for the device | ||
* | ||
* @param dev - The device structure. | ||
* @param enable - Status to enable/disable adc buffer. | ||
* @return 0 in case of success, negative error code otherwise | ||
*/ | ||
int32_t ad5592r_set_adc_buffer(struct ad5592r_dev *dev, bool enable) | ||
{ | ||
uint16_t temp_reg_val = 0; | ||
int ret; | ||
|
||
if (!dev) | ||
return -1; | ||
|
||
temp_reg_val = enable ? AD5592R_REG_CTRL_ADC_BUFF_EN : 0; | ||
|
||
ret = ad5592r_base_reg_update(dev, AD5592R_REG_CTRL, temp_reg_val, | ||
AD5592R_REG_CTRL_ADC_BUFF_EN); | ||
if (ret < 0) | ||
return ret; | ||
|
||
dev->adc_buf = enable; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* @brief Header file of AD5592R Base Driver. | ||
* @author Mircea Caprioru ([email protected]) | ||
******************************************************************************** | ||
* Copyright 2018, 2020(c) Analog Devices, Inc. | ||
* Copyright 2018, 2020, 2025(c) Analog Devices, Inc. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
|
@@ -38,6 +38,7 @@ | |
#include "no_os_spi.h" | ||
#include "no_os_i2c.h" | ||
#include "no_os_util.h" | ||
#include "no_os_alloc.h" | ||
#include <stdbool.h> | ||
|
||
#define CH_MODE_UNUSED 0 | ||
|
@@ -92,6 +93,8 @@ enum ad5592r_registers { | |
|
||
#define INTERNAL_VREF_VOLTAGE 2.5 | ||
|
||
#define NUM_OF_CHANNELS 8 | ||
|
||
struct ad5592r_dev; | ||
|
||
struct ad5592r_rw_ops { | ||
|
@@ -108,8 +111,21 @@ struct ad5592r_rw_ops { | |
int32_t (*gpio_read)(struct ad5592r_dev *dev, uint8_t *value); | ||
}; | ||
|
||
enum ad559xr_range { | ||
ZERO_TO_VREF, | ||
ZERO_TO_2VREF | ||
}; | ||
|
||
struct ad5592r_init_param { | ||
bool int_ref; | ||
struct no_os_spi_init_param *spi_init; | ||
struct no_os_i2c_init_param *i2c_init; | ||
uint8_t channel_modes[8]; | ||
uint8_t channel_offstate[8]; | ||
enum ad559xr_range adc_range; | ||
enum ad559xr_range dac_range; | ||
bool adc_buf; | ||
uint8_t power_down[8]; | ||
}; | ||
|
||
struct ad5592r_dev { | ||
|
@@ -126,6 +142,11 @@ struct ad5592r_dev { | |
uint8_t gpio_in; | ||
uint8_t gpio_val; | ||
uint8_t ldac_mode; | ||
enum ad559xr_range adc_range; | ||
enum ad559xr_range dac_range; | ||
bool int_ref; | ||
uint8_t power_down[8]; | ||
bool adc_buf; | ||
}; | ||
|
||
int32_t ad5592r_base_reg_write(struct ad5592r_dev *dev, uint8_t reg, | ||
|
@@ -141,5 +162,14 @@ int32_t ad5592r_gpio_direction_output(struct ad5592r_dev *dev, | |
int32_t ad5592r_software_reset(struct ad5592r_dev *dev); | ||
int32_t ad5592r_set_channel_modes(struct ad5592r_dev *dev); | ||
int32_t ad5592r_reset_channel_modes(struct ad5592r_dev *dev); | ||
int32_t ad5592r_set_adc_range(struct ad5592r_dev *dev, | ||
enum ad559xr_range adc_range); | ||
int32_t ad5592r_set_dac_range(struct ad5592r_dev *dev, | ||
enum ad559xr_range dac_range); | ||
int32_t ad5592r_power_down(struct ad5592r_dev *dev, uint8_t chan, bool enable); | ||
int32_t ad5592r_set_int_ref(struct ad5592r_dev *dev, bool enable); | ||
int32_t ad5592r_set_adc_buffer(struct ad5592r_dev *dev, bool enable); | ||
int32_t ad5592r_base_reg_update(struct ad5592r_dev* dev, uint16_t reg_addr, | ||
uint16_t data, uint16_t mask); | ||
|
||
#endif /* AD5592R_BASE_H_ */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually avoid caching register values at the driver level, since the value could be changed by a direct register access. There are some cases where you may want to do that to avoid recurrent register reads, but it doesn't look like it applies here. Is there a reason for which you may want these values to be stored?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The adc and dac range are runtime attributes in the IIO firmware where a user can change the values on the fly. This in turn updates the scale value of the device.
Hence, I considered it best to have it as a device member instead of doing multiple reads.
I have also had it as a part of init_param member as well so the user can easily update the ranges compile time as well if needed.