Use native ADC resolution where possible (#15719)

2.0.x
LinFor 5 years ago committed by Scott Lahteine
parent 9201197878
commit a84e3d1b80

@ -25,6 +25,8 @@
#include HAL_PATH(.,HAL.h)
#define HAL_ADC_RANGE _BV(HAL_ADC_RESOLUTION)
inline void watchdog_refresh() {
#if ENABLED(USE_WATCHDOG)
HAL_watchdog_refresh();

@ -365,6 +365,7 @@ inline void HAL_adc_init() {
#define HAL_START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
#endif
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() ADC
#define HAL_ADC_READY() !TEST(ADCSRA, ADSC)

@ -133,6 +133,7 @@ extern uint16_t HAL_adc_result; // result of last ADC conversion
inline void HAL_adc_init() {}//todo
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true

@ -111,6 +111,7 @@ void eeprom_update_block (const void *__src, void *__dst, size_t __n);
void HAL_adc_init();
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true

@ -89,6 +89,7 @@ int freeMemory();
// ADC
#define HAL_ANALOG_SELECT(pin) HAL_adc_enable_channel(pin)
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_get_result()
#define HAL_ADC_READY() true

@ -131,11 +131,14 @@ int freeMemory();
// K = 6, 565 samples, 500Hz sample rate, 1.13s convergence on full range step
// Memory usage per ADC channel (bytes): 4 (32 Bytes for 8 channels)
using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
#define HAL_ADC_RESULT_BITS 12 // 15 bit maximum, raw temperature is stored as int16_t
#define HAL_ADC_FILTERED // Disable oversampling done in Marlin as ADC values already filtered in HAL
#define HAL_ADC_RESOLUTION HAL_ADC_RESULT_BITS
using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
extern uint32_t HAL_adc_reading;
[[gnu::always_inline]] inline void HAL_start_adc(const pin_t pin) {
HAL_adc_reading = FilteredADC::read(pin) >> 6; // returns 16bit value, reduce to 10bit
HAL_adc_reading = FilteredADC::read(pin) >> (16 - HAL_ADC_RESULT_BITS); // returns 16bit value, reduce to required bits
}
[[gnu::always_inline]] inline uint16_t HAL_read_adc() {
return HAL_adc_reading;

@ -110,6 +110,7 @@ extern uint16_t HAL_adc_result; // result of last ADC conversion
void HAL_adc_init();
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true

@ -188,6 +188,7 @@ void eeprom_update_block(const void *__src, void *__dst, size_t __n);
inline void HAL_adc_init() {}
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true

@ -238,6 +238,7 @@ void eeprom_update_block(const void *__src, void *__dst, size_t __n);
void HAL_adc_init();
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true

@ -208,6 +208,7 @@ void eeprom_update_block (const void *__src, void *__dst, size_t __n);
inline void HAL_adc_init() {}
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true

@ -109,6 +109,7 @@ extern "C" {
void HAL_adc_init();
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_get_result()
#define HAL_ADC_READY() true

@ -115,6 +115,7 @@ extern "C" {
void HAL_adc_init();
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_ADC_RESOLUTION 10
#define HAL_READ_ADC() HAL_adc_get_result()
#define HAL_ADC_READY() true

@ -23,6 +23,7 @@
#include "../inc/MarlinConfig.h"
#include "../module/planner.h"
#include "../module/thermistor/thermistors.h"
class FilamentWidthSensor {
public:
@ -66,7 +67,7 @@ public:
}
// Convert raw measurement to mm
static inline float raw_to_mm(const uint16_t v) { return v * 5.0f * RECIPROCAL(16383.0f); }
static inline float raw_to_mm(const uint16_t v) { return v * 5.0f * RECIPROCAL(float(MAX_RAW_THERMISTOR_VALUE)); }
static inline float raw_to_mm() { return raw_to_mm(raw); }
// A scaled reading is ready

@ -103,6 +103,10 @@
#include "../feature/tmc_util.h"
#endif
#if HAS_ADC_BUTTONS
#include "../module/thermistor/thermistors.h"
#endif
#if HAS_ENCODER_ACTION
volatile uint8_t MarlinUI::buttons;
#if HAS_SLOW_BUTTONS
@ -1102,31 +1106,33 @@ void MarlinUI::update() {
#endif
// Calculate the ADC value for the voltage divider with specified pull-down resistor value
#define ADC_BUTTON_VALUE(r) (int(4096.0 * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP)))
#define ADC_BUTTON_VALUE(r) int(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
static constexpr uint16_t adc_button_tolerance = HAL_ADC_RANGE * 25 / 1024,
adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
// VALUE_MIN, VALUE_MAX, KEY
{ 4000, 4096, 1 + BLEN_KEYPAD_F1 }, // F1
{ 4000, 4096, 1 + BLEN_KEYPAD_F2 }, // F2
{ 4000, 4096, 1 + BLEN_KEYPAD_F3 }, // F3
{ ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) - 100,
ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_LEFT }, // LEFT ( 272 ... 472)
{ ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) - 100,
ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_RIGHT }, // RIGHT (1948 ... 2148)
{ ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) - 100,
ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_UP }, // UP ( 618 ... 818)
{ ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) - 100,
ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_DOWN }, // DOWN (2686 ... 2886)
{ ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) - 100,
ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_MIDDLE }, // ENTER (1205 ... 1405)
{ adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1 }, // F1
{ adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F2 }, // F2
{ adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F3 }, // F3
{ ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) - adc_button_tolerance,
ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_LEFT }, // LEFT ( 272 ... 472)
{ ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) - adc_button_tolerance,
ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_RIGHT }, // RIGHT (1948 ... 2148)
{ ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) - adc_button_tolerance,
ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_UP }, // UP ( 618 ... 818)
{ ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) - adc_button_tolerance,
ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_DOWN }, // DOWN (2686 ... 2886)
{ ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) - adc_button_tolerance,
ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_MIDDLE }, // ENTER (1205 ... 1405)
};
uint8_t get_ADC_keyValue() {
if (thermalManager.ADCKey_count >= 16) {
const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw << 2;
thermalManager.current_ADCKey_raw = 1024;
const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw;
thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
thermalManager.ADCKey_count = 0;
if (currentkpADCValue < 4000)
if (currentkpADCValue < adc_other_button)
for (uint8_t i = 0; i < ADC_KEY_NUM; i++) {
const uint16_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);

@ -302,7 +302,7 @@ volatile bool Temperature::temp_meas_ready = false;
// public:
#if HAS_ADC_BUTTONS
uint32_t Temperature::current_ADCKey_raw = 1024;
uint32_t Temperature::current_ADCKey_raw = HAL_ADC_RANGE;
uint8_t Temperature::ADCKey_count = 0;
#endif
@ -1308,7 +1308,7 @@ void Temperature::manage_heater() {
}
// maximum adc value .. take into account the over sampling
const int adc_max = (THERMISTOR_ADC_RESOLUTION * OVERSAMPLENR) - 1,
const int adc_max = MAX_RAW_THERMISTOR_VALUE,
adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
const float adc_inverse = (adc_max - adc_raw) - 0.5f,
@ -2742,7 +2742,7 @@ void Temperature::isr() {
next_sensor_state = adc_sensor_state; // redo this state
else if (ADCKey_count < 16) {
raw_ADCKey_value = HAL_READ_ADC();
if (raw_ADCKey_value <= 900) {
if (raw_ADCKey_value <= 900UL * HAL_ADC_RANGE / 1024UL) {
NOMORE(current_ADCKey_raw, raw_ADCKey_value);
ADCKey_count++;
}
@ -2750,7 +2750,7 @@ void Temperature::isr() {
if (ADCKey_count > 0) ADCKey_count++; else ADCKey_pressed = false;
if (ADCKey_pressed) {
ADCKey_count = 0;
current_ADCKey_raw = 1024;
current_ADCKey_raw = HAL_ADC_RANGE;
}
}
}

@ -217,7 +217,6 @@ typedef struct { int16_t raw_min, raw_max; } raw_range_t;
typedef struct { int16_t mintemp, maxtemp; } celsius_range_t;
typedef struct { int16_t raw_min, raw_max, mintemp, maxtemp; } temp_range_t;
#define THERMISTOR_ADC_RESOLUTION 1024 // 10-bit ADC .. shame to waste 12-bits of resolution on 32-bit
#define THERMISTOR_ABS_ZERO_C -273.15f // bbbbrrrrr cold !
#define THERMISTOR_RESISTANCE_NOMINAL_C 25.0f // mmmmm comfortable

@ -23,8 +23,21 @@
#include "../../inc/MarlinConfig.h"
#define OVERSAMPLENR 16
#define OV(N) int16_t((N) * (OVERSAMPLENR))
#define THERMISTOR_TABLE_ADC_RESOLUTION 1024
#define THERMISTOR_TABLE_SCALE (HAL_ADC_RANGE / (THERMISTOR_TABLE_ADC_RESOLUTION))
#if ENABLED(HAL_ADC_FILTERED)
#define OVERSAMPLENR 1
#else
#define OVERSAMPLENR 16
#endif
#define MAX_RAW_THERMISTOR_VALUE (HAL_ADC_RANGE * (OVERSAMPLENR) - 1)
// Currently Marlin stores all oversampled ADC values as int16_t, make sure the HAL settings do not overflow 15bit
#if MAX_RAW_THERMISTOR_VALUE > ((1 << 15) - 1)
#error "MAX_RAW_THERMISTOR_VALUE is too large for int16_t. Reduce OVERSAMPLENR or HAL_ADC_RESOLUTION."
#endif
#define OV(N) int16_t((N) * (OVERSAMPLENR) * (THERMISTOR_TABLE_SCALE))
#define ANY_THERMISTOR_IS(n) (THERMISTOR_HEATER_0 == n || THERMISTOR_HEATER_1 == n || THERMISTOR_HEATER_2 == n || THERMISTOR_HEATER_3 == n || THERMISTOR_HEATER_4 == n || THERMISTOR_HEATER_5 == n || THERMISTORBED == n || THERMISTORCHAMBER == n)
@ -251,74 +264,74 @@ static_assert(
// For thermocouples the highest temperature results in the highest ADC value
#ifndef HEATER_0_RAW_HI_TEMP
#if defined(REVERSE_TEMP_SENSOR_RANGE) || !defined(HEATER_0_USES_THERMISTOR)
#define HEATER_0_RAW_HI_TEMP 16383
#define HEATER_0_RAW_HI_TEMP MAX_RAW_THERMISTOR_VALUE
#define HEATER_0_RAW_LO_TEMP 0
#else
#define HEATER_0_RAW_HI_TEMP 0
#define HEATER_0_RAW_LO_TEMP 16383
#define HEATER_0_RAW_LO_TEMP MAX_RAW_THERMISTOR_VALUE
#endif
#endif
#ifndef HEATER_1_RAW_HI_TEMP
#if defined(REVERSE_TEMP_SENSOR_RANGE) || !defined(HEATER_1_USES_THERMISTOR)
#define HEATER_1_RAW_HI_TEMP 16383
#define HEATER_1_RAW_HI_TEMP MAX_RAW_THERMISTOR_VALUE
#define HEATER_1_RAW_LO_TEMP 0
#else
#define HEATER_1_RAW_HI_TEMP 0
#define HEATER_1_RAW_LO_TEMP 16383
#define HEATER_1_RAW_LO_TEMP MAX_RAW_THERMISTOR_VALUE
#endif
#endif
#ifndef HEATER_2_RAW_HI_TEMP
#if defined(REVERSE_TEMP_SENSOR_RANGE) || !defined(HEATER_2_USES_THERMISTOR)
#define HEATER_2_RAW_HI_TEMP 16383
#define HEATER_2_RAW_HI_TEMP MAX_RAW_THERMISTOR_VALUE
#define HEATER_2_RAW_LO_TEMP 0
#else
#define HEATER_2_RAW_HI_TEMP 0
#define HEATER_2_RAW_LO_TEMP 16383
#define HEATER_2_RAW_LO_TEMP MAX_RAW_THERMISTOR_VALUE
#endif
#endif
#ifndef HEATER_3_RAW_HI_TEMP
#if defined(REVERSE_TEMP_SENSOR_RANGE) || !defined(HEATER_3_USES_THERMISTOR)
#define HEATER_3_RAW_HI_TEMP 16383
#define HEATER_3_RAW_HI_TEMP MAX_RAW_THERMISTOR_VALUE
#define HEATER_3_RAW_LO_TEMP 0
#else
#define HEATER_3_RAW_HI_TEMP 0
#define HEATER_3_RAW_LO_TEMP 16383
#define HEATER_3_RAW_LO_TEMP MAX_RAW_THERMISTOR_VALUE
#endif
#endif
#ifndef HEATER_4_RAW_HI_TEMP
#if defined(REVERSE_TEMP_SENSOR_RANGE) || !defined(HEATER_4_USES_THERMISTOR)
#define HEATER_4_RAW_HI_TEMP 16383
#define HEATER_4_RAW_HI_TEMP MAX_RAW_THERMISTOR_VALUE
#define HEATER_4_RAW_LO_TEMP 0
#else
#define HEATER_4_RAW_HI_TEMP 0
#define HEATER_4_RAW_LO_TEMP 16383
#define HEATER_4_RAW_LO_TEMP MAX_RAW_THERMISTOR_VALUE
#endif
#endif
#ifndef HEATER_5_RAW_HI_TEMP
#if defined(REVERSE_TEMP_SENSOR_RANGE) || !defined(HEATER_5_USES_THERMISTOR)
#define HEATER_5_RAW_HI_TEMP 16383
#define HEATER_5_RAW_HI_TEMP MAX_RAW_THERMISTOR_VALUE
#define HEATER_5_RAW_LO_TEMP 0
#else
#define HEATER_5_RAW_HI_TEMP 0
#define HEATER_5_RAW_LO_TEMP 16383
#define HEATER_5_RAW_LO_TEMP MAX_RAW_THERMISTOR_VALUE
#endif
#endif
#ifndef HEATER_BED_RAW_HI_TEMP
#if defined(REVERSE_TEMP_SENSOR_RANGE) || !defined(HEATER_BED_USES_THERMISTOR)
#define HEATER_BED_RAW_HI_TEMP 16383
#define HEATER_BED_RAW_HI_TEMP MAX_RAW_THERMISTOR_VALUE
#define HEATER_BED_RAW_LO_TEMP 0
#else
#define HEATER_BED_RAW_HI_TEMP 0
#define HEATER_BED_RAW_LO_TEMP 16383
#define HEATER_BED_RAW_LO_TEMP MAX_RAW_THERMISTOR_VALUE
#endif
#endif
#ifndef HEATER_CHAMBER_RAW_HI_TEMP
#if defined(REVERSE_TEMP_SENSOR_RANGE) || !defined(HEATER_CHAMBER_USES_THERMISTOR)
#define HEATER_CHAMBER_RAW_HI_TEMP 16383
#define HEATER_CHAMBER_RAW_HI_TEMP MAX_RAW_THERMISTOR_VALUE
#define HEATER_CHAMBER_RAW_LO_TEMP 0
#else
#define HEATER_CHAMBER_RAW_HI_TEMP 0
#define HEATER_CHAMBER_RAW_LO_TEMP 16383
#define HEATER_CHAMBER_RAW_LO_TEMP MAX_RAW_THERMISTOR_VALUE
#endif
#endif

Loading…
Cancel
Save