mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_STM32F4/pwmout_api.c@613:bc40b8d2aec4, 2015-08-20 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Aug 20 10:45:13 2015 +0100
- Revision:
- 613:bc40b8d2aec4
- Parent:
- 605:ded2017be6ab
Synchronized with git revision 92ca8c7b60a283b6bb60eb65b183dac1599f0ade
Full URL: https://github.com/mbedmicro/mbed/commit/92ca8c7b60a283b6bb60eb65b183dac1599f0ade/
Nordic: update application start address in GCC linker script
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 340:28d1f895c6fe | 1 | /* mbed Microcontroller Library |
mbed_official | 340:28d1f895c6fe | 2 | ******************************************************************************* |
mbed_official | 340:28d1f895c6fe | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 340:28d1f895c6fe | 4 | * All rights reserved. |
mbed_official | 340:28d1f895c6fe | 5 | * |
mbed_official | 340:28d1f895c6fe | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 340:28d1f895c6fe | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 340:28d1f895c6fe | 8 | * |
mbed_official | 340:28d1f895c6fe | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 340:28d1f895c6fe | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 340:28d1f895c6fe | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 340:28d1f895c6fe | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 340:28d1f895c6fe | 13 | * and/or other materials provided with the distribution. |
mbed_official | 340:28d1f895c6fe | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 340:28d1f895c6fe | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 340:28d1f895c6fe | 16 | * without specific prior written permission. |
mbed_official | 340:28d1f895c6fe | 17 | * |
mbed_official | 340:28d1f895c6fe | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 340:28d1f895c6fe | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 340:28d1f895c6fe | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 340:28d1f895c6fe | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 340:28d1f895c6fe | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 340:28d1f895c6fe | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 340:28d1f895c6fe | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 340:28d1f895c6fe | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 340:28d1f895c6fe | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 340:28d1f895c6fe | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 340:28d1f895c6fe | 28 | ******************************************************************************* |
mbed_official | 340:28d1f895c6fe | 29 | */ |
mbed_official | 340:28d1f895c6fe | 30 | #include "pwmout_api.h" |
mbed_official | 340:28d1f895c6fe | 31 | |
mbed_official | 340:28d1f895c6fe | 32 | #if DEVICE_PWMOUT |
mbed_official | 340:28d1f895c6fe | 33 | |
mbed_official | 340:28d1f895c6fe | 34 | #include "cmsis.h" |
mbed_official | 340:28d1f895c6fe | 35 | #include "pinmap.h" |
mbed_official | 340:28d1f895c6fe | 36 | #include "mbed_error.h" |
mbed_official | 428:4ddf7f7eabbb | 37 | #include "PeripheralPins.h" |
mbed_official | 340:28d1f895c6fe | 38 | |
mbed_official | 340:28d1f895c6fe | 39 | static TIM_HandleTypeDef TimHandle; |
mbed_official | 340:28d1f895c6fe | 40 | |
mbed_official | 340:28d1f895c6fe | 41 | void pwmout_init(pwmout_t* obj, PinName pin) |
mbed_official | 340:28d1f895c6fe | 42 | { |
mbed_official | 340:28d1f895c6fe | 43 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 340:28d1f895c6fe | 44 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 613:bc40b8d2aec4 | 45 | MBED_ASSERT(obj->pwm != (PWMName)NC); |
mbed_official | 340:28d1f895c6fe | 46 | |
mbed_official | 511:532f83b66a7f | 47 | // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object |
mbed_official | 511:532f83b66a7f | 48 | uint32_t function = pinmap_function(pin, PinMap_PWM); |
mbed_official | 511:532f83b66a7f | 49 | MBED_ASSERT(function != (uint32_t)NC); |
mbed_official | 613:bc40b8d2aec4 | 50 | obj->channel = STM_PIN_CHANNEL(function); |
mbed_official | 511:532f83b66a7f | 51 | obj->inverted = STM_PIN_INVERTED(function); |
mbed_official | 511:532f83b66a7f | 52 | |
mbed_official | 340:28d1f895c6fe | 53 | // Enable TIM clock |
mbed_official | 613:bc40b8d2aec4 | 54 | if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); |
mbed_official | 613:bc40b8d2aec4 | 55 | if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); |
mbed_official | 613:bc40b8d2aec4 | 56 | if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); |
mbed_official | 613:bc40b8d2aec4 | 57 | if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); |
mbed_official | 511:532f83b66a7f | 58 | #if defined(TIM8_BASE) |
mbed_official | 613:bc40b8d2aec4 | 59 | if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); |
mbed_official | 511:532f83b66a7f | 60 | #endif |
mbed_official | 613:bc40b8d2aec4 | 61 | if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); |
mbed_official | 613:bc40b8d2aec4 | 62 | if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); |
mbed_official | 613:bc40b8d2aec4 | 63 | if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); |
mbed_official | 613:bc40b8d2aec4 | 64 | #if defined(TIM12_BASE) |
mbed_official | 613:bc40b8d2aec4 | 65 | if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); |
mbed_official | 613:bc40b8d2aec4 | 66 | #endif |
mbed_official | 511:532f83b66a7f | 67 | #if defined(TIM13_BASE) |
mbed_official | 613:bc40b8d2aec4 | 68 | if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); |
mbed_official | 511:532f83b66a7f | 69 | #endif |
mbed_official | 511:532f83b66a7f | 70 | #if defined(TIM14_BASE) |
mbed_official | 613:bc40b8d2aec4 | 71 | if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); |
mbed_official | 511:532f83b66a7f | 72 | #endif |
mbed_official | 340:28d1f895c6fe | 73 | |
mbed_official | 340:28d1f895c6fe | 74 | // Configure GPIO |
mbed_official | 340:28d1f895c6fe | 75 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 340:28d1f895c6fe | 76 | |
mbed_official | 340:28d1f895c6fe | 77 | obj->pin = pin; |
mbed_official | 340:28d1f895c6fe | 78 | obj->period = 0; |
mbed_official | 340:28d1f895c6fe | 79 | obj->pulse = 0; |
mbed_official | 340:28d1f895c6fe | 80 | |
mbed_official | 340:28d1f895c6fe | 81 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 340:28d1f895c6fe | 82 | } |
mbed_official | 340:28d1f895c6fe | 83 | |
mbed_official | 340:28d1f895c6fe | 84 | void pwmout_free(pwmout_t* obj) |
mbed_official | 340:28d1f895c6fe | 85 | { |
mbed_official | 340:28d1f895c6fe | 86 | // Configure GPIO |
mbed_official | 340:28d1f895c6fe | 87 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 340:28d1f895c6fe | 88 | } |
mbed_official | 340:28d1f895c6fe | 89 | |
mbed_official | 340:28d1f895c6fe | 90 | void pwmout_write(pwmout_t* obj, float value) |
mbed_official | 340:28d1f895c6fe | 91 | { |
mbed_official | 340:28d1f895c6fe | 92 | TIM_OC_InitTypeDef sConfig; |
mbed_official | 340:28d1f895c6fe | 93 | int channel = 0; |
mbed_official | 340:28d1f895c6fe | 94 | |
mbed_official | 340:28d1f895c6fe | 95 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 340:28d1f895c6fe | 96 | |
mbed_official | 340:28d1f895c6fe | 97 | if (value < (float)0.0) { |
mbed_official | 340:28d1f895c6fe | 98 | value = 0.0; |
mbed_official | 340:28d1f895c6fe | 99 | } else if (value > (float)1.0) { |
mbed_official | 340:28d1f895c6fe | 100 | value = 1.0; |
mbed_official | 340:28d1f895c6fe | 101 | } |
mbed_official | 340:28d1f895c6fe | 102 | |
mbed_official | 340:28d1f895c6fe | 103 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 340:28d1f895c6fe | 104 | |
mbed_official | 340:28d1f895c6fe | 105 | // Configure channels |
mbed_official | 340:28d1f895c6fe | 106 | sConfig.OCMode = TIM_OCMODE_PWM1; |
mbed_official | 340:28d1f895c6fe | 107 | sConfig.Pulse = obj->pulse; |
mbed_official | 340:28d1f895c6fe | 108 | sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
mbed_official | 340:28d1f895c6fe | 109 | sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
mbed_official | 340:28d1f895c6fe | 110 | sConfig.OCFastMode = TIM_OCFAST_DISABLE; |
mbed_official | 340:28d1f895c6fe | 111 | sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; |
mbed_official | 340:28d1f895c6fe | 112 | sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
mbed_official | 340:28d1f895c6fe | 113 | |
mbed_official | 511:532f83b66a7f | 114 | switch (obj->channel) { |
mbed_official | 511:532f83b66a7f | 115 | case 1: |
mbed_official | 340:28d1f895c6fe | 116 | channel = TIM_CHANNEL_1; |
mbed_official | 340:28d1f895c6fe | 117 | break; |
mbed_official | 511:532f83b66a7f | 118 | case 2: |
mbed_official | 340:28d1f895c6fe | 119 | channel = TIM_CHANNEL_2; |
mbed_official | 340:28d1f895c6fe | 120 | break; |
mbed_official | 511:532f83b66a7f | 121 | case 3: |
mbed_official | 340:28d1f895c6fe | 122 | channel = TIM_CHANNEL_3; |
mbed_official | 340:28d1f895c6fe | 123 | break; |
mbed_official | 511:532f83b66a7f | 124 | case 4: |
mbed_official | 340:28d1f895c6fe | 125 | channel = TIM_CHANNEL_4; |
mbed_official | 340:28d1f895c6fe | 126 | break; |
mbed_official | 340:28d1f895c6fe | 127 | default: |
mbed_official | 340:28d1f895c6fe | 128 | return; |
mbed_official | 340:28d1f895c6fe | 129 | } |
mbed_official | 340:28d1f895c6fe | 130 | |
mbed_official | 613:bc40b8d2aec4 | 131 | if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { |
mbed_official | 613:bc40b8d2aec4 | 132 | error("Cannot initialize PWM\n"); |
mbed_official | 613:bc40b8d2aec4 | 133 | } |
mbed_official | 613:bc40b8d2aec4 | 134 | |
mbed_official | 613:bc40b8d2aec4 | 135 | if (obj->inverted) { |
mbed_official | 340:28d1f895c6fe | 136 | HAL_TIMEx_PWMN_Start(&TimHandle, channel); |
mbed_official | 340:28d1f895c6fe | 137 | } else { |
mbed_official | 340:28d1f895c6fe | 138 | HAL_TIM_PWM_Start(&TimHandle, channel); |
mbed_official | 340:28d1f895c6fe | 139 | } |
mbed_official | 340:28d1f895c6fe | 140 | } |
mbed_official | 340:28d1f895c6fe | 141 | |
mbed_official | 340:28d1f895c6fe | 142 | float pwmout_read(pwmout_t* obj) |
mbed_official | 340:28d1f895c6fe | 143 | { |
mbed_official | 340:28d1f895c6fe | 144 | float value = 0; |
mbed_official | 340:28d1f895c6fe | 145 | if (obj->period > 0) { |
mbed_official | 340:28d1f895c6fe | 146 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 340:28d1f895c6fe | 147 | } |
mbed_official | 340:28d1f895c6fe | 148 | return ((value > (float)1.0) ? (float)(1.0) : (value)); |
mbed_official | 340:28d1f895c6fe | 149 | } |
mbed_official | 340:28d1f895c6fe | 150 | |
mbed_official | 340:28d1f895c6fe | 151 | void pwmout_period(pwmout_t* obj, float seconds) |
mbed_official | 340:28d1f895c6fe | 152 | { |
mbed_official | 340:28d1f895c6fe | 153 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 340:28d1f895c6fe | 154 | } |
mbed_official | 340:28d1f895c6fe | 155 | |
mbed_official | 340:28d1f895c6fe | 156 | void pwmout_period_ms(pwmout_t* obj, int ms) |
mbed_official | 340:28d1f895c6fe | 157 | { |
mbed_official | 340:28d1f895c6fe | 158 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 340:28d1f895c6fe | 159 | } |
mbed_official | 340:28d1f895c6fe | 160 | |
mbed_official | 340:28d1f895c6fe | 161 | void pwmout_period_us(pwmout_t* obj, int us) |
mbed_official | 340:28d1f895c6fe | 162 | { |
mbed_official | 340:28d1f895c6fe | 163 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 555:f8b0f61305ee | 164 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
mbed_official | 555:f8b0f61305ee | 165 | uint32_t PclkFreq; |
mbed_official | 555:f8b0f61305ee | 166 | uint32_t APBxCLKDivider; |
mbed_official | 340:28d1f895c6fe | 167 | float dc = pwmout_read(obj); |
mbed_official | 340:28d1f895c6fe | 168 | |
mbed_official | 340:28d1f895c6fe | 169 | __HAL_TIM_DISABLE(&TimHandle); |
mbed_official | 340:28d1f895c6fe | 170 | |
mbed_official | 613:bc40b8d2aec4 | 171 | // Get clock configuration |
mbed_official | 613:bc40b8d2aec4 | 172 | // Note: PclkFreq contains here the Latency (not used after) |
mbed_official | 555:f8b0f61305ee | 173 | HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); |
mbed_official | 555:f8b0f61305ee | 174 | |
mbed_official | 613:bc40b8d2aec4 | 175 | // Get the PCLK and APBCLK divider related to the timer |
mbed_official | 555:f8b0f61305ee | 176 | switch (obj->pwm) { |
mbed_official | 613:bc40b8d2aec4 | 177 | |
mbed_official | 613:bc40b8d2aec4 | 178 | // APB1 clock |
mbed_official | 555:f8b0f61305ee | 179 | case PWM_2: |
mbed_official | 613:bc40b8d2aec4 | 180 | case PWM_3: |
mbed_official | 613:bc40b8d2aec4 | 181 | case PWM_4: |
mbed_official | 613:bc40b8d2aec4 | 182 | case PWM_5: |
mbed_official | 613:bc40b8d2aec4 | 183 | #if defined(TIM12_BASE) |
mbed_official | 613:bc40b8d2aec4 | 184 | case PWM_12: |
mbed_official | 613:bc40b8d2aec4 | 185 | #endif |
mbed_official | 613:bc40b8d2aec4 | 186 | #if defined(TIM13_BASE) |
mbed_official | 613:bc40b8d2aec4 | 187 | case PWM_13: |
mbed_official | 613:bc40b8d2aec4 | 188 | #endif |
mbed_official | 613:bc40b8d2aec4 | 189 | #if defined(TIM14_BASE) |
mbed_official | 613:bc40b8d2aec4 | 190 | case PWM_14: |
mbed_official | 613:bc40b8d2aec4 | 191 | #endif |
mbed_official | 613:bc40b8d2aec4 | 192 | PclkFreq = HAL_RCC_GetPCLK1Freq(); |
mbed_official | 555:f8b0f61305ee | 193 | APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; |
mbed_official | 555:f8b0f61305ee | 194 | break; |
mbed_official | 613:bc40b8d2aec4 | 195 | |
mbed_official | 613:bc40b8d2aec4 | 196 | // APB2 clock |
mbed_official | 613:bc40b8d2aec4 | 197 | case PWM_1: |
mbed_official | 555:f8b0f61305ee | 198 | #if defined(TIM8_BASE) |
mbed_official | 555:f8b0f61305ee | 199 | case PWM_8: |
mbed_official | 613:bc40b8d2aec4 | 200 | #endif |
mbed_official | 613:bc40b8d2aec4 | 201 | case PWM_9: |
mbed_official | 613:bc40b8d2aec4 | 202 | case PWM_10: |
mbed_official | 613:bc40b8d2aec4 | 203 | case PWM_11: |
mbed_official | 613:bc40b8d2aec4 | 204 | PclkFreq = HAL_RCC_GetPCLK2Freq(); |
mbed_official | 555:f8b0f61305ee | 205 | APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; |
mbed_official | 555:f8b0f61305ee | 206 | break; |
mbed_official | 555:f8b0f61305ee | 207 | default: |
mbed_official | 555:f8b0f61305ee | 208 | return; |
mbed_official | 555:f8b0f61305ee | 209 | } |
mbed_official | 613:bc40b8d2aec4 | 210 | |
mbed_official | 340:28d1f895c6fe | 211 | TimHandle.Init.Period = us - 1; |
mbed_official | 613:bc40b8d2aec4 | 212 | // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx |
mbed_official | 555:f8b0f61305ee | 213 | if (APBxCLKDivider == RCC_HCLK_DIV1) |
mbed_official | 613:bc40b8d2aec4 | 214 | TimHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick |
mbed_official | 555:f8b0f61305ee | 215 | else |
mbed_official | 613:bc40b8d2aec4 | 216 | TimHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick |
mbed_official | 340:28d1f895c6fe | 217 | TimHandle.Init.ClockDivision = 0; |
mbed_official | 340:28d1f895c6fe | 218 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
mbed_official | 613:bc40b8d2aec4 | 219 | |
mbed_official | 613:bc40b8d2aec4 | 220 | if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { |
mbed_official | 613:bc40b8d2aec4 | 221 | error("Cannot initialize PWM\n"); |
mbed_official | 613:bc40b8d2aec4 | 222 | } |
mbed_official | 340:28d1f895c6fe | 223 | |
mbed_official | 340:28d1f895c6fe | 224 | // Set duty cycle again |
mbed_official | 340:28d1f895c6fe | 225 | pwmout_write(obj, dc); |
mbed_official | 340:28d1f895c6fe | 226 | |
mbed_official | 340:28d1f895c6fe | 227 | // Save for future use |
mbed_official | 340:28d1f895c6fe | 228 | obj->period = us; |
mbed_official | 340:28d1f895c6fe | 229 | |
mbed_official | 340:28d1f895c6fe | 230 | __HAL_TIM_ENABLE(&TimHandle); |
mbed_official | 340:28d1f895c6fe | 231 | } |
mbed_official | 340:28d1f895c6fe | 232 | |
mbed_official | 340:28d1f895c6fe | 233 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
mbed_official | 340:28d1f895c6fe | 234 | { |
mbed_official | 340:28d1f895c6fe | 235 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 340:28d1f895c6fe | 236 | } |
mbed_official | 340:28d1f895c6fe | 237 | |
mbed_official | 340:28d1f895c6fe | 238 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
mbed_official | 340:28d1f895c6fe | 239 | { |
mbed_official | 340:28d1f895c6fe | 240 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 340:28d1f895c6fe | 241 | } |
mbed_official | 340:28d1f895c6fe | 242 | |
mbed_official | 340:28d1f895c6fe | 243 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
mbed_official | 340:28d1f895c6fe | 244 | { |
mbed_official | 340:28d1f895c6fe | 245 | float value = (float)us / (float)obj->period; |
mbed_official | 340:28d1f895c6fe | 246 | pwmout_write(obj, value); |
mbed_official | 340:28d1f895c6fe | 247 | } |
mbed_official | 340:28d1f895c6fe | 248 | |
mbed_official | 340:28d1f895c6fe | 249 | #endif |