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:
Fri Aug 14 13:15:17 2015 +0100
Revision:
610:813dcc80987e
Synchronized with git revision 6d84db41c6833e0b9b024741eb0616a5f62d5599

Full URL: https://github.com/mbedmicro/mbed/commit/6d84db41c6833e0b9b024741eb0616a5f62d5599/

DISCO_F746NG - Improvements

Who changed what in which revision?

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