Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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