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