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 MBED_ASSERT(obj->pwm != (PWMName)NC);
<> 149:156823d33999 46
<> 149:156823d33999 47 // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object
<> 149:156823d33999 48 uint32_t function = pinmap_function(pin, PinMap_PWM);
<> 149:156823d33999 49 MBED_ASSERT(function != (uint32_t)NC);
<> 149:156823d33999 50 obj->channel = STM_PIN_CHANNEL(function);
<> 149:156823d33999 51 obj->inverted = STM_PIN_INVERTED(function);
<> 149:156823d33999 52
<> 149:156823d33999 53 // Enable TIM clock
<> 149:156823d33999 54 if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
<> 149:156823d33999 55 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
<> 149:156823d33999 56 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
<> 149:156823d33999 57
<> 149:156823d33999 58 // Configure GPIO
<> 149:156823d33999 59 pinmap_pinout(pin, PinMap_PWM);
<> 149:156823d33999 60
<> 149:156823d33999 61 obj->pin = pin;
<> 149:156823d33999 62 obj->period = 0;
<> 149:156823d33999 63 obj->pulse = 0;
<> 149:156823d33999 64 obj->prescaler = 1;
<> 149:156823d33999 65
<> 149:156823d33999 66 pwmout_period_us(obj, 20000); // 20 ms per default
<> 149:156823d33999 67 }
<> 149:156823d33999 68
<> 149:156823d33999 69 void pwmout_free(pwmout_t* obj)
<> 149:156823d33999 70 {
<> 149:156823d33999 71 // Configure GPIO
<> 149:156823d33999 72 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
<> 149:156823d33999 73 }
<> 149:156823d33999 74
<> 149:156823d33999 75 void pwmout_write(pwmout_t* obj, float value)
<> 149:156823d33999 76 {
<> 149:156823d33999 77 TIM_OC_InitTypeDef sConfig;
<> 149:156823d33999 78 int channel = 0;
<> 149:156823d33999 79 int complementary_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.OCNPolarity = TIM_OCNPOLARITY_HIGH;
<> 149:156823d33999 96 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
<> 149:156823d33999 97 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
<> 149:156823d33999 98 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
<> 149:156823d33999 99
<> 149:156823d33999 100 switch (obj->channel) {
<> 149:156823d33999 101 case 1:
<> 149:156823d33999 102 channel = TIM_CHANNEL_1;
<> 149:156823d33999 103 break;
<> 149:156823d33999 104 case 2:
<> 149:156823d33999 105 channel = TIM_CHANNEL_2;
<> 149:156823d33999 106 break;
<> 149:156823d33999 107 case 3:
<> 149:156823d33999 108 channel = TIM_CHANNEL_3;
<> 149:156823d33999 109 break;
<> 149:156823d33999 110 case 4:
<> 149:156823d33999 111 channel = TIM_CHANNEL_4;
<> 149:156823d33999 112 break;
<> 149:156823d33999 113 default:
<> 149:156823d33999 114 return;
<> 149:156823d33999 115 }
<> 149:156823d33999 116
<> 149:156823d33999 117 if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
<> 149:156823d33999 118 error("Cannot initialize PWM\n");
<> 149:156823d33999 119 }
<> 149:156823d33999 120
<> 149:156823d33999 121 if (obj->inverted) {
<> 149:156823d33999 122 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
<> 149:156823d33999 123 } else {
<> 149:156823d33999 124 HAL_TIM_PWM_Start(&TimHandle, channel);
<> 149:156823d33999 125 }
<> 149:156823d33999 126 }
<> 149:156823d33999 127
<> 149:156823d33999 128 float pwmout_read(pwmout_t* obj)
<> 149:156823d33999 129 {
<> 149:156823d33999 130 float value = 0;
<> 149:156823d33999 131 if (obj->period > 0) {
<> 149:156823d33999 132 value = (float)(obj->pulse) / (float)(obj->period);
<> 149:156823d33999 133 }
<> 149:156823d33999 134 return ((value > (float)1.0) ? (float)(1.0) : (value));
<> 149:156823d33999 135 }
<> 149:156823d33999 136
<> 149:156823d33999 137 void pwmout_period(pwmout_t* obj, float seconds)
<> 149:156823d33999 138 {
<> 149:156823d33999 139 pwmout_period_us(obj, seconds * 1000000.0f);
<> 149:156823d33999 140 }
<> 149:156823d33999 141
<> 149:156823d33999 142 void pwmout_period_ms(pwmout_t* obj, int ms)
<> 149:156823d33999 143 {
<> 149:156823d33999 144 pwmout_period_us(obj, ms * 1000);
<> 149:156823d33999 145 }
<> 149:156823d33999 146
<> 149:156823d33999 147 void pwmout_period_us(pwmout_t* obj, int us)
<> 149:156823d33999 148 {
<> 149:156823d33999 149 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 149:156823d33999 150
<> 149:156823d33999 151 float dc = pwmout_read(obj);
<> 149:156823d33999 152
<> 149:156823d33999 153 __HAL_TIM_DISABLE(&TimHandle);
<> 149:156823d33999 154
<> 149:156823d33999 155 /* To make it simple, we use to possible prescaler values which lead to:
<> 149:156823d33999 156 * pwm unit = 1us, period/pulse can be from 1us to 65535us
<> 149:156823d33999 157 * or
<> 149:156823d33999 158 * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
<> 149:156823d33999 159 * Be careful that all the channels of a PWM shares the same prescaler
<> 149:156823d33999 160 */
<> 149:156823d33999 161 if (us > 0xFFFF) {
<> 149:156823d33999 162 obj->prescaler = 500;
<> 149:156823d33999 163 } else {
<> 149:156823d33999 164 obj->prescaler = 1;
<> 149:156823d33999 165 }
<> 149:156823d33999 166
<> 149:156823d33999 167 TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1;
<> 149:156823d33999 168
<> 149:156823d33999 169 if (TimHandle.Init.Prescaler > 0xFFFF)
<> 149:156823d33999 170 error("PWM: out of range prescaler");
<> 149:156823d33999 171
<> 149:156823d33999 172 TimHandle.Init.Period = (us - 1) / obj->prescaler;
<> 149:156823d33999 173 if (TimHandle.Init.Period > 0xFFFF)
<> 149:156823d33999 174 error("PWM: out of range period");
<> 149:156823d33999 175
<> 149:156823d33999 176 TimHandle.Init.ClockDivision = 0;
<> 149:156823d33999 177 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
<> 149:156823d33999 178 HAL_TIM_PWM_Init(&TimHandle);
<> 149:156823d33999 179
<> 149:156823d33999 180 // Save for future use
<> 149:156823d33999 181 obj->period = us;
<> 149:156823d33999 182
<> 149:156823d33999 183 // Set duty cycle again
<> 149:156823d33999 184 pwmout_write(obj, dc);
<> 149:156823d33999 185
<> 149:156823d33999 186 __HAL_TIM_ENABLE(&TimHandle);
<> 149:156823d33999 187 }
<> 149:156823d33999 188
<> 149:156823d33999 189 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
<> 149:156823d33999 190 {
<> 149:156823d33999 191 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
<> 149:156823d33999 192 }
<> 149:156823d33999 193
<> 149:156823d33999 194 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
<> 149:156823d33999 195 {
<> 149:156823d33999 196 pwmout_pulsewidth_us(obj, ms * 1000);
<> 149:156823d33999 197 }
<> 149:156823d33999 198
<> 149:156823d33999 199 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
<> 149:156823d33999 200 {
<> 149:156823d33999 201 float value = (float)us / (float)obj->period;
<> 149:156823d33999 202 pwmout_write(obj, value);
<> 149:156823d33999 203 }
<> 149:156823d33999 204
<> 149:156823d33999 205 #endif