mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Jul 29 19:00:07 2014 +0100
Revision:
268:402bcc0c870b
Parent:
251:de9a1e4ffd79
Child:
285:31249416b6f9
Synchronized with git revision 490d1a6606b3138f165c5edf2f2370ca616587c0

Full URL: https://github.com/mbedmicro/mbed/commit/490d1a6606b3138f165c5edf2f2370ca616587c0/

[LPC1114] Sleep fix + some device.h settings

Who changed what in which revision?

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