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