mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

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 Dec 16 08:15:08 2014 +0000
Revision:
440:8a0b45cd594f
Parent:
428:4ddf7f7eabbb
Synchronized with git revision 67fbbf0b635d0c0d93fbe433306c537c2ad206aa

Full URL: https://github.com/mbedmicro/mbed/commit/67fbbf0b635d0c0d93fbe433306c537c2ad206aa/

Targets: nrf51 - updating app_timer.c from Norid'c SDKv7.1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 133:d4dda5c437f0 1 /* mbed Microcontroller Library
mbed_official 133:d4dda5c437f0 2 *******************************************************************************
mbed_official 133:d4dda5c437f0 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 133:d4dda5c437f0 4 * All rights reserved.
mbed_official 133:d4dda5c437f0 5 *
mbed_official 133:d4dda5c437f0 6 * Redistribution and use in source and binary forms, with or without
mbed_official 133:d4dda5c437f0 7 * modification, are permitted provided that the following conditions are met:
mbed_official 133:d4dda5c437f0 8 *
mbed_official 133:d4dda5c437f0 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 133:d4dda5c437f0 10 * this list of conditions and the following disclaimer.
mbed_official 133:d4dda5c437f0 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 133:d4dda5c437f0 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 133:d4dda5c437f0 13 * and/or other materials provided with the distribution.
mbed_official 133:d4dda5c437f0 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 133:d4dda5c437f0 15 * may be used to endorse or promote products derived from this software
mbed_official 133:d4dda5c437f0 16 * without specific prior written permission.
mbed_official 133:d4dda5c437f0 17 *
mbed_official 133:d4dda5c437f0 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 133:d4dda5c437f0 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 133:d4dda5c437f0 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 133:d4dda5c437f0 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 133:d4dda5c437f0 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 133:d4dda5c437f0 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 133:d4dda5c437f0 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 133:d4dda5c437f0 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 133:d4dda5c437f0 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 133:d4dda5c437f0 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 133:d4dda5c437f0 28 *******************************************************************************
mbed_official 133:d4dda5c437f0 29 */
mbed_official 133:d4dda5c437f0 30 #include "pwmout_api.h"
mbed_official 133:d4dda5c437f0 31
mbed_official 242:7074e42da0b2 32 #if DEVICE_PWMOUT
mbed_official 242:7074e42da0b2 33
mbed_official 133:d4dda5c437f0 34 #include "cmsis.h"
mbed_official 133:d4dda5c437f0 35 #include "pinmap.h"
mbed_official 285:31249416b6f9 36 #include "mbed_error.h"
mbed_official 428:4ddf7f7eabbb 37 #include "PeripheralPins.h"
mbed_official 133:d4dda5c437f0 38
mbed_official 133:d4dda5c437f0 39 static TIM_HandleTypeDef TimHandle;
mbed_official 428:4ddf7f7eabbb 40
mbed_official 242:7074e42da0b2 41 static uint8_t ClockDivider;
mbed_official 242:7074e42da0b2 42 /* 0, 1, 2, 3, 4, 5, 6, 7 */
mbed_official 242:7074e42da0b2 43 const uint8_t APBxPrescTable[]={ 0, 0, 0, 0, 1, 2, 3, 4 };
mbed_official 242:7074e42da0b2 44 extern const uint8_t AHBPrescTable[];
mbed_official 133:d4dda5c437f0 45
mbed_official 133:d4dda5c437f0 46 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 133:d4dda5c437f0 47 // Get the peripheral name from the pin and assign it to the object
mbed_official 133:d4dda5c437f0 48 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 242:7074e42da0b2 49
mbed_official 242:7074e42da0b2 50 if (obj->pwm == (PWMName)NC) {
mbed_official 242:7074e42da0b2 51 error("PWM error: pinout mapping failed.");
mbed_official 242:7074e42da0b2 52 }
mbed_official 227:7bd0639b8911 53
mbed_official 133:d4dda5c437f0 54 // Enable TIM clock
mbed_official 242:7074e42da0b2 55 /* APB1 Timers */
mbed_official 133:d4dda5c437f0 56 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 57 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 58 if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
mbed_official 242:7074e42da0b2 59 /* APB2 Timers */
mbed_official 242:7074e42da0b2 60 if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
mbed_official 242:7074e42da0b2 61 if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 62 if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 63 if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE();
mbed_official 242:7074e42da0b2 64 /* Decide APBx divider for timer base clock
mbed_official 242:7074e42da0b2 65 * Auto detect the Clockdivider value according to AHB,APB1 and APB2 prescaler register.
mbed_official 242:7074e42da0b2 66 */
mbed_official 242:7074e42da0b2 67 ClockDivider = AHBPrescTable[ ( ( RCC->CFGR & RCC_CFGR_HPRE ) >> 4 ) ];
mbed_official 242:7074e42da0b2 68 switch( obj->pwm ) {
mbed_official 242:7074e42da0b2 69 /* APB1 low clock */
mbed_official 242:7074e42da0b2 70 case PWM_2:
mbed_official 242:7074e42da0b2 71 case PWM_3:
mbed_official 242:7074e42da0b2 72 case PWM_4:
mbed_official 242:7074e42da0b2 73 ClockDivider += APBxPrescTable[ ( (RCC->CFGR & RCC_CFGR_PPRE1 ) >> 13 ) ];
mbed_official 242:7074e42da0b2 74 break;
mbed_official 242:7074e42da0b2 75 /* APB2 fast clock */
mbed_official 242:7074e42da0b2 76 case PWM_1:
mbed_official 242:7074e42da0b2 77 case PWM_9:
mbed_official 242:7074e42da0b2 78 case PWM_10:
mbed_official 242:7074e42da0b2 79 case PWM_11:
mbed_official 242:7074e42da0b2 80 ClockDivider += APBxPrescTable[ ( (RCC->CFGR & RCC_CFGR_PPRE2 ) >> 10 ) ];
mbed_official 242:7074e42da0b2 81 break;
mbed_official 242:7074e42da0b2 82 default:
mbed_official 242:7074e42da0b2 83 break;
mbed_official 242:7074e42da0b2 84 }
mbed_official 133:d4dda5c437f0 85
mbed_official 133:d4dda5c437f0 86 // Configure GPIO
mbed_official 133:d4dda5c437f0 87 pinmap_pinout(pin, PinMap_PWM);
mbed_official 133:d4dda5c437f0 88
mbed_official 133:d4dda5c437f0 89 obj->pin = pin;
mbed_official 133:d4dda5c437f0 90 obj->period = 0;
mbed_official 133:d4dda5c437f0 91 obj->pulse = 0;
mbed_official 133:d4dda5c437f0 92
mbed_official 133:d4dda5c437f0 93 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 133:d4dda5c437f0 94 }
mbed_official 133:d4dda5c437f0 95
mbed_official 133:d4dda5c437f0 96 void pwmout_free(pwmout_t* obj) {
mbed_official 242:7074e42da0b2 97 // Configure GPIO
mbed_official 133:d4dda5c437f0 98 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 133:d4dda5c437f0 99 }
mbed_official 133:d4dda5c437f0 100
mbed_official 133:d4dda5c437f0 101 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 133:d4dda5c437f0 102 TIM_OC_InitTypeDef sConfig;
mbed_official 133:d4dda5c437f0 103 int channel = 0;
mbed_official 133:d4dda5c437f0 104 int complementary_channel = 0;
mbed_official 133:d4dda5c437f0 105
mbed_official 133:d4dda5c437f0 106 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 107
mbed_official 133:d4dda5c437f0 108 if (value < (float)0.0) {
mbed_official 133:d4dda5c437f0 109 value = 0.0;
mbed_official 133:d4dda5c437f0 110 } else if (value > (float)1.0) {
mbed_official 133:d4dda5c437f0 111 value = 1.0;
mbed_official 133:d4dda5c437f0 112 }
mbed_official 133:d4dda5c437f0 113
mbed_official 133:d4dda5c437f0 114 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 133:d4dda5c437f0 115
mbed_official 133:d4dda5c437f0 116 // Configure channels
mbed_official 133:d4dda5c437f0 117 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 133:d4dda5c437f0 118 sConfig.Pulse = obj->pulse;
mbed_official 133:d4dda5c437f0 119 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 133:d4dda5c437f0 120 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
mbed_official 133:d4dda5c437f0 121 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 133:d4dda5c437f0 122 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
mbed_official 133:d4dda5c437f0 123 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
mbed_official 133:d4dda5c437f0 124
mbed_official 133:d4dda5c437f0 125 switch (obj->pin) {
mbed_official 133:d4dda5c437f0 126 // Channels 1
mbed_official 133:d4dda5c437f0 127 case PA_0:
mbed_official 133:d4dda5c437f0 128 case PA_5:
mbed_official 133:d4dda5c437f0 129 case PA_6:
mbed_official 133:d4dda5c437f0 130 case PA_8:
mbed_official 133:d4dda5c437f0 131 case PA_15:
mbed_official 133:d4dda5c437f0 132 case PB_4:
mbed_official 133:d4dda5c437f0 133 case PB_6:
mbed_official 133:d4dda5c437f0 134 case PC_6:
mbed_official 133:d4dda5c437f0 135 channel = TIM_CHANNEL_1;
mbed_official 133:d4dda5c437f0 136 break;
mbed_official 133:d4dda5c437f0 137 // Channels 1N
mbed_official 133:d4dda5c437f0 138 case PA_7:
mbed_official 133:d4dda5c437f0 139 case PB_13:
mbed_official 133:d4dda5c437f0 140 channel = TIM_CHANNEL_1;
mbed_official 133:d4dda5c437f0 141 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 142 break;
mbed_official 133:d4dda5c437f0 143 // Channels 2
mbed_official 133:d4dda5c437f0 144 case PA_1:
mbed_official 133:d4dda5c437f0 145 case PA_9:
mbed_official 133:d4dda5c437f0 146 case PB_3:
mbed_official 133:d4dda5c437f0 147 case PB_5:
mbed_official 133:d4dda5c437f0 148 case PB_7:
mbed_official 133:d4dda5c437f0 149 case PC_7:
mbed_official 133:d4dda5c437f0 150 channel = TIM_CHANNEL_2;
mbed_official 133:d4dda5c437f0 151 break;
mbed_official 133:d4dda5c437f0 152 // Channels 2N
mbed_official 133:d4dda5c437f0 153 case PB_0:
mbed_official 133:d4dda5c437f0 154 case PB_14:
mbed_official 133:d4dda5c437f0 155 channel = TIM_CHANNEL_2;
mbed_official 133:d4dda5c437f0 156 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 157 break;
mbed_official 133:d4dda5c437f0 158 // Channels 3
mbed_official 133:d4dda5c437f0 159 case PA_2:
mbed_official 133:d4dda5c437f0 160 case PA_10:
mbed_official 133:d4dda5c437f0 161 case PB_8:
mbed_official 133:d4dda5c437f0 162 case PB_10:
mbed_official 133:d4dda5c437f0 163 case PC_8:
mbed_official 133:d4dda5c437f0 164 channel = TIM_CHANNEL_3;
mbed_official 133:d4dda5c437f0 165 break;
mbed_official 133:d4dda5c437f0 166 // Channels 3N
mbed_official 133:d4dda5c437f0 167 case PB_1:
mbed_official 133:d4dda5c437f0 168 case PB_15:
mbed_official 133:d4dda5c437f0 169 channel = TIM_CHANNEL_3;
mbed_official 133:d4dda5c437f0 170 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 171 break;
mbed_official 133:d4dda5c437f0 172 // Channels 4
mbed_official 159:3b23f6d9ecb9 173 case PA_3:
mbed_official 133:d4dda5c437f0 174 case PA_11:
mbed_official 133:d4dda5c437f0 175 case PB_9:
mbed_official 159:3b23f6d9ecb9 176 case PC_9:
mbed_official 133:d4dda5c437f0 177 channel = TIM_CHANNEL_4;
mbed_official 133:d4dda5c437f0 178 break;
mbed_official 133:d4dda5c437f0 179 default:
mbed_official 133:d4dda5c437f0 180 return;
mbed_official 133:d4dda5c437f0 181 }
mbed_official 133:d4dda5c437f0 182
mbed_official 133:d4dda5c437f0 183 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
mbed_official 133:d4dda5c437f0 184 if (complementary_channel) {
mbed_official 133:d4dda5c437f0 185 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
mbed_official 242:7074e42da0b2 186 } else {
mbed_official 133:d4dda5c437f0 187 HAL_TIM_PWM_Start(&TimHandle, channel);
mbed_official 133:d4dda5c437f0 188 }
mbed_official 133:d4dda5c437f0 189 }
mbed_official 133:d4dda5c437f0 190
mbed_official 133:d4dda5c437f0 191 float pwmout_read(pwmout_t* obj) {
mbed_official 133:d4dda5c437f0 192 float value = 0;
mbed_official 133:d4dda5c437f0 193 if (obj->period > 0) {
mbed_official 133:d4dda5c437f0 194 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 133:d4dda5c437f0 195 }
mbed_official 133:d4dda5c437f0 196 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 133:d4dda5c437f0 197 }
mbed_official 133:d4dda5c437f0 198
mbed_official 133:d4dda5c437f0 199 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 133:d4dda5c437f0 200 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 133:d4dda5c437f0 201 }
mbed_official 133:d4dda5c437f0 202
mbed_official 133:d4dda5c437f0 203 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 133:d4dda5c437f0 204 pwmout_period_us(obj, ms * 1000);
mbed_official 133:d4dda5c437f0 205 }
mbed_official 133:d4dda5c437f0 206
mbed_official 133:d4dda5c437f0 207 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 133:d4dda5c437f0 208 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 209
mbed_official 133:d4dda5c437f0 210 float dc = pwmout_read(obj);
mbed_official 133:d4dda5c437f0 211
mbed_official 133:d4dda5c437f0 212 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 133:d4dda5c437f0 213
mbed_official 242:7074e42da0b2 214 // Update the SystemCoreClock variable
mbed_official 242:7074e42da0b2 215 SystemCoreClockUpdate();
mbed_official 133:d4dda5c437f0 216 TimHandle.Init.Period = us - 1;
mbed_official 242:7074e42da0b2 217 TimHandle.Init.Prescaler = (uint16_t)( ( SystemCoreClock >> ClockDivider ) / 1000000) - 1; // 1 µs tick
mbed_official 133:d4dda5c437f0 218 TimHandle.Init.ClockDivision = 0;
mbed_official 133:d4dda5c437f0 219 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 133:d4dda5c437f0 220 HAL_TIM_PWM_Init(&TimHandle);
mbed_official 133:d4dda5c437f0 221
mbed_official 133:d4dda5c437f0 222 // Set duty cycle again
mbed_official 133:d4dda5c437f0 223 pwmout_write(obj, dc);
mbed_official 133:d4dda5c437f0 224
mbed_official 133:d4dda5c437f0 225 // Save for future use
mbed_official 133:d4dda5c437f0 226 obj->period = us;
mbed_official 133:d4dda5c437f0 227
mbed_official 133:d4dda5c437f0 228 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 133:d4dda5c437f0 229 }
mbed_official 133:d4dda5c437f0 230
mbed_official 133:d4dda5c437f0 231 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 133:d4dda5c437f0 232 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 133:d4dda5c437f0 233 }
mbed_official 133:d4dda5c437f0 234
mbed_official 133:d4dda5c437f0 235 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 133:d4dda5c437f0 236 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 133:d4dda5c437f0 237 }
mbed_official 133:d4dda5c437f0 238
mbed_official 133:d4dda5c437f0 239 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 133:d4dda5c437f0 240 float value = (float)us / (float)obj->period;
mbed_official 133:d4dda5c437f0 241 pwmout_write(obj, value);
mbed_official 133:d4dda5c437f0 242 }
mbed_official 242:7074e42da0b2 243
mbed_official 242:7074e42da0b2 244 #endif