mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jun 11 16:00:09 2014 +0100
Revision:
227:7bd0639b8911
Parent:
159:3b23f6d9ecb9
Child:
242:7074e42da0b2
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af

Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 133:d4dda5c437f0 1 /* mbed Microcontroller Library
mbed_official 133:d4dda5c437f0 2 *******************************************************************************
mbed_official 133:d4dda5c437f0 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 133:d4dda5c437f0 4 * All rights reserved.
mbed_official 133:d4dda5c437f0 5 *
mbed_official 133:d4dda5c437f0 6 * Redistribution and use in source and binary forms, with or without
mbed_official 133:d4dda5c437f0 7 * modification, are permitted provided that the following conditions are met:
mbed_official 133:d4dda5c437f0 8 *
mbed_official 133:d4dda5c437f0 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 133:d4dda5c437f0 10 * this list of conditions and the following disclaimer.
mbed_official 133:d4dda5c437f0 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 133:d4dda5c437f0 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 133:d4dda5c437f0 13 * and/or other materials provided with the distribution.
mbed_official 133:d4dda5c437f0 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 133:d4dda5c437f0 15 * may be used to endorse or promote products derived from this software
mbed_official 133:d4dda5c437f0 16 * without specific prior written permission.
mbed_official 133:d4dda5c437f0 17 *
mbed_official 133:d4dda5c437f0 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 133:d4dda5c437f0 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 133:d4dda5c437f0 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 133:d4dda5c437f0 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 133:d4dda5c437f0 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 133:d4dda5c437f0 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 133:d4dda5c437f0 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 133:d4dda5c437f0 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 133:d4dda5c437f0 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 133:d4dda5c437f0 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 133:d4dda5c437f0 28 *******************************************************************************
mbed_official 133:d4dda5c437f0 29 */
mbed_official 227:7bd0639b8911 30 #include "mbed_assert.h"
mbed_official 133:d4dda5c437f0 31 #include "pwmout_api.h"
mbed_official 133:d4dda5c437f0 32
mbed_official 133:d4dda5c437f0 33 #include "cmsis.h"
mbed_official 133:d4dda5c437f0 34 #include "pinmap.h"
mbed_official 133:d4dda5c437f0 35 #include "error.h"
mbed_official 133:d4dda5c437f0 36 #include "stm32f4xx_hal.h"
mbed_official 133:d4dda5c437f0 37
mbed_official 133:d4dda5c437f0 38 // TIM5 cannot be used because already used by the us_ticker
mbed_official 133:d4dda5c437f0 39 static const PinMap PinMap_PWM[] = {
mbed_official 133:d4dda5c437f0 40 {PA_0, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 133:d4dda5c437f0 41 //{PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1
mbed_official 133:d4dda5c437f0 42 {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 133:d4dda5c437f0 43 //{PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH2
mbed_official 133:d4dda5c437f0 44 {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 133:d4dda5c437f0 45 //{PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3
mbed_official 133:d4dda5c437f0 46 //{PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1
mbed_official 159:3b23f6d9ecb9 47 {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4
mbed_official 133:d4dda5c437f0 48 //{PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4
mbed_official 133:d4dda5c437f0 49 //{PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2
mbed_official 133:d4dda5c437f0 50 {PA_5, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 133:d4dda5c437f0 51 {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 52 {PA_7, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 133:d4dda5c437f0 53 //{PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 54 {PA_8, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1
mbed_official 133:d4dda5c437f0 55 {PA_9, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2
mbed_official 133:d4dda5c437f0 56 {PA_10, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3
mbed_official 133:d4dda5c437f0 57 {PA_11, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH4
mbed_official 133:d4dda5c437f0 58 {PA_15, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 133:d4dda5c437f0 59
mbed_official 133:d4dda5c437f0 60 {PB_0, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 133:d4dda5c437f0 61 //{PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 133:d4dda5c437f0 62 {PB_1, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 133:d4dda5c437f0 63 //{PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4
mbed_official 133:d4dda5c437f0 64 {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 133:d4dda5c437f0 65 {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 66 {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 67 {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1
mbed_official 133:d4dda5c437f0 68 {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2
mbed_official 133:d4dda5c437f0 69 {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3
mbed_official 133:d4dda5c437f0 70 //{PB_8, PWM_10,STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1
mbed_official 133:d4dda5c437f0 71 {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4
mbed_official 133:d4dda5c437f0 72 //{PB_9, PWM_11,STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1
mbed_official 133:d4dda5c437f0 73 {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 133:d4dda5c437f0 74 {PB_13, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 133:d4dda5c437f0 75 {PB_14, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 133:d4dda5c437f0 76 {PB_15, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 133:d4dda5c437f0 77
mbed_official 133:d4dda5c437f0 78 {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 79 {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 80 {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 159:3b23f6d9ecb9 81 {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4
mbed_official 133:d4dda5c437f0 82
mbed_official 133:d4dda5c437f0 83 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 84 };
mbed_official 133:d4dda5c437f0 85
mbed_official 133:d4dda5c437f0 86 static TIM_HandleTypeDef TimHandle;
mbed_official 133:d4dda5c437f0 87
mbed_official 133:d4dda5c437f0 88 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 133:d4dda5c437f0 89 // Get the peripheral name from the pin and assign it to the object
mbed_official 133:d4dda5c437f0 90 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 227:7bd0639b8911 91 MBED_ASSERT(obj->pwm != (PWMName)NC);
mbed_official 227:7bd0639b8911 92
mbed_official 133:d4dda5c437f0 93 // Enable TIM clock
mbed_official 133:d4dda5c437f0 94 if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 95 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 96 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 97 if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 98 if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 99 if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 100 if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 101
mbed_official 133:d4dda5c437f0 102 // Configure GPIO
mbed_official 133:d4dda5c437f0 103 pinmap_pinout(pin, PinMap_PWM);
mbed_official 133:d4dda5c437f0 104
mbed_official 133:d4dda5c437f0 105 obj->pin = pin;
mbed_official 133:d4dda5c437f0 106 obj->period = 0;
mbed_official 133:d4dda5c437f0 107 obj->pulse = 0;
mbed_official 133:d4dda5c437f0 108
mbed_official 133:d4dda5c437f0 109 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 133:d4dda5c437f0 110 }
mbed_official 133:d4dda5c437f0 111
mbed_official 133:d4dda5c437f0 112 void pwmout_free(pwmout_t* obj) {
mbed_official 133:d4dda5c437f0 113 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 114
mbed_official 133:d4dda5c437f0 115 HAL_TIM_PWM_DeInit(&TimHandle);
mbed_official 133:d4dda5c437f0 116
mbed_official 133:d4dda5c437f0 117 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 133:d4dda5c437f0 118 }
mbed_official 133:d4dda5c437f0 119
mbed_official 133:d4dda5c437f0 120 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 133:d4dda5c437f0 121 TIM_OC_InitTypeDef sConfig;
mbed_official 133:d4dda5c437f0 122 int channel = 0;
mbed_official 133:d4dda5c437f0 123 int complementary_channel = 0;
mbed_official 133:d4dda5c437f0 124
mbed_official 133:d4dda5c437f0 125 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 126
mbed_official 133:d4dda5c437f0 127 if (value < (float)0.0) {
mbed_official 133:d4dda5c437f0 128 value = 0.0;
mbed_official 133:d4dda5c437f0 129 } else if (value > (float)1.0) {
mbed_official 133:d4dda5c437f0 130 value = 1.0;
mbed_official 133:d4dda5c437f0 131 }
mbed_official 133:d4dda5c437f0 132
mbed_official 133:d4dda5c437f0 133 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 133:d4dda5c437f0 134
mbed_official 133:d4dda5c437f0 135 // Configure channels
mbed_official 133:d4dda5c437f0 136 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 133:d4dda5c437f0 137 sConfig.Pulse = obj->pulse;
mbed_official 133:d4dda5c437f0 138 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 133:d4dda5c437f0 139 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
mbed_official 133:d4dda5c437f0 140 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 133:d4dda5c437f0 141 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
mbed_official 133:d4dda5c437f0 142 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
mbed_official 133:d4dda5c437f0 143
mbed_official 133:d4dda5c437f0 144 switch (obj->pin) {
mbed_official 133:d4dda5c437f0 145 // Channels 1
mbed_official 133:d4dda5c437f0 146 case PA_0:
mbed_official 133:d4dda5c437f0 147 //case PA_2:
mbed_official 133:d4dda5c437f0 148 case PA_5:
mbed_official 133:d4dda5c437f0 149 case PA_6:
mbed_official 133:d4dda5c437f0 150 case PA_8:
mbed_official 133:d4dda5c437f0 151 case PA_15:
mbed_official 133:d4dda5c437f0 152 case PB_4:
mbed_official 133:d4dda5c437f0 153 case PB_6:
mbed_official 133:d4dda5c437f0 154 //case PB_8:
mbed_official 133:d4dda5c437f0 155 //case PB_9:
mbed_official 133:d4dda5c437f0 156 case PC_6:
mbed_official 133:d4dda5c437f0 157 channel = TIM_CHANNEL_1;
mbed_official 133:d4dda5c437f0 158 break;
mbed_official 133:d4dda5c437f0 159 // Channels 1N
mbed_official 133:d4dda5c437f0 160 case PA_7:
mbed_official 133:d4dda5c437f0 161 case PB_13:
mbed_official 133:d4dda5c437f0 162 channel = TIM_CHANNEL_1;
mbed_official 133:d4dda5c437f0 163 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 164 break;
mbed_official 133:d4dda5c437f0 165 // Channels 2
mbed_official 133:d4dda5c437f0 166 case PA_1:
mbed_official 133:d4dda5c437f0 167 //case PA_3:
mbed_official 133:d4dda5c437f0 168 //case PA_7:
mbed_official 133:d4dda5c437f0 169 case PA_9:
mbed_official 133:d4dda5c437f0 170 case PB_3:
mbed_official 133:d4dda5c437f0 171 case PB_5:
mbed_official 133:d4dda5c437f0 172 case PB_7:
mbed_official 133:d4dda5c437f0 173 case PC_7:
mbed_official 133:d4dda5c437f0 174 channel = TIM_CHANNEL_2;
mbed_official 133:d4dda5c437f0 175 break;
mbed_official 133:d4dda5c437f0 176 // Channels 2N
mbed_official 133:d4dda5c437f0 177 case PB_0:
mbed_official 133:d4dda5c437f0 178 case PB_14:
mbed_official 133:d4dda5c437f0 179 channel = TIM_CHANNEL_2;
mbed_official 133:d4dda5c437f0 180 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 181 break;
mbed_official 133:d4dda5c437f0 182 // Channels 3
mbed_official 133:d4dda5c437f0 183 case PA_2:
mbed_official 159:3b23f6d9ecb9 184 //case PA_3:
mbed_official 133:d4dda5c437f0 185 case PA_10:
mbed_official 133:d4dda5c437f0 186 //case PB_0:
mbed_official 133:d4dda5c437f0 187 case PB_8:
mbed_official 133:d4dda5c437f0 188 case PB_10:
mbed_official 133:d4dda5c437f0 189 case PC_8:
mbed_official 159:3b23f6d9ecb9 190 //case PC_9:
mbed_official 133:d4dda5c437f0 191 channel = TIM_CHANNEL_3;
mbed_official 133:d4dda5c437f0 192 break;
mbed_official 133:d4dda5c437f0 193 // Channels 3N
mbed_official 133:d4dda5c437f0 194 case PB_1:
mbed_official 133:d4dda5c437f0 195 case PB_15:
mbed_official 133:d4dda5c437f0 196 channel = TIM_CHANNEL_3;
mbed_official 133:d4dda5c437f0 197 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 198 break;
mbed_official 133:d4dda5c437f0 199 // Channels 4
mbed_official 159:3b23f6d9ecb9 200 case PA_3:
mbed_official 133:d4dda5c437f0 201 case PA_11:
mbed_official 133:d4dda5c437f0 202 //case PB_1:
mbed_official 133:d4dda5c437f0 203 case PB_9:
mbed_official 159:3b23f6d9ecb9 204 case PC_9:
mbed_official 133:d4dda5c437f0 205 channel = TIM_CHANNEL_4;
mbed_official 133:d4dda5c437f0 206 break;
mbed_official 133:d4dda5c437f0 207 default:
mbed_official 133:d4dda5c437f0 208 return;
mbed_official 133:d4dda5c437f0 209 }
mbed_official 133:d4dda5c437f0 210
mbed_official 133:d4dda5c437f0 211 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
mbed_official 133:d4dda5c437f0 212 if (complementary_channel) {
mbed_official 133:d4dda5c437f0 213 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
mbed_official 133:d4dda5c437f0 214 }
mbed_official 133:d4dda5c437f0 215 else {
mbed_official 133:d4dda5c437f0 216 HAL_TIM_PWM_Start(&TimHandle, channel);
mbed_official 133:d4dda5c437f0 217 }
mbed_official 133:d4dda5c437f0 218 }
mbed_official 133:d4dda5c437f0 219
mbed_official 133:d4dda5c437f0 220 float pwmout_read(pwmout_t* obj) {
mbed_official 133:d4dda5c437f0 221 float value = 0;
mbed_official 133:d4dda5c437f0 222 if (obj->period > 0) {
mbed_official 133:d4dda5c437f0 223 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 133:d4dda5c437f0 224 }
mbed_official 133:d4dda5c437f0 225 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 133:d4dda5c437f0 226 }
mbed_official 133:d4dda5c437f0 227
mbed_official 133:d4dda5c437f0 228 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 133:d4dda5c437f0 229 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 133:d4dda5c437f0 230 }
mbed_official 133:d4dda5c437f0 231
mbed_official 133:d4dda5c437f0 232 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 133:d4dda5c437f0 233 pwmout_period_us(obj, ms * 1000);
mbed_official 133:d4dda5c437f0 234 }
mbed_official 133:d4dda5c437f0 235
mbed_official 133:d4dda5c437f0 236 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 133:d4dda5c437f0 237 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 238
mbed_official 133:d4dda5c437f0 239 float dc = pwmout_read(obj);
mbed_official 133:d4dda5c437f0 240
mbed_official 133:d4dda5c437f0 241 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 133:d4dda5c437f0 242
mbed_official 133:d4dda5c437f0 243 TimHandle.Init.Period = us - 1;
mbed_official 133:d4dda5c437f0 244 TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 2 / 1000000) - 1; // 1 µs tick
mbed_official 133:d4dda5c437f0 245 TimHandle.Init.ClockDivision = 0;
mbed_official 133:d4dda5c437f0 246 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 133:d4dda5c437f0 247 HAL_TIM_PWM_Init(&TimHandle);
mbed_official 133:d4dda5c437f0 248
mbed_official 133:d4dda5c437f0 249 // Set duty cycle again
mbed_official 133:d4dda5c437f0 250 pwmout_write(obj, dc);
mbed_official 133:d4dda5c437f0 251
mbed_official 133:d4dda5c437f0 252 // Save for future use
mbed_official 133:d4dda5c437f0 253 obj->period = us;
mbed_official 133:d4dda5c437f0 254
mbed_official 133:d4dda5c437f0 255 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 133:d4dda5c437f0 256 }
mbed_official 133:d4dda5c437f0 257
mbed_official 133:d4dda5c437f0 258 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 133:d4dda5c437f0 259 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 133:d4dda5c437f0 260 }
mbed_official 133:d4dda5c437f0 261
mbed_official 133:d4dda5c437f0 262 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 133:d4dda5c437f0 263 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 133:d4dda5c437f0 264 }
mbed_official 133:d4dda5c437f0 265
mbed_official 133:d4dda5c437f0 266 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 133:d4dda5c437f0 267 float value = (float)us / (float)obj->period;
mbed_official 133:d4dda5c437f0 268 pwmout_write(obj, value);
mbed_official 133:d4dda5c437f0 269 }