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 06 09:00:34 2015 +0100
Revision:
582:a89625bcd809
Parent:
573:ad23fe03a082
Child:
596:abfb2833c9f5
Synchronized with git revision 45004fb61e4af8f74f4e916318df5207fcf6076d

Full URL: https://github.com/mbedmicro/mbed/commit/45004fb61e4af8f74f4e916318df5207fcf6076d/

DISCO_F746NG - Fix several issues

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 582:a89625bcd809 65 if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE();
mbed_official 582:a89625bcd809 66 if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE();
mbed_official 573:ad23fe03a082 67
mbed_official 573:ad23fe03a082 68 // Configure GPIO
mbed_official 573:ad23fe03a082 69 pinmap_pinout(pin, PinMap_PWM);
mbed_official 573:ad23fe03a082 70
mbed_official 573:ad23fe03a082 71 obj->pin = pin;
mbed_official 573:ad23fe03a082 72 obj->period = 0;
mbed_official 573:ad23fe03a082 73 obj->pulse = 0;
mbed_official 573:ad23fe03a082 74
mbed_official 573:ad23fe03a082 75 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 573:ad23fe03a082 76 }
mbed_official 573:ad23fe03a082 77
mbed_official 573:ad23fe03a082 78 void pwmout_free(pwmout_t* obj)
mbed_official 573:ad23fe03a082 79 {
mbed_official 573:ad23fe03a082 80 // Configure GPIO
mbed_official 573:ad23fe03a082 81 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 573:ad23fe03a082 82 }
mbed_official 573:ad23fe03a082 83
mbed_official 573:ad23fe03a082 84 void pwmout_write(pwmout_t* obj, float value)
mbed_official 573:ad23fe03a082 85 {
mbed_official 573:ad23fe03a082 86 TIM_OC_InitTypeDef sConfig;
mbed_official 573:ad23fe03a082 87 int channel = 0;
mbed_official 573:ad23fe03a082 88
mbed_official 573:ad23fe03a082 89 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 573:ad23fe03a082 90
mbed_official 573:ad23fe03a082 91 if (value < (float)0.0) {
mbed_official 573:ad23fe03a082 92 value = 0.0;
mbed_official 573:ad23fe03a082 93 } else if (value > (float)1.0) {
mbed_official 573:ad23fe03a082 94 value = 1.0;
mbed_official 573:ad23fe03a082 95 }
mbed_official 573:ad23fe03a082 96
mbed_official 573:ad23fe03a082 97 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 573:ad23fe03a082 98
mbed_official 573:ad23fe03a082 99 // Configure channels
mbed_official 573:ad23fe03a082 100 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 573:ad23fe03a082 101 sConfig.Pulse = obj->pulse;
mbed_official 573:ad23fe03a082 102 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 573:ad23fe03a082 103 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
mbed_official 573:ad23fe03a082 104 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 573:ad23fe03a082 105 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
mbed_official 573:ad23fe03a082 106 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
mbed_official 573:ad23fe03a082 107
mbed_official 573:ad23fe03a082 108 switch (obj->channel) {
mbed_official 573:ad23fe03a082 109 case 1:
mbed_official 573:ad23fe03a082 110 channel = TIM_CHANNEL_1;
mbed_official 573:ad23fe03a082 111 break;
mbed_official 573:ad23fe03a082 112 case 2:
mbed_official 573:ad23fe03a082 113 channel = TIM_CHANNEL_2;
mbed_official 573:ad23fe03a082 114 break;
mbed_official 573:ad23fe03a082 115 case 3:
mbed_official 573:ad23fe03a082 116 channel = TIM_CHANNEL_3;
mbed_official 573:ad23fe03a082 117 break;
mbed_official 573:ad23fe03a082 118 case 4:
mbed_official 573:ad23fe03a082 119 channel = TIM_CHANNEL_4;
mbed_official 573:ad23fe03a082 120 break;
mbed_official 573:ad23fe03a082 121 default:
mbed_official 573:ad23fe03a082 122 return;
mbed_official 573:ad23fe03a082 123 }
mbed_official 573:ad23fe03a082 124
mbed_official 582:a89625bcd809 125 if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
mbed_official 582:a89625bcd809 126 error("Cannot configure PWM channel");
mbed_official 582:a89625bcd809 127 }
mbed_official 582:a89625bcd809 128
mbed_official 582:a89625bcd809 129 if (obj->inverted) {
mbed_official 573:ad23fe03a082 130 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
mbed_official 573:ad23fe03a082 131 } else {
mbed_official 573:ad23fe03a082 132 HAL_TIM_PWM_Start(&TimHandle, channel);
mbed_official 573:ad23fe03a082 133 }
mbed_official 573:ad23fe03a082 134 }
mbed_official 573:ad23fe03a082 135
mbed_official 573:ad23fe03a082 136 float pwmout_read(pwmout_t* obj)
mbed_official 573:ad23fe03a082 137 {
mbed_official 573:ad23fe03a082 138 float value = 0;
mbed_official 573:ad23fe03a082 139 if (obj->period > 0) {
mbed_official 573:ad23fe03a082 140 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 573:ad23fe03a082 141 }
mbed_official 573:ad23fe03a082 142 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 573:ad23fe03a082 143 }
mbed_official 573:ad23fe03a082 144
mbed_official 573:ad23fe03a082 145 void pwmout_period(pwmout_t* obj, float seconds)
mbed_official 573:ad23fe03a082 146 {
mbed_official 573:ad23fe03a082 147 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 573:ad23fe03a082 148 }
mbed_official 573:ad23fe03a082 149
mbed_official 573:ad23fe03a082 150 void pwmout_period_ms(pwmout_t* obj, int ms)
mbed_official 573:ad23fe03a082 151 {
mbed_official 573:ad23fe03a082 152 pwmout_period_us(obj, ms * 1000);
mbed_official 573:ad23fe03a082 153 }
mbed_official 573:ad23fe03a082 154
mbed_official 573:ad23fe03a082 155 void pwmout_period_us(pwmout_t* obj, int us)
mbed_official 573:ad23fe03a082 156 {
mbed_official 573:ad23fe03a082 157 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 573:ad23fe03a082 158 uint32_t PclkFreq;
mbed_official 582:a89625bcd809 159
mbed_official 573:ad23fe03a082 160 float dc = pwmout_read(obj);
mbed_official 573:ad23fe03a082 161
mbed_official 573:ad23fe03a082 162 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 573:ad23fe03a082 163
mbed_official 582:a89625bcd809 164 // Get the PCLK used by the timer
mbed_official 573:ad23fe03a082 165 switch (obj->pwm) {
mbed_official 573:ad23fe03a082 166 case PWM_2:
mbed_official 573:ad23fe03a082 167 case PWM_3:
mbed_official 573:ad23fe03a082 168 case PWM_4:
mbed_official 582:a89625bcd809 169 case PWM_5:
mbed_official 582:a89625bcd809 170 case PWM_12:
mbed_official 582:a89625bcd809 171 case PWM_13:
mbed_official 582:a89625bcd809 172 case PWM_14:
mbed_official 582:a89625bcd809 173 PclkFreq = HAL_RCC_GetPCLK1Freq();
mbed_official 573:ad23fe03a082 174 break;
mbed_official 582:a89625bcd809 175 case PWM_1:
mbed_official 582:a89625bcd809 176 case PWM_8:
mbed_official 573:ad23fe03a082 177 case PWM_9:
mbed_official 573:ad23fe03a082 178 case PWM_10:
mbed_official 573:ad23fe03a082 179 case PWM_11:
mbed_official 582:a89625bcd809 180 PclkFreq = HAL_RCC_GetPCLK2Freq();
mbed_official 573:ad23fe03a082 181 break;
mbed_official 573:ad23fe03a082 182 default:
mbed_official 573:ad23fe03a082 183 return;
mbed_official 573:ad23fe03a082 184 }
mbed_official 573:ad23fe03a082 185
mbed_official 573:ad23fe03a082 186 TimHandle.Init.Period = us - 1;
mbed_official 582:a89625bcd809 187 // TIMxCLK = 2 x PCLKx when the APB prescaler is not equal to 1 (DIV4 or DIV2 in our case)
mbed_official 582:a89625bcd809 188 TimHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
mbed_official 573:ad23fe03a082 189 TimHandle.Init.ClockDivision = 0;
mbed_official 573:ad23fe03a082 190 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 582:a89625bcd809 191
mbed_official 582:a89625bcd809 192 if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
mbed_official 582:a89625bcd809 193 error("Cannot initialize PWM");
mbed_official 582:a89625bcd809 194 }
mbed_official 573:ad23fe03a082 195
mbed_official 573:ad23fe03a082 196 // Set duty cycle again
mbed_official 573:ad23fe03a082 197 pwmout_write(obj, dc);
mbed_official 573:ad23fe03a082 198
mbed_official 573:ad23fe03a082 199 // Save for future use
mbed_official 573:ad23fe03a082 200 obj->period = us;
mbed_official 573:ad23fe03a082 201
mbed_official 573:ad23fe03a082 202 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 573:ad23fe03a082 203 }
mbed_official 573:ad23fe03a082 204
mbed_official 573:ad23fe03a082 205 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
mbed_official 573:ad23fe03a082 206 {
mbed_official 573:ad23fe03a082 207 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 573:ad23fe03a082 208 }
mbed_official 573:ad23fe03a082 209
mbed_official 573:ad23fe03a082 210 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
mbed_official 573:ad23fe03a082 211 {
mbed_official 573:ad23fe03a082 212 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 573:ad23fe03a082 213 }
mbed_official 573:ad23fe03a082 214
mbed_official 573:ad23fe03a082 215 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
mbed_official 573:ad23fe03a082 216 {
mbed_official 573:ad23fe03a082 217 float value = (float)us / (float)obj->period;
mbed_official 573:ad23fe03a082 218 pwmout_write(obj, value);
mbed_official 573:ad23fe03a082 219 }
mbed_official 573:ad23fe03a082 220
mbed_official 573:ad23fe03a082 221 #endif