mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Mar 24 17:45:07 2014 +0000
Revision:
133:d4dda5c437f0
Child:
159:3b23f6d9ecb9
Synchronized with git revision 47b961246bed973fe4cb8932781ffc8025b78a61

Full URL: https://github.com/mbedmicro/mbed/commit/47b961246bed973fe4cb8932781ffc8025b78a61/

[STM32F4-Discovery (STM32F407VG)] initial port

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 133:d4dda5c437f0 30 #include "pwmout_api.h"
mbed_official 133:d4dda5c437f0 31
mbed_official 133:d4dda5c437f0 32 #include "cmsis.h"
mbed_official 133:d4dda5c437f0 33 #include "pinmap.h"
mbed_official 133:d4dda5c437f0 34 #include "error.h"
mbed_official 133:d4dda5c437f0 35 #include "stm32f4xx_hal.h"
mbed_official 133:d4dda5c437f0 36
mbed_official 133:d4dda5c437f0 37 // TIM5 cannot be used because already used by the us_ticker
mbed_official 133:d4dda5c437f0 38 static const PinMap PinMap_PWM[] = {
mbed_official 133:d4dda5c437f0 39 {PA_0, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 133:d4dda5c437f0 40 //{PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1
mbed_official 133:d4dda5c437f0 41 {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 133:d4dda5c437f0 42 //{PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH2
mbed_official 133:d4dda5c437f0 43 {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 133:d4dda5c437f0 44 //{PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3
mbed_official 133:d4dda5c437f0 45 //{PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1
mbed_official 133:d4dda5c437f0 46 {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 133:d4dda5c437f0 47 //{PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4
mbed_official 133:d4dda5c437f0 48 //{PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2
mbed_official 133:d4dda5c437f0 49 {PA_5, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 133:d4dda5c437f0 50 {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 51 {PA_7, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 133:d4dda5c437f0 52 //{PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 53 {PA_8, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1
mbed_official 133:d4dda5c437f0 54 {PA_9, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2
mbed_official 133:d4dda5c437f0 55 {PA_10, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3
mbed_official 133:d4dda5c437f0 56 {PA_11, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH4
mbed_official 133:d4dda5c437f0 57 {PA_15, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 133:d4dda5c437f0 58
mbed_official 133:d4dda5c437f0 59 {PB_0, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 133:d4dda5c437f0 60 //{PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 133:d4dda5c437f0 61 {PB_1, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 133:d4dda5c437f0 62 //{PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4
mbed_official 133:d4dda5c437f0 63 {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 133:d4dda5c437f0 64 {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 65 {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 66 {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1
mbed_official 133:d4dda5c437f0 67 {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2
mbed_official 133:d4dda5c437f0 68 {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3
mbed_official 133:d4dda5c437f0 69 //{PB_8, PWM_10,STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1
mbed_official 133:d4dda5c437f0 70 {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4
mbed_official 133:d4dda5c437f0 71 //{PB_9, PWM_11,STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1
mbed_official 133:d4dda5c437f0 72 {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 133:d4dda5c437f0 73 {PB_13, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 133:d4dda5c437f0 74 {PB_14, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 133:d4dda5c437f0 75 {PB_15, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 133:d4dda5c437f0 76
mbed_official 133:d4dda5c437f0 77 {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 78 {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 79 {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 133:d4dda5c437f0 80 {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 133:d4dda5c437f0 81
mbed_official 133:d4dda5c437f0 82 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 83 };
mbed_official 133:d4dda5c437f0 84
mbed_official 133:d4dda5c437f0 85 static TIM_HandleTypeDef TimHandle;
mbed_official 133:d4dda5c437f0 86
mbed_official 133:d4dda5c437f0 87 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 133:d4dda5c437f0 88 // Get the peripheral name from the pin and assign it to the object
mbed_official 133:d4dda5c437f0 89 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 133:d4dda5c437f0 90
mbed_official 133:d4dda5c437f0 91 if (obj->pwm == (PWMName)NC) {
mbed_official 133:d4dda5c437f0 92 error("PWM error: pinout mapping failed.");
mbed_official 133:d4dda5c437f0 93 }
mbed_official 133:d4dda5c437f0 94
mbed_official 133:d4dda5c437f0 95 // Enable TIM clock
mbed_official 133:d4dda5c437f0 96 if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 97 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 98 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 99 if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 100 if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 101 if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 102 if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 103
mbed_official 133:d4dda5c437f0 104 // Configure GPIO
mbed_official 133:d4dda5c437f0 105 pinmap_pinout(pin, PinMap_PWM);
mbed_official 133:d4dda5c437f0 106
mbed_official 133:d4dda5c437f0 107 obj->pin = pin;
mbed_official 133:d4dda5c437f0 108 obj->period = 0;
mbed_official 133:d4dda5c437f0 109 obj->pulse = 0;
mbed_official 133:d4dda5c437f0 110
mbed_official 133:d4dda5c437f0 111 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 133:d4dda5c437f0 112 }
mbed_official 133:d4dda5c437f0 113
mbed_official 133:d4dda5c437f0 114 void pwmout_free(pwmout_t* obj) {
mbed_official 133:d4dda5c437f0 115 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 116
mbed_official 133:d4dda5c437f0 117 HAL_TIM_PWM_DeInit(&TimHandle);
mbed_official 133:d4dda5c437f0 118
mbed_official 133:d4dda5c437f0 119 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 133:d4dda5c437f0 120 }
mbed_official 133:d4dda5c437f0 121
mbed_official 133:d4dda5c437f0 122 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 133:d4dda5c437f0 123 TIM_OC_InitTypeDef sConfig;
mbed_official 133:d4dda5c437f0 124 int channel = 0;
mbed_official 133:d4dda5c437f0 125 int complementary_channel = 0;
mbed_official 133:d4dda5c437f0 126
mbed_official 133:d4dda5c437f0 127 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 128
mbed_official 133:d4dda5c437f0 129 if (value < (float)0.0) {
mbed_official 133:d4dda5c437f0 130 value = 0.0;
mbed_official 133:d4dda5c437f0 131 } else if (value > (float)1.0) {
mbed_official 133:d4dda5c437f0 132 value = 1.0;
mbed_official 133:d4dda5c437f0 133 }
mbed_official 133:d4dda5c437f0 134
mbed_official 133:d4dda5c437f0 135 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 133:d4dda5c437f0 136
mbed_official 133:d4dda5c437f0 137 // Configure channels
mbed_official 133:d4dda5c437f0 138 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 133:d4dda5c437f0 139 sConfig.Pulse = obj->pulse;
mbed_official 133:d4dda5c437f0 140 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 133:d4dda5c437f0 141 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
mbed_official 133:d4dda5c437f0 142 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 133:d4dda5c437f0 143 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
mbed_official 133:d4dda5c437f0 144 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
mbed_official 133:d4dda5c437f0 145
mbed_official 133:d4dda5c437f0 146 switch (obj->pin) {
mbed_official 133:d4dda5c437f0 147 // Channels 1
mbed_official 133:d4dda5c437f0 148 case PA_0:
mbed_official 133:d4dda5c437f0 149 //case PA_2:
mbed_official 133:d4dda5c437f0 150 case PA_5:
mbed_official 133:d4dda5c437f0 151 case PA_6:
mbed_official 133:d4dda5c437f0 152 case PA_8:
mbed_official 133:d4dda5c437f0 153 case PA_15:
mbed_official 133:d4dda5c437f0 154 case PB_4:
mbed_official 133:d4dda5c437f0 155 case PB_6:
mbed_official 133:d4dda5c437f0 156 //case PB_8:
mbed_official 133:d4dda5c437f0 157 //case PB_9:
mbed_official 133:d4dda5c437f0 158 case PC_6:
mbed_official 133:d4dda5c437f0 159 channel = TIM_CHANNEL_1;
mbed_official 133:d4dda5c437f0 160 break;
mbed_official 133:d4dda5c437f0 161 // Channels 1N
mbed_official 133:d4dda5c437f0 162 case PA_7:
mbed_official 133:d4dda5c437f0 163 case PB_13:
mbed_official 133:d4dda5c437f0 164 channel = TIM_CHANNEL_1;
mbed_official 133:d4dda5c437f0 165 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 166 break;
mbed_official 133:d4dda5c437f0 167 // Channels 2
mbed_official 133:d4dda5c437f0 168 case PA_1:
mbed_official 133:d4dda5c437f0 169 //case PA_3:
mbed_official 133:d4dda5c437f0 170 //case PA_7:
mbed_official 133:d4dda5c437f0 171 case PA_9:
mbed_official 133:d4dda5c437f0 172 case PB_3:
mbed_official 133:d4dda5c437f0 173 case PB_5:
mbed_official 133:d4dda5c437f0 174 case PB_7:
mbed_official 133:d4dda5c437f0 175 case PC_7:
mbed_official 133:d4dda5c437f0 176 channel = TIM_CHANNEL_2;
mbed_official 133:d4dda5c437f0 177 break;
mbed_official 133:d4dda5c437f0 178 // Channels 2N
mbed_official 133:d4dda5c437f0 179 case PB_0:
mbed_official 133:d4dda5c437f0 180 case PB_14:
mbed_official 133:d4dda5c437f0 181 channel = TIM_CHANNEL_2;
mbed_official 133:d4dda5c437f0 182 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 183 break;
mbed_official 133:d4dda5c437f0 184 // Channels 3
mbed_official 133:d4dda5c437f0 185 case PA_2:
mbed_official 133:d4dda5c437f0 186 case PA_3:
mbed_official 133:d4dda5c437f0 187 case PA_10:
mbed_official 133:d4dda5c437f0 188 //case PB_0:
mbed_official 133:d4dda5c437f0 189 case PB_8:
mbed_official 133:d4dda5c437f0 190 case PB_10:
mbed_official 133:d4dda5c437f0 191 case PC_8:
mbed_official 133:d4dda5c437f0 192 case PC_9:
mbed_official 133:d4dda5c437f0 193 channel = TIM_CHANNEL_3;
mbed_official 133:d4dda5c437f0 194 break;
mbed_official 133:d4dda5c437f0 195 // Channels 3N
mbed_official 133:d4dda5c437f0 196 case PB_1:
mbed_official 133:d4dda5c437f0 197 case PB_15:
mbed_official 133:d4dda5c437f0 198 channel = TIM_CHANNEL_3;
mbed_official 133:d4dda5c437f0 199 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 200 break;
mbed_official 133:d4dda5c437f0 201 // Channels 4
mbed_official 133:d4dda5c437f0 202 //case PA_3:
mbed_official 133:d4dda5c437f0 203 case PA_11:
mbed_official 133:d4dda5c437f0 204 //case PB_1:
mbed_official 133:d4dda5c437f0 205 case PB_9:
mbed_official 133:d4dda5c437f0 206 channel = TIM_CHANNEL_4;
mbed_official 133:d4dda5c437f0 207 break;
mbed_official 133:d4dda5c437f0 208 default:
mbed_official 133:d4dda5c437f0 209 return;
mbed_official 133:d4dda5c437f0 210 }
mbed_official 133:d4dda5c437f0 211
mbed_official 133:d4dda5c437f0 212 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
mbed_official 133:d4dda5c437f0 213 if (complementary_channel) {
mbed_official 133:d4dda5c437f0 214 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
mbed_official 133:d4dda5c437f0 215 }
mbed_official 133:d4dda5c437f0 216 else {
mbed_official 133:d4dda5c437f0 217 HAL_TIM_PWM_Start(&TimHandle, channel);
mbed_official 133:d4dda5c437f0 218 }
mbed_official 133:d4dda5c437f0 219 }
mbed_official 133:d4dda5c437f0 220
mbed_official 133:d4dda5c437f0 221 float pwmout_read(pwmout_t* obj) {
mbed_official 133:d4dda5c437f0 222 float value = 0;
mbed_official 133:d4dda5c437f0 223 if (obj->period > 0) {
mbed_official 133:d4dda5c437f0 224 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 133:d4dda5c437f0 225 }
mbed_official 133:d4dda5c437f0 226 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 133:d4dda5c437f0 227 }
mbed_official 133:d4dda5c437f0 228
mbed_official 133:d4dda5c437f0 229 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 133:d4dda5c437f0 230 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 133:d4dda5c437f0 231 }
mbed_official 133:d4dda5c437f0 232
mbed_official 133:d4dda5c437f0 233 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 133:d4dda5c437f0 234 pwmout_period_us(obj, ms * 1000);
mbed_official 133:d4dda5c437f0 235 }
mbed_official 133:d4dda5c437f0 236
mbed_official 133:d4dda5c437f0 237 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 133:d4dda5c437f0 238 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 239
mbed_official 133:d4dda5c437f0 240 float dc = pwmout_read(obj);
mbed_official 133:d4dda5c437f0 241
mbed_official 133:d4dda5c437f0 242 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 133:d4dda5c437f0 243
mbed_official 133:d4dda5c437f0 244 TimHandle.Init.Period = us - 1;
mbed_official 133:d4dda5c437f0 245 TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 2 / 1000000) - 1; // 1 µs tick
mbed_official 133:d4dda5c437f0 246 TimHandle.Init.ClockDivision = 0;
mbed_official 133:d4dda5c437f0 247 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 133:d4dda5c437f0 248 HAL_TIM_PWM_Init(&TimHandle);
mbed_official 133:d4dda5c437f0 249
mbed_official 133:d4dda5c437f0 250 // Set duty cycle again
mbed_official 133:d4dda5c437f0 251 pwmout_write(obj, dc);
mbed_official 133:d4dda5c437f0 252
mbed_official 133:d4dda5c437f0 253 // Save for future use
mbed_official 133:d4dda5c437f0 254 obj->period = us;
mbed_official 133:d4dda5c437f0 255
mbed_official 133:d4dda5c437f0 256 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 133:d4dda5c437f0 257 }
mbed_official 133:d4dda5c437f0 258
mbed_official 133:d4dda5c437f0 259 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 133:d4dda5c437f0 260 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 133:d4dda5c437f0 261 }
mbed_official 133:d4dda5c437f0 262
mbed_official 133:d4dda5c437f0 263 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 133:d4dda5c437f0 264 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 133:d4dda5c437f0 265 }
mbed_official 133:d4dda5c437f0 266
mbed_official 133:d4dda5c437f0 267 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 133:d4dda5c437f0 268 float value = (float)us / (float)obj->period;
mbed_official 133:d4dda5c437f0 269 pwmout_write(obj, value);
mbed_official 133:d4dda5c437f0 270 }