Skip to content
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

adc: adc_read() not check for error (#1575) v2 #1599

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/rp2_common/hardware_adc/include/hardware/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,24 @@ static inline void adc_set_temp_sensor_enabled(bool enable) {
*
* Performs an ADC conversion, waits for the result, and then returns it.
*
* \return Result of the conversion.
* \return Result of the conversion or negative value on error.
*/
static inline uint16_t adc_read(void) {
static inline int32_t adc_read(void) {
hw_set_bits(&adc_hw->cs, ADC_CS_START_ONCE_BITS);

while ((adc_hw->cs & (ADC_CS_READY_BITS|ADC_CS_ERR_BITS|ADC_CS_ERR_STICKY_BITS))==0) {
tight_loop_contents();
}

while (!(adc_hw->cs & ADC_CS_READY_BITS))
tight_loop_contents();

return (uint16_t) adc_hw->result;
if ((adc_hw->cs & ADC_CS_READY_BITS)>0) {
return adc_hw->result & 0xfff;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to use ADC_RESULT_BITS

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have a look if that will have benefit. The original code check for the ADC_CS_READY_BITS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, :)
Thanks for the eye opener.

}
if ((adc_hw->cs & ADC_CS_ERR_BITS)>0) {
return -ADC_CS_ERR_BITS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit random to return -ADC_CS_ERR_BITS. It would be better to return a "standard" error code? Maybe PICO_ERROR_GENERIC or PICO_ERROR_NO_DATA?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point, but if we doing this we can't distinguished what caused the error.

Use
PICO_ERROR_NO_DATA
and additional, define a new code:
PICO_ERROR_ADC_STICKY_BIT

}
/* clear sticky bit */
hw_set_bits(&adc_hw->cs, ADC_CS_ERR_STICKY_BITS);
return -ADC_CS_ERR_STICKY_BITS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

}

/*! \brief Enable or disable free-running sampling mode
Expand Down