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