mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
Diff: targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c
- Revision:
- 174:8bb9f3a33240
- Parent:
- 167:d5744491c362
- Child:
- 208:4557f4bb2dd5
diff -r 033f1c328f6e -r 8bb9f3a33240 targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c --- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c Mon Apr 28 18:15:06 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c Tue Apr 29 11:15:07 2014 +0100 @@ -29,6 +29,8 @@ */ #include "pwmout_api.h" +#if DEVICE_PWMOUT + #include "cmsis.h" #include "pinmap.h" #include "error.h" @@ -40,7 +42,7 @@ {PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 - Default (warning: not connected on D0 per default) {PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 - Default {PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 - Default - //{PA_7, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH1N - GPIO_PartialRemap_TIM1 +// {PA_7, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH1N - GPIO_PartialRemap_TIM1 {PA_8, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH1 - Default {PA_9, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH2 - Default {PA_10, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH3 - Default @@ -48,22 +50,22 @@ {PA_15, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 8)}, // TIM2_CH1_ETR - GPIO_FullRemap_TIM2 {PB_0, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH3 - Default - //{PB_0, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH2N - GPIO_PartialRemap_TIM1 - {PB_1, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH4 - Default - //{PB_1, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH3N - GPIO_PartialRemap_TIM1 +// {PB_0, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH2N - GPIO_PartialRemap_TIM1 + {PB_1, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH4 - Default +// {PB_1, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH3N - GPIO_PartialRemap_TIM1 {PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 8)}, // TIM2_CH2 - GPIO_FullRemap_TIM2 {PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 7)}, // TIM3_CH1 - GPIO_PartialRemap_TIM3 {PB_5, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 7)}, // TIM3_CH2 - GPIO_PartialRemap_TIM3 - //{PB_6, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH1 - Default (used by ticker) - //{PB_7, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH2 - Default (used by ticker) - //{PB_8, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH3 - Default (used by ticker) - //{PB_9, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH4 - Default (used by ticker) +// {PB_6, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH1 - Default (used by ticker) +// {PB_7, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH2 - Default (used by ticker) +// {PB_8, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH3 - Default (used by ticker) +// {PB_9, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH4 - Default (used by ticker) {PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 8)}, // TIM2_CH3 - GPIO_FullRemap_TIM2 {PB_11, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 8)}, // TIM2_CH4 - GPIO_FullRemap_TIM2 {PB_13, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH1N - Default {PB_14, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH2N - Default {PB_15, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH3N - Default - + {PC_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 9)}, // TIM3_CH1 - GPIO_FullRemap_TIM3 {PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 9)}, // TIM3_CH2 - GPIO_FullRemap_TIM3 {PC_8, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 9)}, // TIM3_CH3 - GPIO_FullRemap_TIM3 @@ -71,27 +73,27 @@ {NC, NC, 0} }; -void pwmout_init(pwmout_t* obj, PinName pin) { +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 pinout mapping failed"); } - + // Enable TIM clock if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); if (obj->pwm == PWM_4) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); - + // 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 } @@ -103,15 +105,15 @@ void pwmout_write(pwmout_t* obj, float value) { TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); TIM_OCInitTypeDef TIM_OCInitStructure; - + if (value < 0.0) { value = 0.0; } else if (value > 1.0) { value = 1.0; } - + obj->pulse = (uint32_t)((float)obj->period * value); - + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Pulse = obj->pulse; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; @@ -126,14 +128,14 @@ case PA_8: case PA_15: case PB_4: - //case PB_6: + //case PB_6: case PC_6: TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); TIM_OC1Init(tim, &TIM_OCInitStructure); break; // Channels 1N - //case PA_7: + //case PA_7: case PB_13: TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); @@ -145,52 +147,52 @@ case PA_9: case PB_3: case PB_5: - //case PB_7: + //case PB_7: case PC_7: - TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); TIM_OC2Init(tim, &TIM_OCInitStructure); break; // Channels 2N - //case PB_0: + //case PB_0: case PB_14: - TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; + TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); TIM_OC2Init(tim, &TIM_OCInitStructure); - break; - // Channels 3 + break; + // Channels 3 case PA_2: case PA_10: case PB_0: - //case PB_8: + //case PB_8: case PB_10: case PC_8: TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); TIM_OC3Init(tim, &TIM_OCInitStructure); break; - // Channels 3N - //case PB_1: + // Channels 3N + //case PB_1: case PB_15: TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); TIM_OC3Init(tim, &TIM_OCInitStructure); - break; - // Channels 4 + break; + // Channels 4 case PA_3: case PA_11: case PB_1: - //case PB_9: + //case PB_9: case PB_11: case PC_9: TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable); TIM_OC4Init(tim, &TIM_OCInitStructure); - break; + break; default: return; } - + TIM_CtrlPWMOutputs(tim, ENABLE); } @@ -215,20 +217,20 @@ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; float dc = pwmout_read(obj); - TIM_Cmd(tim, DISABLE); - + TIM_Cmd(tim, DISABLE); + obj->period = us; - - TIM_TimeBaseStructure.TIM_Period = obj->period - 1; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + + TIM_TimeBaseStructure.TIM_Period = obj->period - 1; + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; - TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); // Set duty cycle again pwmout_write(obj, dc); - - TIM_ARRPreloadConfig(tim, ENABLE); + + TIM_ARRPreloadConfig(tim, ENABLE); TIM_Cmd(tim, ENABLE); } @@ -244,3 +246,5 @@ float value = (float)us / (float)obj->period; pwmout_write(obj, value); } + +#endif