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

Dependents:   SensorTileTest

Fork of mbed-dev by mbed official

Committer:
shaoziyang
Date:
Mon Jan 02 15:52:04 2017 +0000
Revision:
154:1375a99fb16d
Parent:
149:156823d33999
Mbed for ST SensorTile kit, fixed GPIOG bug, add PORTG support.

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 functions (timer channel, (non)inverted) from the pin and assign it 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
<> 144:ef7eb2e8f9f7 53 // Enable TIM clock
<> 144:ef7eb2e8f9f7 54 if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 55 if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 56 if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 57 if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 58 if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 59 if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 60 if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 61 if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 62 if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 63 if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 64 if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 65
<> 144:ef7eb2e8f9f7 66 // Configure GPIO
<> 144:ef7eb2e8f9f7 67 pinmap_pinout(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 68
<> 144:ef7eb2e8f9f7 69 obj->pin = pin;
<> 144:ef7eb2e8f9f7 70 obj->period = 0;
<> 144:ef7eb2e8f9f7 71 obj->pulse = 0;
<> 144:ef7eb2e8f9f7 72 obj->prescaler = 1;
<> 144:ef7eb2e8f9f7 73
<> 144:ef7eb2e8f9f7 74 pwmout_period_us(obj, 20000); // 20 ms per default
<> 144:ef7eb2e8f9f7 75 }
<> 144:ef7eb2e8f9f7 76
<> 144:ef7eb2e8f9f7 77 void pwmout_free(pwmout_t* obj)
<> 144:ef7eb2e8f9f7 78 {
<> 144:ef7eb2e8f9f7 79 // Configure GPIO
<> 144:ef7eb2e8f9f7 80 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
<> 144:ef7eb2e8f9f7 81 }
<> 144:ef7eb2e8f9f7 82
<> 144:ef7eb2e8f9f7 83 void pwmout_write(pwmout_t* obj, float value)
<> 144:ef7eb2e8f9f7 84 {
<> 144:ef7eb2e8f9f7 85 TIM_OC_InitTypeDef sConfig;
<> 144:ef7eb2e8f9f7 86 int channel = 0;
<> 144:ef7eb2e8f9f7 87
<> 144:ef7eb2e8f9f7 88 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 144:ef7eb2e8f9f7 89
<> 144:ef7eb2e8f9f7 90 if (value < (float)0.0) {
<> 144:ef7eb2e8f9f7 91 value = 0.0;
<> 144:ef7eb2e8f9f7 92 } else if (value > (float)1.0) {
<> 144:ef7eb2e8f9f7 93 value = 1.0;
<> 144:ef7eb2e8f9f7 94 }
<> 144:ef7eb2e8f9f7 95
<> 144:ef7eb2e8f9f7 96 obj->pulse = (uint32_t)((float)obj->period * value);
<> 144:ef7eb2e8f9f7 97
<> 144:ef7eb2e8f9f7 98 // Configure channels
<> 144:ef7eb2e8f9f7 99 sConfig.OCMode = TIM_OCMODE_PWM1;
<> 144:ef7eb2e8f9f7 100 sConfig.Pulse = obj->pulse / obj->prescaler;
<> 144:ef7eb2e8f9f7 101 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
<> 144:ef7eb2e8f9f7 102 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
<> 144:ef7eb2e8f9f7 103 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
<> 144:ef7eb2e8f9f7 104 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
<> 144:ef7eb2e8f9f7 105 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
<> 144:ef7eb2e8f9f7 106
<> 144:ef7eb2e8f9f7 107 switch (obj->channel) {
<> 144:ef7eb2e8f9f7 108 case 1:
<> 144:ef7eb2e8f9f7 109 channel = TIM_CHANNEL_1;
<> 144:ef7eb2e8f9f7 110 break;
<> 144:ef7eb2e8f9f7 111 case 2:
<> 144:ef7eb2e8f9f7 112 channel = TIM_CHANNEL_2;
<> 144:ef7eb2e8f9f7 113 break;
<> 144:ef7eb2e8f9f7 114 case 3:
<> 144:ef7eb2e8f9f7 115 channel = TIM_CHANNEL_3;
<> 144:ef7eb2e8f9f7 116 break;
<> 144:ef7eb2e8f9f7 117 case 4:
<> 144:ef7eb2e8f9f7 118 channel = TIM_CHANNEL_4;
<> 144:ef7eb2e8f9f7 119 break;
<> 144:ef7eb2e8f9f7 120 default:
<> 144:ef7eb2e8f9f7 121 return;
<> 144:ef7eb2e8f9f7 122 }
<> 144:ef7eb2e8f9f7 123
<> 144:ef7eb2e8f9f7 124 if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
<> 144:ef7eb2e8f9f7 125 error("Cannot configure PWM channel\n");
<> 144:ef7eb2e8f9f7 126 }
<> 144:ef7eb2e8f9f7 127
<> 144:ef7eb2e8f9f7 128 if (obj->inverted) {
<> 144:ef7eb2e8f9f7 129 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
<> 144:ef7eb2e8f9f7 130 } else {
<> 144:ef7eb2e8f9f7 131 HAL_TIM_PWM_Start(&TimHandle, channel);
<> 144:ef7eb2e8f9f7 132 }
<> 144:ef7eb2e8f9f7 133 }
<> 144:ef7eb2e8f9f7 134
<> 144:ef7eb2e8f9f7 135 float pwmout_read(pwmout_t* obj)
<> 144:ef7eb2e8f9f7 136 {
<> 144:ef7eb2e8f9f7 137 float value = 0;
<> 144:ef7eb2e8f9f7 138 if (obj->period > 0) {
<> 144:ef7eb2e8f9f7 139 value = (float)(obj->pulse) / (float)(obj->period);
<> 144:ef7eb2e8f9f7 140 }
<> 144:ef7eb2e8f9f7 141 return ((value > (float)1.0) ? (float)(1.0) : (value));
<> 144:ef7eb2e8f9f7 142 }
<> 144:ef7eb2e8f9f7 143
<> 144:ef7eb2e8f9f7 144 void pwmout_period(pwmout_t* obj, float seconds)
<> 144:ef7eb2e8f9f7 145 {
<> 144:ef7eb2e8f9f7 146 pwmout_period_us(obj, seconds * 1000000.0f);
<> 144:ef7eb2e8f9f7 147 }
<> 144:ef7eb2e8f9f7 148
<> 144:ef7eb2e8f9f7 149 void pwmout_period_ms(pwmout_t* obj, int ms)
<> 144:ef7eb2e8f9f7 150 {
<> 144:ef7eb2e8f9f7 151 pwmout_period_us(obj, ms * 1000);
<> 144:ef7eb2e8f9f7 152 }
<> 144:ef7eb2e8f9f7 153
<> 144:ef7eb2e8f9f7 154 void pwmout_period_us(pwmout_t* obj, int us)
<> 144:ef7eb2e8f9f7 155 {
<> 144:ef7eb2e8f9f7 156 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 144:ef7eb2e8f9f7 157 RCC_ClkInitTypeDef RCC_ClkInitStruct;
<> 144:ef7eb2e8f9f7 158 uint32_t PclkFreq;
<> 144:ef7eb2e8f9f7 159 uint32_t APBxCLKDivider;
<> 144:ef7eb2e8f9f7 160 float dc = pwmout_read(obj);
<> 144:ef7eb2e8f9f7 161
<> 144:ef7eb2e8f9f7 162 __HAL_TIM_DISABLE(&TimHandle);
<> 144:ef7eb2e8f9f7 163
<> 144:ef7eb2e8f9f7 164 // Get clock configuration
<> 144:ef7eb2e8f9f7 165 // Note: PclkFreq contains here the Latency (not used after)
<> 144:ef7eb2e8f9f7 166 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
<> 144:ef7eb2e8f9f7 167
<> 144:ef7eb2e8f9f7 168 // Get the PCLK and APBCLK divider related to the timer
<> 144:ef7eb2e8f9f7 169 switch (obj->pwm) {
<> 144:ef7eb2e8f9f7 170
<> 144:ef7eb2e8f9f7 171 // APB1 clock
<> 144:ef7eb2e8f9f7 172 case PWM_2:
<> 144:ef7eb2e8f9f7 173 case PWM_3:
<> 144:ef7eb2e8f9f7 174 case PWM_4:
<> 144:ef7eb2e8f9f7 175 case PWM_5:
<> 144:ef7eb2e8f9f7 176 case PWM_12:
<> 144:ef7eb2e8f9f7 177 case PWM_13:
<> 144:ef7eb2e8f9f7 178 case PWM_14:
<> 144:ef7eb2e8f9f7 179 PclkFreq = HAL_RCC_GetPCLK1Freq();
<> 144:ef7eb2e8f9f7 180 APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider;
<> 144:ef7eb2e8f9f7 181 break;
<> 144:ef7eb2e8f9f7 182
<> 144:ef7eb2e8f9f7 183 // APB2 clock
<> 144:ef7eb2e8f9f7 184 case PWM_1:
<> 144:ef7eb2e8f9f7 185 case PWM_8:
<> 144:ef7eb2e8f9f7 186 case PWM_9:
<> 144:ef7eb2e8f9f7 187 case PWM_10:
<> 144:ef7eb2e8f9f7 188 case PWM_11:
<> 144:ef7eb2e8f9f7 189 PclkFreq = HAL_RCC_GetPCLK2Freq();
<> 144:ef7eb2e8f9f7 190 APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider;
<> 144:ef7eb2e8f9f7 191 break;
<> 144:ef7eb2e8f9f7 192 default:
<> 144:ef7eb2e8f9f7 193 return;
<> 144:ef7eb2e8f9f7 194 }
<> 144:ef7eb2e8f9f7 195
<> 144:ef7eb2e8f9f7 196 /* To make it simple, we use to possible prescaler values which lead to:
<> 144:ef7eb2e8f9f7 197 * pwm unit = 1us, period/pulse can be from 1us to 65535us
<> 144:ef7eb2e8f9f7 198 * or
<> 144:ef7eb2e8f9f7 199 * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
<> 144:ef7eb2e8f9f7 200 * Be careful that all the channels of a PWM shares the same prescaler
<> 144:ef7eb2e8f9f7 201 */
<> 144:ef7eb2e8f9f7 202 if (us > 0xFFFF) {
<> 144:ef7eb2e8f9f7 203 obj->prescaler = 500;
<> 144:ef7eb2e8f9f7 204 } else {
<> 144:ef7eb2e8f9f7 205 obj->prescaler = 1;
<> 144:ef7eb2e8f9f7 206 }
<> 144:ef7eb2e8f9f7 207
<> 144:ef7eb2e8f9f7 208 // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
<> 144:ef7eb2e8f9f7 209 if (APBxCLKDivider == RCC_HCLK_DIV1)
<> 144:ef7eb2e8f9f7 210 TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq) / 1000000) * obj->prescaler) - 1; // 1 us tick
<> 144:ef7eb2e8f9f7 211 else
<> 144:ef7eb2e8f9f7 212 TimHandle.Init.Prescaler = (uint16_t)(((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; // 1 us tick
<> 144:ef7eb2e8f9f7 213
<> 144:ef7eb2e8f9f7 214 if (TimHandle.Init.Prescaler > 0xFFFF)
<> 144:ef7eb2e8f9f7 215 error("PWM: out of range prescaler");
<> 144:ef7eb2e8f9f7 216
<> 144:ef7eb2e8f9f7 217 TimHandle.Init.Period = (us - 1) / obj->prescaler;
<> 144:ef7eb2e8f9f7 218 if (TimHandle.Init.Period > 0xFFFF)
<> 144:ef7eb2e8f9f7 219 error("PWM: out of range period");
<> 144:ef7eb2e8f9f7 220
<> 144:ef7eb2e8f9f7 221 TimHandle.Init.ClockDivision = 0;
<> 144:ef7eb2e8f9f7 222 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
<> 144:ef7eb2e8f9f7 223
<> 144:ef7eb2e8f9f7 224 if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
<> 144:ef7eb2e8f9f7 225 error("Cannot initialize PWM\n");
<> 144:ef7eb2e8f9f7 226 }
<> 144:ef7eb2e8f9f7 227
<> 144:ef7eb2e8f9f7 228 // Save for future use
<> 144:ef7eb2e8f9f7 229 obj->period = us;
<> 144:ef7eb2e8f9f7 230
<> 144:ef7eb2e8f9f7 231 // Set duty cycle again
<> 144:ef7eb2e8f9f7 232 pwmout_write(obj, dc);
<> 144:ef7eb2e8f9f7 233
<> 144:ef7eb2e8f9f7 234 __HAL_TIM_ENABLE(&TimHandle);
<> 144:ef7eb2e8f9f7 235 }
<> 144:ef7eb2e8f9f7 236
<> 144:ef7eb2e8f9f7 237 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
<> 144:ef7eb2e8f9f7 238 {
<> 144:ef7eb2e8f9f7 239 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
<> 144:ef7eb2e8f9f7 240 }
<> 144:ef7eb2e8f9f7 241
<> 144:ef7eb2e8f9f7 242 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
<> 144:ef7eb2e8f9f7 243 {
<> 144:ef7eb2e8f9f7 244 pwmout_pulsewidth_us(obj, ms * 1000);
<> 144:ef7eb2e8f9f7 245 }
<> 144:ef7eb2e8f9f7 246
<> 144:ef7eb2e8f9f7 247 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
<> 144:ef7eb2e8f9f7 248 {
<> 144:ef7eb2e8f9f7 249 float value = (float)us / (float)obj->period;
<> 144:ef7eb2e8f9f7 250 pwmout_write(obj, value);
<> 144:ef7eb2e8f9f7 251 }
<> 144:ef7eb2e8f9f7 252
<> 144:ef7eb2e8f9f7 253 #endif