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