From bfa33a9df767aab290ee1c27bdf67efaacc4bcfe Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 10 Apr 2023 01:47:00 +0200 Subject: [PATCH] Fix STM32 set right port mode bits for analog port --- DCCTimerSTM32.cpp | 17 ++++++++++++++--- GITHUB_SHA.h | 2 +- MotorDriver.cpp | 5 ++++- version.h | 3 ++- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/DCCTimerSTM32.cpp b/DCCTimerSTM32.cpp index 5dd72db..5660be1 100644 --- a/DCCTimerSTM32.cpp +++ b/DCCTimerSTM32.cpp @@ -240,7 +240,10 @@ int ADCee::init(uint8_t pin) { int value = 0; PinName stmpin = analogInputToPinName(pin); - uint32_t stmgpio = stmpin / 16; // 16-bits per GPIO port group on STM32 + if (stmpin == NC) // do not continue if this is not an analog pin at all + return -1024; // some silly value as error + + uint32_t stmgpio = STM_PORT(stmpin); // converts to the GPIO port (16-bits per port group on STM32) uint32_t adcchan = STM_PIN_CHANNEL(pinmap_function(stmpin, PinMap_ADC)); // find ADC channel (only valid for ADC1!) GPIO_TypeDef * gpioBase; @@ -258,12 +261,20 @@ int ADCee::init(uint8_t pin) { RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; //Power up PORTC gpioBase = GPIOC; break; + default: + return -1023; // some silly value as error } - // Set pin mux mode to analog input - gpioBase->MODER |= (0b011 << (stmpin << 1)); // Set pin mux to analog mode + // Set pin mux mode to analog input, the 32 bit port mode register has 2 bits per pin + gpioBase->MODER |= (0b011 << (STM_PIN(stmpin) << 1)); // Set pin mux to analog mode (binary 11) // Set the sampling rate for that analog input + // This is F411x specific! Different on for example F334 + // STM32F11xC/E Reference manual + // 11.12.4 ADC sample time register 1 (ADC_SMPR1) (channels 10 to 18) + // 11.12.5 ADC sample time register 2 (ADC_SMPR2) (channels 0 to 9) + if (adcchan > 18) + return -1022; // silly value as error if (adcchan < 10) ADC1->SMPR2 |= (0b111 << (adcchan * 3)); // Channel sampling rate 480 cycles else diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index fbd3f68..fbecd70 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "devel-202304071845Z" +#define GITHUB_SHA "devel-202304092344Z" diff --git a/MotorDriver.cpp b/MotorDriver.cpp index a62dc9c..28ecba6 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -210,7 +210,10 @@ int MotorDriver::getCurrentRaw(bool fromISR) { (void)fromISR; if (currentPin==UNUSED_PIN) return 0; int current; - current = ADCee::read(currentPin, fromISR)-senseOffset; + current = ADCee::read(currentPin, fromISR); + // here one can diag raw value + // if (fromISR == false) DIAG(F("%c: %d"), trackLetter, current); + current = current-senseOffset; // adjust with offset if (current<0) current=0-current; if ((faultPin != UNUSED_PIN) && isLOW(fastFaultPin) && powerMode==POWERMODE::ON) return (current == 0 ? -1 : -current); diff --git a/version.h b/version.h index 768e8b2..442c351 100644 --- a/version.h +++ b/version.h @@ -4,7 +4,8 @@ #include "StringFormatter.h" -#define VERSION "4.2.41" +#define VERSION "4.2.43" +// 4.2.43 - Fix STM32 set right port mode bits for analog // 4.2.42 - Added EXRAIL TURNOUTL Macro definition // 4.2.41 - Move HAl startup to ASAP in setup() // - Fix DNOU8 output pin setup to all LOW