mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pwmout_api.c@174:8bb9f3a33240, 2014-04-29 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Apr 29 11:15:07 2014 +0100
- Revision:
- 174:8bb9f3a33240
- Parent:
- 129:0182c99221bc
- Child:
- 215:83cf97a28428
Synchronized with git revision 5bf985ebc651a2c31cefabd9d62c51dc465ef60a
Full URL: https://github.com/mbedmicro/mbed/commit/5bf985ebc651a2c31cefabd9d62c51dc465ef60a/
[NUCLEO_L152RE/F103RB] Add LSE configuration for RTC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 76:aeb1df146756 | 1 | /* mbed Microcontroller Library |
mbed_official | 76:aeb1df146756 | 2 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 76:aeb1df146756 | 4 | * All rights reserved. |
mbed_official | 76:aeb1df146756 | 5 | * |
mbed_official | 76:aeb1df146756 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 76:aeb1df146756 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 76:aeb1df146756 | 8 | * |
mbed_official | 76:aeb1df146756 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 76:aeb1df146756 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 76:aeb1df146756 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 76:aeb1df146756 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 76:aeb1df146756 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 76:aeb1df146756 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 76:aeb1df146756 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 76:aeb1df146756 | 16 | * without specific prior written permission. |
mbed_official | 76:aeb1df146756 | 17 | * |
mbed_official | 76:aeb1df146756 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 76:aeb1df146756 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 76:aeb1df146756 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 76:aeb1df146756 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 76:aeb1df146756 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 76:aeb1df146756 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 76:aeb1df146756 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 76:aeb1df146756 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 76:aeb1df146756 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 76:aeb1df146756 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 76:aeb1df146756 | 28 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 29 | */ |
mbed_official | 76:aeb1df146756 | 30 | #include "pwmout_api.h" |
mbed_official | 76:aeb1df146756 | 31 | |
mbed_official | 174:8bb9f3a33240 | 32 | #if DEVICE_PWMOUT |
mbed_official | 174:8bb9f3a33240 | 33 | |
mbed_official | 76:aeb1df146756 | 34 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 35 | #include "pinmap.h" |
mbed_official | 76:aeb1df146756 | 36 | #include "error.h" |
mbed_official | 76:aeb1df146756 | 37 | |
mbed_official | 129:0182c99221bc | 38 | // TIM5 cannot be used because already used by the us_ticker |
mbed_official | 76:aeb1df146756 | 39 | static const PinMap PinMap_PWM[] = { |
mbed_official | 174:8bb9f3a33240 | 40 | // {PA_0, PWM_5, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM5)}, // TIM5_CH1 |
mbed_official | 129:0182c99221bc | 41 | {PA_1, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM2)}, // TIM2_CH2 |
mbed_official | 174:8bb9f3a33240 | 42 | // {PA_1, PWM_5, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM5)}, // TIM5_CH1 |
mbed_official | 129:0182c99221bc | 43 | {PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM2)}, // TIM2_CH3 |
mbed_official | 174:8bb9f3a33240 | 44 | // {PA_2, PWM_5, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM5)}, // TIM5_CH3 |
mbed_official | 174:8bb9f3a33240 | 45 | // {PA_2, PWM_9, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM9)}, // TIM9_CH1 |
mbed_official | 129:0182c99221bc | 46 | {PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM2)}, // TIM2_CH4 |
mbed_official | 174:8bb9f3a33240 | 47 | // {PA_3, PWM_5, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM5)}, // TIM5_CH4 |
mbed_official | 174:8bb9f3a33240 | 48 | // {PA_3, PWM_9, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM9)}, // TIM9_CH2 |
mbed_official | 129:0182c99221bc | 49 | {PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH1 |
mbed_official | 174:8bb9f3a33240 | 50 | // {PA_6, PWM_10, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM10)}, // TIM10_CH1 |
mbed_official | 129:0182c99221bc | 51 | {PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH2 |
mbed_official | 174:8bb9f3a33240 | 52 | // {PA_7, PWM_11, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM11)}, // TIM11_CH1 |
mbed_official | 129:0182c99221bc | 53 | {PB_0, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH3 |
mbed_official | 129:0182c99221bc | 54 | {PB_1, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH4 |
mbed_official | 129:0182c99221bc | 55 | {PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM2)}, // TIM2_CH2 |
mbed_official | 129:0182c99221bc | 56 | {PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH1 |
mbed_official | 129:0182c99221bc | 57 | {PB_5, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH2 |
mbed_official | 129:0182c99221bc | 58 | {PB_6, PWM_4, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM4)}, // TIM4_CH1 |
mbed_official | 129:0182c99221bc | 59 | {PB_7, PWM_4, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM4)}, // TIM4_CH2 |
mbed_official | 129:0182c99221bc | 60 | {PB_8, PWM_4, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM4)}, // TIM4_CH3 |
mbed_official | 174:8bb9f3a33240 | 61 | // {PB_8, PWM_10, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM10)}, // TIM10_CH1 |
mbed_official | 129:0182c99221bc | 62 | {PB_9, PWM_4, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM4)}, // TIM4_CH4 |
mbed_official | 174:8bb9f3a33240 | 63 | // {PB_9, PWM_11, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM11)}, // TIM11_CH1 |
mbed_official | 129:0182c99221bc | 64 | {PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM2)}, // TIM2_CH3 |
mbed_official | 129:0182c99221bc | 65 | {PB_11, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM2)}, // TIM2_CH4 |
mbed_official | 129:0182c99221bc | 66 | {PB_12, PWM_10, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM10)}, // TIM10_CH1 |
mbed_official | 129:0182c99221bc | 67 | {PB_13, PWM_9, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM9)}, // TIM9_CH1 |
mbed_official | 129:0182c99221bc | 68 | {PB_14, PWM_9, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM9)}, // TIM9_CH2 |
mbed_official | 174:8bb9f3a33240 | 69 | {PB_15, PWM_11, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM11)}, // TIM11_CH1 |
mbed_official | 129:0182c99221bc | 70 | {PC_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH1 |
mbed_official | 129:0182c99221bc | 71 | {PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH2 |
mbed_official | 129:0182c99221bc | 72 | {PC_8, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH3 |
mbed_official | 174:8bb9f3a33240 | 73 | {PC_9, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH4 |
mbed_official | 129:0182c99221bc | 74 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 75 | }; |
mbed_official | 76:aeb1df146756 | 76 | |
mbed_official | 174:8bb9f3a33240 | 77 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 76:aeb1df146756 | 78 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 76:aeb1df146756 | 79 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 174:8bb9f3a33240 | 80 | |
mbed_official | 76:aeb1df146756 | 81 | if (obj->pwm == (PWMName)NC) { |
mbed_official | 76:aeb1df146756 | 82 | error("PWM pinout mapping failed"); |
mbed_official | 76:aeb1df146756 | 83 | } |
mbed_official | 174:8bb9f3a33240 | 84 | |
mbed_official | 76:aeb1df146756 | 85 | // Enable TIM clock |
mbed_official | 76:aeb1df146756 | 86 | if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); |
mbed_official | 76:aeb1df146756 | 87 | if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); |
mbed_official | 84:f54042cbc282 | 88 | if (obj->pwm == PWM_4) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); |
mbed_official | 129:0182c99221bc | 89 | if (obj->pwm == PWM_5) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); |
mbed_official | 129:0182c99221bc | 90 | if (obj->pwm == PWM_9) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE); |
mbed_official | 129:0182c99221bc | 91 | if (obj->pwm == PWM_10) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE); |
mbed_official | 129:0182c99221bc | 92 | if (obj->pwm == PWM_11) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, ENABLE); |
mbed_official | 174:8bb9f3a33240 | 93 | |
mbed_official | 76:aeb1df146756 | 94 | // Configure GPIO |
mbed_official | 76:aeb1df146756 | 95 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 174:8bb9f3a33240 | 96 | |
mbed_official | 76:aeb1df146756 | 97 | obj->pin = pin; |
mbed_official | 76:aeb1df146756 | 98 | obj->period = 0; |
mbed_official | 76:aeb1df146756 | 99 | obj->pulse = 0; |
mbed_official | 174:8bb9f3a33240 | 100 | |
mbed_official | 76:aeb1df146756 | 101 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 76:aeb1df146756 | 102 | } |
mbed_official | 76:aeb1df146756 | 103 | |
mbed_official | 76:aeb1df146756 | 104 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 76:aeb1df146756 | 105 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 76:aeb1df146756 | 106 | TIM_DeInit(tim); |
mbed_official | 76:aeb1df146756 | 107 | } |
mbed_official | 76:aeb1df146756 | 108 | |
mbed_official | 76:aeb1df146756 | 109 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 76:aeb1df146756 | 110 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 76:aeb1df146756 | 111 | TIM_OCInitTypeDef TIM_OCInitStructure; |
mbed_official | 129:0182c99221bc | 112 | |
mbed_official | 76:aeb1df146756 | 113 | if (value < 0.0) { |
mbed_official | 76:aeb1df146756 | 114 | value = 0.0; |
mbed_official | 76:aeb1df146756 | 115 | } else if (value > 1.0) { |
mbed_official | 76:aeb1df146756 | 116 | value = 1.0; |
mbed_official | 76:aeb1df146756 | 117 | } |
mbed_official | 174:8bb9f3a33240 | 118 | |
mbed_official | 76:aeb1df146756 | 119 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 174:8bb9f3a33240 | 120 | |
mbed_official | 129:0182c99221bc | 121 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
mbed_official | 76:aeb1df146756 | 122 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 129:0182c99221bc | 123 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
mbed_official | 129:0182c99221bc | 124 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 76:aeb1df146756 | 125 | |
mbed_official | 129:0182c99221bc | 126 | // Configure the channels |
mbed_official | 129:0182c99221bc | 127 | switch (obj->pin) { |
mbed_official | 129:0182c99221bc | 128 | // Channels 1 |
mbed_official | 174:8bb9f3a33240 | 129 | //case PA_0: |
mbed_official | 174:8bb9f3a33240 | 130 | //case PA_1: |
mbed_official | 174:8bb9f3a33240 | 131 | //case PA_2: |
mbed_official | 129:0182c99221bc | 132 | case PA_6: |
mbed_official | 174:8bb9f3a33240 | 133 | //case PA_7: |
mbed_official | 129:0182c99221bc | 134 | case PB_4: |
mbed_official | 129:0182c99221bc | 135 | case PB_6: |
mbed_official | 174:8bb9f3a33240 | 136 | //case PB_8: |
mbed_official | 174:8bb9f3a33240 | 137 | //case PB_9: |
mbed_official | 129:0182c99221bc | 138 | case PB_12: |
mbed_official | 129:0182c99221bc | 139 | case PB_13: |
mbed_official | 129:0182c99221bc | 140 | case PB_15: |
mbed_official | 129:0182c99221bc | 141 | case PC_6: |
mbed_official | 129:0182c99221bc | 142 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 129:0182c99221bc | 143 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 129:0182c99221bc | 144 | break; |
mbed_official | 129:0182c99221bc | 145 | // Channels 2 |
mbed_official | 129:0182c99221bc | 146 | case PA_1: |
mbed_official | 174:8bb9f3a33240 | 147 | //case PA_3: |
mbed_official | 129:0182c99221bc | 148 | case PA_7: |
mbed_official | 129:0182c99221bc | 149 | case PB_3: |
mbed_official | 129:0182c99221bc | 150 | case PB_5: |
mbed_official | 129:0182c99221bc | 151 | case PB_7: |
mbed_official | 129:0182c99221bc | 152 | case PB_14: |
mbed_official | 129:0182c99221bc | 153 | case PC_7: |
mbed_official | 129:0182c99221bc | 154 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 129:0182c99221bc | 155 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 129:0182c99221bc | 156 | break; |
mbed_official | 129:0182c99221bc | 157 | // Channels 3 |
mbed_official | 129:0182c99221bc | 158 | case PA_2: |
mbed_official | 129:0182c99221bc | 159 | case PB_0: |
mbed_official | 129:0182c99221bc | 160 | case PB_8: |
mbed_official | 129:0182c99221bc | 161 | case PB_10: |
mbed_official | 129:0182c99221bc | 162 | case PC_8: |
mbed_official | 129:0182c99221bc | 163 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 129:0182c99221bc | 164 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 129:0182c99221bc | 165 | break; |
mbed_official | 129:0182c99221bc | 166 | // Channels 4 |
mbed_official | 129:0182c99221bc | 167 | case PA_3: |
mbed_official | 129:0182c99221bc | 168 | case PB_1: |
mbed_official | 129:0182c99221bc | 169 | case PB_9: |
mbed_official | 129:0182c99221bc | 170 | case PB_11: |
mbed_official | 129:0182c99221bc | 171 | case PC_9: |
mbed_official | 129:0182c99221bc | 172 | TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 129:0182c99221bc | 173 | TIM_OC4Init(tim, &TIM_OCInitStructure); |
mbed_official | 174:8bb9f3a33240 | 174 | break; |
mbed_official | 129:0182c99221bc | 175 | default: |
mbed_official | 129:0182c99221bc | 176 | return; |
mbed_official | 76:aeb1df146756 | 177 | } |
mbed_official | 76:aeb1df146756 | 178 | } |
mbed_official | 76:aeb1df146756 | 179 | |
mbed_official | 76:aeb1df146756 | 180 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 76:aeb1df146756 | 181 | float value = 0; |
mbed_official | 76:aeb1df146756 | 182 | if (obj->period > 0) { |
mbed_official | 76:aeb1df146756 | 183 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 76:aeb1df146756 | 184 | } |
mbed_official | 76:aeb1df146756 | 185 | return ((value > 1.0) ? (1.0) : (value)); |
mbed_official | 76:aeb1df146756 | 186 | } |
mbed_official | 76:aeb1df146756 | 187 | |
mbed_official | 76:aeb1df146756 | 188 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 76:aeb1df146756 | 189 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 76:aeb1df146756 | 190 | } |
mbed_official | 76:aeb1df146756 | 191 | |
mbed_official | 76:aeb1df146756 | 192 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 76:aeb1df146756 | 193 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 76:aeb1df146756 | 194 | } |
mbed_official | 76:aeb1df146756 | 195 | |
mbed_official | 76:aeb1df146756 | 196 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 76:aeb1df146756 | 197 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 76:aeb1df146756 | 198 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
mbed_official | 76:aeb1df146756 | 199 | float dc = pwmout_read(obj); |
mbed_official | 76:aeb1df146756 | 200 | |
mbed_official | 174:8bb9f3a33240 | 201 | TIM_Cmd(tim, DISABLE); |
mbed_official | 174:8bb9f3a33240 | 202 | |
mbed_official | 76:aeb1df146756 | 203 | obj->period = us; |
mbed_official | 174:8bb9f3a33240 | 204 | |
mbed_official | 129:0182c99221bc | 205 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
mbed_official | 129:0182c99221bc | 206 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 76:aeb1df146756 | 207 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
mbed_official | 129:0182c99221bc | 208 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
mbed_official | 76:aeb1df146756 | 209 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
mbed_official | 76:aeb1df146756 | 210 | |
mbed_official | 76:aeb1df146756 | 211 | // Set duty cycle again |
mbed_official | 76:aeb1df146756 | 212 | pwmout_write(obj, dc); |
mbed_official | 174:8bb9f3a33240 | 213 | |
mbed_official | 174:8bb9f3a33240 | 214 | TIM_ARRPreloadConfig(tim, ENABLE); |
mbed_official | 76:aeb1df146756 | 215 | TIM_Cmd(tim, ENABLE); |
mbed_official | 76:aeb1df146756 | 216 | } |
mbed_official | 76:aeb1df146756 | 217 | |
mbed_official | 76:aeb1df146756 | 218 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 76:aeb1df146756 | 219 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 76:aeb1df146756 | 220 | } |
mbed_official | 76:aeb1df146756 | 221 | |
mbed_official | 76:aeb1df146756 | 222 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 76:aeb1df146756 | 223 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 76:aeb1df146756 | 224 | } |
mbed_official | 76:aeb1df146756 | 225 | |
mbed_official | 76:aeb1df146756 | 226 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 76:aeb1df146756 | 227 | float value = (float)us / (float)obj->period; |
mbed_official | 76:aeb1df146756 | 228 | pwmout_write(obj, value); |
mbed_official | 76:aeb1df146756 | 229 | } |
mbed_official | 174:8bb9f3a33240 | 230 | |
mbed_official | 174:8bb9f3a33240 | 231 | #endif |