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