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:
Mon Jul 27 09:30:09 2015 +0100
Revision:
596:abfb2833c9f5
Parent:
582:a89625bcd809
Child:
610:813dcc80987e
Synchronized with git revision bfd1ddeee4467ba8c929c1274abc5d907ff9e669

Full URL: https://github.com/mbedmicro/mbed/commit/bfd1ddeee4467ba8c929c1274abc5d907ff9e669/

DISCO_F746NG - add pwm12 clock

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 573:ad23fe03a082 1 /* mbed Microcontroller Library
mbed_official 573:ad23fe03a082 2 *******************************************************************************
mbed_official 573:ad23fe03a082 3 * Copyright (c) 2015, STMicroelectronics
mbed_official 573:ad23fe03a082 4 * All rights reserved.
mbed_official 573:ad23fe03a082 5 *
mbed_official 573:ad23fe03a082 6 * Redistribution and use in source and binary forms, with or without
mbed_official 573:ad23fe03a082 7 * modification, are permitted provided that the following conditions are met:
mbed_official 573:ad23fe03a082 8 *
mbed_official 573:ad23fe03a082 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 573:ad23fe03a082 10 * this list of conditions and the following disclaimer.
mbed_official 573:ad23fe03a082 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 573:ad23fe03a082 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 573:ad23fe03a082 13 * and/or other materials provided with the distribution.
mbed_official 573:ad23fe03a082 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 573:ad23fe03a082 15 * may be used to endorse or promote products derived from this software
mbed_official 573:ad23fe03a082 16 * without specific prior written permission.
mbed_official 573:ad23fe03a082 17 *
mbed_official 573:ad23fe03a082 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 573:ad23fe03a082 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 573:ad23fe03a082 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 573:ad23fe03a082 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 573:ad23fe03a082 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 573:ad23fe03a082 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 573:ad23fe03a082 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 573:ad23fe03a082 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 573:ad23fe03a082 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 573:ad23fe03a082 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 573:ad23fe03a082 28 *******************************************************************************
mbed_official 573:ad23fe03a082 29 */
mbed_official 573:ad23fe03a082 30 #include "pwmout_api.h"
mbed_official 573:ad23fe03a082 31
mbed_official 573:ad23fe03a082 32 #if DEVICE_PWMOUT
mbed_official 573:ad23fe03a082 33
mbed_official 573:ad23fe03a082 34 #include "cmsis.h"
mbed_official 573:ad23fe03a082 35 #include "pinmap.h"
mbed_official 573:ad23fe03a082 36 #include "mbed_error.h"
mbed_official 573:ad23fe03a082 37 #include "PeripheralPins.h"
mbed_official 573:ad23fe03a082 38
mbed_official 573:ad23fe03a082 39 static TIM_HandleTypeDef TimHandle;
mbed_official 573:ad23fe03a082 40
mbed_official 573:ad23fe03a082 41 void pwmout_init(pwmout_t* obj, PinName pin)
mbed_official 573:ad23fe03a082 42 {
mbed_official 573:ad23fe03a082 43 // Get the peripheral name from the pin and assign it to the object
mbed_official 573:ad23fe03a082 44 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 573:ad23fe03a082 45
mbed_official 573:ad23fe03a082 46 // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object
mbed_official 573:ad23fe03a082 47 uint32_t function = pinmap_function(pin, PinMap_PWM);
mbed_official 573:ad23fe03a082 48 MBED_ASSERT(function != (uint32_t)NC);
mbed_official 573:ad23fe03a082 49 obj->channel = STM_PIN_CHANNEL(function);
mbed_official 573:ad23fe03a082 50 obj->inverted = STM_PIN_INVERTED(function);
mbed_official 573:ad23fe03a082 51
mbed_official 573:ad23fe03a082 52 if (obj->pwm == (PWMName)NC) {
mbed_official 573:ad23fe03a082 53 error("PWM error: pinout mapping failed.");
mbed_official 573:ad23fe03a082 54 }
mbed_official 573:ad23fe03a082 55
mbed_official 573:ad23fe03a082 56 // Enable TIM clock
mbed_official 582:a89625bcd809 57 if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE();
mbed_official 582:a89625bcd809 58 if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE();
mbed_official 582:a89625bcd809 59 if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE();
mbed_official 582:a89625bcd809 60 if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE();
mbed_official 582:a89625bcd809 61 if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE();
mbed_official 582:a89625bcd809 62 if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE();
mbed_official 582:a89625bcd809 63 if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE();
mbed_official 582:a89625bcd809 64 if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE();
mbed_official 596:abfb2833c9f5 65 if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE();
mbed_official 582:a89625bcd809 66 if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE();
mbed_official 582:a89625bcd809 67 if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE();
mbed_official 573:ad23fe03a082 68
mbed_official 573:ad23fe03a082 69 // Configure GPIO
mbed_official 573:ad23fe03a082 70 pinmap_pinout(pin, PinMap_PWM);
mbed_official 573:ad23fe03a082 71
mbed_official 573:ad23fe03a082 72 obj->pin = pin;
mbed_official 573:ad23fe03a082 73 obj->period = 0;
mbed_official 573:ad23fe03a082 74 obj->pulse = 0;
mbed_official 573:ad23fe03a082 75
mbed_official 573:ad23fe03a082 76 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 573:ad23fe03a082 77 }
mbed_official 573:ad23fe03a082 78
mbed_official 573:ad23fe03a082 79 void pwmout_free(pwmout_t* obj)
mbed_official 573:ad23fe03a082 80 {
mbed_official 573:ad23fe03a082 81 // Configure GPIO
mbed_official 573:ad23fe03a082 82 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 573:ad23fe03a082 83 }
mbed_official 573:ad23fe03a082 84
mbed_official 573:ad23fe03a082 85 void pwmout_write(pwmout_t* obj, float value)
mbed_official 573:ad23fe03a082 86 {
mbed_official 573:ad23fe03a082 87 TIM_OC_InitTypeDef sConfig;
mbed_official 573:ad23fe03a082 88 int channel = 0;
mbed_official 573:ad23fe03a082 89
mbed_official 573:ad23fe03a082 90 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 573:ad23fe03a082 91
mbed_official 573:ad23fe03a082 92 if (value < (float)0.0) {
mbed_official 573:ad23fe03a082 93 value = 0.0;
mbed_official 573:ad23fe03a082 94 } else if (value > (float)1.0) {
mbed_official 573:ad23fe03a082 95 value = 1.0;
mbed_official 573:ad23fe03a082 96 }
mbed_official 573:ad23fe03a082 97
mbed_official 573:ad23fe03a082 98 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 573:ad23fe03a082 99
mbed_official 573:ad23fe03a082 100 // Configure channels
mbed_official 573:ad23fe03a082 101 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 573:ad23fe03a082 102 sConfig.Pulse = obj->pulse;
mbed_official 573:ad23fe03a082 103 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 573:ad23fe03a082 104 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
mbed_official 573:ad23fe03a082 105 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 573:ad23fe03a082 106 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
mbed_official 573:ad23fe03a082 107 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
mbed_official 573:ad23fe03a082 108
mbed_official 573:ad23fe03a082 109 switch (obj->channel) {
mbed_official 573:ad23fe03a082 110 case 1:
mbed_official 573:ad23fe03a082 111 channel = TIM_CHANNEL_1;
mbed_official 573:ad23fe03a082 112 break;
mbed_official 573:ad23fe03a082 113 case 2:
mbed_official 573:ad23fe03a082 114 channel = TIM_CHANNEL_2;
mbed_official 573:ad23fe03a082 115 break;
mbed_official 573:ad23fe03a082 116 case 3:
mbed_official 573:ad23fe03a082 117 channel = TIM_CHANNEL_3;
mbed_official 573:ad23fe03a082 118 break;
mbed_official 573:ad23fe03a082 119 case 4:
mbed_official 573:ad23fe03a082 120 channel = TIM_CHANNEL_4;
mbed_official 573:ad23fe03a082 121 break;
mbed_official 573:ad23fe03a082 122 default:
mbed_official 573:ad23fe03a082 123 return;
mbed_official 573:ad23fe03a082 124 }
mbed_official 573:ad23fe03a082 125
mbed_official 582:a89625bcd809 126 if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
mbed_official 582:a89625bcd809 127 error("Cannot configure PWM channel");
mbed_official 582:a89625bcd809 128 }
mbed_official 582:a89625bcd809 129
mbed_official 582:a89625bcd809 130 if (obj->inverted) {
mbed_official 573:ad23fe03a082 131 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
mbed_official 573:ad23fe03a082 132 } else {
mbed_official 573:ad23fe03a082 133 HAL_TIM_PWM_Start(&TimHandle, channel);
mbed_official 573:ad23fe03a082 134 }
mbed_official 573:ad23fe03a082 135 }
mbed_official 573:ad23fe03a082 136
mbed_official 573:ad23fe03a082 137 float pwmout_read(pwmout_t* obj)
mbed_official 573:ad23fe03a082 138 {
mbed_official 573:ad23fe03a082 139 float value = 0;
mbed_official 573:ad23fe03a082 140 if (obj->period > 0) {
mbed_official 573:ad23fe03a082 141 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 573:ad23fe03a082 142 }
mbed_official 573:ad23fe03a082 143 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 573:ad23fe03a082 144 }
mbed_official 573:ad23fe03a082 145
mbed_official 573:ad23fe03a082 146 void pwmout_period(pwmout_t* obj, float seconds)
mbed_official 573:ad23fe03a082 147 {
mbed_official 573:ad23fe03a082 148 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 573:ad23fe03a082 149 }
mbed_official 573:ad23fe03a082 150
mbed_official 573:ad23fe03a082 151 void pwmout_period_ms(pwmout_t* obj, int ms)
mbed_official 573:ad23fe03a082 152 {
mbed_official 573:ad23fe03a082 153 pwmout_period_us(obj, ms * 1000);
mbed_official 573:ad23fe03a082 154 }
mbed_official 573:ad23fe03a082 155
mbed_official 573:ad23fe03a082 156 void pwmout_period_us(pwmout_t* obj, int us)
mbed_official 573:ad23fe03a082 157 {
mbed_official 573:ad23fe03a082 158 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 573:ad23fe03a082 159 uint32_t PclkFreq;
mbed_official 582:a89625bcd809 160
mbed_official 573:ad23fe03a082 161 float dc = pwmout_read(obj);
mbed_official 573:ad23fe03a082 162
mbed_official 573:ad23fe03a082 163 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 573:ad23fe03a082 164
mbed_official 582:a89625bcd809 165 // Get the PCLK used by the timer
mbed_official 573:ad23fe03a082 166 switch (obj->pwm) {
mbed_official 573:ad23fe03a082 167 case PWM_2:
mbed_official 573:ad23fe03a082 168 case PWM_3:
mbed_official 573:ad23fe03a082 169 case PWM_4:
mbed_official 582:a89625bcd809 170 case PWM_5:
mbed_official 582:a89625bcd809 171 case PWM_12:
mbed_official 582:a89625bcd809 172 case PWM_13:
mbed_official 582:a89625bcd809 173 case PWM_14:
mbed_official 582:a89625bcd809 174 PclkFreq = HAL_RCC_GetPCLK1Freq();
mbed_official 573:ad23fe03a082 175 break;
mbed_official 582:a89625bcd809 176 case PWM_1:
mbed_official 582:a89625bcd809 177 case PWM_8:
mbed_official 573:ad23fe03a082 178 case PWM_9:
mbed_official 573:ad23fe03a082 179 case PWM_10:
mbed_official 573:ad23fe03a082 180 case PWM_11:
mbed_official 582:a89625bcd809 181 PclkFreq = HAL_RCC_GetPCLK2Freq();
mbed_official 573:ad23fe03a082 182 break;
mbed_official 573:ad23fe03a082 183 default:
mbed_official 573:ad23fe03a082 184 return;
mbed_official 573:ad23fe03a082 185 }
mbed_official 573:ad23fe03a082 186
mbed_official 573:ad23fe03a082 187 TimHandle.Init.Period = us - 1;
mbed_official 582:a89625bcd809 188 // TIMxCLK = 2 x PCLKx when the APB prescaler is not equal to 1 (DIV4 or DIV2 in our case)
mbed_official 582:a89625bcd809 189 TimHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
mbed_official 573:ad23fe03a082 190 TimHandle.Init.ClockDivision = 0;
mbed_official 573:ad23fe03a082 191 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 582:a89625bcd809 192
mbed_official 582:a89625bcd809 193 if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
mbed_official 582:a89625bcd809 194 error("Cannot initialize PWM");
mbed_official 582:a89625bcd809 195 }
mbed_official 573:ad23fe03a082 196
mbed_official 573:ad23fe03a082 197 // Set duty cycle again
mbed_official 573:ad23fe03a082 198 pwmout_write(obj, dc);
mbed_official 573:ad23fe03a082 199
mbed_official 573:ad23fe03a082 200 // Save for future use
mbed_official 573:ad23fe03a082 201 obj->period = us;
mbed_official 573:ad23fe03a082 202
mbed_official 573:ad23fe03a082 203 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 573:ad23fe03a082 204 }
mbed_official 573:ad23fe03a082 205
mbed_official 573:ad23fe03a082 206 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
mbed_official 573:ad23fe03a082 207 {
mbed_official 573:ad23fe03a082 208 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 573:ad23fe03a082 209 }
mbed_official 573:ad23fe03a082 210
mbed_official 573:ad23fe03a082 211 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
mbed_official 573:ad23fe03a082 212 {
mbed_official 573:ad23fe03a082 213 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 573:ad23fe03a082 214 }
mbed_official 573:ad23fe03a082 215
mbed_official 573:ad23fe03a082 216 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
mbed_official 573:ad23fe03a082 217 {
mbed_official 573:ad23fe03a082 218 float value = (float)us / (float)obj->period;
mbed_official 573:ad23fe03a082 219 pwmout_write(obj, value);
mbed_official 573:ad23fe03a082 220 }
mbed_official 573:ad23fe03a082 221
mbed_official 573:ad23fe03a082 222 #endif