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 driver report back vref alone with sample count #14136

Merged
merged 10 commits into from
Feb 21, 2020

Conversation

SalimTerryLi
Copy link
Contributor

@SalimTerryLi SalimTerryLi commented Feb 10, 2020

Describe problem solved by this pull request
Current ADC system does not provide reference voltage in any way. This PR focuses on introduce the v_ref in to ADC, potentially improve the ADC accuracy.

Describe your solution
I'm adding this new arch side function float px4_arch_adc_reference_v(unsigned channel) to get vref properly provided.
This function should be implemented differently on each board. On the side of stm32, this function should sample the internal 1.2V reference and apply factory calibration to get the true vref level.

Describe possible alternatives
In short term a fixed value can be set to calculate ADC results, but dynamically calculated value helps improving ADC accuracy.
vref would be provided within px4_arch_adc_sample.

Test data / coverage
An idea around system structure. No test available.

Additional context
This is another forward dependency of #14087
If this idea has certain advanced nature that is worth to introduce, I'm ask for help from someone who is experienced in stm32 development to implement that function on stm32_common and stm32h7 platforms.

@SalimTerryLi
Copy link
Contributor Author

SalimTerryLi commented Feb 11, 2020

Addition:
I'd rather like to have is function implemented within px4_arch_adc_sample, to guarantee its synchronization. This would be useful if vref is directly connected to board 3v3 power, which is not a stable reference source. vref should be sampled synchronously with target channel ideally to improve ADC accuracy.
px4_arch_adc_sample should return a structure which contains adc sample count and vref.

Why ADC should report vref instead of lsb voltage:
If we face with lsb then float error should be considered. But vref would not be that annoying.
Some boards using 0.0004394531 as DN_to_V, but actually it has a 12bit ADC with 1.8v as vref. Similarly, 0.0008 could be boards with 12bit ADC but 3.3v as vref. The true DN_to_V value is not that perfect but vref should be a representable number and DN_full_count is already provided. Let upper function makes a division would be better, I think.

And what is current status of ADC:
There is no vref provided. 3v3 is assumed as default and being hard-coded everywhere.
I'll kill the use of BOARD_ADC_POS_REF_V_FOR_*_CHAN in the future PRs for that this value should be provided by ADC drivers.
This will also optimize some macros used in system_power module which currently is contained in drivers/adc, but would be moved out as a standalone module some day.

@SalimTerryLi SalimTerryLi force-pushed the pr-adc_support_get_vref-improve branch 3 times, most recently from 1641807 to 7e8f1ea Compare February 18, 2020 16:42
@SalimTerryLi SalimTerryLi requested a review from dagar February 18, 2020 16:53
@SalimTerryLi SalimTerryLi force-pushed the pr-adc_support_get_vref-improve branch 2 times, most recently from ba39b0e to b0f6b42 Compare February 18, 2020 16:56
@SalimTerryLi
Copy link
Contributor Author

I have done it through the second way. It is able to merge as currently it has no effect on anything. The actual vref can be provided in any time. Before it is provided, hard-coded value should work. But, I still need some conformation about default vref value of each boards, which is marked with TODO.

@SalimTerryLi SalimTerryLi changed the title Introduce "px4_arch_adc_reference_v" ADC driver report back vref alone with sample count Feb 18, 2020
@SalimTerryLi SalimTerryLi requested a review from davids5 February 18, 2020 17:20
@SalimTerryLi SalimTerryLi force-pushed the pr-adc_support_get_vref-improve branch from b0f6b42 to 9cfd1c1 Compare February 19, 2020 17:27
@SalimTerryLi
Copy link
Contributor Author

Back to the first commit again.

@SalimTerryLi SalimTerryLi force-pushed the pr-adc_support_get_vref-improve branch 2 times, most recently from b5e5700 to 147adea Compare February 20, 2020 06:37
Copy link
Member

@davids5 davids5 left a comment

Choose a reason for hiding this comment

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

@SalimTerryLi - Thank you this is great. See inline comments.

A board_config represents the wiring of the SOC per board. This includes what ADC channels are used and what the ref voltage is.
Therefore that constant needs to come form board_common.h and be overridden by the board that do not use the default in board_config.h

Please add that change.

platforms/nuttx/src/px4/nxp/imxrt/adc/adc.cpp Outdated Show resolved Hide resolved
src/drivers/drv_adc.h Outdated Show resolved Hide resolved
src/drivers/drv_adc.h Outdated Show resolved Hide resolved
@SalimTerryLi SalimTerryLi requested a review from davids5 February 20, 2020 17:09
davids5
davids5 previously approved these changes Feb 20, 2020
Copy link
Member

@davids5 davids5 left a comment

Choose a reason for hiding this comment

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

@SalimTerryLi - can we kill off?

#ifndef BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN
#define BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN (3.3f) // Reference voltage for reading out the current channel	#define BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN (3.3f) // Reference voltage for reading out the current channel
#endif	#endif
#ifndef BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN	#ifndef BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN
#define BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN (3.3f) // Reference voltage for reading out the voltage channel	#define BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN (3.3f) // Reference voltage for reading out the voltage channel
#endif	#endif

@SalimTerryLi
Copy link
Contributor Author

SalimTerryLi commented Feb 21, 2020

@SalimTerryLi - can we kill off?

#ifndef BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN
#define BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN (3.3f) // Reference voltage for reading out the current channel	#define BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN (3.3f) // Reference voltage for reading out the current channel
#endif	#endif
#ifndef BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN	#ifndef BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN
#define BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN (3.3f) // Reference voltage for reading out the voltage channel	#define BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN (3.3f) // Reference voltage for reading out the voltage channel
#endif	#endif

I'll do that in the next PR #14087.
BTW, may I request you to make a proper implement of px4_arch_adc_reference_v() function on stm32 based fmu? Sorry but I have no experience with stm32 developing at register level.

@davids5 davids5 merged commit 77a9135 into PX4:master Feb 21, 2020
@SalimTerryLi SalimTerryLi deleted the pr-adc_support_get_vref-improve branch February 21, 2020 14:59
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