From 1356de230ff0b9938f3ab40fc8b49447f26159e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Thu, 17 Sep 2020 10:33:05 +0200 Subject: [PATCH] Configure half_bridge PWM via board.dts and pinmux.c Removes some of the hard-coded configuration in half_bridge.cpp and moves it to board configuration. The previous manual pinmux for the PWM output pins is now completely replaced with Zephyr pinmux. --- boards/arm/mppt_1210_hus/mppt_1210_hus.dts | 14 +++++ boards/arm/mppt_1210_hus/pinmux.c | 8 +++ boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts | 1 + boards/arm/mppt_2420_hpx/pinmux.c | 8 +++ boards/arm/mppt_2420_lc/mppt_2420_lc.dts | 1 + boards/arm/mppt_2420_lc/pinmux.c | 8 +++ boards/arm/mppt_2420_rc/mppt_2420_rc.dts | 1 + boards/arm/mppt_2420_rc/pinmux.c | 10 ++++ src/half_bridge.cpp | 60 ++++++---------------- zephyr/dts/bindings/dcdc.yaml | 4 ++ 10 files changed, 72 insertions(+), 43 deletions(-) diff --git a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts index 67e92af..a8dd312 100644 --- a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts +++ b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts @@ -24,6 +24,7 @@ dcdc { compatible = "dcdc"; + timer = <&timers3>; pwm-frequency = <50000>; pwm-deadtime = <230>; current-max = <10>; @@ -139,6 +140,19 @@ spi-uext = &spi1; i2c-uext = &i2c1; }; + + soc { + // not (yet) provided by Zephyr dts, but necessary for dcdc node + timers3: timers@40000400 { + compatible = "st,stm32-timers"; + reg = <0x40000400 0x400>; + clocks = <&rcc STM32_CLOCK_BUS_APB1 0x00000002>; + interrupts = <16 0>; + interrupt-names = "global"; + status = "disabled"; + label = "TIMERS_3"; + }; + }; }; &usart1 { diff --git a/boards/arm/mppt_1210_hus/pinmux.c b/boards/arm/mppt_1210_hus/pinmux.c index 37d2b98..8e8c275 100644 --- a/boards/arm/mppt_1210_hus/pinmux.c +++ b/boards/arm/mppt_1210_hus/pinmux.c @@ -12,7 +12,15 @@ #include +#define STM32L0_PINMUX_FUNC_PB0_TIM3_CH3 \ + (STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL) +#define STM32L0_PINMUX_FUNC_PB1_TIM3_CH4 \ + (STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL) + static const struct pin_config pinconf[] = { + /* TIM3_CH3 and TIM3_CH4 */ + {STM32_PIN_PB0, STM32L0_PINMUX_FUNC_PB0_TIM3_CH3}, + {STM32_PIN_PB1, STM32L0_PINMUX_FUNC_PB1_TIM3_CH4}, #if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL {STM32_PIN_PA9, STM32L0_PINMUX_FUNC_PA9_USART1_TX}, {STM32_PIN_PA10, STM32L0_PINMUX_FUNC_PA10_USART1_RX}, diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index 0339051..04c737f 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -24,6 +24,7 @@ dcdc { compatible = "dcdc"; + timer = <&timers1>; pwm-frequency = <70000>; pwm-deadtime = <300>; current-max = <20>; diff --git a/boards/arm/mppt_2420_hpx/pinmux.c b/boards/arm/mppt_2420_hpx/pinmux.c index 412c348..dfc0a30 100644 --- a/boards/arm/mppt_2420_hpx/pinmux.c +++ b/boards/arm/mppt_2420_hpx/pinmux.c @@ -16,7 +16,15 @@ #define STM32G4X_PINMUX_FUNC_PC6_PWM8_CH1 \ (STM32_PINMUX_ALT_FUNC_4 | STM32_PUSHPULL_NOPULL) +#define STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1 \ + (STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL) +#define STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N \ + (STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL) + static const struct pin_config pinconf[] = { + /* TIM1_CH1 and TIM1_CH1N */ + {STM32_PIN_PA8, STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1}, + {STM32_PIN_PB13, STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N}, #if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL {STM32_PIN_PA9, STM32G4X_PINMUX_FUNC_PA9_USART1_TX}, {STM32_PIN_PA10, STM32G4X_PINMUX_FUNC_PA10_USART1_RX}, diff --git a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts index 06ce89c..64a0ed6 100644 --- a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts +++ b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts @@ -24,6 +24,7 @@ dcdc { compatible = "dcdc"; + timer = <&timers1>; pwm-frequency = <70000>; pwm-deadtime = <300>; current-max = <20>; diff --git a/boards/arm/mppt_2420_lc/pinmux.c b/boards/arm/mppt_2420_lc/pinmux.c index d42f476..47f56cc 100644 --- a/boards/arm/mppt_2420_lc/pinmux.c +++ b/boards/arm/mppt_2420_lc/pinmux.c @@ -22,7 +22,15 @@ (STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP) #endif +#define STM32F0_PINMUX_FUNC_PA8_TIM1_CH1 \ + (STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL) +#define STM32F0_PINMUX_FUNC_PB13_TIM1_CH1N \ + (STM32_PINMUX_ALT_FUNC_2 | STM32_PUSHPULL_NOPULL) + static const struct pin_config pinconf[] = { + /* TIM1_CH1 and TIM1_CH1N */ + {STM32_PIN_PA8, STM32F0_PINMUX_FUNC_PA8_TIM1_CH1}, + {STM32_PIN_PB13, STM32F0_PINMUX_FUNC_PB13_TIM1_CH1N}, #if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL {STM32_PIN_PA9, STM32F0_PINMUX_FUNC_PA9_USART1_TX}, {STM32_PIN_PA10, STM32F0_PINMUX_FUNC_PA10_USART1_RX}, diff --git a/boards/arm/mppt_2420_rc/mppt_2420_rc.dts b/boards/arm/mppt_2420_rc/mppt_2420_rc.dts index fed15b4..c3243d5 100644 --- a/boards/arm/mppt_2420_rc/mppt_2420_rc.dts +++ b/boards/arm/mppt_2420_rc/mppt_2420_rc.dts @@ -24,6 +24,7 @@ dcdc { compatible = "dcdc"; + timer = <&timers1>; pwm-frequency = <70000>; pwm-deadtime = <300>; current-max = <20>; diff --git a/boards/arm/mppt_2420_rc/pinmux.c b/boards/arm/mppt_2420_rc/pinmux.c index 86bd663..255f907 100644 --- a/boards/arm/mppt_2420_rc/pinmux.c +++ b/boards/arm/mppt_2420_rc/pinmux.c @@ -12,11 +12,21 @@ #include +#define STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1 \ + (STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL) +#define STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N \ + (STM32_PINMUX_ALT_FUNC_6 | STM32_PUSHPULL_NOPULL) + static const struct pin_config pinconf[] = { + /* USART1 */ {STM32_PIN_PA9, STM32G4X_PINMUX_FUNC_PA9_USART1_TX}, {STM32_PIN_PA10, STM32G4X_PINMUX_FUNC_PA10_USART1_RX}, + /* USART2 */ {STM32_PIN_PA2, STM32G4X_PINMUX_FUNC_PA2_USART2_TX}, {STM32_PIN_PA3, STM32G4X_PINMUX_FUNC_PA3_USART2_RX}, + /* TIM1_CH1 and TIM1_CH1N */ + {STM32_PIN_PA8, STM32G4X_PINMUX_FUNC_PA8_TIM1_CH1}, + {STM32_PIN_PB13, STM32G4X_PINMUX_FUNC_PB13_TIM1_CH1N}, #if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay) && CONFIG_I2C {STM32_PIN_PA15, STM32G4X_PINMUX_FUNC_PA15_I2C1_SCL}, {STM32_PIN_PB7, STM32G4X_PINMUX_FUNC_PB7_I2C1_SDA}, diff --git a/src/half_bridge.cpp b/src/half_bridge.cpp index e00a1e8..b48f883 100644 --- a/src/half_bridge.cpp +++ b/src/half_bridge.cpp @@ -6,10 +6,14 @@ #include "half_bridge.h" +#include + #include #include "mcu.h" +#if DT_NODE_EXISTS(DT_PATH(dcdc)) + static uint16_t tim_arr; // auto-reload register (defines PWM frequency) static uint16_t tim_ccr_min; // capture/compare register min/max static uint16_t tim_ccr_max; @@ -17,24 +21,18 @@ static uint16_t tim_dt_clocks; static bool pwm_enabled; -#if defined(CONFIG_SOC_SERIES_STM32L0X) +#ifndef UNIT_TEST + +// Get address of used timer from board dts +#define PWM_TIMER_ADDR DT_REG_ADDR(DT_PHANDLE(DT_PATH(dcdc), timer)) + +#if PWM_TIMER_ADDR == TIM3_BASE static void pwm_init_registers(uint16_t arr) { - // Enable peripheral clock of GPIOB - RCC->IOPENR |= RCC_IOPENR_IOPBEN; - // Enable TIM3 clock RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; - // Select alternate function mode on PB0 and PB1 (first bit _1 = 1, second bit _0 = 0) - GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE0)) | GPIO_MODER_MODE0_1; - GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1; - - // Select AF2 on PB0 and PB1 - GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL0_Pos; - GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL1_Pos; - // No prescaler --> timer frequency == SystemClock TIM3->PSC = 0; @@ -54,10 +52,6 @@ static void pwm_init_registers(uint16_t arr) // TIM_CR1_CEN = 1: Counter enable TIM3->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN; - // Control Register 2 - // OIS1 = OIS1N = 0: Output Idle State is set to off - //TIM3->CR2 |= ; - // Force update generation (UG = 1) TIM3->EGR |= TIM_EGR_UG; @@ -71,8 +65,6 @@ static void pwm_start() // Capture/Compare Enable Register // CCxE = 1: Enable the output on OCx // CCxP = 0: Active high polarity on OCx (default) - // CCxNE = 1: Enable the output on OC1N - // CCxNP = 0: Active high polarity on OC1N (default) TIM3->CCER |= TIM_CCER_CC3E; TIM3->CCER |= TIM_CCER_CC4E; } @@ -94,31 +86,13 @@ static void pwm_set_ccr(uint16_t ccr) TIM3->CCR4 = ccr + tim_dt_clocks; // low-side } -#elif defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32G4X) +#elif PWM_TIMER_ADDR == TIM1_BASE static void pwm_init_registers(uint16_t arr) { - // Enable peripheral clock of GPIOA and GPIOB -#ifdef CONFIG_SOC_SERIES_STM32F0X - RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; -#else // CONFIG_SOC_SERIES_STM32G4X - RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN | RCC_AHB2ENR_GPIOBEN; -#endif - // Enable TIM1 clock RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; - GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8)) | GPIO_MODER_MODER8_1; - GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER13)) | GPIO_MODER_MODER13_1; - - // Select AF2 on PA8 (TIM1_CH1) and PB13 (TIM1_CH1N) -#ifdef CONFIG_SOC_SERIES_STM32F0X - GPIOA->AFR[1] |= 0x2 << GPIO_AFRH_AFSEL8_Pos; - GPIOB->AFR[1] |= 0x2 << GPIO_AFRH_AFSEL13_Pos; -#else // CONFIG_SOC_SERIES_STM32G4X - GPIOA->AFR[1] |= 0x6 << GPIO_AFRH_AFSEL8_Pos; - GPIOB->AFR[1] |= 0x6 << GPIO_AFRH_AFSEL13_Pos; -#endif // No prescaler --> timer frequency == SystemClock TIM1->PSC = 0; @@ -132,17 +106,13 @@ static void pwm_init_registers(uint16_t arr) // CC1P = 0: Active high polarity on OC1 (default) // CC1NE = 1: Enable the output on OC1N // CC1NP = 0: Active high polarity on OC1N (default) - TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE; // | TIM_CCER_CC1NP; + TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE; // Control Register 1 // TIM_CR1_CMS = 01: Select center-aligned mode 1 // TIM_CR1_CEN = 1: Counter enable TIM1->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN; - // Control Register 2 - // OIS1 = OIS1N = 0: Output Idle State is set to off - //TIM1->CR2 |= ; - #ifdef CONFIG_SOC_SERIES_STM32G4X // Boards with STM32G4 MCU use ADC2 for synchronized ADC sampling. We use OC2 // for triggering, but but don't configure any pin for PWM mode @@ -197,7 +167,9 @@ static void pwm_set_ccr(uint32_t ccr) TIM1->CCR2 = 1; } -#elif defined(UNIT_TEST) +#endif // TIM1 + +#else // UNIT_TEST const uint32_t SystemCoreClock = 24000000; @@ -308,3 +280,5 @@ bool half_bridge_enabled() { return pwm_enabled; } + +#endif // DT_NODE_EXISTS(DT_PATH(dcdc)) diff --git a/zephyr/dts/bindings/dcdc.yaml b/zephyr/dts/bindings/dcdc.yaml index 0d74f75..3c32903 100644 --- a/zephyr/dts/bindings/dcdc.yaml +++ b/zephyr/dts/bindings/dcdc.yaml @@ -6,6 +6,10 @@ description: DC/DC converter as part of MPPT charge controllers compatible: "dcdc" properties: + timer: + type: phandle + required: true + description: Timer used for PWM signal generation pwm-frequency: type: int required: true