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

Increase ADC resolution for FYSETC S6, STM32 #20519

Merged
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions Marlin/src/HAL/STM32/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ static inline int freeMemory() {

#define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)

inline void HAL_adc_init() {}

#define HAL_ADC_VREF 3.3
#define HAL_ADC_RESOLUTION 10
#define HAL_ADC_RESOLUTION ADC_RESOLUTION // 12
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true

inline void HAL_adc_init() { analogReadResolution(HAL_ADC_RESOLUTION); }

void HAL_adc_start_conversion(const uint8_t adc_pin);

uint16_t HAL_adc_get_result();
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/module/thermistor/thermistors.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#define THERMISTOR_TABLE_SCALE (HAL_ADC_RANGE / _BV(THERMISTOR_TABLE_ADC_RESOLUTION))
#if ENABLED(HAL_ADC_FILTERED)
#define OVERSAMPLENR 1
#elif HAL_ADC_RESOLUTION > 10
#define OVERSAMPLENR (20 - HAL_ADC_RESOLUTION)
#else
#define OVERSAMPLENR 16
#endif
Comment on lines +30 to 34
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this change achieve. I'm not doubting it, Is this simply because fewer samples are needed to generate an averaged reading when using the higher resolution?

Is the number choice here arbitrary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, this is a crutch so as not to change the existing oversampling mechanism. All measurements are summed in a 16-bit field, so a high ADC bit depth simultaneously with a large oversampling ratio will cause an overflow.
A few lines below there is a static validation for this (MAX_RAW_THERMISTOR_VALUE).

Since there is no easy way to transfer OVERSAMPLENR from hal (well, at least there are doubts that this needs to be introduced), I preferred to calculate it automatically for high-bit ADCs to avoid overflow. There is, of course, an opportunity to set HAL_ADC_FILTERED at the hal level, which will completely disable oversampling, but this is semantically incorrect, since no filtration is performed in this hal.

Copy link
Member

Choose a reason for hiding this comment

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

Works for me. The main thing is to have at least some number of oversamples to deal with the natural variation in ADC readings. Guaranteed to be over 10 is reasonable.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ extern "C" {
#define NUM_ANALOG_INPUTS 7
#define NUM_ANALOG_FIRST 80

#define ADC_RESOLUTION 12

// PWM resolution
// #define PWM_RESOLUTION 12
#define PWM_FREQUENCY 20000 // >= 20 Khz => inaudible noise for fans
Expand Down