From 76aed2bec7b4a111e15f59af78331abfccc7ce8d Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 6 Jun 2022 10:28:50 +0200 Subject: [PATCH 1/5] refactor(analog): move get_pwm_channel to timer api Moved from analog.* to timer.*. Signed-off-by: Frederic Pillon --- cores/arduino/stm32/analog.h | 1 - cores/arduino/stm32/timer.h | 2 ++ libraries/SrcWrapper/src/stm32/analog.cpp | 26 --------------------- libraries/SrcWrapper/src/stm32/timer.c | 28 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/cores/arduino/stm32/analog.h b/cores/arduino/stm32/analog.h index dc459a845c..c2cb305745 100644 --- a/cores/arduino/stm32/analog.h +++ b/cores/arduino/stm32/analog.h @@ -56,7 +56,6 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution); void pwm_start(PinName pin, uint32_t clock_freq, uint32_t value, TimerCompareFormat_t resolution); void pwm_stop(PinName pin); #endif -uint32_t get_pwm_channel(PinName pin); #ifdef __cplusplus } diff --git a/cores/arduino/stm32/timer.h b/cores/arduino/stm32/timer.h index 344afa6061..9296759bf8 100644 --- a/cores/arduino/stm32/timer.h +++ b/cores/arduino/stm32/timer.h @@ -284,6 +284,8 @@ uint8_t getTimerClkSrc(TIM_TypeDef *tim); IRQn_Type getTimerUpIrq(TIM_TypeDef *tim); IRQn_Type getTimerCCIrq(TIM_TypeDef *tim); +uint32_t get_pwm_channel(PinName pin); + #endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */ #ifdef __cplusplus diff --git a/libraries/SrcWrapper/src/stm32/analog.cpp b/libraries/SrcWrapper/src/stm32/analog.cpp index 18b026fad8..9949ecaa2b 100644 --- a/libraries/SrcWrapper/src/stm32/analog.cpp +++ b/libraries/SrcWrapper/src/stm32/analog.cpp @@ -270,32 +270,6 @@ static uint32_t get_adc_internal_channel(PinName pin) } #endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY */ -#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY) -uint32_t get_pwm_channel(PinName pin) -{ - uint32_t function = pinmap_function(pin, PinMap_TIM); - uint32_t channel = 0; - switch (STM_PIN_CHANNEL(function)) { - case 1: - channel = TIM_CHANNEL_1; - break; - case 2: - channel = TIM_CHANNEL_2; - break; - case 3: - channel = TIM_CHANNEL_3; - break; - case 4: - channel = TIM_CHANNEL_4; - break; - default: - channel = 0; - break; - } - return channel; -} -#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */ - #if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY) static uint32_t get_dac_channel(PinName pin) { diff --git a/libraries/SrcWrapper/src/stm32/timer.c b/libraries/SrcWrapper/src/stm32/timer.c index 945d79569a..b32f6d7c75 100644 --- a/libraries/SrcWrapper/src/stm32/timer.c +++ b/libraries/SrcWrapper/src/stm32/timer.c @@ -715,6 +715,34 @@ uint8_t getTimerClkSrc(TIM_TypeDef *tim) return clkSrc; } +/** + * @brief Return HAL timer channel linked to a PinName + * @param pin: PinName + * @retval HAL channel. return 0 if pin has no timer + */ +uint32_t get_pwm_channel(PinName pin) +{ + uint32_t function = pinmap_function(pin, PinMap_TIM); + uint32_t channel = 0; + switch (STM_PIN_CHANNEL(function)) { + case 1: + channel = TIM_CHANNEL_1; + break; + case 2: + channel = TIM_CHANNEL_2; + break; + case 3: + channel = TIM_CHANNEL_3; + break; + case 4: + channel = TIM_CHANNEL_4; + break; + default: + channel = 0; + break; + } + return channel; +} #endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */ From 449b721509af77b5c04f31d950fae12c96ef9e9b Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Fri, 3 Jun 2022 16:25:30 +0200 Subject: [PATCH 2/5] refactor(timer): rename channel function Signed-off-by: Frederic Pillon --- cores/arduino/stm32/timer.h | 2 +- libraries/SrcWrapper/src/HardwareTimer.cpp | 2 +- libraries/SrcWrapper/src/stm32/timer.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/arduino/stm32/timer.h b/cores/arduino/stm32/timer.h index 9296759bf8..7b71dc6b0e 100644 --- a/cores/arduino/stm32/timer.h +++ b/cores/arduino/stm32/timer.h @@ -284,7 +284,7 @@ uint8_t getTimerClkSrc(TIM_TypeDef *tim); IRQn_Type getTimerUpIrq(TIM_TypeDef *tim); IRQn_Type getTimerCCIrq(TIM_TypeDef *tim); -uint32_t get_pwm_channel(PinName pin); +uint32_t getTimerChannel(PinName pin); #endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */ diff --git a/libraries/SrcWrapper/src/HardwareTimer.cpp b/libraries/SrcWrapper/src/HardwareTimer.cpp index 9e948bc53a..d9de8dfcd2 100644 --- a/libraries/SrcWrapper/src/HardwareTimer.cpp +++ b/libraries/SrcWrapper/src/HardwareTimer.cpp @@ -740,7 +740,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin) _ChannelMode[channel - 1] = mode; if (pin != NC) { - if ((int)get_pwm_channel(pin) == timChannel) { + if ((int)getTimerChannel(pin) == timChannel) { /* Configure PWM GPIO pins */ pinmap_pinout(pin, PinMap_TIM); #if defined(STM32F1xx) diff --git a/libraries/SrcWrapper/src/stm32/timer.c b/libraries/SrcWrapper/src/stm32/timer.c index b32f6d7c75..55a74d1327 100644 --- a/libraries/SrcWrapper/src/stm32/timer.c +++ b/libraries/SrcWrapper/src/stm32/timer.c @@ -720,7 +720,7 @@ uint8_t getTimerClkSrc(TIM_TypeDef *tim) * @param pin: PinName * @retval HAL channel. return 0 if pin has no timer */ -uint32_t get_pwm_channel(PinName pin) +uint32_t getTimerChannel(PinName pin) { uint32_t function = pinmap_function(pin, PinMap_TIM); uint32_t channel = 0; From 98b73abcb08ea5823ac2c53899980747d53667e6 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Fri, 3 Jun 2022 16:50:09 +0200 Subject: [PATCH 3/5] feat(analog): export adc/dac get channel functions Fixes #1669 Signed-off-by: Frederic Pillon --- cores/arduino/stm32/analog.h | 7 ++++++ libraries/SrcWrapper/src/stm32/analog.cpp | 26 +++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cores/arduino/stm32/analog.h b/cores/arduino/stm32/analog.h index c2cb305745..b43c1d1755 100644 --- a/cores/arduino/stm32/analog.h +++ b/cores/arduino/stm32/analog.h @@ -49,6 +49,13 @@ extern "C" { #endif /* Exported functions ------------------------------------------------------- */ +#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY) +uint32_t get_adc_channel(PinName pin, uint32_t *bank); +uint32_t get_adc_internal_channel(PinName pin); +#endif +#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY) +uint32_t get_dac_channel(PinName pin); +#endif void dac_write_value(PinName pin, uint32_t value, uint8_t do_init); void dac_stop(PinName pin); uint16_t adc_read_value(PinName pin, uint32_t resolution); diff --git a/libraries/SrcWrapper/src/stm32/analog.cpp b/libraries/SrcWrapper/src/stm32/analog.cpp index 9949ecaa2b..07ec93f98b 100644 --- a/libraries/SrcWrapper/src/stm32/analog.cpp +++ b/libraries/SrcWrapper/src/stm32/analog.cpp @@ -101,8 +101,14 @@ static PinName g_current_pin = NC; #define ADC_REGULAR_RANK_1 1 #endif -/* Private Functions */ -static uint32_t get_adc_channel(PinName pin, uint32_t *bank) +/* Exported Functions */ +/** + * @brief Return ADC HAL channel linked to a PinName + * @param pin: PinName + * @param bank: pointer to get ADC channel bank if required + * @retval HAL channel. return 0 if pin has no ADC + */ +uint32_t get_adc_channel(PinName pin, uint32_t *bank) { uint32_t function = pinmap_function(pin, PinMap_ADC); uint32_t channel = 0; @@ -233,7 +239,14 @@ static uint32_t get_adc_channel(PinName pin, uint32_t *bank) return channel; } -static uint32_t get_adc_internal_channel(PinName pin) +/** + * @brief Return ADC HAL internal channel linked to a PinName + * @param pin: specific PinName's for ADC internal. Value can be: + * PADC_TEMP, PADC_TEMP_ADC5, PADC_VREF, PADC_VBAT + * Note that not all of these values ​​may be available for all series. + * @retval HAL internal channel. return 0 if pin has no ADC internal + */ +uint32_t get_adc_internal_channel(PinName pin) { uint32_t channel = 0; switch (pin) { @@ -271,7 +284,12 @@ static uint32_t get_adc_internal_channel(PinName pin) #endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY */ #if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY) -static uint32_t get_dac_channel(PinName pin) +/** + * @brief Return DAC HAL channel linked to a PinName + * @param pin: specific PinName's for ADC internal. + * @retval HAL channel. return 0 if pin has no dac + */ +uint32_t get_dac_channel(PinName pin) { uint32_t function = pinmap_function(pin, PinMap_DAC); uint32_t channel = 0; From 14ba2033990979ebfbafbeabd8ad9c11178294ca Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 7 Jun 2022 13:53:37 +0200 Subject: [PATCH 4/5] refactor: harden analog and timer get channel functions as 0 can be a valid value. Signed-off-by: Frederic Pillon --- libraries/SrcWrapper/src/stm32/analog.cpp | 12 ++++++------ libraries/SrcWrapper/src/stm32/timer.c | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libraries/SrcWrapper/src/stm32/analog.cpp b/libraries/SrcWrapper/src/stm32/analog.cpp index 07ec93f98b..479cfcd630 100644 --- a/libraries/SrcWrapper/src/stm32/analog.cpp +++ b/libraries/SrcWrapper/src/stm32/analog.cpp @@ -106,7 +106,7 @@ static PinName g_current_pin = NC; * @brief Return ADC HAL channel linked to a PinName * @param pin: PinName * @param bank: pointer to get ADC channel bank if required - * @retval HAL channel. return 0 if pin has no ADC + * @retval Valid HAL channel */ uint32_t get_adc_channel(PinName pin, uint32_t *bank) { @@ -224,7 +224,7 @@ uint32_t get_adc_channel(PinName pin, uint32_t *bank) #endif #endif default: - channel = 0; + _Error_Handler("ADC: Unknown adc channel", (int)(STM_PIN_CHANNEL(function))); break; } #ifdef ADC_CHANNELS_BANK_B @@ -244,7 +244,7 @@ uint32_t get_adc_channel(PinName pin, uint32_t *bank) * @param pin: specific PinName's for ADC internal. Value can be: * PADC_TEMP, PADC_TEMP_ADC5, PADC_VREF, PADC_VBAT * Note that not all of these values ​​may be available for all series. - * @retval HAL internal channel. return 0 if pin has no ADC internal + * @retval Valid HAL internal channel. */ uint32_t get_adc_internal_channel(PinName pin) { @@ -276,7 +276,7 @@ uint32_t get_adc_internal_channel(PinName pin) break; #endif default: - channel = 0; + _Error_Handler("ADC: Unknown adc internal PiName", (int)(pin)); break; } return channel; @@ -287,7 +287,7 @@ uint32_t get_adc_internal_channel(PinName pin) /** * @brief Return DAC HAL channel linked to a PinName * @param pin: specific PinName's for ADC internal. - * @retval HAL channel. return 0 if pin has no dac + * @retval Valid HAL channel */ uint32_t get_dac_channel(PinName pin) { @@ -308,7 +308,7 @@ uint32_t get_dac_channel(PinName pin) break; #endif default: - channel = 0; + _Error_Handler("DAC: Unknown dac channel", (int)(STM_PIN_CHANNEL(function))); break; } return channel; diff --git a/libraries/SrcWrapper/src/stm32/timer.c b/libraries/SrcWrapper/src/stm32/timer.c index 55a74d1327..e9febec358 100644 --- a/libraries/SrcWrapper/src/stm32/timer.c +++ b/libraries/SrcWrapper/src/stm32/timer.c @@ -718,7 +718,7 @@ uint8_t getTimerClkSrc(TIM_TypeDef *tim) /** * @brief Return HAL timer channel linked to a PinName * @param pin: PinName - * @retval HAL channel. return 0 if pin has no timer + * @retval Valid HAL channel */ uint32_t getTimerChannel(PinName pin) { @@ -738,7 +738,7 @@ uint32_t getTimerChannel(PinName pin) channel = TIM_CHANNEL_4; break; default: - channel = 0; + _Error_Handler("TIM: Unknown timer channel", (int)(STM_PIN_CHANNEL(function))); break; } return channel; From f731fdb6bc89c4dbfbbf36175c1f90311ba366d6 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 8 Jun 2022 14:26:21 +0200 Subject: [PATCH 5/5] fix(analog): guard some declarations Signed-off-by: Frederic Pillon --- cores/arduino/stm32/analog.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/stm32/analog.h b/cores/arduino/stm32/analog.h index b43c1d1755..0d0494f03e 100644 --- a/cores/arduino/stm32/analog.h +++ b/cores/arduino/stm32/analog.h @@ -52,13 +52,13 @@ extern "C" { #if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY) uint32_t get_adc_channel(PinName pin, uint32_t *bank); uint32_t get_adc_internal_channel(PinName pin); +uint16_t adc_read_value(PinName pin, uint32_t resolution); #endif #if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY) uint32_t get_dac_channel(PinName pin); -#endif void dac_write_value(PinName pin, uint32_t value, uint8_t do_init); void dac_stop(PinName pin); -uint16_t adc_read_value(PinName pin, uint32_t resolution); +#endif #if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY) void pwm_start(PinName pin, uint32_t clock_freq, uint32_t value, TimerCompareFormat_t resolution); void pwm_stop(PinName pin);