mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c@56:99eb381a3269, 2013-12-12 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Dec 12 10:45:05 2013 +0000
- Revision:
- 56:99eb381a3269
- Parent:
- 52:a51c77007319
- Child:
- 58:3b55b7a41411
Synchronized with git revision bd51e4eb73a1706f1d5379ec5cebcbd6d978cb4f
Full URL: https://github.com/mbedmicro/mbed/commit/bd51e4eb73a1706f1d5379ec5cebcbd6d978cb4f/
[NUCLEO_F103RB] Add I2C master, code cleanup, ...
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 | 56:99eb381a3269 | 25 | {PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH3 - ARDUINO D1 (extra) |
mbed_official | 56:99eb381a3269 | 26 | {PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 - ARDUINO D0 (extra) |
mbed_official | 52:a51c77007319 | 27 | // TIM2 remap |
mbed_official | 56:99eb381a3269 | 28 | {PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH2 - ARDUINO D3 |
mbed_official | 56:99eb381a3269 | 29 | {PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH3 - ARDUINO D6 |
mbed_official | 52:a51c77007319 | 30 | // TIM3 |
mbed_official | 56:99eb381a3269 | 31 | {PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 - ARDUINO D12 (extra) |
mbed_official | 56:99eb381a3269 | 32 | {PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 - ARDUINO D11 |
mbed_official | 52:a51c77007319 | 33 | // TIM3 remap |
mbed_official | 56:99eb381a3269 | 34 | {PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH1 - ARDUINO D5 |
mbed_official | 56:99eb381a3269 | 35 | {PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH2 - ARDUINO D9 |
mbed_official | 52:a51c77007319 | 36 | {NC, NC, 0} |
mbed_official | 52:a51c77007319 | 37 | }; |
mbed_official | 52:a51c77007319 | 38 | |
mbed_official | 52:a51c77007319 | 39 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 52:a51c77007319 | 40 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 52:a51c77007319 | 41 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 52:a51c77007319 | 42 | |
mbed_official | 52:a51c77007319 | 43 | if (obj->pwm == (PWMName)NC) { |
mbed_official | 52:a51c77007319 | 44 | error("PWM pinout mapping failed"); |
mbed_official | 52:a51c77007319 | 45 | } |
mbed_official | 52:a51c77007319 | 46 | |
mbed_official | 52:a51c77007319 | 47 | // Enable TIM clock |
mbed_official | 52:a51c77007319 | 48 | if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); |
mbed_official | 52:a51c77007319 | 49 | if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); |
mbed_official | 52:a51c77007319 | 50 | |
mbed_official | 52:a51c77007319 | 51 | // Configure GPIO |
mbed_official | 52:a51c77007319 | 52 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 52:a51c77007319 | 53 | |
mbed_official | 52:a51c77007319 | 54 | obj->pin = pin; |
mbed_official | 52:a51c77007319 | 55 | obj->period = 0; |
mbed_official | 52:a51c77007319 | 56 | obj->pulse = 0; |
mbed_official | 52:a51c77007319 | 57 | |
mbed_official | 52:a51c77007319 | 58 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 52:a51c77007319 | 59 | } |
mbed_official | 52:a51c77007319 | 60 | |
mbed_official | 52:a51c77007319 | 61 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 52:a51c77007319 | 62 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 52:a51c77007319 | 63 | TIM_DeInit(tim); |
mbed_official | 52:a51c77007319 | 64 | } |
mbed_official | 52:a51c77007319 | 65 | |
mbed_official | 52:a51c77007319 | 66 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 52:a51c77007319 | 67 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 52:a51c77007319 | 68 | TIM_OCInitTypeDef TIM_OCInitStructure; |
mbed_official | 52:a51c77007319 | 69 | |
mbed_official | 52:a51c77007319 | 70 | if (value < 0.0) { |
mbed_official | 52:a51c77007319 | 71 | value = 0.0; |
mbed_official | 52:a51c77007319 | 72 | } else if (value > 1.0) { |
mbed_official | 52:a51c77007319 | 73 | value = 1.0; |
mbed_official | 52:a51c77007319 | 74 | } |
mbed_official | 52:a51c77007319 | 75 | |
mbed_official | 52:a51c77007319 | 76 | //while(TIM_GetFlagStatus(tim, TIM_FLAG_Update) == RESET); |
mbed_official | 52:a51c77007319 | 77 | //TIM_ClearFlag(tim, TIM_FLAG_Update); |
mbed_official | 52:a51c77007319 | 78 | |
mbed_official | 52:a51c77007319 | 79 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 52:a51c77007319 | 80 | |
mbed_official | 52:a51c77007319 | 81 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
mbed_official | 52:a51c77007319 | 82 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 52:a51c77007319 | 83 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
mbed_official | 52:a51c77007319 | 84 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 52:a51c77007319 | 85 | |
mbed_official | 56:99eb381a3269 | 86 | // TIM Channel 1 |
mbed_official | 56:99eb381a3269 | 87 | if ((obj->pin == PA_6) || (obj->pin == PB_4)) { |
mbed_official | 52:a51c77007319 | 88 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 52:a51c77007319 | 89 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 52:a51c77007319 | 90 | } |
mbed_official | 52:a51c77007319 | 91 | |
mbed_official | 56:99eb381a3269 | 92 | // TIM Channel 2 |
mbed_official | 56:99eb381a3269 | 93 | if ((obj->pin == PA_7) || (obj->pin == PB_3) || (obj->pin == PC_7)) { |
mbed_official | 52:a51c77007319 | 94 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 52:a51c77007319 | 95 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 52:a51c77007319 | 96 | } |
mbed_official | 52:a51c77007319 | 97 | |
mbed_official | 56:99eb381a3269 | 98 | // TIM Channel 3 |
mbed_official | 56:99eb381a3269 | 99 | if ((obj->pin == PA_2) || (obj->pin == PB_10)) { |
mbed_official | 52:a51c77007319 | 100 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 52:a51c77007319 | 101 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 52:a51c77007319 | 102 | } |
mbed_official | 52:a51c77007319 | 103 | |
mbed_official | 56:99eb381a3269 | 104 | // TIM Channel 4 |
mbed_official | 56:99eb381a3269 | 105 | if (obj->pin == PA_3) { |
mbed_official | 52:a51c77007319 | 106 | TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 52:a51c77007319 | 107 | TIM_OC4Init(tim, &TIM_OCInitStructure); |
mbed_official | 52:a51c77007319 | 108 | } |
mbed_official | 52:a51c77007319 | 109 | } |
mbed_official | 52:a51c77007319 | 110 | |
mbed_official | 52:a51c77007319 | 111 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 52:a51c77007319 | 112 | float value = 0; |
mbed_official | 52:a51c77007319 | 113 | if (obj->period > 0) { |
mbed_official | 52:a51c77007319 | 114 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 52:a51c77007319 | 115 | } |
mbed_official | 52:a51c77007319 | 116 | return ((value > 1.0) ? (1.0) : (value)); |
mbed_official | 52:a51c77007319 | 117 | } |
mbed_official | 52:a51c77007319 | 118 | |
mbed_official | 52:a51c77007319 | 119 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 52:a51c77007319 | 120 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 52:a51c77007319 | 121 | } |
mbed_official | 52:a51c77007319 | 122 | |
mbed_official | 52:a51c77007319 | 123 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 52:a51c77007319 | 124 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 52:a51c77007319 | 125 | } |
mbed_official | 52:a51c77007319 | 126 | |
mbed_official | 52:a51c77007319 | 127 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 52:a51c77007319 | 128 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 52:a51c77007319 | 129 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
mbed_official | 52:a51c77007319 | 130 | float dc = pwmout_read(obj); |
mbed_official | 52:a51c77007319 | 131 | |
mbed_official | 52:a51c77007319 | 132 | TIM_Cmd(tim, DISABLE); |
mbed_official | 52:a51c77007319 | 133 | |
mbed_official | 52:a51c77007319 | 134 | obj->period = us; |
mbed_official | 52:a51c77007319 | 135 | |
mbed_official | 52:a51c77007319 | 136 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
mbed_official | 52:a51c77007319 | 137 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 52:a51c77007319 | 138 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
mbed_official | 52:a51c77007319 | 139 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
mbed_official | 52:a51c77007319 | 140 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
mbed_official | 52:a51c77007319 | 141 | |
mbed_official | 52:a51c77007319 | 142 | // Set duty cycle again |
mbed_official | 52:a51c77007319 | 143 | pwmout_write(obj, dc); |
mbed_official | 52:a51c77007319 | 144 | |
mbed_official | 52:a51c77007319 | 145 | TIM_ARRPreloadConfig(tim, ENABLE); |
mbed_official | 52:a51c77007319 | 146 | TIM_Cmd(tim, ENABLE); |
mbed_official | 52:a51c77007319 | 147 | } |
mbed_official | 52:a51c77007319 | 148 | |
mbed_official | 52:a51c77007319 | 149 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 52:a51c77007319 | 150 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 52:a51c77007319 | 151 | } |
mbed_official | 52:a51c77007319 | 152 | |
mbed_official | 52:a51c77007319 | 153 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 52:a51c77007319 | 154 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 52:a51c77007319 | 155 | } |
mbed_official | 52:a51c77007319 | 156 | |
mbed_official | 52:a51c77007319 | 157 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 52:a51c77007319 | 158 | float value = (float)us / (float)obj->period; |
mbed_official | 52:a51c77007319 | 159 | pwmout_write(obj, value); |
mbed_official | 52:a51c77007319 | 160 | } |