mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/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:
- 167:d5744491c362
- Child:
- 208:4557f4bb2dd5
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 | 52:a51c77007319 | 1 | /* mbed Microcontroller Library |
mbed_official | 70:c1fbde68b492 | 2 | ******************************************************************************* |
mbed_official | 70:c1fbde68b492 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 70:c1fbde68b492 | 4 | * All rights reserved. |
mbed_official | 52:a51c77007319 | 5 | * |
mbed_official | 70:c1fbde68b492 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 70:c1fbde68b492 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 52:a51c77007319 | 8 | * |
mbed_official | 70:c1fbde68b492 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 70:c1fbde68b492 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 70:c1fbde68b492 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 70:c1fbde68b492 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 70:c1fbde68b492 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 70:c1fbde68b492 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 70:c1fbde68b492 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 70:c1fbde68b492 | 16 | * without specific prior written permission. |
mbed_official | 52:a51c77007319 | 17 | * |
mbed_official | 70:c1fbde68b492 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 70:c1fbde68b492 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 70:c1fbde68b492 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 70:c1fbde68b492 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 70:c1fbde68b492 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 70:c1fbde68b492 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 70:c1fbde68b492 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 70:c1fbde68b492 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 70:c1fbde68b492 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 70:c1fbde68b492 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 70:c1fbde68b492 | 28 | ******************************************************************************* |
mbed_official | 52:a51c77007319 | 29 | */ |
mbed_official | 52:a51c77007319 | 30 | #include "pwmout_api.h" |
mbed_official | 52:a51c77007319 | 31 | |
mbed_official | 174:8bb9f3a33240 | 32 | #if DEVICE_PWMOUT |
mbed_official | 174:8bb9f3a33240 | 33 | |
mbed_official | 52:a51c77007319 | 34 | #include "cmsis.h" |
mbed_official | 52:a51c77007319 | 35 | #include "pinmap.h" |
mbed_official | 52:a51c77007319 | 36 | #include "error.h" |
mbed_official | 52:a51c77007319 | 37 | |
mbed_official | 167:d5744491c362 | 38 | // TIM4 cannot be used because already used by the us_ticker |
mbed_official | 52:a51c77007319 | 39 | static const PinMap PinMap_PWM[] = { |
mbed_official | 167:d5744491c362 | 40 | {PA_1, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH2 - Default |
mbed_official | 167:d5744491c362 | 41 | {PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH3 - Default (warning: not connected on D1 per default) |
mbed_official | 167:d5744491c362 | 42 | {PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 - Default (warning: not connected on D0 per default) |
mbed_official | 167:d5744491c362 | 43 | {PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 - Default |
mbed_official | 167:d5744491c362 | 44 | {PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 - Default |
mbed_official | 174:8bb9f3a33240 | 45 | // {PA_7, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH1N - GPIO_PartialRemap_TIM1 |
mbed_official | 167:d5744491c362 | 46 | {PA_8, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH1 - Default |
mbed_official | 167:d5744491c362 | 47 | {PA_9, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH2 - Default |
mbed_official | 167:d5744491c362 | 48 | {PA_10, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH3 - Default |
mbed_official | 167:d5744491c362 | 49 | {PA_11, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH4 - Default |
mbed_official | 167:d5744491c362 | 50 | {PA_15, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 8)}, // TIM2_CH1_ETR - GPIO_FullRemap_TIM2 |
mbed_official | 167:d5744491c362 | 51 | |
mbed_official | 167:d5744491c362 | 52 | {PB_0, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH3 - Default |
mbed_official | 174:8bb9f3a33240 | 53 | // {PB_0, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH2N - GPIO_PartialRemap_TIM1 |
mbed_official | 174:8bb9f3a33240 | 54 | {PB_1, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH4 - Default |
mbed_official | 174:8bb9f3a33240 | 55 | // {PB_1, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM1_CH3N - GPIO_PartialRemap_TIM1 |
mbed_official | 167:d5744491c362 | 56 | {PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 8)}, // TIM2_CH2 - GPIO_FullRemap_TIM2 |
mbed_official | 167:d5744491c362 | 57 | {PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 7)}, // TIM3_CH1 - GPIO_PartialRemap_TIM3 |
mbed_official | 167:d5744491c362 | 58 | {PB_5, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 7)}, // TIM3_CH2 - GPIO_PartialRemap_TIM3 |
mbed_official | 174:8bb9f3a33240 | 59 | // {PB_6, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH1 - Default (used by ticker) |
mbed_official | 174:8bb9f3a33240 | 60 | // {PB_7, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH2 - Default (used by ticker) |
mbed_official | 174:8bb9f3a33240 | 61 | // {PB_8, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH3 - Default (used by ticker) |
mbed_official | 174:8bb9f3a33240 | 62 | // {PB_9, PWM_4, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM4_CH4 - Default (used by ticker) |
mbed_official | 167:d5744491c362 | 63 | {PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 8)}, // TIM2_CH3 - GPIO_FullRemap_TIM2 |
mbed_official | 167:d5744491c362 | 64 | {PB_11, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 8)}, // TIM2_CH4 - GPIO_FullRemap_TIM2 |
mbed_official | 167:d5744491c362 | 65 | {PB_13, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH1N - Default |
mbed_official | 167:d5744491c362 | 66 | {PB_14, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH2N - Default |
mbed_official | 167:d5744491c362 | 67 | {PB_15, PWM_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM1_CH3N - Default |
mbed_official | 174:8bb9f3a33240 | 68 | |
mbed_official | 167:d5744491c362 | 69 | {PC_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 9)}, // TIM3_CH1 - GPIO_FullRemap_TIM3 |
mbed_official | 167:d5744491c362 | 70 | {PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 9)}, // TIM3_CH2 - GPIO_FullRemap_TIM3 |
mbed_official | 167:d5744491c362 | 71 | {PC_8, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 9)}, // TIM3_CH3 - GPIO_FullRemap_TIM3 |
mbed_official | 167:d5744491c362 | 72 | {PC_9, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 9)}, // TIM3_CH4 - GPIO_FullRemap_TIM3 |
mbed_official | 52:a51c77007319 | 73 | {NC, NC, 0} |
mbed_official | 52:a51c77007319 | 74 | }; |
mbed_official | 52:a51c77007319 | 75 | |
mbed_official | 174:8bb9f3a33240 | 76 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 52:a51c77007319 | 77 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 52:a51c77007319 | 78 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 174:8bb9f3a33240 | 79 | |
mbed_official | 52:a51c77007319 | 80 | if (obj->pwm == (PWMName)NC) { |
mbed_official | 52:a51c77007319 | 81 | error("PWM pinout mapping failed"); |
mbed_official | 52:a51c77007319 | 82 | } |
mbed_official | 174:8bb9f3a33240 | 83 | |
mbed_official | 52:a51c77007319 | 84 | // Enable TIM clock |
mbed_official | 167:d5744491c362 | 85 | if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); |
mbed_official | 52:a51c77007319 | 86 | if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); |
mbed_official | 52:a51c77007319 | 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 | 174:8bb9f3a33240 | 89 | |
mbed_official | 52:a51c77007319 | 90 | // Configure GPIO |
mbed_official | 52:a51c77007319 | 91 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 174:8bb9f3a33240 | 92 | |
mbed_official | 52:a51c77007319 | 93 | obj->pin = pin; |
mbed_official | 52:a51c77007319 | 94 | obj->period = 0; |
mbed_official | 52:a51c77007319 | 95 | obj->pulse = 0; |
mbed_official | 174:8bb9f3a33240 | 96 | |
mbed_official | 52:a51c77007319 | 97 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 52:a51c77007319 | 98 | } |
mbed_official | 52:a51c77007319 | 99 | |
mbed_official | 52:a51c77007319 | 100 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 52:a51c77007319 | 101 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 52:a51c77007319 | 102 | TIM_DeInit(tim); |
mbed_official | 52:a51c77007319 | 103 | } |
mbed_official | 52:a51c77007319 | 104 | |
mbed_official | 52:a51c77007319 | 105 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 52:a51c77007319 | 106 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 52:a51c77007319 | 107 | TIM_OCInitTypeDef TIM_OCInitStructure; |
mbed_official | 174:8bb9f3a33240 | 108 | |
mbed_official | 52:a51c77007319 | 109 | if (value < 0.0) { |
mbed_official | 52:a51c77007319 | 110 | value = 0.0; |
mbed_official | 52:a51c77007319 | 111 | } else if (value > 1.0) { |
mbed_official | 52:a51c77007319 | 112 | value = 1.0; |
mbed_official | 52:a51c77007319 | 113 | } |
mbed_official | 174:8bb9f3a33240 | 114 | |
mbed_official | 52:a51c77007319 | 115 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 174:8bb9f3a33240 | 116 | |
mbed_official | 167:d5744491c362 | 117 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
mbed_official | 167:d5744491c362 | 118 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
mbed_official | 167:d5744491c362 | 119 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 167:d5744491c362 | 120 | TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; |
mbed_official | 167:d5744491c362 | 121 | TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; |
mbed_official | 167:d5744491c362 | 122 | TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; |
mbed_official | 52:a51c77007319 | 123 | |
mbed_official | 167:d5744491c362 | 124 | // Configure channels |
mbed_official | 167:d5744491c362 | 125 | switch (obj->pin) { |
mbed_official | 167:d5744491c362 | 126 | // Channels 1 |
mbed_official | 167:d5744491c362 | 127 | case PA_6: |
mbed_official | 167:d5744491c362 | 128 | case PA_8: |
mbed_official | 167:d5744491c362 | 129 | case PA_15: |
mbed_official | 167:d5744491c362 | 130 | case PB_4: |
mbed_official | 174:8bb9f3a33240 | 131 | //case PB_6: |
mbed_official | 167:d5744491c362 | 132 | case PC_6: |
mbed_official | 167:d5744491c362 | 133 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 167:d5744491c362 | 134 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 167:d5744491c362 | 135 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 167:d5744491c362 | 136 | break; |
mbed_official | 167:d5744491c362 | 137 | // Channels 1N |
mbed_official | 174:8bb9f3a33240 | 138 | //case PA_7: |
mbed_official | 167:d5744491c362 | 139 | case PB_13: |
mbed_official | 167:d5744491c362 | 140 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 167:d5744491c362 | 141 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 167:d5744491c362 | 142 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 167:d5744491c362 | 143 | break; |
mbed_official | 167:d5744491c362 | 144 | // Channels 2 |
mbed_official | 167:d5744491c362 | 145 | case PA_1: |
mbed_official | 167:d5744491c362 | 146 | case PA_7: |
mbed_official | 167:d5744491c362 | 147 | case PA_9: |
mbed_official | 167:d5744491c362 | 148 | case PB_3: |
mbed_official | 167:d5744491c362 | 149 | case PB_5: |
mbed_official | 174:8bb9f3a33240 | 150 | //case PB_7: |
mbed_official | 167:d5744491c362 | 151 | case PC_7: |
mbed_official | 174:8bb9f3a33240 | 152 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 167:d5744491c362 | 153 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 167:d5744491c362 | 154 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 167:d5744491c362 | 155 | break; |
mbed_official | 167:d5744491c362 | 156 | // Channels 2N |
mbed_official | 174:8bb9f3a33240 | 157 | //case PB_0: |
mbed_official | 167:d5744491c362 | 158 | case PB_14: |
mbed_official | 174:8bb9f3a33240 | 159 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 167:d5744491c362 | 160 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 167:d5744491c362 | 161 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 174:8bb9f3a33240 | 162 | break; |
mbed_official | 174:8bb9f3a33240 | 163 | // Channels 3 |
mbed_official | 167:d5744491c362 | 164 | case PA_2: |
mbed_official | 167:d5744491c362 | 165 | case PA_10: |
mbed_official | 167:d5744491c362 | 166 | case PB_0: |
mbed_official | 174:8bb9f3a33240 | 167 | //case PB_8: |
mbed_official | 167:d5744491c362 | 168 | case PB_10: |
mbed_official | 167:d5744491c362 | 169 | case PC_8: |
mbed_official | 167:d5744491c362 | 170 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 167:d5744491c362 | 171 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 167:d5744491c362 | 172 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 167:d5744491c362 | 173 | break; |
mbed_official | 174:8bb9f3a33240 | 174 | // Channels 3N |
mbed_official | 174:8bb9f3a33240 | 175 | //case PB_1: |
mbed_official | 167:d5744491c362 | 176 | case PB_15: |
mbed_official | 167:d5744491c362 | 177 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 167:d5744491c362 | 178 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 167:d5744491c362 | 179 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 174:8bb9f3a33240 | 180 | break; |
mbed_official | 174:8bb9f3a33240 | 181 | // Channels 4 |
mbed_official | 167:d5744491c362 | 182 | case PA_3: |
mbed_official | 167:d5744491c362 | 183 | case PA_11: |
mbed_official | 167:d5744491c362 | 184 | case PB_1: |
mbed_official | 174:8bb9f3a33240 | 185 | //case PB_9: |
mbed_official | 167:d5744491c362 | 186 | case PB_11: |
mbed_official | 167:d5744491c362 | 187 | case PC_9: |
mbed_official | 167:d5744491c362 | 188 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 167:d5744491c362 | 189 | TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 167:d5744491c362 | 190 | TIM_OC4Init(tim, &TIM_OCInitStructure); |
mbed_official | 174:8bb9f3a33240 | 191 | break; |
mbed_official | 167:d5744491c362 | 192 | default: |
mbed_official | 167:d5744491c362 | 193 | return; |
mbed_official | 52:a51c77007319 | 194 | } |
mbed_official | 174:8bb9f3a33240 | 195 | |
mbed_official | 167:d5744491c362 | 196 | TIM_CtrlPWMOutputs(tim, ENABLE); |
mbed_official | 52:a51c77007319 | 197 | } |
mbed_official | 52:a51c77007319 | 198 | |
mbed_official | 52:a51c77007319 | 199 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 52:a51c77007319 | 200 | float value = 0; |
mbed_official | 52:a51c77007319 | 201 | if (obj->period > 0) { |
mbed_official | 52:a51c77007319 | 202 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 52:a51c77007319 | 203 | } |
mbed_official | 52:a51c77007319 | 204 | return ((value > 1.0) ? (1.0) : (value)); |
mbed_official | 52:a51c77007319 | 205 | } |
mbed_official | 52:a51c77007319 | 206 | |
mbed_official | 52:a51c77007319 | 207 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 52:a51c77007319 | 208 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 52:a51c77007319 | 209 | } |
mbed_official | 52:a51c77007319 | 210 | |
mbed_official | 52:a51c77007319 | 211 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 52:a51c77007319 | 212 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 52:a51c77007319 | 213 | } |
mbed_official | 52:a51c77007319 | 214 | |
mbed_official | 52:a51c77007319 | 215 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 52:a51c77007319 | 216 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 52:a51c77007319 | 217 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
mbed_official | 52:a51c77007319 | 218 | float dc = pwmout_read(obj); |
mbed_official | 52:a51c77007319 | 219 | |
mbed_official | 174:8bb9f3a33240 | 220 | TIM_Cmd(tim, DISABLE); |
mbed_official | 174:8bb9f3a33240 | 221 | |
mbed_official | 52:a51c77007319 | 222 | obj->period = us; |
mbed_official | 174:8bb9f3a33240 | 223 | |
mbed_official | 174:8bb9f3a33240 | 224 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
mbed_official | 174:8bb9f3a33240 | 225 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 52:a51c77007319 | 226 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
mbed_official | 174:8bb9f3a33240 | 227 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
mbed_official | 52:a51c77007319 | 228 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
mbed_official | 52:a51c77007319 | 229 | |
mbed_official | 52:a51c77007319 | 230 | // Set duty cycle again |
mbed_official | 52:a51c77007319 | 231 | pwmout_write(obj, dc); |
mbed_official | 174:8bb9f3a33240 | 232 | |
mbed_official | 174:8bb9f3a33240 | 233 | TIM_ARRPreloadConfig(tim, ENABLE); |
mbed_official | 52:a51c77007319 | 234 | TIM_Cmd(tim, ENABLE); |
mbed_official | 52:a51c77007319 | 235 | } |
mbed_official | 52:a51c77007319 | 236 | |
mbed_official | 52:a51c77007319 | 237 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 52:a51c77007319 | 238 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 52:a51c77007319 | 239 | } |
mbed_official | 52:a51c77007319 | 240 | |
mbed_official | 52:a51c77007319 | 241 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 52:a51c77007319 | 242 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 52:a51c77007319 | 243 | } |
mbed_official | 52:a51c77007319 | 244 | |
mbed_official | 52:a51c77007319 | 245 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 52:a51c77007319 | 246 | float value = (float)us / (float)obj->period; |
mbed_official | 52:a51c77007319 | 247 | pwmout_write(obj, value); |
mbed_official | 52:a51c77007319 | 248 | } |
mbed_official | 174:8bb9f3a33240 | 249 | |
mbed_official | 174:8bb9f3a33240 | 250 | #endif |