Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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