Modification of Mbed-dev library for LQFP48 package microcontrollers: STM32F103C8 (STM32F103C8T6) and STM32F103CB (STM32F103CBT6) (Bluepill boards, Maple mini etc. )
Fork of mbed-STM32F103C8_org by
Library for STM32F103C8 (Bluepill boards etc.).
Use this instead of mbed library.
This library allows the size of the code in the FLASH up to 128kB. Therefore, code also runs on microcontrollers STM32F103CB (eg. Maple mini).
But in the case of STM32F103C8, check the size of the resulting code would not exceed 64kB.
To compile a program with this library, use NUCLEO-F103RB as the target name. !
Changes:
- Corrected initialization of the HSE + crystal clock (mbed permanent bug), allowing the use of on-board xtal (8MHz).(1)
- Additionally, it also set USB clock (48Mhz).(2)
- Definitions of pins and peripherals adjusted to LQFP48 case.
- Board led LED1 is now PC_13 (3)
- USER_BUTTON is now PC_14 (4)
Now the library is complete rebuilt based on mbed-dev v160 (and not yet fully tested).
notes
(1) - In case 8MHz xtal on board, CPU frequency is 72MHz. Without xtal is 64MHz.
(2) - Using the USB interface is only possible if STM32 is clocking by on-board 8MHz xtal or external clock signal 8MHz on the OSC_IN pin.
(3) - On Bluepill board led operation is reversed, i.e. 0 - led on, 1 - led off.
(4) - Bluepill board has no real user button
Information
After export to SW4STM (AC6):
- add line
#include "mbed_config.h"
in files Serial.h and RawSerial.h - in project properties change
Optimisation Level
toOptimise for size (-Os)
targets/TARGET_STM/pwmout_api.c@146:03e976389d16, 2017-03-16 (annotated)
- Committer:
- mega64
- Date:
- Thu Mar 16 06:15:53 2017 +0000
- Revision:
- 146:03e976389d16
fully rebuild, now based on mbed-dev v160
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mega64 | 146:03e976389d16 | 1 | /* mbed Microcontroller Library |
mega64 | 146:03e976389d16 | 2 | ******************************************************************************* |
mega64 | 146:03e976389d16 | 3 | * Copyright (c) 2015, STMicroelectronics |
mega64 | 146:03e976389d16 | 4 | * All rights reserved. |
mega64 | 146:03e976389d16 | 5 | * |
mega64 | 146:03e976389d16 | 6 | * Redistribution and use in source and binary forms, with or without |
mega64 | 146:03e976389d16 | 7 | * modification, are permitted provided that the following conditions are met: |
mega64 | 146:03e976389d16 | 8 | * |
mega64 | 146:03e976389d16 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mega64 | 146:03e976389d16 | 10 | * this list of conditions and the following disclaimer. |
mega64 | 146:03e976389d16 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mega64 | 146:03e976389d16 | 12 | * this list of conditions and the following disclaimer in the documentation |
mega64 | 146:03e976389d16 | 13 | * and/or other materials provided with the distribution. |
mega64 | 146:03e976389d16 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mega64 | 146:03e976389d16 | 15 | * may be used to endorse or promote products derived from this software |
mega64 | 146:03e976389d16 | 16 | * without specific prior written permission. |
mega64 | 146:03e976389d16 | 17 | * |
mega64 | 146:03e976389d16 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mega64 | 146:03e976389d16 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mega64 | 146:03e976389d16 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mega64 | 146:03e976389d16 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mega64 | 146:03e976389d16 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mega64 | 146:03e976389d16 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mega64 | 146:03e976389d16 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mega64 | 146:03e976389d16 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mega64 | 146:03e976389d16 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mega64 | 146:03e976389d16 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mega64 | 146:03e976389d16 | 28 | ******************************************************************************* |
mega64 | 146:03e976389d16 | 29 | */ |
mega64 | 146:03e976389d16 | 30 | #include "pwmout_api.h" |
mega64 | 146:03e976389d16 | 31 | |
mega64 | 146:03e976389d16 | 32 | #if DEVICE_PWMOUT |
mega64 | 146:03e976389d16 | 33 | |
mega64 | 146:03e976389d16 | 34 | #include "cmsis.h" |
mega64 | 146:03e976389d16 | 35 | #include "pinmap.h" |
mega64 | 146:03e976389d16 | 36 | #include "mbed_error.h" |
mega64 | 146:03e976389d16 | 37 | #include "PeripheralPins.h" |
mega64 | 146:03e976389d16 | 38 | #include "pwmout_device.h" |
mega64 | 146:03e976389d16 | 39 | |
mega64 | 146:03e976389d16 | 40 | static TIM_HandleTypeDef TimHandle; |
mega64 | 146:03e976389d16 | 41 | |
mega64 | 146:03e976389d16 | 42 | void pwmout_init(pwmout_t* obj, PinName pin) |
mega64 | 146:03e976389d16 | 43 | { |
mega64 | 146:03e976389d16 | 44 | // Get the peripheral name from the pin and assign it to the object |
mega64 | 146:03e976389d16 | 45 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mega64 | 146:03e976389d16 | 46 | MBED_ASSERT(obj->pwm != (PWMName)NC); |
mega64 | 146:03e976389d16 | 47 | |
mega64 | 146:03e976389d16 | 48 | // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object |
mega64 | 146:03e976389d16 | 49 | uint32_t function = pinmap_function(pin, PinMap_PWM); |
mega64 | 146:03e976389d16 | 50 | MBED_ASSERT(function != (uint32_t)NC); |
mega64 | 146:03e976389d16 | 51 | obj->channel = STM_PIN_CHANNEL(function); |
mega64 | 146:03e976389d16 | 52 | obj->inverted = STM_PIN_INVERTED(function); |
mega64 | 146:03e976389d16 | 53 | |
mega64 | 146:03e976389d16 | 54 | // Enable TIM clock |
mega64 | 146:03e976389d16 | 55 | #if defined(TIM1_BASE) |
mega64 | 146:03e976389d16 | 56 | if (obj->pwm == PWM_1){ |
mega64 | 146:03e976389d16 | 57 | __HAL_RCC_TIM1_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 58 | } |
mega64 | 146:03e976389d16 | 59 | #endif |
mega64 | 146:03e976389d16 | 60 | #if defined(TIM2_BASE) |
mega64 | 146:03e976389d16 | 61 | if (obj->pwm == PWM_2) { |
mega64 | 146:03e976389d16 | 62 | __HAL_RCC_TIM2_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 63 | } |
mega64 | 146:03e976389d16 | 64 | #endif |
mega64 | 146:03e976389d16 | 65 | #if defined(TIM3_BASE) |
mega64 | 146:03e976389d16 | 66 | if (obj->pwm == PWM_3) { |
mega64 | 146:03e976389d16 | 67 | __HAL_RCC_TIM3_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 68 | } |
mega64 | 146:03e976389d16 | 69 | #endif |
mega64 | 146:03e976389d16 | 70 | #if defined(TIM4_BASE) |
mega64 | 146:03e976389d16 | 71 | if (obj->pwm == PWM_4) { |
mega64 | 146:03e976389d16 | 72 | __HAL_RCC_TIM4_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 73 | } |
mega64 | 146:03e976389d16 | 74 | #endif |
mega64 | 146:03e976389d16 | 75 | #if defined(TIM5_BASE) |
mega64 | 146:03e976389d16 | 76 | if (obj->pwm == PWM_5) { |
mega64 | 146:03e976389d16 | 77 | __HAL_RCC_TIM5_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 78 | } |
mega64 | 146:03e976389d16 | 79 | #endif |
mega64 | 146:03e976389d16 | 80 | #if defined(TIM8_BASE) |
mega64 | 146:03e976389d16 | 81 | if (obj->pwm == PWM_8) { |
mega64 | 146:03e976389d16 | 82 | __HAL_RCC_TIM8_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 83 | } |
mega64 | 146:03e976389d16 | 84 | #endif |
mega64 | 146:03e976389d16 | 85 | #if defined(TIM9_BASE) |
mega64 | 146:03e976389d16 | 86 | if (obj->pwm == PWM_9) { |
mega64 | 146:03e976389d16 | 87 | __HAL_RCC_TIM9_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 88 | } |
mega64 | 146:03e976389d16 | 89 | #endif |
mega64 | 146:03e976389d16 | 90 | #if defined(TIM10_BASE) |
mega64 | 146:03e976389d16 | 91 | if (obj->pwm == PWM_10) { |
mega64 | 146:03e976389d16 | 92 | __HAL_RCC_TIM10_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 93 | } |
mega64 | 146:03e976389d16 | 94 | #endif |
mega64 | 146:03e976389d16 | 95 | #if defined(TIM11_BASE) |
mega64 | 146:03e976389d16 | 96 | if (obj->pwm == PWM_11) { |
mega64 | 146:03e976389d16 | 97 | __HAL_RCC_TIM11_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 98 | } |
mega64 | 146:03e976389d16 | 99 | #endif |
mega64 | 146:03e976389d16 | 100 | #if defined(TIM12_BASE) |
mega64 | 146:03e976389d16 | 101 | if (obj->pwm == PWM_12) { |
mega64 | 146:03e976389d16 | 102 | __HAL_RCC_TIM12_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 103 | } |
mega64 | 146:03e976389d16 | 104 | #endif |
mega64 | 146:03e976389d16 | 105 | #if defined(TIM13_BASE) |
mega64 | 146:03e976389d16 | 106 | if (obj->pwm == PWM_13) { |
mega64 | 146:03e976389d16 | 107 | __HAL_RCC_TIM13_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 108 | } |
mega64 | 146:03e976389d16 | 109 | #endif |
mega64 | 146:03e976389d16 | 110 | #if defined(TIM14_BASE) |
mega64 | 146:03e976389d16 | 111 | if (obj->pwm == PWM_14) { |
mega64 | 146:03e976389d16 | 112 | __HAL_RCC_TIM14_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 113 | } |
mega64 | 146:03e976389d16 | 114 | #endif |
mega64 | 146:03e976389d16 | 115 | #if defined(TIM15_BASE) |
mega64 | 146:03e976389d16 | 116 | if (obj->pwm == PWM_15) { |
mega64 | 146:03e976389d16 | 117 | __HAL_RCC_TIM15_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 118 | } |
mega64 | 146:03e976389d16 | 119 | #endif |
mega64 | 146:03e976389d16 | 120 | #if defined(TIM16_BASE) |
mega64 | 146:03e976389d16 | 121 | if (obj->pwm == PWM_16) { |
mega64 | 146:03e976389d16 | 122 | __HAL_RCC_TIM16_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 123 | } |
mega64 | 146:03e976389d16 | 124 | #endif |
mega64 | 146:03e976389d16 | 125 | #if defined(TIM17_BASE) |
mega64 | 146:03e976389d16 | 126 | if (obj->pwm == PWM_17) { |
mega64 | 146:03e976389d16 | 127 | __HAL_RCC_TIM17_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 128 | } |
mega64 | 146:03e976389d16 | 129 | #endif |
mega64 | 146:03e976389d16 | 130 | #if defined(TIM18_BASE) |
mega64 | 146:03e976389d16 | 131 | if (obj->pwm == PWM_18) { |
mega64 | 146:03e976389d16 | 132 | __HAL_RCC_TIM18_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 133 | } |
mega64 | 146:03e976389d16 | 134 | #endif |
mega64 | 146:03e976389d16 | 135 | #if defined(TIM19_BASE) |
mega64 | 146:03e976389d16 | 136 | if (obj->pwm == PWM_19) { |
mega64 | 146:03e976389d16 | 137 | __HAL_RCC_TIM19_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 138 | } |
mega64 | 146:03e976389d16 | 139 | #endif |
mega64 | 146:03e976389d16 | 140 | #if defined(TIM20_BASE) |
mega64 | 146:03e976389d16 | 141 | if (obj->pwm == PWM_20) { |
mega64 | 146:03e976389d16 | 142 | __HAL_RCC_TIM20_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 143 | } |
mega64 | 146:03e976389d16 | 144 | #endif |
mega64 | 146:03e976389d16 | 145 | #if defined(TIM21_BASE) |
mega64 | 146:03e976389d16 | 146 | if (obj->pwm == PWM_21) { |
mega64 | 146:03e976389d16 | 147 | __HAL_RCC_TIM21_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 148 | } |
mega64 | 146:03e976389d16 | 149 | #endif |
mega64 | 146:03e976389d16 | 150 | #if defined(TIM22_BASE) |
mega64 | 146:03e976389d16 | 151 | if (obj->pwm == PWM_22) { |
mega64 | 146:03e976389d16 | 152 | __HAL_RCC_TIM22_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 153 | } |
mega64 | 146:03e976389d16 | 154 | #endif |
mega64 | 146:03e976389d16 | 155 | // Configure GPIO |
mega64 | 146:03e976389d16 | 156 | pinmap_pinout(pin, PinMap_PWM); |
mega64 | 146:03e976389d16 | 157 | |
mega64 | 146:03e976389d16 | 158 | obj->pin = pin; |
mega64 | 146:03e976389d16 | 159 | obj->period = 0; |
mega64 | 146:03e976389d16 | 160 | obj->pulse = 0; |
mega64 | 146:03e976389d16 | 161 | obj->prescaler = 1; |
mega64 | 146:03e976389d16 | 162 | |
mega64 | 146:03e976389d16 | 163 | pwmout_period_us(obj, 20000); // 20 ms per default |
mega64 | 146:03e976389d16 | 164 | } |
mega64 | 146:03e976389d16 | 165 | |
mega64 | 146:03e976389d16 | 166 | void pwmout_free(pwmout_t* obj) |
mega64 | 146:03e976389d16 | 167 | { |
mega64 | 146:03e976389d16 | 168 | // Configure GPIO |
mega64 | 146:03e976389d16 | 169 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mega64 | 146:03e976389d16 | 170 | } |
mega64 | 146:03e976389d16 | 171 | |
mega64 | 146:03e976389d16 | 172 | void pwmout_write(pwmout_t* obj, float value) |
mega64 | 146:03e976389d16 | 173 | { |
mega64 | 146:03e976389d16 | 174 | TIM_OC_InitTypeDef sConfig; |
mega64 | 146:03e976389d16 | 175 | int channel = 0; |
mega64 | 146:03e976389d16 | 176 | |
mega64 | 146:03e976389d16 | 177 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
mega64 | 146:03e976389d16 | 178 | |
mega64 | 146:03e976389d16 | 179 | if (value < (float)0.0) { |
mega64 | 146:03e976389d16 | 180 | value = 0.0; |
mega64 | 146:03e976389d16 | 181 | } else if (value > (float)1.0) { |
mega64 | 146:03e976389d16 | 182 | value = 1.0; |
mega64 | 146:03e976389d16 | 183 | } |
mega64 | 146:03e976389d16 | 184 | |
mega64 | 146:03e976389d16 | 185 | obj->pulse = (uint32_t)((float)obj->period * value); |
mega64 | 146:03e976389d16 | 186 | |
mega64 | 146:03e976389d16 | 187 | // Configure channels |
mega64 | 146:03e976389d16 | 188 | sConfig.OCMode = TIM_OCMODE_PWM1; |
mega64 | 146:03e976389d16 | 189 | sConfig.Pulse = obj->pulse / obj->prescaler; |
mega64 | 146:03e976389d16 | 190 | sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
mega64 | 146:03e976389d16 | 191 | sConfig.OCFastMode = TIM_OCFAST_DISABLE; |
mega64 | 146:03e976389d16 | 192 | #if defined(TIM_OCIDLESTATE_RESET) |
mega64 | 146:03e976389d16 | 193 | sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; |
mega64 | 146:03e976389d16 | 194 | #endif |
mega64 | 146:03e976389d16 | 195 | #if defined(TIM_OCNIDLESTATE_RESET) |
mega64 | 146:03e976389d16 | 196 | sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
mega64 | 146:03e976389d16 | 197 | sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
mega64 | 146:03e976389d16 | 198 | #endif |
mega64 | 146:03e976389d16 | 199 | |
mega64 | 146:03e976389d16 | 200 | switch (obj->channel) { |
mega64 | 146:03e976389d16 | 201 | case 1: |
mega64 | 146:03e976389d16 | 202 | channel = TIM_CHANNEL_1; |
mega64 | 146:03e976389d16 | 203 | break; |
mega64 | 146:03e976389d16 | 204 | case 2: |
mega64 | 146:03e976389d16 | 205 | channel = TIM_CHANNEL_2; |
mega64 | 146:03e976389d16 | 206 | break; |
mega64 | 146:03e976389d16 | 207 | case 3: |
mega64 | 146:03e976389d16 | 208 | channel = TIM_CHANNEL_3; |
mega64 | 146:03e976389d16 | 209 | break; |
mega64 | 146:03e976389d16 | 210 | case 4: |
mega64 | 146:03e976389d16 | 211 | channel = TIM_CHANNEL_4; |
mega64 | 146:03e976389d16 | 212 | break; |
mega64 | 146:03e976389d16 | 213 | default: |
mega64 | 146:03e976389d16 | 214 | return; |
mega64 | 146:03e976389d16 | 215 | } |
mega64 | 146:03e976389d16 | 216 | |
mega64 | 146:03e976389d16 | 217 | if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { |
mega64 | 146:03e976389d16 | 218 | error("Cannot initialize PWM\n"); |
mega64 | 146:03e976389d16 | 219 | } |
mega64 | 146:03e976389d16 | 220 | |
mega64 | 146:03e976389d16 | 221 | #if !defined(PWMOUT_INVERTED_NOT_SUPPORTED) |
mega64 | 146:03e976389d16 | 222 | if (obj->inverted) { |
mega64 | 146:03e976389d16 | 223 | HAL_TIMEx_PWMN_Start(&TimHandle, channel); |
mega64 | 146:03e976389d16 | 224 | } else |
mega64 | 146:03e976389d16 | 225 | #endif |
mega64 | 146:03e976389d16 | 226 | { |
mega64 | 146:03e976389d16 | 227 | HAL_TIM_PWM_Start(&TimHandle, channel); |
mega64 | 146:03e976389d16 | 228 | } |
mega64 | 146:03e976389d16 | 229 | } |
mega64 | 146:03e976389d16 | 230 | |
mega64 | 146:03e976389d16 | 231 | float pwmout_read(pwmout_t* obj) |
mega64 | 146:03e976389d16 | 232 | { |
mega64 | 146:03e976389d16 | 233 | float value = 0; |
mega64 | 146:03e976389d16 | 234 | if (obj->period > 0) { |
mega64 | 146:03e976389d16 | 235 | value = (float)(obj->pulse) / (float)(obj->period); |
mega64 | 146:03e976389d16 | 236 | } |
mega64 | 146:03e976389d16 | 237 | return ((value > (float)1.0) ? (float)(1.0) : (value)); |
mega64 | 146:03e976389d16 | 238 | } |
mega64 | 146:03e976389d16 | 239 | |
mega64 | 146:03e976389d16 | 240 | void pwmout_period(pwmout_t* obj, float seconds) |
mega64 | 146:03e976389d16 | 241 | { |
mega64 | 146:03e976389d16 | 242 | pwmout_period_us(obj, seconds * 1000000.0f); |
mega64 | 146:03e976389d16 | 243 | } |
mega64 | 146:03e976389d16 | 244 | |
mega64 | 146:03e976389d16 | 245 | void pwmout_period_ms(pwmout_t* obj, int ms) |
mega64 | 146:03e976389d16 | 246 | { |
mega64 | 146:03e976389d16 | 247 | pwmout_period_us(obj, ms * 1000); |
mega64 | 146:03e976389d16 | 248 | } |
mega64 | 146:03e976389d16 | 249 | |
mega64 | 146:03e976389d16 | 250 | void pwmout_period_us(pwmout_t* obj, int us) |
mega64 | 146:03e976389d16 | 251 | { |
mega64 | 146:03e976389d16 | 252 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
mega64 | 146:03e976389d16 | 253 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
mega64 | 146:03e976389d16 | 254 | uint32_t PclkFreq = 0; |
mega64 | 146:03e976389d16 | 255 | uint32_t APBxCLKDivider = RCC_HCLK_DIV1; |
mega64 | 146:03e976389d16 | 256 | float dc = pwmout_read(obj); |
mega64 | 146:03e976389d16 | 257 | uint8_t i = 0; |
mega64 | 146:03e976389d16 | 258 | |
mega64 | 146:03e976389d16 | 259 | __HAL_TIM_DISABLE(&TimHandle); |
mega64 | 146:03e976389d16 | 260 | |
mega64 | 146:03e976389d16 | 261 | // Get clock configuration |
mega64 | 146:03e976389d16 | 262 | // Note: PclkFreq contains here the Latency (not used after) |
mega64 | 146:03e976389d16 | 263 | HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); |
mega64 | 146:03e976389d16 | 264 | |
mega64 | 146:03e976389d16 | 265 | /* Parse the pwm / apb mapping table to find the right entry */ |
mega64 | 146:03e976389d16 | 266 | while(pwm_apb_map_table[i].pwm != obj->pwm) { |
mega64 | 146:03e976389d16 | 267 | i++; |
mega64 | 146:03e976389d16 | 268 | } |
mega64 | 146:03e976389d16 | 269 | |
mega64 | 146:03e976389d16 | 270 | if(pwm_apb_map_table[i].pwm == 0) |
mega64 | 146:03e976389d16 | 271 | error("Unknown PWM instance"); |
mega64 | 146:03e976389d16 | 272 | |
mega64 | 146:03e976389d16 | 273 | if(pwm_apb_map_table[i].pwmoutApb == PWMOUT_ON_APB1) { |
mega64 | 146:03e976389d16 | 274 | PclkFreq = HAL_RCC_GetPCLK1Freq(); |
mega64 | 146:03e976389d16 | 275 | APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; |
mega64 | 146:03e976389d16 | 276 | } else { |
mega64 | 146:03e976389d16 | 277 | #if !defined(PWMOUT_APB2_NOT_SUPPORTED) |
mega64 | 146:03e976389d16 | 278 | PclkFreq = HAL_RCC_GetPCLK2Freq(); |
mega64 | 146:03e976389d16 | 279 | APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; |
mega64 | 146:03e976389d16 | 280 | #endif |
mega64 | 146:03e976389d16 | 281 | } |
mega64 | 146:03e976389d16 | 282 | |
mega64 | 146:03e976389d16 | 283 | |
mega64 | 146:03e976389d16 | 284 | /* By default use, 1us as SW pre-scaler */ |
mega64 | 146:03e976389d16 | 285 | obj->prescaler = 1; |
mega64 | 146:03e976389d16 | 286 | // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx |
mega64 | 146:03e976389d16 | 287 | if (APBxCLKDivider == RCC_HCLK_DIV1) { |
mega64 | 146:03e976389d16 | 288 | TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick |
mega64 | 146:03e976389d16 | 289 | } else { |
mega64 | 146:03e976389d16 | 290 | TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000)) - 1; // 1 us tick |
mega64 | 146:03e976389d16 | 291 | } |
mega64 | 146:03e976389d16 | 292 | TimHandle.Init.Period = (us - 1); |
mega64 | 146:03e976389d16 | 293 | |
mega64 | 146:03e976389d16 | 294 | /* In case period or pre-scalers are out of range, loop-in to get valid values */ |
mega64 | 146:03e976389d16 | 295 | while ((TimHandle.Init.Period > 0xFFFF) || (TimHandle.Init.Prescaler > 0xFFFF)) { |
mega64 | 146:03e976389d16 | 296 | obj->prescaler = obj->prescaler * 2; |
mega64 | 146:03e976389d16 | 297 | if (APBxCLKDivider == RCC_HCLK_DIV1) { |
mega64 | 146:03e976389d16 | 298 | TimHandle.Init.Prescaler = (((PclkFreq) / 1000000) * obj->prescaler) - 1; |
mega64 | 146:03e976389d16 | 299 | } else { |
mega64 | 146:03e976389d16 | 300 | TimHandle.Init.Prescaler = (((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; |
mega64 | 146:03e976389d16 | 301 | } |
mega64 | 146:03e976389d16 | 302 | TimHandle.Init.Period = (us - 1) / obj->prescaler; |
mega64 | 146:03e976389d16 | 303 | /* Period decreases and prescaler increases over loops, so check for |
mega64 | 146:03e976389d16 | 304 | * possible out of range cases */ |
mega64 | 146:03e976389d16 | 305 | if ((TimHandle.Init.Period < 0xFFFF) && (TimHandle.Init.Prescaler > 0xFFFF)) { |
mega64 | 146:03e976389d16 | 306 | error("Cannot initialize PWM\n"); |
mega64 | 146:03e976389d16 | 307 | break; |
mega64 | 146:03e976389d16 | 308 | } |
mega64 | 146:03e976389d16 | 309 | } |
mega64 | 146:03e976389d16 | 310 | |
mega64 | 146:03e976389d16 | 311 | TimHandle.Init.ClockDivision = 0; |
mega64 | 146:03e976389d16 | 312 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
mega64 | 146:03e976389d16 | 313 | |
mega64 | 146:03e976389d16 | 314 | if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { |
mega64 | 146:03e976389d16 | 315 | error("Cannot initialize PWM\n"); |
mega64 | 146:03e976389d16 | 316 | } |
mega64 | 146:03e976389d16 | 317 | |
mega64 | 146:03e976389d16 | 318 | // Save for future use |
mega64 | 146:03e976389d16 | 319 | obj->period = us; |
mega64 | 146:03e976389d16 | 320 | |
mega64 | 146:03e976389d16 | 321 | // Set duty cycle again |
mega64 | 146:03e976389d16 | 322 | pwmout_write(obj, dc); |
mega64 | 146:03e976389d16 | 323 | |
mega64 | 146:03e976389d16 | 324 | __HAL_TIM_ENABLE(&TimHandle); |
mega64 | 146:03e976389d16 | 325 | } |
mega64 | 146:03e976389d16 | 326 | |
mega64 | 146:03e976389d16 | 327 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
mega64 | 146:03e976389d16 | 328 | { |
mega64 | 146:03e976389d16 | 329 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mega64 | 146:03e976389d16 | 330 | } |
mega64 | 146:03e976389d16 | 331 | |
mega64 | 146:03e976389d16 | 332 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
mega64 | 146:03e976389d16 | 333 | { |
mega64 | 146:03e976389d16 | 334 | pwmout_pulsewidth_us(obj, ms * 1000); |
mega64 | 146:03e976389d16 | 335 | } |
mega64 | 146:03e976389d16 | 336 | |
mega64 | 146:03e976389d16 | 337 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
mega64 | 146:03e976389d16 | 338 | { |
mega64 | 146:03e976389d16 | 339 | float value = (float)us / (float)obj->period; |
mega64 | 146:03e976389d16 | 340 | pwmout_write(obj, value); |
mega64 | 146:03e976389d16 | 341 | } |
mega64 | 146:03e976389d16 | 342 | |
mega64 | 146:03e976389d16 | 343 | #endif |