Guido Grassel / mbed-src

Dependents:   Seeed_Barometer_Sensor_Example

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Oct 09 08:15:07 2014 +0100
Revision:
340:28d1f895c6fe
Child:
344:ed24f9926218
Synchronized with git revision b5a4c8e80393336b2656fb29ab46d405d3068602

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

HAL: nrf51822 - Few fixes for PWM and Serial

Who changed what in which revision?

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