forked
targets/TARGET_STM/pwmout_api.c@170:19eb464bc2be, 2017-08-03 (annotated)
- Committer:
- Kojto
- Date:
- Thu Aug 03 13:13:39 2017 +0100
- Revision:
- 170:19eb464bc2be
- Parent:
- 160:d5399cc887bb
This updates the lib to the mbed lib v 148
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 160:d5399cc887bb | 1 | /* mbed Microcontroller Library |
<> | 160:d5399cc887bb | 2 | ******************************************************************************* |
<> | 160:d5399cc887bb | 3 | * Copyright (c) 2015, STMicroelectronics |
<> | 160:d5399cc887bb | 4 | * All rights reserved. |
<> | 160:d5399cc887bb | 5 | * |
<> | 160:d5399cc887bb | 6 | * Redistribution and use in source and binary forms, with or without |
<> | 160:d5399cc887bb | 7 | * modification, are permitted provided that the following conditions are met: |
<> | 160:d5399cc887bb | 8 | * |
<> | 160:d5399cc887bb | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
<> | 160:d5399cc887bb | 10 | * this list of conditions and the following disclaimer. |
<> | 160:d5399cc887bb | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
<> | 160:d5399cc887bb | 12 | * this list of conditions and the following disclaimer in the documentation |
<> | 160:d5399cc887bb | 13 | * and/or other materials provided with the distribution. |
<> | 160:d5399cc887bb | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
<> | 160:d5399cc887bb | 15 | * may be used to endorse or promote products derived from this software |
<> | 160:d5399cc887bb | 16 | * without specific prior written permission. |
<> | 160:d5399cc887bb | 17 | * |
<> | 160:d5399cc887bb | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
<> | 160:d5399cc887bb | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
<> | 160:d5399cc887bb | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
<> | 160:d5399cc887bb | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
<> | 160:d5399cc887bb | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
<> | 160:d5399cc887bb | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
<> | 160:d5399cc887bb | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
<> | 160:d5399cc887bb | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
<> | 160:d5399cc887bb | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
<> | 160:d5399cc887bb | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
<> | 160:d5399cc887bb | 28 | ******************************************************************************* |
<> | 160:d5399cc887bb | 29 | */ |
<> | 160:d5399cc887bb | 30 | #include "pwmout_api.h" |
<> | 160:d5399cc887bb | 31 | |
<> | 160:d5399cc887bb | 32 | #if DEVICE_PWMOUT |
<> | 160:d5399cc887bb | 33 | |
<> | 160:d5399cc887bb | 34 | #include "cmsis.h" |
<> | 160:d5399cc887bb | 35 | #include "pinmap.h" |
<> | 160:d5399cc887bb | 36 | #include "mbed_error.h" |
<> | 160:d5399cc887bb | 37 | #include "PeripheralPins.h" |
<> | 160:d5399cc887bb | 38 | #include "pwmout_device.h" |
<> | 160:d5399cc887bb | 39 | |
<> | 160:d5399cc887bb | 40 | static TIM_HandleTypeDef TimHandle; |
<> | 160:d5399cc887bb | 41 | |
<> | 160:d5399cc887bb | 42 | void pwmout_init(pwmout_t* obj, PinName pin) |
<> | 160:d5399cc887bb | 43 | { |
<> | 160:d5399cc887bb | 44 | // Get the peripheral name from the pin and assign it to the object |
<> | 160:d5399cc887bb | 45 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
<> | 160:d5399cc887bb | 46 | MBED_ASSERT(obj->pwm != (PWMName)NC); |
<> | 160:d5399cc887bb | 47 | |
<> | 160:d5399cc887bb | 48 | // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object |
<> | 160:d5399cc887bb | 49 | uint32_t function = pinmap_function(pin, PinMap_PWM); |
<> | 160:d5399cc887bb | 50 | MBED_ASSERT(function != (uint32_t)NC); |
<> | 160:d5399cc887bb | 51 | obj->channel = STM_PIN_CHANNEL(function); |
<> | 160:d5399cc887bb | 52 | obj->inverted = STM_PIN_INVERTED(function); |
<> | 160:d5399cc887bb | 53 | |
<> | 160:d5399cc887bb | 54 | // Enable TIM clock |
<> | 160:d5399cc887bb | 55 | #if defined(TIM1_BASE) |
<> | 160:d5399cc887bb | 56 | if (obj->pwm == PWM_1){ |
<> | 160:d5399cc887bb | 57 | __HAL_RCC_TIM1_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 58 | } |
<> | 160:d5399cc887bb | 59 | #endif |
<> | 160:d5399cc887bb | 60 | #if defined(TIM2_BASE) |
<> | 160:d5399cc887bb | 61 | if (obj->pwm == PWM_2) { |
<> | 160:d5399cc887bb | 62 | __HAL_RCC_TIM2_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 63 | } |
<> | 160:d5399cc887bb | 64 | #endif |
<> | 160:d5399cc887bb | 65 | #if defined(TIM3_BASE) |
<> | 160:d5399cc887bb | 66 | if (obj->pwm == PWM_3) { |
<> | 160:d5399cc887bb | 67 | __HAL_RCC_TIM3_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 68 | } |
<> | 160:d5399cc887bb | 69 | #endif |
<> | 160:d5399cc887bb | 70 | #if defined(TIM4_BASE) |
<> | 160:d5399cc887bb | 71 | if (obj->pwm == PWM_4) { |
<> | 160:d5399cc887bb | 72 | __HAL_RCC_TIM4_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 73 | } |
<> | 160:d5399cc887bb | 74 | #endif |
<> | 160:d5399cc887bb | 75 | #if defined(TIM5_BASE) |
<> | 160:d5399cc887bb | 76 | if (obj->pwm == PWM_5) { |
<> | 160:d5399cc887bb | 77 | __HAL_RCC_TIM5_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 78 | } |
<> | 160:d5399cc887bb | 79 | #endif |
<> | 160:d5399cc887bb | 80 | #if defined(TIM8_BASE) |
<> | 160:d5399cc887bb | 81 | if (obj->pwm == PWM_8) { |
<> | 160:d5399cc887bb | 82 | __HAL_RCC_TIM8_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 83 | } |
<> | 160:d5399cc887bb | 84 | #endif |
<> | 160:d5399cc887bb | 85 | #if defined(TIM9_BASE) |
<> | 160:d5399cc887bb | 86 | if (obj->pwm == PWM_9) { |
<> | 160:d5399cc887bb | 87 | __HAL_RCC_TIM9_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 88 | } |
<> | 160:d5399cc887bb | 89 | #endif |
<> | 160:d5399cc887bb | 90 | #if defined(TIM10_BASE) |
<> | 160:d5399cc887bb | 91 | if (obj->pwm == PWM_10) { |
<> | 160:d5399cc887bb | 92 | __HAL_RCC_TIM10_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 93 | } |
<> | 160:d5399cc887bb | 94 | #endif |
<> | 160:d5399cc887bb | 95 | #if defined(TIM11_BASE) |
<> | 160:d5399cc887bb | 96 | if (obj->pwm == PWM_11) { |
<> | 160:d5399cc887bb | 97 | __HAL_RCC_TIM11_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 98 | } |
<> | 160:d5399cc887bb | 99 | #endif |
<> | 160:d5399cc887bb | 100 | #if defined(TIM12_BASE) |
<> | 160:d5399cc887bb | 101 | if (obj->pwm == PWM_12) { |
<> | 160:d5399cc887bb | 102 | __HAL_RCC_TIM12_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 103 | } |
<> | 160:d5399cc887bb | 104 | #endif |
<> | 160:d5399cc887bb | 105 | #if defined(TIM13_BASE) |
<> | 160:d5399cc887bb | 106 | if (obj->pwm == PWM_13) { |
<> | 160:d5399cc887bb | 107 | __HAL_RCC_TIM13_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 108 | } |
<> | 160:d5399cc887bb | 109 | #endif |
<> | 160:d5399cc887bb | 110 | #if defined(TIM14_BASE) |
<> | 160:d5399cc887bb | 111 | if (obj->pwm == PWM_14) { |
<> | 160:d5399cc887bb | 112 | __HAL_RCC_TIM14_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 113 | } |
<> | 160:d5399cc887bb | 114 | #endif |
<> | 160:d5399cc887bb | 115 | #if defined(TIM15_BASE) |
<> | 160:d5399cc887bb | 116 | if (obj->pwm == PWM_15) { |
<> | 160:d5399cc887bb | 117 | __HAL_RCC_TIM15_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 118 | } |
<> | 160:d5399cc887bb | 119 | #endif |
<> | 160:d5399cc887bb | 120 | #if defined(TIM16_BASE) |
<> | 160:d5399cc887bb | 121 | if (obj->pwm == PWM_16) { |
<> | 160:d5399cc887bb | 122 | __HAL_RCC_TIM16_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 123 | } |
<> | 160:d5399cc887bb | 124 | #endif |
<> | 160:d5399cc887bb | 125 | #if defined(TIM17_BASE) |
<> | 160:d5399cc887bb | 126 | if (obj->pwm == PWM_17) { |
<> | 160:d5399cc887bb | 127 | __HAL_RCC_TIM17_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 128 | } |
<> | 160:d5399cc887bb | 129 | #endif |
<> | 160:d5399cc887bb | 130 | #if defined(TIM18_BASE) |
<> | 160:d5399cc887bb | 131 | if (obj->pwm == PWM_18) { |
<> | 160:d5399cc887bb | 132 | __HAL_RCC_TIM18_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 133 | } |
<> | 160:d5399cc887bb | 134 | #endif |
<> | 160:d5399cc887bb | 135 | #if defined(TIM19_BASE) |
<> | 160:d5399cc887bb | 136 | if (obj->pwm == PWM_19) { |
<> | 160:d5399cc887bb | 137 | __HAL_RCC_TIM19_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 138 | } |
<> | 160:d5399cc887bb | 139 | #endif |
<> | 160:d5399cc887bb | 140 | #if defined(TIM20_BASE) |
<> | 160:d5399cc887bb | 141 | if (obj->pwm == PWM_20) { |
<> | 160:d5399cc887bb | 142 | __HAL_RCC_TIM20_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 143 | } |
<> | 160:d5399cc887bb | 144 | #endif |
<> | 160:d5399cc887bb | 145 | #if defined(TIM21_BASE) |
<> | 160:d5399cc887bb | 146 | if (obj->pwm == PWM_21) { |
<> | 160:d5399cc887bb | 147 | __HAL_RCC_TIM21_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 148 | } |
<> | 160:d5399cc887bb | 149 | #endif |
<> | 160:d5399cc887bb | 150 | #if defined(TIM22_BASE) |
<> | 160:d5399cc887bb | 151 | if (obj->pwm == PWM_22) { |
<> | 160:d5399cc887bb | 152 | __HAL_RCC_TIM22_CLK_ENABLE(); |
<> | 160:d5399cc887bb | 153 | } |
<> | 160:d5399cc887bb | 154 | #endif |
<> | 160:d5399cc887bb | 155 | // Configure GPIO |
<> | 160:d5399cc887bb | 156 | pinmap_pinout(pin, PinMap_PWM); |
<> | 160:d5399cc887bb | 157 | |
<> | 160:d5399cc887bb | 158 | obj->pin = pin; |
<> | 160:d5399cc887bb | 159 | obj->period = 0; |
<> | 160:d5399cc887bb | 160 | obj->pulse = 0; |
<> | 160:d5399cc887bb | 161 | obj->prescaler = 1; |
<> | 160:d5399cc887bb | 162 | |
<> | 160:d5399cc887bb | 163 | pwmout_period_us(obj, 20000); // 20 ms per default |
<> | 160:d5399cc887bb | 164 | } |
<> | 160:d5399cc887bb | 165 | |
<> | 160:d5399cc887bb | 166 | void pwmout_free(pwmout_t* obj) |
<> | 160:d5399cc887bb | 167 | { |
<> | 160:d5399cc887bb | 168 | // Configure GPIO |
<> | 160:d5399cc887bb | 169 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
<> | 160:d5399cc887bb | 170 | } |
<> | 160:d5399cc887bb | 171 | |
<> | 160:d5399cc887bb | 172 | void pwmout_write(pwmout_t* obj, float value) |
<> | 160:d5399cc887bb | 173 | { |
<> | 160:d5399cc887bb | 174 | TIM_OC_InitTypeDef sConfig; |
<> | 160:d5399cc887bb | 175 | int channel = 0; |
<> | 160:d5399cc887bb | 176 | |
<> | 160:d5399cc887bb | 177 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
<> | 160:d5399cc887bb | 178 | |
<> | 160:d5399cc887bb | 179 | if (value < (float)0.0) { |
<> | 160:d5399cc887bb | 180 | value = 0.0; |
<> | 160:d5399cc887bb | 181 | } else if (value > (float)1.0) { |
<> | 160:d5399cc887bb | 182 | value = 1.0; |
<> | 160:d5399cc887bb | 183 | } |
<> | 160:d5399cc887bb | 184 | |
<> | 160:d5399cc887bb | 185 | obj->pulse = (uint32_t)((float)obj->period * value); |
<> | 160:d5399cc887bb | 186 | |
<> | 160:d5399cc887bb | 187 | // Configure channels |
<> | 160:d5399cc887bb | 188 | sConfig.OCMode = TIM_OCMODE_PWM1; |
<> | 160:d5399cc887bb | 189 | sConfig.Pulse = obj->pulse / obj->prescaler; |
<> | 160:d5399cc887bb | 190 | sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
<> | 160:d5399cc887bb | 191 | sConfig.OCFastMode = TIM_OCFAST_DISABLE; |
<> | 160:d5399cc887bb | 192 | #if defined(TIM_OCIDLESTATE_RESET) |
<> | 160:d5399cc887bb | 193 | sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; |
<> | 160:d5399cc887bb | 194 | #endif |
<> | 160:d5399cc887bb | 195 | #if defined(TIM_OCNIDLESTATE_RESET) |
<> | 160:d5399cc887bb | 196 | sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
<> | 160:d5399cc887bb | 197 | sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
<> | 160:d5399cc887bb | 198 | #endif |
<> | 160:d5399cc887bb | 199 | |
<> | 160:d5399cc887bb | 200 | switch (obj->channel) { |
<> | 160:d5399cc887bb | 201 | case 1: |
<> | 160:d5399cc887bb | 202 | channel = TIM_CHANNEL_1; |
<> | 160:d5399cc887bb | 203 | break; |
<> | 160:d5399cc887bb | 204 | case 2: |
<> | 160:d5399cc887bb | 205 | channel = TIM_CHANNEL_2; |
<> | 160:d5399cc887bb | 206 | break; |
<> | 160:d5399cc887bb | 207 | case 3: |
<> | 160:d5399cc887bb | 208 | channel = TIM_CHANNEL_3; |
<> | 160:d5399cc887bb | 209 | break; |
<> | 160:d5399cc887bb | 210 | case 4: |
<> | 160:d5399cc887bb | 211 | channel = TIM_CHANNEL_4; |
<> | 160:d5399cc887bb | 212 | break; |
<> | 160:d5399cc887bb | 213 | default: |
<> | 160:d5399cc887bb | 214 | return; |
<> | 160:d5399cc887bb | 215 | } |
<> | 160:d5399cc887bb | 216 | |
<> | 160:d5399cc887bb | 217 | if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { |
<> | 160:d5399cc887bb | 218 | error("Cannot initialize PWM\n"); |
<> | 160:d5399cc887bb | 219 | } |
<> | 160:d5399cc887bb | 220 | |
<> | 160:d5399cc887bb | 221 | #if !defined(PWMOUT_INVERTED_NOT_SUPPORTED) |
<> | 160:d5399cc887bb | 222 | if (obj->inverted) { |
<> | 160:d5399cc887bb | 223 | HAL_TIMEx_PWMN_Start(&TimHandle, channel); |
<> | 160:d5399cc887bb | 224 | } else |
<> | 160:d5399cc887bb | 225 | #endif |
<> | 160:d5399cc887bb | 226 | { |
<> | 160:d5399cc887bb | 227 | HAL_TIM_PWM_Start(&TimHandle, channel); |
<> | 160:d5399cc887bb | 228 | } |
<> | 160:d5399cc887bb | 229 | } |
<> | 160:d5399cc887bb | 230 | |
<> | 160:d5399cc887bb | 231 | float pwmout_read(pwmout_t* obj) |
<> | 160:d5399cc887bb | 232 | { |
<> | 160:d5399cc887bb | 233 | float value = 0; |
<> | 160:d5399cc887bb | 234 | if (obj->period > 0) { |
<> | 160:d5399cc887bb | 235 | value = (float)(obj->pulse) / (float)(obj->period); |
<> | 160:d5399cc887bb | 236 | } |
<> | 160:d5399cc887bb | 237 | return ((value > (float)1.0) ? (float)(1.0) : (value)); |
<> | 160:d5399cc887bb | 238 | } |
<> | 160:d5399cc887bb | 239 | |
<> | 160:d5399cc887bb | 240 | void pwmout_period(pwmout_t* obj, float seconds) |
<> | 160:d5399cc887bb | 241 | { |
<> | 160:d5399cc887bb | 242 | pwmout_period_us(obj, seconds * 1000000.0f); |
<> | 160:d5399cc887bb | 243 | } |
<> | 160:d5399cc887bb | 244 | |
<> | 160:d5399cc887bb | 245 | void pwmout_period_ms(pwmout_t* obj, int ms) |
<> | 160:d5399cc887bb | 246 | { |
<> | 160:d5399cc887bb | 247 | pwmout_period_us(obj, ms * 1000); |
<> | 160:d5399cc887bb | 248 | } |
<> | 160:d5399cc887bb | 249 | |
<> | 160:d5399cc887bb | 250 | void pwmout_period_us(pwmout_t* obj, int us) |
<> | 160:d5399cc887bb | 251 | { |
<> | 160:d5399cc887bb | 252 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
<> | 160:d5399cc887bb | 253 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
<> | 160:d5399cc887bb | 254 | uint32_t PclkFreq = 0; |
<> | 160:d5399cc887bb | 255 | uint32_t APBxCLKDivider = RCC_HCLK_DIV1; |
<> | 160:d5399cc887bb | 256 | float dc = pwmout_read(obj); |
<> | 160:d5399cc887bb | 257 | uint8_t i = 0; |
<> | 160:d5399cc887bb | 258 | |
<> | 160:d5399cc887bb | 259 | __HAL_TIM_DISABLE(&TimHandle); |
<> | 160:d5399cc887bb | 260 | |
<> | 160:d5399cc887bb | 261 | // Get clock configuration |
<> | 160:d5399cc887bb | 262 | // Note: PclkFreq contains here the Latency (not used after) |
<> | 160:d5399cc887bb | 263 | HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); |
<> | 160:d5399cc887bb | 264 | |
<> | 160:d5399cc887bb | 265 | /* Parse the pwm / apb mapping table to find the right entry */ |
<> | 160:d5399cc887bb | 266 | while(pwm_apb_map_table[i].pwm != obj->pwm) { |
<> | 160:d5399cc887bb | 267 | i++; |
<> | 160:d5399cc887bb | 268 | } |
<> | 160:d5399cc887bb | 269 | |
<> | 160:d5399cc887bb | 270 | if(pwm_apb_map_table[i].pwm == 0) |
<> | 160:d5399cc887bb | 271 | error("Unknown PWM instance"); |
<> | 160:d5399cc887bb | 272 | |
<> | 160:d5399cc887bb | 273 | if(pwm_apb_map_table[i].pwmoutApb == PWMOUT_ON_APB1) { |
<> | 160:d5399cc887bb | 274 | PclkFreq = HAL_RCC_GetPCLK1Freq(); |
<> | 160:d5399cc887bb | 275 | APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; |
<> | 160:d5399cc887bb | 276 | } else { |
<> | 160:d5399cc887bb | 277 | #if !defined(PWMOUT_APB2_NOT_SUPPORTED) |
<> | 160:d5399cc887bb | 278 | PclkFreq = HAL_RCC_GetPCLK2Freq(); |
<> | 160:d5399cc887bb | 279 | APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; |
<> | 160:d5399cc887bb | 280 | #endif |
<> | 160:d5399cc887bb | 281 | } |
<> | 160:d5399cc887bb | 282 | |
<> | 160:d5399cc887bb | 283 | |
<> | 160:d5399cc887bb | 284 | /* By default use, 1us as SW pre-scaler */ |
<> | 160:d5399cc887bb | 285 | obj->prescaler = 1; |
<> | 160:d5399cc887bb | 286 | // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx |
<> | 160:d5399cc887bb | 287 | if (APBxCLKDivider == RCC_HCLK_DIV1) { |
<> | 160:d5399cc887bb | 288 | TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick |
<> | 160:d5399cc887bb | 289 | } else { |
<> | 160:d5399cc887bb | 290 | TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000)) - 1; // 1 us tick |
<> | 160:d5399cc887bb | 291 | } |
<> | 160:d5399cc887bb | 292 | TimHandle.Init.Period = (us - 1); |
<> | 160:d5399cc887bb | 293 | |
<> | 160:d5399cc887bb | 294 | /* In case period or pre-scalers are out of range, loop-in to get valid values */ |
<> | 160:d5399cc887bb | 295 | while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Prescaler > 0xFFFF)) { |
<> | 160:d5399cc887bb | 296 | obj->prescaler = obj->prescaler * 2; |
<> | 160:d5399cc887bb | 297 | if (APBxCLKDivider == RCC_HCLK_DIV1) { |
<> | 160:d5399cc887bb | 298 | TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; |
<> | 160:d5399cc887bb | 299 | } else { |
<> | 160:d5399cc887bb | 300 | TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; |
<> | 160:d5399cc887bb | 301 | } |
<> | 160:d5399cc887bb | 302 | TimHandle.Init.Period = (us - 1) / obj->prescaler; |
<> | 160:d5399cc887bb | 303 | /* Period decreases and prescaler increases over loops, so check for |
<> | 160:d5399cc887bb | 304 | * possible out of range cases */ |
<> | 160:d5399cc887bb | 305 | if ((TimHandle.Init.Period < 0xFFFF) && (TimHandle.Init.Prescaler > 0xFFFF)) { |
<> | 160:d5399cc887bb | 306 | error("Cannot initialize PWM\n"); |
<> | 160:d5399cc887bb | 307 | break; |
<> | 160:d5399cc887bb | 308 | } |
<> | 160:d5399cc887bb | 309 | } |
<> | 160:d5399cc887bb | 310 | |
<> | 160:d5399cc887bb | 311 | TimHandle.Init.ClockDivision = 0; |
<> | 160:d5399cc887bb | 312 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
<> | 160:d5399cc887bb | 313 | |
<> | 160:d5399cc887bb | 314 | if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { |
<> | 160:d5399cc887bb | 315 | error("Cannot initialize PWM\n"); |
<> | 160:d5399cc887bb | 316 | } |
<> | 160:d5399cc887bb | 317 | |
<> | 160:d5399cc887bb | 318 | // Save for future use |
<> | 160:d5399cc887bb | 319 | obj->period = us; |
<> | 160:d5399cc887bb | 320 | |
<> | 160:d5399cc887bb | 321 | // Set duty cycle again |
<> | 160:d5399cc887bb | 322 | pwmout_write(obj, dc); |
<> | 160:d5399cc887bb | 323 | |
<> | 160:d5399cc887bb | 324 | __HAL_TIM_ENABLE(&TimHandle); |
<> | 160:d5399cc887bb | 325 | } |
<> | 160:d5399cc887bb | 326 | |
<> | 160:d5399cc887bb | 327 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
<> | 160:d5399cc887bb | 328 | { |
<> | 160:d5399cc887bb | 329 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
<> | 160:d5399cc887bb | 330 | } |
<> | 160:d5399cc887bb | 331 | |
<> | 160:d5399cc887bb | 332 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
<> | 160:d5399cc887bb | 333 | { |
<> | 160:d5399cc887bb | 334 | pwmout_pulsewidth_us(obj, ms * 1000); |
<> | 160:d5399cc887bb | 335 | } |
<> | 160:d5399cc887bb | 336 | |
<> | 160:d5399cc887bb | 337 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
<> | 160:d5399cc887bb | 338 | { |
<> | 160:d5399cc887bb | 339 | float value = (float)us / (float)obj->period; |
<> | 160:d5399cc887bb | 340 | pwmout_write(obj, value); |
<> | 160:d5399cc887bb | 341 | } |
<> | 160:d5399cc887bb | 342 | |
<> | 160:d5399cc887bb | 343 | #endif |