From c3eb6b8d8a9774d8e308f8f80d5e51d134d0de18 Mon Sep 17 00:00:00 2001 From: pmantoine Date: Mon, 19 Jun 2023 17:47:42 +0800 Subject: [PATCH] STM32 ADCee highestPin --- DCCTimerAVR.cpp | 6 +++--- DCCTimerSTM32.cpp | 16 ++++++++++------ platformio.ini | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/DCCTimerAVR.cpp b/DCCTimerAVR.cpp index 80cd245..3e6c436 100644 --- a/DCCTimerAVR.cpp +++ b/DCCTimerAVR.cpp @@ -199,7 +199,7 @@ void ADCee::scan() { waiting = false; id++; mask = mask << 1; - if (id > highestPin) { // the 1 has been shifted out + if (id > highestPin) { id = 0; mask = 1; } @@ -230,8 +230,8 @@ void ADCee::scan() { id++; mask = mask << 1; if (id > highestPin) { - id = 0; - mask = 1; + id = 0; + mask = 1; } } } diff --git a/DCCTimerSTM32.cpp b/DCCTimerSTM32.cpp index 7ed2b95..17ad84e 100644 --- a/DCCTimerSTM32.cpp +++ b/DCCTimerSTM32.cpp @@ -233,13 +233,16 @@ void DCCTimer::reset() { while(true) {}; } -#define NUM_ADC_INPUTS NUM_ANALOG_INPUTS - // TODO: may need to use uint32_t on STMF4xx variants with > 16 analog inputs! #if defined(ARDUINO_NUCLEO_F446RE) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE) #warning STM32 board selected not fully supported - only use ADC1 inputs 0-15 for current sensing! #endif +// For now, define the max of 16 ports - some variants have more, but this not **yet** supported +#define NUM_ADC_INPUTS 16 +// #define NUM_ADC_INPUTS NUM_ANALOG_INPUTS + uint16_t ADCee::usedpins = 0; +uint8_t ADCee::highestPin = 0; int * ADCee::analogvals = NULL; uint32_t * analogchans = NULL; bool adc1configured = false; @@ -310,6 +313,7 @@ int ADCee::init(uint8_t pin) { analogvals[id] = value; // Store sampled value analogchans[id] = adcchan; // Keep track of which ADC channel is used for reading this pin usedpins |= (1 << id); // This pin is now ready + if (id > highestPin) highestPin = id; // Store our highest pin in use DIAG(F("ADCee::init(): value=%d, channel=%d, id=%d"), value, adcchan, id); @@ -352,7 +356,7 @@ void ADCee::scan() { waiting = false; id++; mask = mask << 1; - if (mask == 0) { // the 1 has been shifted out + if (id > highestPin) { // the 1 has been shifted out id = 0; mask = 1; } @@ -374,9 +378,9 @@ void ADCee::scan() { } id++; mask = mask << 1; - if (mask == 0) { // the 1 has been shifted out - id = 0; - mask = 1; + if (id > highestPin) { + id = 0; + mask = 1; } } } diff --git a/platformio.ini b/platformio.ini index d85c76c..3091d7f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -188,7 +188,7 @@ platform = ststm32 board = nucleo_f446re framework = arduino lib_deps = ${env.lib_deps} -build_flags = -std=c++17 -Os -g2 -Wunused-variable -DDIAG_LOOPTIMES ; -DDIAG_IO +build_flags = -std=c++17 -Os -g2 -Wunused-variable ; -DDIAG_LOOPTIMES ; -DDIAG_IO monitor_speed = 115200 monitor_echo = yes