-
Notifications
You must be signed in to change notification settings - Fork 367
ADC raw values calibration #555
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
69208ec
adc_cal: c2: Add efuse functions for reading calibration
katyo bfb094c
adc_cal: c3: Add efuse functions for reading calibration
katyo dfe7744
adc_cal: c6: Add efuse functions for reading calibration
katyo a5be5a2
adc_cal: Add extra traits to support calibration
katyo 7924750
adc_cal: Add basic ADC calibration scheme
katyo dc02547
adc_cal: Add line fitting ADC calibration scheme
katyo 53e7912
adc_cal: Add curve fitting ADC calibration scheme
katyo 5a78b91
adc_cal: riscv: Add ADC calibration implementation for riscv chips
katyo 71140d1
adc_cal: c2: Add calibrated ADC reading example
katyo 15eea18
adc_cal: c3: Add calibrated ADC reading example
katyo 9313534
adc_cal: c6: Add calibrated ADC reading example
katyo d2e2816
adc_cal: riscv: Add changelog entry for ADC calibration
katyo 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
Next
Next commit
adc_cal: c2: Add efuse functions for reading calibration
- Loading branch information
commit 69208ecc86ff922bfccd0e6651c6e63ba71fe854
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| //! Reading of eFuses | ||
|
|
||
| use crate::peripherals::EFUSE; | ||
| pub use crate::soc::efuse_field::*; | ||
| use crate::{adc::Attenuation, peripherals::EFUSE}; | ||
|
|
||
| pub struct Efuse; | ||
|
|
||
|
|
@@ -36,6 +36,102 @@ impl Efuse { | |
| pub fn get_rwdt_multiplier() -> u8 { | ||
| Self::read_field_le::<u8>(WDT_DELAY_SEL) | ||
| } | ||
|
|
||
| /// Get efuse block version | ||
| /// | ||
| /// see https://github.com/espressif/esp-idf/blob/dc016f5987/components/hal/efuse_hal.c#L27-L30 | ||
| pub fn get_block_version() -> (u8, u8) { | ||
| // see https://github.com/espressif/esp-idf/blob/dc016f5987/components/hal/esp32c2/include/hal/efuse_ll.h#L65-L73 | ||
| // https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_table.csv#L90-L91 | ||
| ( | ||
| Self::read_field_le::<u8>(BLK_VERSION_MAJOR), | ||
| Self::read_field_le::<u8>(BLK_VERSION_MINOR), | ||
| ) | ||
| } | ||
|
|
||
| /// Get version of RTC calibration block | ||
| /// | ||
| /// see https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_rtc_calib.c#L14 | ||
| pub fn get_rtc_calib_version() -> u8 { | ||
| let (major, _minor) = Self::get_block_version(); | ||
| if major == 0 { | ||
| 1 | ||
| } else { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| /// Get ADC initial code for specified attenuation from efuse | ||
| /// | ||
| /// see https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_rtc_calib.c#L27 | ||
| pub fn get_rtc_calib_init_code(_unit: u8, atten: Attenuation) -> Option<u16> { | ||
| let version = Self::get_rtc_calib_version(); | ||
|
|
||
| if version != 1 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| return None; | ||
| } | ||
|
|
||
| // see https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_table.csv#L94 | ||
| let diff_code0: u16 = Self::read_field_le(ADC1_INIT_CODE_ATTEN0); | ||
| let code0 = if diff_code0 & (1 << 7) != 0 { | ||
| 2160 - (diff_code0 & 0x7f) | ||
| } else { | ||
| 2160 + diff_code0 | ||
| }; | ||
|
|
||
| if matches!(atten, Attenuation::Attenuation0dB) { | ||
| return Some(code0); | ||
| } | ||
|
|
||
| // see https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_table.csv#L95 | ||
| let diff_code11: u16 = Self::read_field_le(ADC1_INIT_CODE_ATTEN3); | ||
| let code11 = code0 + diff_code11; | ||
|
|
||
| Some(code11) | ||
| } | ||
|
|
||
| /// Get ADC reference point voltage for specified attenuation in millivolts | ||
| /// | ||
| /// see https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_rtc_calib.c#L65 | ||
| pub fn get_rtc_calib_cal_mv(_unit: u8, atten: Attenuation) -> u16 { | ||
| match atten { | ||
| Attenuation::Attenuation0dB => 400, | ||
| Attenuation::Attenuation11dB => 1370, | ||
| } | ||
| } | ||
|
|
||
| /// Get ADC reference point digital code for specified attenuation | ||
| /// | ||
| /// see https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_rtc_calib.c#L65 | ||
| pub fn get_rtc_calib_cal_code(_unit: u8, atten: Attenuation) -> Option<u16> { | ||
| let version = Self::get_rtc_calib_version(); | ||
|
|
||
| if version != 1 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Version here should also be replaced by a const |
||
| return None; | ||
| } | ||
|
|
||
| // see https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_table.csv#L96 | ||
| let diff_code0: u16 = Self::read_field_le(ADC1_CAL_VOL_ATTEN0); | ||
| let code0 = if diff_code0 & (1 << 7) != 0 { | ||
| 1540 - (diff_code0 & 0x7f) | ||
| } else { | ||
| 1540 + diff_code0 | ||
| }; | ||
|
|
||
| if matches!(atten, Attenuation::Attenuation0dB) { | ||
| return Some(code0); | ||
| } | ||
|
|
||
| // see https://github.com/espressif/esp-idf/blob/903af13e8/components/efuse/esp32c2/esp_efuse_table.csv#L97 | ||
| let diff_code11: u16 = Self::read_field_le(ADC1_CAL_VOL_ATTEN3); | ||
| let code11 = if diff_code0 & (1 << 5) != 0 { | ||
| code0 - (diff_code11 & 0x1f) | ||
| } else { | ||
| code0 + diff_code11 | ||
| } - 123; | ||
|
|
||
| Some(code11) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Copy, Clone)] | ||
|
|
||
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.
This
1needs to be a named constant IMO.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 would like avoid introduce new constants.
Ok, I'll think about it.