Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c@52:a51c77007319, 2013-12-02 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Dec 02 11:30:05 2013 +0000
- Revision:
- 52:a51c77007319
- Child:
- 56:99eb381a3269
Synchronized with git revision 49df530ae72ce97ccc773d1f2c13b38e868e6abd
Full URL: https://github.com/mbedmicro/mbed/commit/49df530ae72ce97ccc773d1f2c13b38e868e6abd/
Add STMicroelectronics NUCLEO_F103RB target
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 52:a51c77007319 | 1 | /* mbed Microcontroller Library |
| mbed_official | 52:a51c77007319 | 2 | * Copyright (c) 2006-2013 ARM Limited |
| mbed_official | 52:a51c77007319 | 3 | * |
| mbed_official | 52:a51c77007319 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| mbed_official | 52:a51c77007319 | 5 | * you may not use this file except in compliance with the License. |
| mbed_official | 52:a51c77007319 | 6 | * You may obtain a copy of the License at |
| mbed_official | 52:a51c77007319 | 7 | * |
| mbed_official | 52:a51c77007319 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| mbed_official | 52:a51c77007319 | 9 | * |
| mbed_official | 52:a51c77007319 | 10 | * Unless required by applicable law or agreed to in writing, software |
| mbed_official | 52:a51c77007319 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| mbed_official | 52:a51c77007319 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| mbed_official | 52:a51c77007319 | 13 | * See the License for the specific language governing permissions and |
| mbed_official | 52:a51c77007319 | 14 | * limitations under the License. |
| mbed_official | 52:a51c77007319 | 15 | */ |
| mbed_official | 52:a51c77007319 | 16 | #include "pwmout_api.h" |
| mbed_official | 52:a51c77007319 | 17 | |
| mbed_official | 52:a51c77007319 | 18 | #include "cmsis.h" |
| mbed_official | 52:a51c77007319 | 19 | #include "pinmap.h" |
| mbed_official | 52:a51c77007319 | 20 | #include "error.h" |
| mbed_official | 52:a51c77007319 | 21 | |
| mbed_official | 52:a51c77007319 | 22 | // Only TIM2 and TIM3 can be used (TIM1 and TIM4 are used by the us_ticker) |
| mbed_official | 52:a51c77007319 | 23 | static const PinMap PinMap_PWM[] = { |
| mbed_official | 52:a51c77007319 | 24 | // TIM2 |
| mbed_official | 52:a51c77007319 | 25 | {PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH3 OK |
| mbed_official | 52:a51c77007319 | 26 | {PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 OK |
| mbed_official | 52:a51c77007319 | 27 | // TIM2 remap |
| mbed_official | 52:a51c77007319 | 28 | {PA_15, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH1 FAIL |
| mbed_official | 52:a51c77007319 | 29 | {PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH2 FAIL - ARDUINO D3 |
| mbed_official | 52:a51c77007319 | 30 | {PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH3 OK - ARDUINO D6 |
| mbed_official | 52:a51c77007319 | 31 | {PB_11, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH4 OK |
| mbed_official | 52:a51c77007319 | 32 | // TIM3 |
| mbed_official | 52:a51c77007319 | 33 | {PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 OK |
| mbed_official | 52:a51c77007319 | 34 | {PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 OK - ARDUINO D11 |
| mbed_official | 52:a51c77007319 | 35 | {PB_1, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH4 OK |
| mbed_official | 52:a51c77007319 | 36 | // TIM3 remap |
| mbed_official | 52:a51c77007319 | 37 | {PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH1 FAIL - ARDUINO D5 |
| mbed_official | 52:a51c77007319 | 38 | {PC_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH1 OK |
| mbed_official | 52:a51c77007319 | 39 | {PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH2 OK - ARDUINO D9 |
| mbed_official | 52:a51c77007319 | 40 | {PB_5, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH2 FAIL - Bug confirmed in ES |
| mbed_official | 52:a51c77007319 | 41 | {PC_8, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH3 OK |
| mbed_official | 52:a51c77007319 | 42 | {PC_9, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH4 OK |
| mbed_official | 52:a51c77007319 | 43 | {NC, NC, 0} |
| mbed_official | 52:a51c77007319 | 44 | }; |
| mbed_official | 52:a51c77007319 | 45 | |
| mbed_official | 52:a51c77007319 | 46 | void pwmout_init(pwmout_t* obj, PinName pin) { |
| mbed_official | 52:a51c77007319 | 47 | // Get the peripheral name from the pin and assign it to the object |
| mbed_official | 52:a51c77007319 | 48 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
| mbed_official | 52:a51c77007319 | 49 | |
| mbed_official | 52:a51c77007319 | 50 | if (obj->pwm == (PWMName)NC) { |
| mbed_official | 52:a51c77007319 | 51 | error("PWM pinout mapping failed"); |
| mbed_official | 52:a51c77007319 | 52 | } |
| mbed_official | 52:a51c77007319 | 53 | |
| mbed_official | 52:a51c77007319 | 54 | // Enable TIM clock |
| mbed_official | 52:a51c77007319 | 55 | if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); |
| mbed_official | 52:a51c77007319 | 56 | if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); |
| mbed_official | 52:a51c77007319 | 57 | |
| mbed_official | 52:a51c77007319 | 58 | // Configure GPIO |
| mbed_official | 52:a51c77007319 | 59 | pinmap_pinout(pin, PinMap_PWM); |
| mbed_official | 52:a51c77007319 | 60 | |
| mbed_official | 52:a51c77007319 | 61 | obj->pin = pin; |
| mbed_official | 52:a51c77007319 | 62 | obj->period = 0; |
| mbed_official | 52:a51c77007319 | 63 | obj->pulse = 0; |
| mbed_official | 52:a51c77007319 | 64 | |
| mbed_official | 52:a51c77007319 | 65 | pwmout_period_us(obj, 20000); // 20 ms per default |
| mbed_official | 52:a51c77007319 | 66 | } |
| mbed_official | 52:a51c77007319 | 67 | |
| mbed_official | 52:a51c77007319 | 68 | void pwmout_free(pwmout_t* obj) { |
| mbed_official | 52:a51c77007319 | 69 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
| mbed_official | 52:a51c77007319 | 70 | TIM_DeInit(tim); |
| mbed_official | 52:a51c77007319 | 71 | } |
| mbed_official | 52:a51c77007319 | 72 | |
| mbed_official | 52:a51c77007319 | 73 | void pwmout_write(pwmout_t* obj, float value) { |
| mbed_official | 52:a51c77007319 | 74 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
| mbed_official | 52:a51c77007319 | 75 | TIM_OCInitTypeDef TIM_OCInitStructure; |
| mbed_official | 52:a51c77007319 | 76 | |
| mbed_official | 52:a51c77007319 | 77 | if (value < 0.0) { |
| mbed_official | 52:a51c77007319 | 78 | value = 0.0; |
| mbed_official | 52:a51c77007319 | 79 | } else if (value > 1.0) { |
| mbed_official | 52:a51c77007319 | 80 | value = 1.0; |
| mbed_official | 52:a51c77007319 | 81 | } |
| mbed_official | 52:a51c77007319 | 82 | |
| mbed_official | 52:a51c77007319 | 83 | //while(TIM_GetFlagStatus(tim, TIM_FLAG_Update) == RESET); |
| mbed_official | 52:a51c77007319 | 84 | //TIM_ClearFlag(tim, TIM_FLAG_Update); |
| mbed_official | 52:a51c77007319 | 85 | |
| mbed_official | 52:a51c77007319 | 86 | obj->pulse = (uint32_t)((float)obj->period * value); |
| mbed_official | 52:a51c77007319 | 87 | |
| mbed_official | 52:a51c77007319 | 88 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
| mbed_official | 52:a51c77007319 | 89 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
| mbed_official | 52:a51c77007319 | 90 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
| mbed_official | 52:a51c77007319 | 91 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
| mbed_official | 52:a51c77007319 | 92 | |
| mbed_official | 52:a51c77007319 | 93 | if ((obj->pin == PA_6) || (obj->pin == PA_15) || (obj->pin == PB_4) || (obj->pin == PC_6)) { // TIM Channel 1 |
| mbed_official | 52:a51c77007319 | 94 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
| mbed_official | 52:a51c77007319 | 95 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
| mbed_official | 52:a51c77007319 | 96 | } |
| mbed_official | 52:a51c77007319 | 97 | |
| mbed_official | 52:a51c77007319 | 98 | if ((obj->pin == PA_7) || (obj->pin == PB_3) || (obj->pin == PB_5) || (obj->pin == PC_7)) { // TIM Channel 2 |
| mbed_official | 52:a51c77007319 | 99 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
| mbed_official | 52:a51c77007319 | 100 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
| mbed_official | 52:a51c77007319 | 101 | } |
| mbed_official | 52:a51c77007319 | 102 | |
| mbed_official | 52:a51c77007319 | 103 | if ((obj->pin == PA_2) || (obj->pin == PB_10) || (obj->pin == PC_8)) { // TIM Channel 3 |
| mbed_official | 52:a51c77007319 | 104 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
| mbed_official | 52:a51c77007319 | 105 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
| mbed_official | 52:a51c77007319 | 106 | } |
| mbed_official | 52:a51c77007319 | 107 | |
| mbed_official | 52:a51c77007319 | 108 | if ((obj->pin == PA_3) || (obj->pin == PB_1) || (obj->pin == PB_11) || (obj->pin == PC_9)) { // TIM Channel 4 |
| mbed_official | 52:a51c77007319 | 109 | TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable); |
| mbed_official | 52:a51c77007319 | 110 | TIM_OC4Init(tim, &TIM_OCInitStructure); |
| mbed_official | 52:a51c77007319 | 111 | } |
| mbed_official | 52:a51c77007319 | 112 | } |
| mbed_official | 52:a51c77007319 | 113 | |
| mbed_official | 52:a51c77007319 | 114 | float pwmout_read(pwmout_t* obj) { |
| mbed_official | 52:a51c77007319 | 115 | float value = 0; |
| mbed_official | 52:a51c77007319 | 116 | if (obj->period > 0) { |
| mbed_official | 52:a51c77007319 | 117 | value = (float)(obj->pulse) / (float)(obj->period); |
| mbed_official | 52:a51c77007319 | 118 | } |
| mbed_official | 52:a51c77007319 | 119 | return ((value > 1.0) ? (1.0) : (value)); |
| mbed_official | 52:a51c77007319 | 120 | } |
| mbed_official | 52:a51c77007319 | 121 | |
| mbed_official | 52:a51c77007319 | 122 | void pwmout_period(pwmout_t* obj, float seconds) { |
| mbed_official | 52:a51c77007319 | 123 | pwmout_period_us(obj, seconds * 1000000.0f); |
| mbed_official | 52:a51c77007319 | 124 | } |
| mbed_official | 52:a51c77007319 | 125 | |
| mbed_official | 52:a51c77007319 | 126 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
| mbed_official | 52:a51c77007319 | 127 | pwmout_period_us(obj, ms * 1000); |
| mbed_official | 52:a51c77007319 | 128 | } |
| mbed_official | 52:a51c77007319 | 129 | |
| mbed_official | 52:a51c77007319 | 130 | void pwmout_period_us(pwmout_t* obj, int us) { |
| mbed_official | 52:a51c77007319 | 131 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
| mbed_official | 52:a51c77007319 | 132 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
| mbed_official | 52:a51c77007319 | 133 | float dc = pwmout_read(obj); |
| mbed_official | 52:a51c77007319 | 134 | |
| mbed_official | 52:a51c77007319 | 135 | TIM_Cmd(tim, DISABLE); |
| mbed_official | 52:a51c77007319 | 136 | |
| mbed_official | 52:a51c77007319 | 137 | obj->period = us; |
| mbed_official | 52:a51c77007319 | 138 | |
| mbed_official | 52:a51c77007319 | 139 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
| mbed_official | 52:a51c77007319 | 140 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
| mbed_official | 52:a51c77007319 | 141 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
| mbed_official | 52:a51c77007319 | 142 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
| mbed_official | 52:a51c77007319 | 143 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
| mbed_official | 52:a51c77007319 | 144 | |
| mbed_official | 52:a51c77007319 | 145 | // Set duty cycle again |
| mbed_official | 52:a51c77007319 | 146 | pwmout_write(obj, dc); |
| mbed_official | 52:a51c77007319 | 147 | |
| mbed_official | 52:a51c77007319 | 148 | TIM_ARRPreloadConfig(tim, ENABLE); |
| mbed_official | 52:a51c77007319 | 149 | TIM_Cmd(tim, ENABLE); |
| mbed_official | 52:a51c77007319 | 150 | } |
| mbed_official | 52:a51c77007319 | 151 | |
| mbed_official | 52:a51c77007319 | 152 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
| mbed_official | 52:a51c77007319 | 153 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
| mbed_official | 52:a51c77007319 | 154 | } |
| mbed_official | 52:a51c77007319 | 155 | |
| mbed_official | 52:a51c77007319 | 156 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
| mbed_official | 52:a51c77007319 | 157 | pwmout_pulsewidth_us(obj, ms * 1000); |
| mbed_official | 52:a51c77007319 | 158 | } |
| mbed_official | 52:a51c77007319 | 159 | |
| mbed_official | 52:a51c77007319 | 160 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
| mbed_official | 52:a51c77007319 | 161 | float value = (float)us / (float)obj->period; |
| mbed_official | 52:a51c77007319 | 162 | pwmout_write(obj, value); |
| mbed_official | 52:a51c77007319 | 163 | } |
