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
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
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 *******************************************************************************
<> 149:156823d33999 3 * Copyright (c) 2014, STMicroelectronics
<> 149:156823d33999 4 * All rights reserved.
<> 149:156823d33999 5 *
<> 149:156823d33999 6 * Redistribution and use in source and binary forms, with or without
<> 149:156823d33999 7 * modification, are permitted provided that the following conditions are met:
<> 149:156823d33999 8 *
<> 149:156823d33999 9 * 1. Redistributions of source code must retain the above copyright notice,
<> 149:156823d33999 10 * this list of conditions and the following disclaimer.
<> 149:156823d33999 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
<> 149:156823d33999 12 * this list of conditions and the following disclaimer in the documentation
<> 149:156823d33999 13 * and/or other materials provided with the distribution.
<> 149:156823d33999 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
<> 149:156823d33999 15 * may be used to endorse or promote products derived from this software
<> 149:156823d33999 16 * without specific prior written permission.
<> 149:156823d33999 17 *
<> 149:156823d33999 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
<> 149:156823d33999 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
<> 149:156823d33999 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 149:156823d33999 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
<> 149:156823d33999 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
<> 149:156823d33999 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
<> 149:156823d33999 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
<> 149:156823d33999 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
<> 149:156823d33999 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
<> 149:156823d33999 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 149:156823d33999 28 *******************************************************************************
<> 149:156823d33999 29 */
<> 149:156823d33999 30 #include "pwmout_api.h"
<> 149:156823d33999 31
<> 149:156823d33999 32 #if DEVICE_PWMOUT
<> 149:156823d33999 33
<> 149:156823d33999 34 #include "cmsis.h"
<> 149:156823d33999 35 #include "pinmap.h"
<> 149:156823d33999 36 #include "mbed_error.h"
<> 149:156823d33999 37 #include "PeripheralPins.h"
<> 149:156823d33999 38
<> 149:156823d33999 39 static TIM_HandleTypeDef TimHandle;
<> 149:156823d33999 40
<> 149:156823d33999 41 void pwmout_init(pwmout_t* obj, PinName pin)
<> 149:156823d33999 42 {
<> 149:156823d33999 43 // Get the peripheral name from the pin and assign it to the object
<> 149:156823d33999 44 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
<> 149:156823d33999 45
<> 149:156823d33999 46 if (obj->pwm == (PWMName)NC) {
<> 149:156823d33999 47 error("PWM error: pinout mapping failed.");
<> 149:156823d33999 48 }
<> 149:156823d33999 49
<> 149:156823d33999 50 // Enable TIM clock
<> 149:156823d33999 51 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
<> 149:156823d33999 52 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
<> 149:156823d33999 53 if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
<> 149:156823d33999 54 if (obj->pwm == PWM_5) __TIM5_CLK_ENABLE();
<> 149:156823d33999 55 if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE();
<> 149:156823d33999 56 if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE();
<> 149:156823d33999 57 if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE();
<> 149:156823d33999 58
<> 149:156823d33999 59 // Configure GPIO
<> 149:156823d33999 60 pinmap_pinout(pin, PinMap_PWM);
<> 149:156823d33999 61
<> 149:156823d33999 62 obj->pin = pin;
<> 149:156823d33999 63 obj->period = 0;
<> 149:156823d33999 64 obj->pulse = 0;
<> 149:156823d33999 65 obj->prescaler = 1;
<> 149:156823d33999 66
<> 149:156823d33999 67 pwmout_period_us(obj, 20000); // 20 ms per default
<> 149:156823d33999 68 }
<> 149:156823d33999 69
<> 149:156823d33999 70 void pwmout_free(pwmout_t* obj)
<> 149:156823d33999 71 {
<> 149:156823d33999 72 // Configure GPIO
<> 149:156823d33999 73 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
<> 149:156823d33999 74 }
<> 149:156823d33999 75
<> 149:156823d33999 76 void pwmout_write(pwmout_t* obj, float value)
<> 149:156823d33999 77 {
<> 149:156823d33999 78 TIM_OC_InitTypeDef sConfig;
<> 149:156823d33999 79 int channel = 0;
<> 149:156823d33999 80
<> 149:156823d33999 81 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 149:156823d33999 82
<> 149:156823d33999 83 if (value < (float)0.0) {
<> 149:156823d33999 84 value = 0.0;
<> 149:156823d33999 85 } else if (value > (float)1.0) {
<> 149:156823d33999 86 value = 1.0;
<> 149:156823d33999 87 }
<> 149:156823d33999 88
<> 149:156823d33999 89 obj->pulse = (uint32_t)((float)obj->period * value);
<> 149:156823d33999 90
<> 149:156823d33999 91 // Configure channels
<> 149:156823d33999 92 sConfig.OCMode = TIM_OCMODE_PWM1;
<> 149:156823d33999 93 sConfig.Pulse = obj->pulse / obj->prescaler;
<> 149:156823d33999 94 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
<> 149:156823d33999 95 sConfig.OCFastMode = TIM_OCFAST_ENABLE;
<> 149:156823d33999 96
<> 149:156823d33999 97 switch (obj->pin) {
<> 149:156823d33999 98 // Channels 1
<> 149:156823d33999 99 case PA_6:
<> 149:156823d33999 100 case PB_4:
<> 149:156823d33999 101 case PB_6:
<> 149:156823d33999 102 case PB_12:
<> 149:156823d33999 103 case PB_13:
<> 149:156823d33999 104 case PB_15:
<> 149:156823d33999 105 case PC_6:
<> 149:156823d33999 106 channel = TIM_CHANNEL_1;
<> 149:156823d33999 107 break;
<> 149:156823d33999 108 // Channels 2
<> 149:156823d33999 109 case PA_1:
<> 149:156823d33999 110 case PA_7:
<> 149:156823d33999 111 case PB_3:
<> 149:156823d33999 112 case PB_5:
<> 149:156823d33999 113 case PB_7:
<> 149:156823d33999 114 case PB_14:
<> 149:156823d33999 115 case PC_7:
<> 149:156823d33999 116 channel = TIM_CHANNEL_2;
<> 149:156823d33999 117 break;
<> 149:156823d33999 118 // Channels 3
<> 149:156823d33999 119 case PA_2:
<> 149:156823d33999 120 case PB_0:
<> 149:156823d33999 121 case PB_8:
<> 149:156823d33999 122 case PB_10:
<> 149:156823d33999 123 case PC_8:
<> 149:156823d33999 124 channel = TIM_CHANNEL_3;
<> 149:156823d33999 125 break;
<> 149:156823d33999 126 // Channels 4
<> 149:156823d33999 127 case PA_3:
<> 149:156823d33999 128 case PB_1:
<> 149:156823d33999 129 case PB_9:
<> 149:156823d33999 130 case PB_11:
<> 149:156823d33999 131 case PC_9:
<> 149:156823d33999 132 channel = TIM_CHANNEL_4;
<> 149:156823d33999 133 break;
<> 149:156823d33999 134 default:
<> 149:156823d33999 135 return;
<> 149:156823d33999 136 }
<> 149:156823d33999 137
<> 149:156823d33999 138 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
<> 149:156823d33999 139 HAL_TIM_PWM_Start(&TimHandle, channel);
<> 149:156823d33999 140 }
<> 149:156823d33999 141
<> 149:156823d33999 142 float pwmout_read(pwmout_t* obj)
<> 149:156823d33999 143 {
<> 149:156823d33999 144 float value = 0;
<> 149:156823d33999 145 if (obj->period > 0) {
<> 149:156823d33999 146 value = (float)(obj->pulse) / (float)(obj->period);
<> 149:156823d33999 147 }
<> 149:156823d33999 148 return ((value > (float)1.0) ? (float)(1.0) : (value));
<> 149:156823d33999 149 }
<> 149:156823d33999 150
<> 149:156823d33999 151 void pwmout_period(pwmout_t* obj, float seconds)
<> 149:156823d33999 152 {
<> 149:156823d33999 153 pwmout_period_us(obj, seconds * 1000000.0f);
<> 149:156823d33999 154 }
<> 149:156823d33999 155
<> 149:156823d33999 156 void pwmout_period_ms(pwmout_t* obj, int ms)
<> 149:156823d33999 157 {
<> 149:156823d33999 158 pwmout_period_us(obj, ms * 1000);
<> 149:156823d33999 159 }
<> 149:156823d33999 160
<> 149:156823d33999 161 void pwmout_period_us(pwmout_t* obj, int us)
<> 149:156823d33999 162 {
<> 149:156823d33999 163 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 149:156823d33999 164
<> 149:156823d33999 165 float dc = pwmout_read(obj);
<> 149:156823d33999 166
<> 149:156823d33999 167 __HAL_TIM_DISABLE(&TimHandle);
<> 149:156823d33999 168
<> 149:156823d33999 169 /* To make it simple, we use to possible prescaler values which lead to:
<> 149:156823d33999 170 * pwm unit = 1us, period/pulse can be from 1us to 65535us
<> 149:156823d33999 171 * or
<> 149:156823d33999 172 * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
<> 149:156823d33999 173 * Be careful that all the channels of a PWM shares the same prescaler
<> 149:156823d33999 174 */
<> 149:156823d33999 175 if (us > 0xFFFF) {
<> 149:156823d33999 176 obj->prescaler = 500;
<> 149:156823d33999 177 } else {
<> 149:156823d33999 178 obj->prescaler = 1;
<> 149:156823d33999 179 }
<> 149:156823d33999 180 TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1;
<> 149:156823d33999 181
<> 149:156823d33999 182 if (TimHandle.Init.Prescaler > 0xFFFF)
<> 149:156823d33999 183 error("PWM: out of range prescaler");
<> 149:156823d33999 184
<> 149:156823d33999 185 TimHandle.Init.Period = (us - 1) / obj->prescaler;
<> 149:156823d33999 186 if (TimHandle.Init.Period > 0xFFFF)
<> 149:156823d33999 187 error("PWM: out of range period");
<> 149:156823d33999 188
<> 149:156823d33999 189 TimHandle.Init.ClockDivision = 0;
<> 149:156823d33999 190 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
<> 149:156823d33999 191
<> 149:156823d33999 192 if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
<> 149:156823d33999 193 error("Cannot initialize PWM");
<> 149:156823d33999 194 }
<> 149:156823d33999 195
<> 149:156823d33999 196 // Save for future use
<> 149:156823d33999 197 obj->period = us;
<> 149:156823d33999 198
<> 149:156823d33999 199 // Set duty cycle again
<> 149:156823d33999 200 pwmout_write(obj, dc);
<> 149:156823d33999 201
<> 149:156823d33999 202 __HAL_TIM_ENABLE(&TimHandle);
<> 149:156823d33999 203 }
<> 149:156823d33999 204
<> 149:156823d33999 205 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
<> 149:156823d33999 206 {
<> 149:156823d33999 207 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
<> 149:156823d33999 208 }
<> 149:156823d33999 209
<> 149:156823d33999 210 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
<> 149:156823d33999 211 {
<> 149:156823d33999 212 pwmout_pulsewidth_us(obj, ms * 1000);
<> 149:156823d33999 213 }
<> 149:156823d33999 214
<> 149:156823d33999 215 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
<> 149:156823d33999 216 {
<> 149:156823d33999 217 float value = (float)us / (float)obj->period;
<> 149:156823d33999 218 pwmout_write(obj, value);
<> 149:156823d33999 219 }
<> 149:156823d33999 220
<> 149:156823d33999 221 #endif