From 293b9fc29797261fc31f3d160d9cc48ba1ac2398 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Thu, 16 Feb 2017 11:18:23 +0100 Subject: [PATCH 1/6] STM32 L1: Define PWM Channels in PeripheralsPins.c As done for other families, let's define the PWM channel in the PWM pins table definition rather than driver. --- .../TARGET_MOTE_L152RC/PeripheralPins.c | 68 +++++++++---------- .../TARGET_NUCLEO_L152RE/PeripheralPins.c | 68 +++++++++---------- .../TARGET_NZ32_SC151/PeripheralPins.c | 68 +++++++++---------- .../TARGET_XDOT_L151CC/PeripheralPins.c | 14 ++-- .../TARGET_STM32L1/common_objects.h | 2 + .../TARGET_STM/TARGET_STM32L1/pwmout_api.c | 55 +++++++-------- 6 files changed, 134 insertions(+), 141 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c index 8c8be93926c..8a26780f570 100755 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c @@ -92,40 +92,40 @@ const PinMap PinMap_I2C_SCL[] = { // TIM5 cannot be used because already used by the us_ticker. const PinMap PinMap_PWM[] = { -// {PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 -// {PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 -// {PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3 -// {PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 -// {PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4 -// {PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 -// {PA_6, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 -// {PA_7, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 - {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 - {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1 - {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2 - {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3 -// {PB_8, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4 -// {PB_9, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 - {PB_11, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 - {PB_12, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_13, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PB_14, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 +// {PA_0, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_1, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 +// {PA_1, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_2, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 +// {PA_2, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 3, 0)}, // TIM5_CH3 +// {PA_2, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PA_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 +// {PA_3, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 4, 0)}, // TIM5_CH4 +// {PA_3, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PA_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 +// {PA_6, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PA_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 +// {PA_7, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_0, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PB_1, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 + {PB_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 + {PB_4, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PB_5, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PB_6, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 1, 0)}, // TIM4_CH1 + {PB_7, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 2, 0)}, // TIM4_CH2 + {PB_8, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3 +// {PB_8, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_9, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4 +// {PB_9, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_10, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 + {PB_11, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 + {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PC_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PC_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PC_8, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PC_9, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c index 89293e669ad..64910782d67 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c @@ -92,40 +92,40 @@ const PinMap PinMap_I2C_SCL[] = { // TIM5 cannot be used because already used by the us_ticker. const PinMap PinMap_PWM[] = { -// {PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 -// {PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 -// {PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3 -// {PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 -// {PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4 -// {PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 -// {PA_6, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 -// {PA_7, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 - {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 - {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1 - {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2 - {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3 -// {PB_8, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4 -// {PB_9, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 - {PB_11, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 - {PB_12, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_13, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PB_14, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 +// {PA_0, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_1, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 +// {PA_1, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_2, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 +// {PA_2, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 3, 0)}, // TIM5_CH3 +// {PA_2, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PA_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 +// {PA_3, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 4, 0)}, // TIM5_CH4 +// {PA_3, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PA_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 +// {PA_6, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PA_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 +// {PA_7, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_0, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PB_1, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 + {PB_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 + {PB_4, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PB_5, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PB_6, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 1, 0)}, // TIM4_CH1 + {PB_7, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 2, 0)}, // TIM4_CH2 + {PB_8, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3 +// {PB_8, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_9, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4 +// {PB_9, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_10, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 + {PB_11, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 + {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PC_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PC_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PC_8, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PC_9, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c index 5db8665982f..e63f41f54e1 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c @@ -92,40 +92,40 @@ const PinMap PinMap_I2C_SCL[] = { // TIM5 cannot be used because already used by the us_ticker. const PinMap PinMap_PWM[] = { -// {PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 -// {PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1 - {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 -// {PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3 -// {PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 -// {PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4 -// {PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 -// {PA_6, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 -// {PA_7, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 - {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 - {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1 - {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2 - {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3 -// {PB_8, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4 -// {PB_9, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3 - {PB_11, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4 - {PB_12, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_13, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PB_14, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 - {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 - {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2 - {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4 +// {PA_0, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_1, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 +// {PA_1, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1 + {PA_2, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 +// {PA_2, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 3, 0)}, // TIM5_CH3 +// {PA_2, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PA_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 +// {PA_3, PWM_5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 4, 0)}, // TIM5_CH4 +// {PA_3, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PA_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 +// {PA_6, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PA_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 +// {PA_7, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_0, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PB_1, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 + {PB_3, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2 + {PB_4, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PB_5, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PB_6, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 1, 0)}, // TIM4_CH1 + {PB_7, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 2, 0)}, // TIM4_CH2 + {PB_8, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3 +// {PB_8, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_9, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4 +// {PB_9, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PB_10, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 + {PB_11, PWM_2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 + {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 + {PC_6, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1 + {PC_7, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2 + {PC_8, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PC_9, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4 {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c index 784776ee7fd..2219954bfe9 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c @@ -79,13 +79,13 @@ const PinMap PinMap_I2C_SCL[] = { // TIM2 cannot be used because it is used to clock the SE // TIM5 cannot be used because already used by the us_ticker. const PinMap PinMap_PWM[] = { - {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3 - {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3 - {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4 - {PB_12, PWM_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1 - {PB_13, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1 - {PB_14, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1 + {PB_0, PWM_3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3 + {PB_8, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3 + {PB_9, PWM_4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4 + {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 + {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 + {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11), 1, 0}, // TIM11_CH1 {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h index ae4f8cf9f8c..f40ffcdb32e 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h @@ -45,6 +45,8 @@ struct pwmout_s { uint32_t prescaler; uint32_t period; uint32_t pulse; + uint8_t channel; + uint8_t inverted; }; struct serial_s { diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c index 6e4e85e835c..02ce8a22b57 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c @@ -47,6 +47,12 @@ void pwmout_init(pwmout_t* obj, PinName pin) error("PWM error: pinout mapping failed."); } + // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object + uint32_t function = pinmap_function(pin, PinMap_PWM); + MBED_ASSERT(function != (uint32_t)NC); + obj->channel = STM_PIN_CHANNEL(function); + obj->inverted = STM_PIN_INVERTED(function); + // Enable TIM clock if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); @@ -92,50 +98,35 @@ void pwmout_write(pwmout_t* obj, float value) sConfig.OCMode = TIM_OCMODE_PWM1; sConfig.Pulse = obj->pulse / obj->prescaler; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_ENABLE; - - switch (obj->pin) { - // Channels 1 - case PA_6: - case PB_4: - case PB_6: - case PB_12: - case PB_13: - case PB_15: - case PC_6: + sConfig.OCFastMode = TIM_OCFAST_DISABLE; +#ifdef TIM_OCIDLESTATE_RESET + sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; +#endif +#ifdef TIM_OCNIDLESTATE_RESET + sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; +#endif + + switch (obj->channel) { + case 1: channel = TIM_CHANNEL_1; break; - // Channels 2 - case PA_1: - case PA_7: - case PB_3: - case PB_5: - case PB_7: - case PB_14: - case PC_7: + case 2: channel = TIM_CHANNEL_2; break; - // Channels 3 - case PA_2: - case PB_0: - case PB_8: - case PB_10: - case PC_8: + case 3: channel = TIM_CHANNEL_3; break; - // Channels 4 - case PA_3: - case PB_1: - case PB_9: - case PB_11: - case PC_9: + case 4: channel = TIM_CHANNEL_4; break; default: return; } - HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel); + if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { + error("Cannot initialize PWM\n"); + } + HAL_TIM_PWM_Start(&TimHandle, channel); } From 1cbb3e18ac1892e235c7b7ef08e6387a0f7aa50d Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Thu, 16 Feb 2017 15:28:25 +0100 Subject: [PATCH 2/6] STM32: make PWM driver into a common file The pwmout driver is very similar for each STM32 family. The only family specific part is defined in pwmout_device.h file. It mainly contains few specific information: - The mapping of PWM/TIMERS to APB1 or APB2 so that we can get the clock - The clock calculation uses the right APB clock, which was sometimes not the case before and could have lead to errors in case dividers were enabled on APB clock settings. This case is now covered. - Inactivation of inverted support on feaw families --- .../TARGET_STM/TARGET_STM32F0/pwmout_api.c | 214 ------------- .../TARGET_STM/TARGET_STM32F0/pwmout_device.h | 83 ++++++ .../TARGET_STM/TARGET_STM32F1/pwmout_api.c | 205 ------------- .../TARGET_STM/TARGET_STM32F1/pwmout_device.h | 72 +++++ targets/TARGET_STM/TARGET_STM32F2/objects.h | 1 + .../TARGET_STM/TARGET_STM32F2/pwmout_api.c | 282 ------------------ .../TARGET_STM/TARGET_STM32F2/pwmout_device.h | 96 ++++++ .../TARGET_STM/TARGET_STM32F3/pwmout_api.c | 259 ---------------- .../TARGET_STM/TARGET_STM32F3/pwmout_device.h | 87 ++++++ .../TARGET_STM/TARGET_STM32F4/pwmout_device.h | 96 ++++++ .../TARGET_STM/TARGET_STM32F7/pwmout_api.c | 253 ---------------- .../TARGET_STM/TARGET_STM32F7/pwmout_device.h | 96 ++++++ .../TARGET_STM/TARGET_STM32L0/pwmout_api.c | 204 ------------- .../TARGET_STM/TARGET_STM32L0/pwmout_device.h | 74 +++++ .../TARGET_STM/TARGET_STM32L1/pwmout_api.c | 212 ------------- .../TARGET_STM/TARGET_STM32L1/pwmout_device.h | 84 ++++++ .../TARGET_STM/TARGET_STM32L4/pwmout_api.c | 222 -------------- .../TARGET_STM/TARGET_STM32L4/pwmout_device.h | 87 ++++++ .../{TARGET_STM32F4 => }/pwmout_api.c | 139 ++++----- 19 files changed, 842 insertions(+), 1924 deletions(-) delete mode 100644 targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h create mode 100644 targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c create mode 100644 targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h rename targets/TARGET_STM/{TARGET_STM32F4 => }/pwmout_api.c (75%) diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c deleted file mode 100644 index 63368cc4c18..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c +++ /dev/null @@ -1,214 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) { - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock -#if defined(TIM1_BASE) - if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); -#endif -#if defined(TIM2_BASE) - if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); -#endif -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); -#endif -#if defined(TIM14_BASE) - if (obj->pwm == PWM_14) __TIM14_CLK_ENABLE(); -#endif -#if defined(TIM15_BASE) - if (obj->pwm == PWM_15) __TIM15_CLK_ENABLE(); -#endif -#if defined(TIM16_BASE) - if (obj->pwm == PWM_16) __TIM16_CLK_ENABLE(); -#endif -#if defined(TIM17_BASE) - if (obj->pwm == PWM_17) __TIM17_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) { - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) { - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - 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: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) { - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) { - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) { - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) { - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) { - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) { - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h new file mode 100644 index 00000000000..2862c30e6bf --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h @@ -0,0 +1,83 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_UNKNOWN = 1 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +/* there is only 1 APB clock in F0 family */ +#define PWMOUT_APB2_NOT_SUPPORTED + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB1}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB1}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB1}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB1}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c deleted file mode 100644 index 4eb3a28c4f3..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c +++ /dev/null @@ -1,205 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); - if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); - if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - int complementary_channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - 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: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - HAL_TIM_PWM_Init(&TimHandle); - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h new file mode 100644 index 00000000000..9f105e7ec6a --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h @@ -0,0 +1,72 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F2/objects.h b/targets/TARGET_STM/TARGET_STM32F2/objects.h index 1313627b844..3bd0a18c6c5 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F2/objects.h @@ -128,6 +128,7 @@ struct i2c_s { struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint8_t channel; diff --git a/targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c deleted file mode 100644 index e8c58bf4b2b..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F2/pwmout_api.c +++ /dev/null @@ -1,282 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock -#if defined(TIM1_BASE) - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); -#endif -#if defined(TIM2_BASE) - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); -#endif -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); -#endif -#if defined(TIM4_BASE) - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); -#endif -#if defined(TIM5_BASE) - if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE(); -#endif -#if defined(TIM8_BASE) - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); -#endif -#if defined(TIM9_BASE) - if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); -#endif -#if defined(TIM10_BASE) - if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); -#endif -#if defined(TIM11_BASE) - if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); -#endif -#if defined(TIM12_BASE) - if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); -#endif -#if defined(TIM13_BASE) - if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); -#endif -#if defined(TIM14_BASE) - if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - 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: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - RCC_ClkInitTypeDef RCC_ClkInitStruct; - uint32_t PclkFreq; - uint32_t APBxCLKDivider; - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - // Get clock configuration - // Note: PclkFreq contains here the Latency (not used after) - HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); - - // Get the PCLK and APBCLK divider related to the timer - switch (obj->pwm) { - - // APB1 clock -#if defined(TIM2_BASE) - case PWM_2: -#endif -#if defined(TIM3_BASE) - case PWM_3: -#endif -#if defined(TIM4_BASE) - case PWM_4: -#endif -#if defined(TIM5_BASE) - case PWM_5: -#endif -#if defined(TIM12_BASE) - case PWM_12: -#endif -#if defined(TIM13_BASE) - case PWM_13: -#endif -#if defined(TIM14_BASE) - case PWM_14: -#endif - PclkFreq = HAL_RCC_GetPCLK1Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; - break; - - // APB2 clock -#if defined(TIM1_BASE) - case PWM_1: -#endif -#if defined(TIM8_BASE) - case PWM_8: -#endif -#if defined(TIM9_BASE) - case PWM_9: -#endif -#if defined(TIM10_BASE) - case PWM_10: -#endif -#if defined(TIM11_BASE) - case PWM_11: -#endif - PclkFreq = HAL_RCC_GetPCLK2Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; - break; - default: - return; - } - - TimHandle.Init.Period = us - 1; - // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx - if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick - else - TimHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - // Set duty cycle again - pwmout_write(obj, dc); - - // Save for future use - obj->period = us; - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h new file mode 100644 index 00000000000..480493e3a19 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h @@ -0,0 +1,96 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c deleted file mode 100644 index 2f3c8a925b3..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F3/pwmout_api.c +++ /dev/null @@ -1,259 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the pin function and assign the used channel to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - -#if defined(TIM1_BASE) - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); -#endif -#if defined(TIM2_BASE) - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); -#endif -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); -#endif -#if defined(TIM4_BASE) - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); -#endif -#if defined(TIM5_BASE) - if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE(); -#endif -#if defined(TIM8_BASE) - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); -#endif -#if defined(TIM9_BASE) - if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); -#endif -#if defined(TIM10_BASE) - if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); -#endif -#if defined(TIM11_BASE) - if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); -#endif -#if defined(TIM12_BASE) - if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); -#endif -#if defined(TIM13_BASE) - if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); -#endif -#if defined(TIM14_BASE) - if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); -#endif -#if defined(TIM15_BASE) - if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE(); -#endif -#if defined(TIM16_BASE) - if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE(); -#endif -#if defined(TIM17_BASE) - if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE(); -#endif -#if defined(TIM18_BASE) - if (obj->pwm == PWM_18) __HAL_RCC_TIM18_CLK_ENABLE(); -#endif -#if defined(TIM19_BASE) - if (obj->pwm == PWM_19) __HAL_RCC_TIM19_CLK_ENABLE(); -#endif -#if defined(TIM20_BASE) - if (obj->pwm == PWM_20) __HAL_RCC_TIM20_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - 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: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - // Update the SystemCoreClock variable - SystemCoreClockUpdate(); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h new file mode 100644 index 00000000000..a0af9d6b014 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h @@ -0,0 +1,87 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB2}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB2}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB2}, +#endif +#if defined(TIM20_BASE) + {PWM_20, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h new file mode 100644 index 00000000000..480493e3a19 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h @@ -0,0 +1,96 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c deleted file mode 100644 index 1e95b476fbc..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F7/pwmout_api.c +++ /dev/null @@ -1,253 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); - if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); - if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); - if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); - if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); - if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); - if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - 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: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot configure PWM channel\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - RCC_ClkInitTypeDef RCC_ClkInitStruct; - uint32_t PclkFreq; - uint32_t APBxCLKDivider; - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - // Get clock configuration - // Note: PclkFreq contains here the Latency (not used after) - HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); - - // Get the PCLK and APBCLK divider related to the timer - switch (obj->pwm) { - - // APB1 clock - case PWM_2: - case PWM_3: - case PWM_4: - case PWM_5: - case PWM_12: - case PWM_13: - case PWM_14: - PclkFreq = HAL_RCC_GetPCLK1Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; - break; - - // APB2 clock - case PWM_1: - case PWM_8: - case PWM_9: - case PWM_10: - case PWM_11: - PclkFreq = HAL_RCC_GetPCLK2Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; - break; - default: - return; - } - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - - // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx - if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq) / 1000000) * obj->prescaler) - 1; // 1 us tick - else - TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; // 1 us tick - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h new file mode 100644 index 00000000000..480493e3a19 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h @@ -0,0 +1,96 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c deleted file mode 100644 index 4f2ef410c9b..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c +++ /dev/null @@ -1,204 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the pin function and assign the used channel to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); -#endif - if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE(); -#if defined(TIM22_BASE) - if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_ENABLE; - - switch (obj->channel) { - 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: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM"); - } - - HAL_TIM_PWM_Start(&TimHandle, channel); -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h new file mode 100644 index 00000000000..71f38e4f3fe --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h @@ -0,0 +1,74 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +#define PWMOUT_INVERTED_NOT_SUPPORTED + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM21_BASE) + {PWM_21, PWMOUT_ON_APB2}, +#endif +#if defined(TIM22_BASE) + {PWM_22, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c deleted file mode 100644 index 02ce8a22b57..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c +++ /dev/null @@ -1,212 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - - if (obj->pwm == (PWMName)NC) { - error("PWM error: pinout mapping failed."); - } - - // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); - if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); - if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE(); - if (obj->pwm == PWM_5) __TIM5_CLK_ENABLE(); - if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE(); - if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE(); - if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE(); - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_DISABLE; -#ifdef TIM_OCIDLESTATE_RESET - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; -#endif -#ifdef TIM_OCNIDLESTATE_RESET - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; -#endif - - switch (obj->channel) { - 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: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - HAL_TIM_PWM_Start(&TimHandle, channel); -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h new file mode 100644 index 00000000000..50cc41cd9e7 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h @@ -0,0 +1,84 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +/* L1 HAL do not offer Output Compare idle nor inverted mode*/ +#define PWMOUT_INVERTED_NOT_SUPPORTED + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c deleted file mode 100644 index 8b8cfe90684..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c +++ /dev/null @@ -1,222 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "pwmout_api.h" - -#if DEVICE_PWMOUT - -#include "cmsis.h" -#include "pinmap.h" -#include "mbed_error.h" -#include "PeripheralPins.h" - -static TIM_HandleTypeDef TimHandle; - -void pwmout_init(pwmout_t* obj, PinName pin) -{ - // Get the peripheral name from the pin and assign it to the object - obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - MBED_ASSERT(obj->pwm != (PWMName)NC); - - // Get the pin function and assign the used channel to the object - uint32_t function = pinmap_function(pin, PinMap_PWM); - MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); - obj->inverted = STM_PIN_INVERTED(function); - - // Enable TIM clock - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); -#if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); -#endif -#if defined(TIM4_BASE) - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); -#endif -#if defined(TIM5_BASE) - if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE(); -#endif -#if defined(TIM8_BASE) - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); -#endif - if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE(); - if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE(); -#if defined(TIM17_BASE) - if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE(); -#endif - - // Configure GPIO - pinmap_pinout(pin, PinMap_PWM); - - obj->pin = pin; - obj->period = 0; - obj->pulse = 0; - obj->prescaler = 1; - - pwmout_period_us(obj, 20000); // 20 ms per default -} - -void pwmout_free(pwmout_t* obj) -{ - // Configure GPIO - pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); -} - -void pwmout_write(pwmout_t* obj, float value) -{ - TIM_OC_InitTypeDef sConfig; - int channel = 0; - - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - if (value < (float)0.0) { - value = 0.0; - } else if (value > (float)1.0) { - value = 1.0; - } - - obj->pulse = (uint32_t)((float)obj->period * value); - - // Configure channels - sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse / obj->prescaler; - sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfig.OCFastMode = TIM_OCFAST_ENABLE; - sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; - sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; - - switch (obj->channel) { - 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: - return; - } - - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - if (obj->inverted) { - HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { - HAL_TIM_PWM_Start(&TimHandle, channel); - } -} - -float pwmout_read(pwmout_t* obj) -{ - float value = 0; - if (obj->period > 0) { - value = (float)(obj->pulse) / (float)(obj->period); - } - return ((value > (float)1.0) ? (float)(1.0) : (value)); -} - -void pwmout_period(pwmout_t* obj, float seconds) -{ - pwmout_period_us(obj, seconds * 1000000.0f); -} - -void pwmout_period_ms(pwmout_t* obj, int ms) -{ - pwmout_period_us(obj, ms * 1000); -} - -void pwmout_period_us(pwmout_t* obj, int us) -{ - TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); - - float dc = pwmout_read(obj); - - __HAL_TIM_DISABLE(&TimHandle); - - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; - } else { - obj->prescaler = 1; - } - TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); - - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { - error("Cannot initialize PWM\n"); - } - - // Save for future use - obj->period = us; - - // Set duty cycle again - pwmout_write(obj, dc); - - __HAL_TIM_ENABLE(&TimHandle); -} - -void pwmout_pulsewidth(pwmout_t* obj, float seconds) -{ - pwmout_pulsewidth_us(obj, seconds * 1000000.0f); -} - -void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) -{ - pwmout_pulsewidth_us(obj, ms * 1000); -} - -void pwmout_pulsewidth_us(pwmout_t* obj, int us) -{ - float value = (float)us / (float)obj->period; - pwmout_write(obj, value); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h new file mode 100644 index 00000000000..382d6fb1493 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h @@ -0,0 +1,87 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#ifndef MBED_PWMOUT_DEVICE_H +#define MBED_PWMOUT_DEVICE_H + +#include "cmsis.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef DEVICE_PWMOUT + +typedef enum { + PWMOUT_ON_APB1 = 0, + PWMOUT_ON_APB2 = 1, + PWMOUT_UNKNOWN = 2 +} PwmoutApb; + +/* Structure to describe Timers to APB */ +typedef struct pwm_apb_map { + PWMName pwm; // an index entry for each EXIT line + PwmoutApb pwmoutApb; +} pwm_apb_map_t; + +static const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB2}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB2}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT + +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F4/pwmout_api.c b/targets/TARGET_STM/pwmout_api.c similarity index 75% rename from targets/TARGET_STM/TARGET_STM32F4/pwmout_api.c rename to targets/TARGET_STM/pwmout_api.c index 04a66505f9a..f8a22e7035b 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/pwmout_api.c +++ b/targets/TARGET_STM/pwmout_api.c @@ -35,6 +35,7 @@ #include "pinmap.h" #include "mbed_error.h" #include "PeripheralPins.h" +#include "pwmout_device.h" static TIM_HandleTypeDef TimHandle; @@ -87,7 +88,30 @@ void pwmout_init(pwmout_t* obj, PinName pin) #if defined(TIM14_BASE) if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); #endif - +#if defined(TIM15_BASE) + if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE(); +#endif +#if defined(TIM16_BASE) + if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE(); +#endif +#if defined(TIM17_BASE) + if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE(); +#endif +#if defined(TIM18_BASE) + if (obj->pwm == PWM_18) __HAL_RCC_TIM18_CLK_ENABLE(); +#endif +#if defined(TIM19_BASE) + if (obj->pwm == PWM_19) __HAL_RCC_TIM19_CLK_ENABLE(); +#endif +#if defined(TIM20_BASE) + if (obj->pwm == PWM_20) __HAL_RCC_TIM20_CLK_ENABLE(); +#endif +#if defined(TIM21_BASE) + if (obj->pwm == PWM_21) __HAL_RCC_TIM21_CLK_ENABLE(); +#endif +#if defined(TIM22_BASE) + if (obj->pwm == PWM_22) __HAL_RCC_TIM22_CLK_ENABLE(); +#endif // Configure GPIO pinmap_pinout(pin, PinMap_PWM); @@ -124,10 +148,14 @@ void pwmout_write(pwmout_t* obj, float value) sConfig.OCMode = TIM_OCMODE_PWM1; sConfig.Pulse = obj->pulse / obj->prescaler; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfig.OCFastMode = TIM_OCFAST_DISABLE; +#if defined(TIM_OCIDLESTATE_RESET) sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; +#endif +#if defined(TIM_OCNIDLESTATE_RESET) + sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; +#endif switch (obj->channel) { case 1: @@ -150,9 +178,12 @@ void pwmout_write(pwmout_t* obj, float value) error("Cannot initialize PWM\n"); } +#if !defined(PWMOUT_INVERTED_NOT_SUPPORTED) if (obj->inverted) { HAL_TIMEx_PWMN_Start(&TimHandle, channel); - } else { + } else +#endif + { HAL_TIM_PWM_Start(&TimHandle, channel); } } @@ -180,9 +211,10 @@ void pwmout_period_us(pwmout_t* obj, int us) { TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); RCC_ClkInitTypeDef RCC_ClkInitStruct; - uint32_t PclkFreq; - uint32_t APBxCLKDivider; + uint32_t PclkFreq = 0; + uint32_t APBxCLKDivider = RCC_HCLK_DIV1; float dc = pwmout_read(obj); + uint8_t i = 0; __HAL_TIM_DISABLE(&TimHandle); @@ -190,82 +222,43 @@ void pwmout_period_us(pwmout_t* obj, int us) // Note: PclkFreq contains here the Latency (not used after) HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); - // Get the PCLK and APBCLK divider related to the timer - switch (obj->pwm) { - - // APB1 clock -#if defined(TIM2_BASE) - case PWM_2: -#endif -#if defined(TIM3_BASE) - case PWM_3: -#endif -#if defined(TIM4_BASE) - case PWM_4: -#endif -#if defined(TIM5_BASE) - case PWM_5: -#endif -#if defined(TIM12_BASE) - case PWM_12: -#endif -#if defined(TIM13_BASE) - case PWM_13: -#endif -#if defined(TIM14_BASE) - case PWM_14: -#endif - PclkFreq = HAL_RCC_GetPCLK1Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; - break; - - // APB2 clock -#if defined(TIM1_BASE) - case PWM_1: -#endif -#if defined(TIM8_BASE) - case PWM_8: -#endif -#if defined(TIM9_BASE) - case PWM_9: -#endif -#if defined(TIM10_BASE) - case PWM_10: -#endif -#if defined(TIM11_BASE) - case PWM_11: -#endif - PclkFreq = HAL_RCC_GetPCLK2Freq(); - APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; - break; - default: - return; + /* Parse the pwm / apb mapping table to find the right entry */ + while(pwm_apb_map_table[i].pwm != obj->pwm) { + i++; } - /* To make it simple, we use to possible prescaler values which lead to: - * pwm unit = 1us, period/pulse can be from 1us to 65535us - * or - * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec - * Be careful that all the channels of a PWM shares the same prescaler - */ - if (us > 0xFFFF) { - obj->prescaler = 500; + if(pwm_apb_map_table[i].pwm == 0) + error("Unknown PWM instance"); + + if(pwm_apb_map_table[i].pwmoutApb == PWMOUT_ON_APB1) { + PclkFreq = HAL_RCC_GetPCLK1Freq(); + APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; } else { - obj->prescaler = 1; +#if !defined(PWMOUT_APB2_NOT_SUPPORTED) + PclkFreq = HAL_RCC_GetPCLK2Freq(); + APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; +#endif } + + /* By default use, 1us as SW pre-scaler */ + obj->prescaler = 1; // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq) / 1000000) * obj->prescaler) - 1; // 1 us tick + TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick else - TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; // 1 us tick - - if (TimHandle.Init.Prescaler > 0xFFFF) - error("PWM: out of range prescaler"); - - TimHandle.Init.Period = (us - 1) / obj->prescaler; - if (TimHandle.Init.Period > 0xFFFF) - error("PWM: out of range period"); + TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000)) - 1; // 1 us tick + TimHandle.Init.Period = (us - 1); + + /* In case period or pre-scalers are out of range, loop-in to get valid values */ + while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Period > 0xFFFF)) { + obj->prescaler = obj->prescaler * 2; + if (APBxCLKDivider == RCC_HCLK_DIV1) + TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; + else + TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; + TimHandle.Init.Period = (us - 1) / obj->prescaler; + } TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; From 8e5d2eb1dcccee29bd2447395f897e502c8b1723 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Mon, 27 Feb 2017 13:47:34 +0100 Subject: [PATCH 3/6] STM32: pwm period and prescaler calculation Correct the while loop limit and add a safe guard to avoid infinite loop. --- targets/TARGET_STM/pwmout_api.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/targets/TARGET_STM/pwmout_api.c b/targets/TARGET_STM/pwmout_api.c index f8a22e7035b..aa015560732 100644 --- a/targets/TARGET_STM/pwmout_api.c +++ b/targets/TARGET_STM/pwmout_api.c @@ -251,13 +251,19 @@ void pwmout_period_us(pwmout_t* obj, int us) TimHandle.Init.Period = (us - 1); /* In case period or pre-scalers are out of range, loop-in to get valid values */ - while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Period > 0xFFFF)) { + while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Prescaler > 0xFFFF)) { obj->prescaler = obj->prescaler * 2; if (APBxCLKDivider == RCC_HCLK_DIV1) TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; else TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; TimHandle.Init.Period = (us - 1) / obj->prescaler; + /* Period decreases and prescaler increases over loops, so check for + * possible out of range cases */ + if ((TimHandle.Init.Period < 0xFFFF) && (TimHandle.Init.Prescaler > 0xFFFF)) { + error("Cannot initialize PWM\n"); + break; + } } TimHandle.Init.ClockDivision = 0; From 747e8e1ea1e70777bbfb2afae3463c60ffd78a5b Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Mon, 27 Feb 2017 13:48:55 +0100 Subject: [PATCH 4/6] STM32: move pwmout device tables to C file In order to avoid possible multiple definitions errors, move the table initialization to the C file instead of header file --- .../TARGET_STM/TARGET_STM32F0/pwmout_device.c | 62 +++++++++++++++ .../TARGET_STM/TARGET_STM32F0/pwmout_device.h | 26 +------ .../TARGET_STM/TARGET_STM32F1/pwmout_device.c | 53 +++++++++++++ .../TARGET_STM/TARGET_STM32F1/pwmout_device.h | 17 +--- .../TARGET_STM/TARGET_STM32F2/pwmout_device.c | 77 +++++++++++++++++++ .../TARGET_STM/TARGET_STM32F2/pwmout_device.h | 41 +--------- .../TARGET_STM/TARGET_STM32F3/pwmout_device.c | 68 ++++++++++++++++ .../TARGET_STM/TARGET_STM32F3/pwmout_device.h | 32 +------- .../TARGET_STM/TARGET_STM32F4/pwmout_device.c | 77 +++++++++++++++++++ .../TARGET_STM/TARGET_STM32F4/pwmout_device.h | 41 +--------- .../TARGET_STM/TARGET_STM32F7/pwmout_device.c | 77 +++++++++++++++++++ .../TARGET_STM/TARGET_STM32F7/pwmout_device.h | 41 +--------- .../TARGET_STM/TARGET_STM32L0/pwmout_device.c | 53 +++++++++++++ .../TARGET_STM/TARGET_STM32L0/pwmout_device.h | 17 +--- .../TARGET_STM/TARGET_STM32L1/pwmout_device.c | 62 +++++++++++++++ .../TARGET_STM/TARGET_STM32L1/pwmout_device.h | 26 +------ .../TARGET_STM/TARGET_STM32L4/pwmout_device.c | 68 ++++++++++++++++ .../TARGET_STM/TARGET_STM32L4/pwmout_device.h | 32 +------- 18 files changed, 606 insertions(+), 264 deletions(-) create mode 100644 targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c create mode 100644 targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c new file mode 100644 index 00000000000..369c7443ec4 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.c @@ -0,0 +1,62 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB1}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB1}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB1}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB1}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h index 2862c30e6bf..91837e63474 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_device.h @@ -52,31 +52,7 @@ typedef struct pwm_apb_map { /* there is only 1 APB clock in F0 family */ #define PWMOUT_APB2_NOT_SUPPORTED -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM14_BASE) - {PWM_14, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB1}, -#endif -#if defined(TIM15_BASE) - {PWM_15, PWMOUT_ON_APB1}, -#endif -#if defined(TIM16_BASE) - {PWM_16, PWMOUT_ON_APB1}, -#endif -#if defined(TIM17_BASE) - {PWM_17, PWMOUT_ON_APB1}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c new file mode 100644 index 00000000000..3331a660cab --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.c @@ -0,0 +1,53 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h index 9f105e7ec6a..0d81dd547a0 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_device.h @@ -50,22 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c new file mode 100644 index 00000000000..a1e131e998f --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.c @@ -0,0 +1,77 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h index 480493e3a19..0d81dd547a0 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F2/pwmout_device.h @@ -50,46 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM12_BASE) - {PWM_12, PWMOUT_ON_APB1}, -#endif -#if defined(TIM13_BASE) - {PWM_13, PWMOUT_ON_APB1}, -#endif -#if defined(TIM14_BASE) - {PWM_14, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM9_BASE) - {PWM_9, PWMOUT_ON_APB2}, -#endif -#if defined(TIM10_BASE) - {PWM_10, PWMOUT_ON_APB2}, -#endif -#if defined(TIM11_BASE) - {PWM_11, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c new file mode 100644 index 00000000000..939331a3fdd --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.c @@ -0,0 +1,68 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB2}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB2}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB2}, +#endif +#if defined(TIM20_BASE) + {PWM_20, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h index a0af9d6b014..0d81dd547a0 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F3/pwmout_device.h @@ -50,37 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM15_BASE) - {PWM_15, PWMOUT_ON_APB2}, -#endif -#if defined(TIM16_BASE) - {PWM_16, PWMOUT_ON_APB2}, -#endif -#if defined(TIM17_BASE) - {PWM_17, PWMOUT_ON_APB2}, -#endif -#if defined(TIM20_BASE) - {PWM_20, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c new file mode 100644 index 00000000000..a1e131e998f --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.c @@ -0,0 +1,77 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h index 480493e3a19..0d81dd547a0 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F4/pwmout_device.h @@ -50,46 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM12_BASE) - {PWM_12, PWMOUT_ON_APB1}, -#endif -#if defined(TIM13_BASE) - {PWM_13, PWMOUT_ON_APB1}, -#endif -#if defined(TIM14_BASE) - {PWM_14, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM9_BASE) - {PWM_9, PWMOUT_ON_APB2}, -#endif -#if defined(TIM10_BASE) - {PWM_10, PWMOUT_ON_APB2}, -#endif -#if defined(TIM11_BASE) - {PWM_11, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c new file mode 100644 index 00000000000..a1e131e998f --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.c @@ -0,0 +1,77 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM12_BASE) + {PWM_12, PWMOUT_ON_APB1}, +#endif +#if defined(TIM13_BASE) + {PWM_13, PWMOUT_ON_APB1}, +#endif +#if defined(TIM14_BASE) + {PWM_14, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h index 480493e3a19..0d81dd547a0 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32F7/pwmout_device.h @@ -50,46 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM12_BASE) - {PWM_12, PWMOUT_ON_APB1}, -#endif -#if defined(TIM13_BASE) - {PWM_13, PWMOUT_ON_APB1}, -#endif -#if defined(TIM14_BASE) - {PWM_14, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM9_BASE) - {PWM_9, PWMOUT_ON_APB2}, -#endif -#if defined(TIM10_BASE) - {PWM_10, PWMOUT_ON_APB2}, -#endif -#if defined(TIM11_BASE) - {PWM_11, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c new file mode 100644 index 00000000000..c91d83f9021 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.c @@ -0,0 +1,53 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM21_BASE) + {PWM_21, PWMOUT_ON_APB2}, +#endif +#if defined(TIM22_BASE) + {PWM_22, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h index 71f38e4f3fe..bdb493818d3 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32L0/pwmout_device.h @@ -52,22 +52,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM21_BASE) - {PWM_21, PWMOUT_ON_APB2}, -#endif -#if defined(TIM22_BASE) - {PWM_22, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c new file mode 100644 index 00000000000..1b9580f2eae --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.c @@ -0,0 +1,62 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM9_BASE) + {PWM_9, PWMOUT_ON_APB2}, +#endif +#if defined(TIM10_BASE) + {PWM_10, PWMOUT_ON_APB2}, +#endif +#if defined(TIM11_BASE) + {PWM_11, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h index 50cc41cd9e7..822156a709a 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_device.h @@ -53,31 +53,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM9_BASE) - {PWM_9, PWMOUT_ON_APB2}, -#endif -#if defined(TIM10_BASE) - {PWM_10, PWMOUT_ON_APB2}, -#endif -#if defined(TIM11_BASE) - {PWM_11, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c new file mode 100644 index 00000000000..708d30534a9 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.c @@ -0,0 +1,68 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "cmsis.h" +#include "pwmout_api.h" +#include "pwmout_device.h" + +#ifdef DEVICE_PWMOUT + +const pwm_apb_map_t pwm_apb_map_table[] = +{ +#if defined(TIM2_BASE) + {PWM_2, PWMOUT_ON_APB1}, +#endif +#if defined(TIM3_BASE) + {PWM_3, PWMOUT_ON_APB1}, +#endif +#if defined(TIM4_BASE) + {PWM_4, PWMOUT_ON_APB1}, +#endif +#if defined(TIM5_BASE) + {PWM_5, PWMOUT_ON_APB1}, +#endif +#if defined(TIM1_BASE) + {PWM_1, PWMOUT_ON_APB2}, +#endif +#if defined(TIM8_BASE) + {PWM_8, PWMOUT_ON_APB2}, +#endif +#if defined(TIM15_BASE) + {PWM_15, PWMOUT_ON_APB2}, +#endif +#if defined(TIM16_BASE) + {PWM_16, PWMOUT_ON_APB2}, +#endif +#if defined(TIM17_BASE) + {PWM_17, PWMOUT_ON_APB2}, +#endif + {(PWMName) 0, PWMOUT_UNKNOWN} +}; + +#endif // DEVICE_PWMOUT diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h index 382d6fb1493..0d81dd547a0 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h +++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_device.h @@ -50,37 +50,7 @@ typedef struct pwm_apb_map { PwmoutApb pwmoutApb; } pwm_apb_map_t; -static const pwm_apb_map_t pwm_apb_map_table[] = -{ -#if defined(TIM2_BASE) - {PWM_2, PWMOUT_ON_APB1}, -#endif -#if defined(TIM3_BASE) - {PWM_3, PWMOUT_ON_APB1}, -#endif -#if defined(TIM4_BASE) - {PWM_4, PWMOUT_ON_APB1}, -#endif -#if defined(TIM5_BASE) - {PWM_5, PWMOUT_ON_APB1}, -#endif -#if defined(TIM1_BASE) - {PWM_1, PWMOUT_ON_APB2}, -#endif -#if defined(TIM8_BASE) - {PWM_8, PWMOUT_ON_APB2}, -#endif -#if defined(TIM15_BASE) - {PWM_15, PWMOUT_ON_APB2}, -#endif -#if defined(TIM16_BASE) - {PWM_16, PWMOUT_ON_APB2}, -#endif -#if defined(TIM17_BASE) - {PWM_17, PWMOUT_ON_APB2}, -#endif - {(PWMName) 0, PWMOUT_UNKNOWN} -}; +extern const pwm_apb_map_t pwm_apb_map_table[]; #endif // DEVICE_PWMOUT From 3842f6ea0a8ef83c90274377609d425f3c3bdf28 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Mon, 27 Feb 2017 13:58:33 +0100 Subject: [PATCH 5/6] STM32: fix formatting Use the recommended style if (condition) { do(); } else { do_else(); } --- targets/TARGET_STM/pwmout_api.c | 102 ++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 30 deletions(-) diff --git a/targets/TARGET_STM/pwmout_api.c b/targets/TARGET_STM/pwmout_api.c index aa015560732..552fde0a792 100644 --- a/targets/TARGET_STM/pwmout_api.c +++ b/targets/TARGET_STM/pwmout_api.c @@ -53,64 +53,104 @@ void pwmout_init(pwmout_t* obj, PinName pin) // Enable TIM clock #if defined(TIM1_BASE) - if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); + if (obj->pwm == PWM_1){ + __HAL_RCC_TIM1_CLK_ENABLE(); + } #endif #if defined(TIM2_BASE) - if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); + if (obj->pwm == PWM_2) { + __HAL_RCC_TIM2_CLK_ENABLE(); + } #endif #if defined(TIM3_BASE) - if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); + if (obj->pwm == PWM_3) { + __HAL_RCC_TIM3_CLK_ENABLE(); + } #endif #if defined(TIM4_BASE) - if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); + if (obj->pwm == PWM_4) { + __HAL_RCC_TIM4_CLK_ENABLE(); + } #endif #if defined(TIM5_BASE) - if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE(); + if (obj->pwm == PWM_5) { + __HAL_RCC_TIM5_CLK_ENABLE(); + } #endif #if defined(TIM8_BASE) - if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); + if (obj->pwm == PWM_8) { + __HAL_RCC_TIM8_CLK_ENABLE(); + } #endif #if defined(TIM9_BASE) - if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); + if (obj->pwm == PWM_9) { + __HAL_RCC_TIM9_CLK_ENABLE(); + } #endif #if defined(TIM10_BASE) - if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); + if (obj->pwm == PWM_10) { + __HAL_RCC_TIM10_CLK_ENABLE(); + } #endif #if defined(TIM11_BASE) - if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); + if (obj->pwm == PWM_11) { + __HAL_RCC_TIM11_CLK_ENABLE(); + } #endif #if defined(TIM12_BASE) - if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); + if (obj->pwm == PWM_12) { + __HAL_RCC_TIM12_CLK_ENABLE(); + } #endif #if defined(TIM13_BASE) - if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); + if (obj->pwm == PWM_13) { + __HAL_RCC_TIM13_CLK_ENABLE(); + } #endif #if defined(TIM14_BASE) - if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); + if (obj->pwm == PWM_14) { + __HAL_RCC_TIM14_CLK_ENABLE(); + } #endif #if defined(TIM15_BASE) - if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE(); + if (obj->pwm == PWM_15) { + __HAL_RCC_TIM15_CLK_ENABLE(); + } #endif #if defined(TIM16_BASE) - if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE(); + if (obj->pwm == PWM_16) { + __HAL_RCC_TIM16_CLK_ENABLE(); + } #endif #if defined(TIM17_BASE) - if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE(); + if (obj->pwm == PWM_17) { + __HAL_RCC_TIM17_CLK_ENABLE(); + } #endif #if defined(TIM18_BASE) - if (obj->pwm == PWM_18) __HAL_RCC_TIM18_CLK_ENABLE(); + if (obj->pwm == PWM_18) { + __HAL_RCC_TIM18_CLK_ENABLE(); + } #endif #if defined(TIM19_BASE) - if (obj->pwm == PWM_19) __HAL_RCC_TIM19_CLK_ENABLE(); + if (obj->pwm == PWM_19) { + __HAL_RCC_TIM19_CLK_ENABLE(); + } #endif #if defined(TIM20_BASE) - if (obj->pwm == PWM_20) __HAL_RCC_TIM20_CLK_ENABLE(); + if (obj->pwm == PWM_20) { + __HAL_RCC_TIM20_CLK_ENABLE(); + } #endif #if defined(TIM21_BASE) - if (obj->pwm == PWM_21) __HAL_RCC_TIM21_CLK_ENABLE(); + if (obj->pwm == PWM_21) { + __HAL_RCC_TIM21_CLK_ENABLE(); + } #endif #if defined(TIM22_BASE) - if (obj->pwm == PWM_22) __HAL_RCC_TIM22_CLK_ENABLE(); + if (obj->pwm == PWM_22) { + __HAL_RCC_TIM22_CLK_ENABLE(); + } #endif // Configure GPIO pinmap_pinout(pin, PinMap_PWM); @@ -244,20 +284,22 @@ void pwmout_period_us(pwmout_t* obj, int us) /* By default use, 1us as SW pre-scaler */ obj->prescaler = 1; // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx - if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick - else - TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000)) - 1; // 1 us tick - TimHandle.Init.Period = (us - 1); + if (APBxCLKDivider == RCC_HCLK_DIV1) { + TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick + } else { + TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000)) - 1; // 1 us tick + } + TimHandle.Init.Period = (us - 1); /* In case period or pre-scalers are out of range, loop-in to get valid values */ while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Prescaler > 0xFFFF)) { obj->prescaler = obj->prescaler * 2; - if (APBxCLKDivider == RCC_HCLK_DIV1) - TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; - else - TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; - TimHandle.Init.Period = (us - 1) / obj->prescaler; + if (APBxCLKDivider == RCC_HCLK_DIV1) { + TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; + } else { + TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; + } + TimHandle.Init.Period = (us - 1) / obj->prescaler; /* Period decreases and prescaler increases over loops, so check for * possible out of range cases */ if ((TimHandle.Init.Period < 0xFFFF) && (TimHandle.Init.Prescaler > 0xFFFF)) { From 400b89eedaf72a08eacb172252499011367b2096 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Tue, 7 Mar 2017 17:22:25 +0100 Subject: [PATCH 6/6] Fix XDOT compilation error Typo with misplaced closing parenthesis leads to compilation error, which is fixed with this patch --- .../TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c index 2219954bfe9..4a8ab11503f 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c @@ -85,7 +85,7 @@ const PinMap PinMap_PWM[] = { {PB_12, PWM_10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1 {PB_13, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 {PB_14, PWM_9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 - {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11), 1, 0}, // TIM11_CH1 + {PB_15, PWM_11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1 {NC, NC, 0} };