mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pwmout_api.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 227:7bd0639b8911
test with CLOCK_SETUP = 0
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 | 227:7bd0639b8911 | 30 | #include "mbed_assert.h" |
mbed_official | 76:aeb1df146756 | 31 | #include "pwmout_api.h" |
mbed_official | 76:aeb1df146756 | 32 | |
mbed_official | 174:8bb9f3a33240 | 33 | #if DEVICE_PWMOUT |
mbed_official | 174:8bb9f3a33240 | 34 | |
mbed_official | 76:aeb1df146756 | 35 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 36 | #include "pinmap.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 | 227:7bd0639b8911 | 80 | MBED_ASSERT(obj->pwm != (PWMName)NC); |
mbed_official | 174:8bb9f3a33240 | 81 | |
mbed_official | 76:aeb1df146756 | 82 | // Enable TIM clock |
mbed_official | 76:aeb1df146756 | 83 | if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); |
mbed_official | 76:aeb1df146756 | 84 | if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); |
mbed_official | 84:f54042cbc282 | 85 | if (obj->pwm == PWM_4) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); |
mbed_official | 129:0182c99221bc | 86 | if (obj->pwm == PWM_5) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); |
mbed_official | 129:0182c99221bc | 87 | if (obj->pwm == PWM_9) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE); |
mbed_official | 129:0182c99221bc | 88 | if (obj->pwm == PWM_10) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE); |
mbed_official | 129:0182c99221bc | 89 | if (obj->pwm == PWM_11) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, ENABLE); |
mbed_official | 174:8bb9f3a33240 | 90 | |
mbed_official | 76:aeb1df146756 | 91 | // Configure GPIO |
mbed_official | 76:aeb1df146756 | 92 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 174:8bb9f3a33240 | 93 | |
mbed_official | 76:aeb1df146756 | 94 | obj->pin = pin; |
mbed_official | 76:aeb1df146756 | 95 | obj->period = 0; |
mbed_official | 76:aeb1df146756 | 96 | obj->pulse = 0; |
mbed_official | 174:8bb9f3a33240 | 97 | |
mbed_official | 76:aeb1df146756 | 98 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 76:aeb1df146756 | 99 | } |
mbed_official | 76:aeb1df146756 | 100 | |
mbed_official | 76:aeb1df146756 | 101 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 215:83cf97a28428 | 102 | // Configure GPIOs |
mbed_official | 215:83cf97a28428 | 103 | pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); |
mbed_official | 76:aeb1df146756 | 104 | } |
mbed_official | 76:aeb1df146756 | 105 | |
mbed_official | 76:aeb1df146756 | 106 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 76:aeb1df146756 | 107 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 76:aeb1df146756 | 108 | TIM_OCInitTypeDef TIM_OCInitStructure; |
mbed_official | 129:0182c99221bc | 109 | |
mbed_official | 76:aeb1df146756 | 110 | if (value < 0.0) { |
mbed_official | 76:aeb1df146756 | 111 | value = 0.0; |
mbed_official | 76:aeb1df146756 | 112 | } else if (value > 1.0) { |
mbed_official | 76:aeb1df146756 | 113 | value = 1.0; |
mbed_official | 76:aeb1df146756 | 114 | } |
mbed_official | 174:8bb9f3a33240 | 115 | |
mbed_official | 76:aeb1df146756 | 116 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 174:8bb9f3a33240 | 117 | |
mbed_official | 129:0182c99221bc | 118 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
mbed_official | 76:aeb1df146756 | 119 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 129:0182c99221bc | 120 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
mbed_official | 129:0182c99221bc | 121 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 76:aeb1df146756 | 122 | |
mbed_official | 129:0182c99221bc | 123 | // Configure the channels |
mbed_official | 129:0182c99221bc | 124 | switch (obj->pin) { |
mbed_official | 129:0182c99221bc | 125 | // Channels 1 |
mbed_official | 174:8bb9f3a33240 | 126 | //case PA_0: |
mbed_official | 174:8bb9f3a33240 | 127 | //case PA_1: |
mbed_official | 174:8bb9f3a33240 | 128 | //case PA_2: |
mbed_official | 129:0182c99221bc | 129 | case PA_6: |
mbed_official | 174:8bb9f3a33240 | 130 | //case PA_7: |
mbed_official | 129:0182c99221bc | 131 | case PB_4: |
mbed_official | 129:0182c99221bc | 132 | case PB_6: |
mbed_official | 174:8bb9f3a33240 | 133 | //case PB_8: |
mbed_official | 174:8bb9f3a33240 | 134 | //case PB_9: |
mbed_official | 129:0182c99221bc | 135 | case PB_12: |
mbed_official | 129:0182c99221bc | 136 | case PB_13: |
mbed_official | 129:0182c99221bc | 137 | case PB_15: |
mbed_official | 129:0182c99221bc | 138 | case PC_6: |
mbed_official | 129:0182c99221bc | 139 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 129:0182c99221bc | 140 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 129:0182c99221bc | 141 | break; |
mbed_official | 129:0182c99221bc | 142 | // Channels 2 |
mbed_official | 129:0182c99221bc | 143 | case PA_1: |
mbed_official | 174:8bb9f3a33240 | 144 | //case PA_3: |
mbed_official | 129:0182c99221bc | 145 | case PA_7: |
mbed_official | 129:0182c99221bc | 146 | case PB_3: |
mbed_official | 129:0182c99221bc | 147 | case PB_5: |
mbed_official | 129:0182c99221bc | 148 | case PB_7: |
mbed_official | 129:0182c99221bc | 149 | case PB_14: |
mbed_official | 129:0182c99221bc | 150 | case PC_7: |
mbed_official | 129:0182c99221bc | 151 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 129:0182c99221bc | 152 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 129:0182c99221bc | 153 | break; |
mbed_official | 129:0182c99221bc | 154 | // Channels 3 |
mbed_official | 129:0182c99221bc | 155 | case PA_2: |
mbed_official | 129:0182c99221bc | 156 | case PB_0: |
mbed_official | 129:0182c99221bc | 157 | case PB_8: |
mbed_official | 129:0182c99221bc | 158 | case PB_10: |
mbed_official | 129:0182c99221bc | 159 | case PC_8: |
mbed_official | 129:0182c99221bc | 160 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 129:0182c99221bc | 161 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 129:0182c99221bc | 162 | break; |
mbed_official | 129:0182c99221bc | 163 | // Channels 4 |
mbed_official | 129:0182c99221bc | 164 | case PA_3: |
mbed_official | 129:0182c99221bc | 165 | case PB_1: |
mbed_official | 129:0182c99221bc | 166 | case PB_9: |
mbed_official | 129:0182c99221bc | 167 | case PB_11: |
mbed_official | 129:0182c99221bc | 168 | case PC_9: |
mbed_official | 129:0182c99221bc | 169 | TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 129:0182c99221bc | 170 | TIM_OC4Init(tim, &TIM_OCInitStructure); |
mbed_official | 174:8bb9f3a33240 | 171 | break; |
mbed_official | 129:0182c99221bc | 172 | default: |
mbed_official | 129:0182c99221bc | 173 | return; |
mbed_official | 76:aeb1df146756 | 174 | } |
mbed_official | 76:aeb1df146756 | 175 | } |
mbed_official | 76:aeb1df146756 | 176 | |
mbed_official | 76:aeb1df146756 | 177 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 76:aeb1df146756 | 178 | float value = 0; |
mbed_official | 76:aeb1df146756 | 179 | if (obj->period > 0) { |
mbed_official | 76:aeb1df146756 | 180 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 76:aeb1df146756 | 181 | } |
mbed_official | 76:aeb1df146756 | 182 | return ((value > 1.0) ? (1.0) : (value)); |
mbed_official | 76:aeb1df146756 | 183 | } |
mbed_official | 76:aeb1df146756 | 184 | |
mbed_official | 76:aeb1df146756 | 185 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 76:aeb1df146756 | 186 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 76:aeb1df146756 | 187 | } |
mbed_official | 76:aeb1df146756 | 188 | |
mbed_official | 76:aeb1df146756 | 189 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 76:aeb1df146756 | 190 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 76:aeb1df146756 | 191 | } |
mbed_official | 76:aeb1df146756 | 192 | |
mbed_official | 76:aeb1df146756 | 193 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 76:aeb1df146756 | 194 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 76:aeb1df146756 | 195 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
mbed_official | 76:aeb1df146756 | 196 | float dc = pwmout_read(obj); |
mbed_official | 76:aeb1df146756 | 197 | |
mbed_official | 174:8bb9f3a33240 | 198 | TIM_Cmd(tim, DISABLE); |
mbed_official | 174:8bb9f3a33240 | 199 | |
mbed_official | 76:aeb1df146756 | 200 | obj->period = us; |
mbed_official | 174:8bb9f3a33240 | 201 | |
mbed_official | 129:0182c99221bc | 202 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
mbed_official | 129:0182c99221bc | 203 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 76:aeb1df146756 | 204 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
mbed_official | 129:0182c99221bc | 205 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
mbed_official | 76:aeb1df146756 | 206 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
mbed_official | 76:aeb1df146756 | 207 | |
mbed_official | 76:aeb1df146756 | 208 | // Set duty cycle again |
mbed_official | 76:aeb1df146756 | 209 | pwmout_write(obj, dc); |
mbed_official | 174:8bb9f3a33240 | 210 | |
mbed_official | 174:8bb9f3a33240 | 211 | TIM_ARRPreloadConfig(tim, ENABLE); |
mbed_official | 76:aeb1df146756 | 212 | TIM_Cmd(tim, ENABLE); |
mbed_official | 76:aeb1df146756 | 213 | } |
mbed_official | 76:aeb1df146756 | 214 | |
mbed_official | 76:aeb1df146756 | 215 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 76:aeb1df146756 | 216 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 76:aeb1df146756 | 217 | } |
mbed_official | 76:aeb1df146756 | 218 | |
mbed_official | 76:aeb1df146756 | 219 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 76:aeb1df146756 | 220 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 76:aeb1df146756 | 221 | } |
mbed_official | 76:aeb1df146756 | 222 | |
mbed_official | 76:aeb1df146756 | 223 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 76:aeb1df146756 | 224 | float value = (float)us / (float)obj->period; |
mbed_official | 76:aeb1df146756 | 225 | pwmout_write(obj, value); |
mbed_official | 76:aeb1df146756 | 226 | } |
mbed_official | 174:8bb9f3a33240 | 227 | |
mbed_official | 174:8bb9f3a33240 | 228 | #endif |