-
Notifications
You must be signed in to change notification settings - Fork 965
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
base: develop
Are you sure you want to change the base?
Conversation
Implementation of adc_read() not check for errors in the ADC_CS register. - change the return type from uint16_t to int32_t - check for ADC_CS_READY_BITS and error ADC_CS_ERR_BITS or ADC_CS_ERR_STICKY_BITS - return the ADC value (12bits) or the negative value of ADC_CS_ERR_BITS or ADC_CS_ERR_STICKY_BITS depending on the error
|
||
return (uint16_t) adc_hw->result; | ||
if ((adc_hw->cs & ADC_CS_READY_BITS)>0) { | ||
return adc_hw->result & 0xfff; |
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.
Might be better to use ADC_RESULT_BITS
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.
I will have a look if that will have benefit. The original code check for the ADC_CS_READY_BITS.
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.
I believe Peter meant use ADC_RESULT_BITS
instead of 0xfff
- see https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2040/hardware_regs/include/hardware/regs/adc.h#L111
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.
Oh, :)
Thanks for the eye opener.
return adc_hw->result & 0xfff; | ||
} | ||
if ((adc_hw->cs & ADC_CS_ERR_BITS)>0) { | ||
return -ADC_CS_ERR_BITS; |
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.
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?
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.
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; |
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.
same comment as above
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.
Looks good but see previous comments concerning minor changes
Implementation of adc_read() not check for errors in the ADC_CS register.
v2: