test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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