TUKS MCU Introductory course / TUKS-COURSE-2-LED
Committer:
elmot
Date:
Sat Feb 25 08:59:21 2017 +0000
Revision:
6:0018763974d3
Parent:
1:d0dfbce63a89
No comment

Who changed what in which revision?

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