mbed library sources. Supersedes mbed-src. Add PORTG support for STM32L476JG (SensorTile kit)

Dependents:   SensorTileTest

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
Parent:
targets/hal/TARGET_STM/TARGET_STM32F3/pwmout_api.c@147:30b64687e01f
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /* mbed Microcontroller Library
<> 144:ef7eb2e8f9f7 2 *******************************************************************************
<> 144:ef7eb2e8f9f7 3 * Copyright (c) 2015, STMicroelectronics
<> 144:ef7eb2e8f9f7 4 * All rights reserved.
<> 144:ef7eb2e8f9f7 5 *
<> 144:ef7eb2e8f9f7 6 * Redistribution and use in source and binary forms, with or without
<> 144:ef7eb2e8f9f7 7 * modification, are permitted provided that the following conditions are met:
<> 144:ef7eb2e8f9f7 8 *
<> 144:ef7eb2e8f9f7 9 * 1. Redistributions of source code must retain the above copyright notice,
<> 144:ef7eb2e8f9f7 10 * this list of conditions and the following disclaimer.
<> 144:ef7eb2e8f9f7 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
<> 144:ef7eb2e8f9f7 12 * this list of conditions and the following disclaimer in the documentation
<> 144:ef7eb2e8f9f7 13 * and/or other materials provided with the distribution.
<> 144:ef7eb2e8f9f7 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
<> 144:ef7eb2e8f9f7 15 * may be used to endorse or promote products derived from this software
<> 144:ef7eb2e8f9f7 16 * without specific prior written permission.
<> 144:ef7eb2e8f9f7 17 *
<> 144:ef7eb2e8f9f7 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
<> 144:ef7eb2e8f9f7 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
<> 144:ef7eb2e8f9f7 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 144:ef7eb2e8f9f7 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
<> 144:ef7eb2e8f9f7 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
<> 144:ef7eb2e8f9f7 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
<> 144:ef7eb2e8f9f7 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
<> 144:ef7eb2e8f9f7 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
<> 144:ef7eb2e8f9f7 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
<> 144:ef7eb2e8f9f7 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 144:ef7eb2e8f9f7 28 *******************************************************************************
<> 144:ef7eb2e8f9f7 29 */
<> 144:ef7eb2e8f9f7 30 #include "pwmout_api.h"
<> 144:ef7eb2e8f9f7 31
<> 144:ef7eb2e8f9f7 32 #if DEVICE_PWMOUT
<> 144:ef7eb2e8f9f7 33
<> 144:ef7eb2e8f9f7 34 #include "cmsis.h"
<> 144:ef7eb2e8f9f7 35 #include "pinmap.h"
<> 144:ef7eb2e8f9f7 36 #include "mbed_error.h"
<> 144:ef7eb2e8f9f7 37 #include "PeripheralPins.h"
<> 144:ef7eb2e8f9f7 38
<> 144:ef7eb2e8f9f7 39 static TIM_HandleTypeDef TimHandle;
<> 144:ef7eb2e8f9f7 40
<> 144:ef7eb2e8f9f7 41 void pwmout_init(pwmout_t* obj, PinName pin)
<> 144:ef7eb2e8f9f7 42 {
<> 144:ef7eb2e8f9f7 43 // Get the peripheral name from the pin and assign it to the object
<> 144:ef7eb2e8f9f7 44 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 45 MBED_ASSERT(obj->pwm != (PWMName)NC);
<> 144:ef7eb2e8f9f7 46
<> 144:ef7eb2e8f9f7 47 // Get the pin function and assign the used channel to the object
<> 144:ef7eb2e8f9f7 48 uint32_t function = pinmap_function(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 49 MBED_ASSERT(function != (uint32_t)NC);
<> 144:ef7eb2e8f9f7 50 obj->channel = STM_PIN_CHANNEL(function);
<> 144:ef7eb2e8f9f7 51 obj->inverted = STM_PIN_INVERTED(function);
<> 144:ef7eb2e8f9f7 52
<> 147:30b64687e01f 53 #if defined(TIM1_BASE)
<> 147:30b64687e01f 54 if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE();
<> 147:30b64687e01f 55 #endif
<> 147:30b64687e01f 56 #if defined(TIM2_BASE)
<> 147:30b64687e01f 57 if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE();
<> 147:30b64687e01f 58 #endif
<> 147:30b64687e01f 59 #if defined(TIM3_BASE)
<> 147:30b64687e01f 60 if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE();
<> 147:30b64687e01f 61 #endif
<> 147:30b64687e01f 62 #if defined(TIM4_BASE)
<> 147:30b64687e01f 63 if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE();
<> 147:30b64687e01f 64 #endif
<> 147:30b64687e01f 65 #if defined(TIM5_BASE)
<> 147:30b64687e01f 66 if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE();
<> 147:30b64687e01f 67 #endif
<> 147:30b64687e01f 68 #if defined(TIM8_BASE)
<> 147:30b64687e01f 69 if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE();
<> 147:30b64687e01f 70 #endif
<> 147:30b64687e01f 71 #if defined(TIM9_BASE)
<> 147:30b64687e01f 72 if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE();
<> 147:30b64687e01f 73 #endif
<> 147:30b64687e01f 74 #if defined(TIM10_BASE)
<> 147:30b64687e01f 75 if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE();
<> 147:30b64687e01f 76 #endif
<> 147:30b64687e01f 77 #if defined(TIM11_BASE)
<> 147:30b64687e01f 78 if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 79 #endif
<> 147:30b64687e01f 80 #if defined(TIM12_BASE)
<> 147:30b64687e01f 81 if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE();
<> 147:30b64687e01f 82 #endif
<> 147:30b64687e01f 83 #if defined(TIM13_BASE)
<> 147:30b64687e01f 84 if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE();
<> 147:30b64687e01f 85 #endif
<> 147:30b64687e01f 86 #if defined(TIM14_BASE)
<> 147:30b64687e01f 87 if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE();
<> 147:30b64687e01f 88 #endif
<> 147:30b64687e01f 89 #if defined(TIM15_BASE)
<> 147:30b64687e01f 90 if (obj->pwm == PWM_15) __HAL_RCC_TIM15_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 91 #endif
<> 147:30b64687e01f 92 #if defined(TIM16_BASE)
<> 147:30b64687e01f 93 if (obj->pwm == PWM_16) __HAL_RCC_TIM16_CLK_ENABLE();
<> 147:30b64687e01f 94 #endif
<> 147:30b64687e01f 95 #if defined(TIM17_BASE)
<> 147:30b64687e01f 96 if (obj->pwm == PWM_17) __HAL_RCC_TIM17_CLK_ENABLE();
<> 147:30b64687e01f 97 #endif
<> 147:30b64687e01f 98 #if defined(TIM18_BASE)
<> 147:30b64687e01f 99 if (obj->pwm == PWM_18) __HAL_RCC_TIM18_CLK_ENABLE();
<> 147:30b64687e01f 100 #endif
<> 147:30b64687e01f 101 #if defined(TIM19_BASE)
<> 147:30b64687e01f 102 if (obj->pwm == PWM_19) __HAL_RCC_TIM19_CLK_ENABLE();
<> 147:30b64687e01f 103 #endif
<> 147:30b64687e01f 104 #if defined(TIM20_BASE)
<> 147:30b64687e01f 105 if (obj->pwm == PWM_20) __HAL_RCC_TIM20_CLK_ENABLE();
<> 147:30b64687e01f 106 #endif
<> 144:ef7eb2e8f9f7 107
<> 144:ef7eb2e8f9f7 108 // Configure GPIO
<> 144:ef7eb2e8f9f7 109 pinmap_pinout(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 110
<> 144:ef7eb2e8f9f7 111 obj->pin = pin;
<> 144:ef7eb2e8f9f7 112 obj->period = 0;
<> 144:ef7eb2e8f9f7 113 obj->pulse = 0;
<> 144:ef7eb2e8f9f7 114 obj->prescaler = 1;
<> 144:ef7eb2e8f9f7 115
<> 144:ef7eb2e8f9f7 116 pwmout_period_us(obj, 20000); // 20 ms per default
<> 144:ef7eb2e8f9f7 117 }
<> 144:ef7eb2e8f9f7 118
<> 144:ef7eb2e8f9f7 119 void pwmout_free(pwmout_t* obj)
<> 144:ef7eb2e8f9f7 120 {
<> 144:ef7eb2e8f9f7 121 // Configure GPIO
<> 144:ef7eb2e8f9f7 122 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
<> 144:ef7eb2e8f9f7 123 }
<> 144:ef7eb2e8f9f7 124
<> 144:ef7eb2e8f9f7 125 void pwmout_write(pwmout_t* obj, float value)
<> 144:ef7eb2e8f9f7 126 {
<> 144:ef7eb2e8f9f7 127 TIM_OC_InitTypeDef sConfig;
<> 144:ef7eb2e8f9f7 128 int channel = 0;
<> 144:ef7eb2e8f9f7 129
<> 144:ef7eb2e8f9f7 130 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 144:ef7eb2e8f9f7 131
<> 144:ef7eb2e8f9f7 132 if (value < (float)0.0) {
<> 144:ef7eb2e8f9f7 133 value = 0.0;
<> 144:ef7eb2e8f9f7 134 } else if (value > (float)1.0) {
<> 144:ef7eb2e8f9f7 135 value = 1.0;
<> 144:ef7eb2e8f9f7 136 }
<> 144:ef7eb2e8f9f7 137
<> 144:ef7eb2e8f9f7 138 obj->pulse = (uint32_t)((float)obj->period * value);
<> 144:ef7eb2e8f9f7 139
<> 144:ef7eb2e8f9f7 140 // Configure channels
<> 144:ef7eb2e8f9f7 141 sConfig.OCMode = TIM_OCMODE_PWM1;
<> 144:ef7eb2e8f9f7 142 sConfig.Pulse = obj->pulse / obj->prescaler;
<> 144:ef7eb2e8f9f7 143 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
<> 144:ef7eb2e8f9f7 144 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
<> 144:ef7eb2e8f9f7 145 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
<> 144:ef7eb2e8f9f7 146 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
<> 144:ef7eb2e8f9f7 147 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
<> 144:ef7eb2e8f9f7 148
<> 144:ef7eb2e8f9f7 149 switch (obj->channel) {
<> 144:ef7eb2e8f9f7 150 case 1:
<> 144:ef7eb2e8f9f7 151 channel = TIM_CHANNEL_1;
<> 144:ef7eb2e8f9f7 152 break;
<> 144:ef7eb2e8f9f7 153 case 2:
<> 144:ef7eb2e8f9f7 154 channel = TIM_CHANNEL_2;
<> 144:ef7eb2e8f9f7 155 break;
<> 144:ef7eb2e8f9f7 156 case 3:
<> 144:ef7eb2e8f9f7 157 channel = TIM_CHANNEL_3;
<> 144:ef7eb2e8f9f7 158 break;
<> 144:ef7eb2e8f9f7 159 case 4:
<> 144:ef7eb2e8f9f7 160 channel = TIM_CHANNEL_4;
<> 144:ef7eb2e8f9f7 161 break;
<> 144:ef7eb2e8f9f7 162 default:
<> 144:ef7eb2e8f9f7 163 return;
<> 144:ef7eb2e8f9f7 164 }
<> 144:ef7eb2e8f9f7 165
<> 144:ef7eb2e8f9f7 166 if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
<> 144:ef7eb2e8f9f7 167 error("Cannot initialize PWM");
<> 144:ef7eb2e8f9f7 168 }
<> 144:ef7eb2e8f9f7 169
<> 144:ef7eb2e8f9f7 170 if (obj->inverted) {
<> 144:ef7eb2e8f9f7 171 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
<> 144:ef7eb2e8f9f7 172 } else {
<> 144:ef7eb2e8f9f7 173 HAL_TIM_PWM_Start(&TimHandle, channel);
<> 144:ef7eb2e8f9f7 174 }
<> 144:ef7eb2e8f9f7 175 }
<> 144:ef7eb2e8f9f7 176
<> 144:ef7eb2e8f9f7 177 float pwmout_read(pwmout_t* obj)
<> 144:ef7eb2e8f9f7 178 {
<> 144:ef7eb2e8f9f7 179 float value = 0;
<> 144:ef7eb2e8f9f7 180 if (obj->period > 0) {
<> 144:ef7eb2e8f9f7 181 value = (float)(obj->pulse) / (float)(obj->period);
<> 144:ef7eb2e8f9f7 182 }
<> 144:ef7eb2e8f9f7 183 return ((value > (float)1.0) ? (float)(1.0) : (value));
<> 144:ef7eb2e8f9f7 184 }
<> 144:ef7eb2e8f9f7 185
<> 144:ef7eb2e8f9f7 186 void pwmout_period(pwmout_t* obj, float seconds)
<> 144:ef7eb2e8f9f7 187 {
<> 144:ef7eb2e8f9f7 188 pwmout_period_us(obj, seconds * 1000000.0f);
<> 144:ef7eb2e8f9f7 189 }
<> 144:ef7eb2e8f9f7 190
<> 144:ef7eb2e8f9f7 191 void pwmout_period_ms(pwmout_t* obj, int ms)
<> 144:ef7eb2e8f9f7 192 {
<> 144:ef7eb2e8f9f7 193 pwmout_period_us(obj, ms * 1000);
<> 144:ef7eb2e8f9f7 194 }
<> 144:ef7eb2e8f9f7 195
<> 144:ef7eb2e8f9f7 196 void pwmout_period_us(pwmout_t* obj, int us)
<> 144:ef7eb2e8f9f7 197 {
<> 144:ef7eb2e8f9f7 198 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 144:ef7eb2e8f9f7 199
<> 144:ef7eb2e8f9f7 200 float dc = pwmout_read(obj);
<> 144:ef7eb2e8f9f7 201
<> 144:ef7eb2e8f9f7 202 __HAL_TIM_DISABLE(&TimHandle);
<> 144:ef7eb2e8f9f7 203
<> 144:ef7eb2e8f9f7 204 // Update the SystemCoreClock variable
<> 144:ef7eb2e8f9f7 205 SystemCoreClockUpdate();
<> 144:ef7eb2e8f9f7 206
<> 144:ef7eb2e8f9f7 207 /* To make it simple, we use to possible prescaler values which lead to:
<> 144:ef7eb2e8f9f7 208 * pwm unit = 1us, period/pulse can be from 1us to 65535us
<> 144:ef7eb2e8f9f7 209 * or
<> 144:ef7eb2e8f9f7 210 * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
<> 144:ef7eb2e8f9f7 211 * Be careful that all the channels of a PWM shares the same prescaler
<> 144:ef7eb2e8f9f7 212 */
<> 144:ef7eb2e8f9f7 213 if (us > 0xFFFF) {
<> 144:ef7eb2e8f9f7 214 obj->prescaler = 500;
<> 144:ef7eb2e8f9f7 215 } else {
<> 144:ef7eb2e8f9f7 216 obj->prescaler = 1;
<> 144:ef7eb2e8f9f7 217 }
<> 144:ef7eb2e8f9f7 218 TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1;
<> 144:ef7eb2e8f9f7 219
<> 144:ef7eb2e8f9f7 220 if (TimHandle.Init.Prescaler > 0xFFFF)
<> 144:ef7eb2e8f9f7 221 error("PWM: out of range prescaler");
<> 144:ef7eb2e8f9f7 222
<> 144:ef7eb2e8f9f7 223 TimHandle.Init.Period = (us - 1) / obj->prescaler;
<> 144:ef7eb2e8f9f7 224 if (TimHandle.Init.Period > 0xFFFF)
<> 144:ef7eb2e8f9f7 225 error("PWM: out of range period");
<> 144:ef7eb2e8f9f7 226
<> 144:ef7eb2e8f9f7 227 TimHandle.Init.ClockDivision = 0;
<> 144:ef7eb2e8f9f7 228 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
<> 144:ef7eb2e8f9f7 229
<> 144:ef7eb2e8f9f7 230 if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
<> 144:ef7eb2e8f9f7 231 error("Cannot initialize PWM");
<> 144:ef7eb2e8f9f7 232 }
<> 144:ef7eb2e8f9f7 233
<> 144:ef7eb2e8f9f7 234 // Save for future use
<> 144:ef7eb2e8f9f7 235 obj->period = us;
<> 144:ef7eb2e8f9f7 236
<> 144:ef7eb2e8f9f7 237 // Set duty cycle again
<> 144:ef7eb2e8f9f7 238 pwmout_write(obj, dc);
<> 144:ef7eb2e8f9f7 239
<> 144:ef7eb2e8f9f7 240 __HAL_TIM_ENABLE(&TimHandle);
<> 144:ef7eb2e8f9f7 241 }
<> 144:ef7eb2e8f9f7 242
<> 144:ef7eb2e8f9f7 243 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
<> 144:ef7eb2e8f9f7 244 {
<> 144:ef7eb2e8f9f7 245 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
<> 144:ef7eb2e8f9f7 246 }
<> 144:ef7eb2e8f9f7 247
<> 144:ef7eb2e8f9f7 248 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
<> 144:ef7eb2e8f9f7 249 {
<> 144:ef7eb2e8f9f7 250 pwmout_pulsewidth_us(obj, ms * 1000);
<> 144:ef7eb2e8f9f7 251 }
<> 144:ef7eb2e8f9f7 252
<> 144:ef7eb2e8f9f7 253 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
<> 144:ef7eb2e8f9f7 254 {
<> 144:ef7eb2e8f9f7 255 float value = (float)us / (float)obj->period;
<> 144:ef7eb2e8f9f7 256 pwmout_write(obj, value);
<> 144:ef7eb2e8f9f7 257 }
<> 144:ef7eb2e8f9f7 258
<> 144:ef7eb2e8f9f7 259 #endif