mbed library sources. Supersedes mbed-src. removed serial

Fork of mbed-dev by mbed official

Committer:
mrcrsch
Date:
Mon Feb 13 14:26:36 2017 +0000
Revision:
158:aa577577efe0
Parent:
149:156823d33999
removed stdio and serial to reduce size

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