mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Jan 07 11:00:05 2014 +0000
Revision:
70:c1fbde68b492
Parent:
58:3b55b7a41411
Child:
84:f54042cbc282
Synchronized with git revision 3f438a307904431f2782db3c8fa49946b9fc1d85

Full URL: https://github.com/mbedmicro/mbed/commit/3f438a307904431f2782db3c8fa49946b9fc1d85/

[NUCLEO_F103RB] license text changed + sleep hal updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 52:a51c77007319 1 /* mbed Microcontroller Library
mbed_official 70:c1fbde68b492 2 *******************************************************************************
mbed_official 70:c1fbde68b492 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 70:c1fbde68b492 4 * All rights reserved.
mbed_official 52:a51c77007319 5 *
mbed_official 70:c1fbde68b492 6 * Redistribution and use in source and binary forms, with or without
mbed_official 70:c1fbde68b492 7 * modification, are permitted provided that the following conditions are met:
mbed_official 52:a51c77007319 8 *
mbed_official 70:c1fbde68b492 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 70:c1fbde68b492 10 * this list of conditions and the following disclaimer.
mbed_official 70:c1fbde68b492 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 70:c1fbde68b492 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 70:c1fbde68b492 13 * and/or other materials provided with the distribution.
mbed_official 70:c1fbde68b492 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 70:c1fbde68b492 15 * may be used to endorse or promote products derived from this software
mbed_official 70:c1fbde68b492 16 * without specific prior written permission.
mbed_official 52:a51c77007319 17 *
mbed_official 70:c1fbde68b492 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 70:c1fbde68b492 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 70:c1fbde68b492 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 70:c1fbde68b492 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 70:c1fbde68b492 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 70:c1fbde68b492 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 70:c1fbde68b492 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 70:c1fbde68b492 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 70:c1fbde68b492 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 70:c1fbde68b492 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 70:c1fbde68b492 28 *******************************************************************************
mbed_official 52:a51c77007319 29 */
mbed_official 52:a51c77007319 30 #include "pwmout_api.h"
mbed_official 52:a51c77007319 31
mbed_official 52:a51c77007319 32 #include "cmsis.h"
mbed_official 52:a51c77007319 33 #include "pinmap.h"
mbed_official 52:a51c77007319 34 #include "error.h"
mbed_official 52:a51c77007319 35
mbed_official 52:a51c77007319 36 // Only TIM2 and TIM3 can be used (TIM1 and TIM4 are used by the us_ticker)
mbed_official 52:a51c77007319 37 static const PinMap PinMap_PWM[] = {
mbed_official 58:3b55b7a41411 38 // TIM2 default
mbed_official 58:3b55b7a41411 39 //{PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH3 - ARDUINO D1
mbed_official 58:3b55b7a41411 40 //{PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 - ARDUINO D0
mbed_official 58:3b55b7a41411 41 // TIM2 full remap
mbed_official 58:3b55b7a41411 42 {PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2fr_CH2 - ARDUINO D3
mbed_official 58:3b55b7a41411 43 //{PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2fr_CH3 - ARDUINO D6
mbed_official 58:3b55b7a41411 44 // TIM3 default
mbed_official 58:3b55b7a41411 45 //{PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 - ARDUINO D12
mbed_official 58:3b55b7a41411 46 //{PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 - ARDUINO D11
mbed_official 58:3b55b7a41411 47 // TIM3 full remap
mbed_official 58:3b55b7a41411 48 //{PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3fr_CH2 - ARDUINO D9
mbed_official 58:3b55b7a41411 49 // TIM3 partial remap
mbed_official 58:3b55b7a41411 50 {PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 7)}, // TIM3pr_CH1 - ARDUINO D5
mbed_official 52:a51c77007319 51 {NC, NC, 0}
mbed_official 52:a51c77007319 52 };
mbed_official 52:a51c77007319 53
mbed_official 52:a51c77007319 54 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 52:a51c77007319 55 // Get the peripheral name from the pin and assign it to the object
mbed_official 52:a51c77007319 56 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 52:a51c77007319 57
mbed_official 52:a51c77007319 58 if (obj->pwm == (PWMName)NC) {
mbed_official 52:a51c77007319 59 error("PWM pinout mapping failed");
mbed_official 52:a51c77007319 60 }
mbed_official 52:a51c77007319 61
mbed_official 52:a51c77007319 62 // Enable TIM clock
mbed_official 52:a51c77007319 63 if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
mbed_official 52:a51c77007319 64 if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
mbed_official 52:a51c77007319 65
mbed_official 52:a51c77007319 66 // Configure GPIO
mbed_official 52:a51c77007319 67 pinmap_pinout(pin, PinMap_PWM);
mbed_official 52:a51c77007319 68
mbed_official 52:a51c77007319 69 obj->pin = pin;
mbed_official 52:a51c77007319 70 obj->period = 0;
mbed_official 52:a51c77007319 71 obj->pulse = 0;
mbed_official 52:a51c77007319 72
mbed_official 52:a51c77007319 73 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 52:a51c77007319 74 }
mbed_official 52:a51c77007319 75
mbed_official 52:a51c77007319 76 void pwmout_free(pwmout_t* obj) {
mbed_official 52:a51c77007319 77 TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm);
mbed_official 52:a51c77007319 78 TIM_DeInit(tim);
mbed_official 52:a51c77007319 79 }
mbed_official 52:a51c77007319 80
mbed_official 52:a51c77007319 81 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 52:a51c77007319 82 TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm);
mbed_official 52:a51c77007319 83 TIM_OCInitTypeDef TIM_OCInitStructure;
mbed_official 52:a51c77007319 84
mbed_official 52:a51c77007319 85 if (value < 0.0) {
mbed_official 52:a51c77007319 86 value = 0.0;
mbed_official 52:a51c77007319 87 } else if (value > 1.0) {
mbed_official 52:a51c77007319 88 value = 1.0;
mbed_official 52:a51c77007319 89 }
mbed_official 52:a51c77007319 90
mbed_official 52:a51c77007319 91 //while(TIM_GetFlagStatus(tim, TIM_FLAG_Update) == RESET);
mbed_official 52:a51c77007319 92 //TIM_ClearFlag(tim, TIM_FLAG_Update);
mbed_official 52:a51c77007319 93
mbed_official 52:a51c77007319 94 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 52:a51c77007319 95
mbed_official 52:a51c77007319 96 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
mbed_official 52:a51c77007319 97 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
mbed_official 52:a51c77007319 98 TIM_OCInitStructure.TIM_Pulse = obj->pulse;
mbed_official 52:a51c77007319 99 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
mbed_official 52:a51c77007319 100
mbed_official 58:3b55b7a41411 101 // Configure channel 1
mbed_official 58:3b55b7a41411 102 if (obj->pin == PB_4) {
mbed_official 52:a51c77007319 103 TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable);
mbed_official 52:a51c77007319 104 TIM_OC1Init(tim, &TIM_OCInitStructure);
mbed_official 52:a51c77007319 105 }
mbed_official 52:a51c77007319 106
mbed_official 58:3b55b7a41411 107 // Configure channel 2
mbed_official 58:3b55b7a41411 108 if (obj->pin == PB_3) {
mbed_official 52:a51c77007319 109 TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable);
mbed_official 52:a51c77007319 110 TIM_OC2Init(tim, &TIM_OCInitStructure);
mbed_official 52:a51c77007319 111 }
mbed_official 52:a51c77007319 112
mbed_official 58:3b55b7a41411 113 // Configure channel 3
mbed_official 58:3b55b7a41411 114 //if (obj->pin == PB_10) {
mbed_official 58:3b55b7a41411 115 // TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable);
mbed_official 58:3b55b7a41411 116 // TIM_OC3Init(tim, &TIM_OCInitStructure);
mbed_official 58:3b55b7a41411 117 //}
mbed_official 52:a51c77007319 118
mbed_official 58:3b55b7a41411 119 // Configure channel 4
mbed_official 58:3b55b7a41411 120 //if (obj->pin == PA_3) {
mbed_official 58:3b55b7a41411 121 // TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable);
mbed_official 58:3b55b7a41411 122 // TIM_OC4Init(tim, &TIM_OCInitStructure);
mbed_official 58:3b55b7a41411 123 //}
mbed_official 52:a51c77007319 124 }
mbed_official 52:a51c77007319 125
mbed_official 52:a51c77007319 126 float pwmout_read(pwmout_t* obj) {
mbed_official 52:a51c77007319 127 float value = 0;
mbed_official 52:a51c77007319 128 if (obj->period > 0) {
mbed_official 52:a51c77007319 129 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 52:a51c77007319 130 }
mbed_official 52:a51c77007319 131 return ((value > 1.0) ? (1.0) : (value));
mbed_official 52:a51c77007319 132 }
mbed_official 52:a51c77007319 133
mbed_official 52:a51c77007319 134 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 52:a51c77007319 135 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 52:a51c77007319 136 }
mbed_official 52:a51c77007319 137
mbed_official 52:a51c77007319 138 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 52:a51c77007319 139 pwmout_period_us(obj, ms * 1000);
mbed_official 52:a51c77007319 140 }
mbed_official 52:a51c77007319 141
mbed_official 52:a51c77007319 142 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 52:a51c77007319 143 TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm);
mbed_official 52:a51c77007319 144 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
mbed_official 52:a51c77007319 145 float dc = pwmout_read(obj);
mbed_official 52:a51c77007319 146
mbed_official 52:a51c77007319 147 TIM_Cmd(tim, DISABLE);
mbed_official 52:a51c77007319 148
mbed_official 52:a51c77007319 149 obj->period = us;
mbed_official 52:a51c77007319 150
mbed_official 52:a51c77007319 151 TIM_TimeBaseStructure.TIM_Period = obj->period - 1;
mbed_official 52:a51c77007319 152 TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
mbed_official 52:a51c77007319 153 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
mbed_official 52:a51c77007319 154 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
mbed_official 52:a51c77007319 155 TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure);
mbed_official 52:a51c77007319 156
mbed_official 52:a51c77007319 157 // Set duty cycle again
mbed_official 52:a51c77007319 158 pwmout_write(obj, dc);
mbed_official 52:a51c77007319 159
mbed_official 52:a51c77007319 160 TIM_ARRPreloadConfig(tim, ENABLE);
mbed_official 52:a51c77007319 161 TIM_Cmd(tim, ENABLE);
mbed_official 52:a51c77007319 162 }
mbed_official 52:a51c77007319 163
mbed_official 52:a51c77007319 164 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 52:a51c77007319 165 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 52:a51c77007319 166 }
mbed_official 52:a51c77007319 167
mbed_official 52:a51c77007319 168 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 52:a51c77007319 169 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 52:a51c77007319 170 }
mbed_official 52:a51c77007319 171
mbed_official 52:a51c77007319 172 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 52:a51c77007319 173 float value = (float)us / (float)obj->period;
mbed_official 52:a51c77007319 174 pwmout_write(obj, value);
mbed_official 52:a51c77007319 175 }