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

Implement ADC self-calibration #233

Merged
merged 2 commits into from
Jan 23, 2024
Merged

Conversation

jcard0na
Copy link
Contributor

@jcard0na jcard0na commented Jan 22, 2024

Implement the ACD self-calibration procedure, which is defined in the RM377 reference manual as follows:

The ADC has a calibration feature. During the procedure, the ADC calculates a calibration factor which is internally applied to the ADC until the next ADC power-off. The application must not use the ADC during calibration and must wait until it is complete. Calibration should be performed before starting A/D conversion.

Implement the ACD self-calibration procedure, which is defined in the
RM377 reference manual as follows:

The ADC has a calibration feature. During the procedure, the ADC
calculates a calibration factor which is internally applied to the ADC
until the next ADC power-off. The application must not use the ADC
during calibration and must wait until it is complete.  Calibration
should be performed before starting A/D convers
@jcard0na jcard0na requested a review from a team as a code owner January 22, 2024 18:54
Copy link
Contributor

@jamwaffles jamwaffles left a comment

Choose a reason for hiding this comment

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

Looks good but I'm a bit hesitant about the 1000 loop iterations. Would it be better to use a while loop and make the assumption that the hardware will always clear ADCAL?

Also, the manual says:

Calibration can only be initiated when the ADC is disabled (when ADEN = 0)

Is that ensured in the context of calibrate()? I'm not familiar with the ADC code in this crate.

@jcard0na
Copy link
Contributor Author

Thanks for looking at this, @jamwaffles

Looks good but I'm a bit hesitant about the 1000 loop iterations. Would it be better to use a while loop and make the assumption that the hardware will always clear ADCAL?

Yes, I'm not too proud about that loop, but also I'm kind paranoid about introducing potential infinite loops so deep down. If you are happy with it, I'll change it to a while loop. At least on my hardware ADCAL seems to always be cleared.

Also, the manual says:

Calibration can only be initiated when the ADC is disabled (when ADEN = 0)

Is that ensured in the context of calibrate()? I'm not familiar with the ADC code in this crate.

Yep, I'm checking for that here: https://github.com/stm32-rs/stm32l0xx-hal/pull/233/files#diff-09d1f58a7f7c271d63f1a9a00c0c48c886e094bc98c207a8d7688c9f874259bbR132

The reference manual says that software should wait until ADCAL = 0.
Do that instead of being paranoid and eventually timing out if this does
not happen.
@jcard0na
Copy link
Contributor Author

By the way, on my STM32L072, the ADC errors dropped from 6% to 0.05% after calibration(), so it is a big deal 📏

@jamwaffles
Copy link
Contributor

Let's go for a while loop... we can change it if someone complains 😁

Yep, I'm checking for that here

Ah thanks, I missed that... 💤

@jamwaffles
Copy link
Contributor

By the way, on my STM32L072, the ADC errors dropped from 6% to 0.05% after calibration(), so it is a big deal 📏

Nice! It's great to validate these changes, and that's a huge improvement.

@jamwaffles jamwaffles merged commit d20524c into stm32-rs:master Jan 23, 2024
11 checks passed
@jamwaffles
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants