mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/pwmout_api.c@216:577900467c9e, 2014-05-27 (annotated)
- Committer:
- mbed_official
- Date:
- Tue May 27 10:00:08 2014 +0100
- Revision:
- 216:577900467c9e
- Parent:
- 162:937d965048d3
- Child:
- 250:a49055e7a707
Synchronized with git revision 1cdfe81b13a631cf282176dd176df20a6ebbc297
Full URL: https://github.com/mbedmicro/mbed/commit/1cdfe81b13a631cf282176dd176df20a6ebbc297/
[NUCLEO_xxx] Update xxx_free() functions + typo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 125:23cc3068a9e4 | 1 | /* mbed Microcontroller Library |
mbed_official | 125:23cc3068a9e4 | 2 | ******************************************************************************* |
mbed_official | 125:23cc3068a9e4 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 125:23cc3068a9e4 | 4 | * All rights reserved. |
mbed_official | 125:23cc3068a9e4 | 5 | * |
mbed_official | 125:23cc3068a9e4 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 125:23cc3068a9e4 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 125:23cc3068a9e4 | 8 | * |
mbed_official | 125:23cc3068a9e4 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 125:23cc3068a9e4 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 125:23cc3068a9e4 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 125:23cc3068a9e4 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 125:23cc3068a9e4 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 125:23cc3068a9e4 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 125:23cc3068a9e4 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 125:23cc3068a9e4 | 16 | * without specific prior written permission. |
mbed_official | 125:23cc3068a9e4 | 17 | * |
mbed_official | 125:23cc3068a9e4 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 125:23cc3068a9e4 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 125:23cc3068a9e4 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 125:23cc3068a9e4 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 125:23cc3068a9e4 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 125:23cc3068a9e4 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 125:23cc3068a9e4 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 125:23cc3068a9e4 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 125:23cc3068a9e4 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 125:23cc3068a9e4 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 125:23cc3068a9e4 | 28 | ******************************************************************************* |
mbed_official | 125:23cc3068a9e4 | 29 | */ |
mbed_official | 125:23cc3068a9e4 | 30 | #include "pwmout_api.h" |
mbed_official | 125:23cc3068a9e4 | 31 | |
mbed_official | 162:937d965048d3 | 32 | #if DEVICE_PWMOUT |
mbed_official | 162:937d965048d3 | 33 | |
mbed_official | 125:23cc3068a9e4 | 34 | #include "cmsis.h" |
mbed_official | 125:23cc3068a9e4 | 35 | #include "pinmap.h" |
mbed_official | 125:23cc3068a9e4 | 36 | #include "error.h" |
mbed_official | 125:23cc3068a9e4 | 37 | |
mbed_official | 125:23cc3068a9e4 | 38 | // TIM2 cannot be used because already used by the us_ticker |
mbed_official | 125:23cc3068a9e4 | 39 | static const PinMap PinMap_PWM[] = { |
mbed_official | 135:067cc8ba23da | 40 | // {PA_0, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM2_CH1 |
mbed_official | 135:067cc8ba23da | 41 | // {PA_1, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM2_CH2 |
mbed_official | 125:23cc3068a9e4 | 42 | {PA_1, PWM_15, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_9)}, // TIM15_CH1N |
mbed_official | 125:23cc3068a9e4 | 43 | {PA_2, PWM_15, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_9)}, // TIM15_CH1 |
mbed_official | 125:23cc3068a9e4 | 44 | {PA_3, PWM_15, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_9)}, // TIM15_CH2 |
mbed_official | 135:067cc8ba23da | 45 | // {PA_5, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM2_CH1 |
mbed_official | 125:23cc3068a9e4 | 46 | {PA_6, PWM_16, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM16_CH1 |
mbed_official | 125:23cc3068a9e4 | 47 | {PA_7, PWM_17, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM17_CH1 |
mbed_official | 135:067cc8ba23da | 48 | // {PA_7, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH1N |
mbed_official | 125:23cc3068a9e4 | 49 | {PA_8, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH1 |
mbed_official | 125:23cc3068a9e4 | 50 | {PA_9, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH2 |
mbed_official | 135:067cc8ba23da | 51 | // {PA_9, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_10)}, // TIM2_CH3 |
mbed_official | 125:23cc3068a9e4 | 52 | {PA_10, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH3 |
mbed_official | 135:067cc8ba23da | 53 | // {PA_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_10)}, // TIM2_CH4 |
mbed_official | 125:23cc3068a9e4 | 54 | {PA_11, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_11)}, // TIM1_CH4 |
mbed_official | 135:067cc8ba23da | 55 | // {PA_11, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH1N |
mbed_official | 125:23cc3068a9e4 | 56 | {PA_12, PWM_16, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM16_CH1 |
mbed_official | 135:067cc8ba23da | 57 | // {PA_12, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH2N |
mbed_official | 125:23cc3068a9e4 | 58 | {PA_13, PWM_16, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM16_CH1N |
mbed_official | 135:067cc8ba23da | 59 | // {PA_15, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM2_CH1 |
mbed_official | 135:067cc8ba23da | 60 | |
mbed_official | 125:23cc3068a9e4 | 61 | {PB_0, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH2N |
mbed_official | 125:23cc3068a9e4 | 62 | {PB_1, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH3N |
mbed_official | 135:067cc8ba23da | 63 | // {PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM2_CH2 |
mbed_official | 125:23cc3068a9e4 | 64 | {PB_4, PWM_16, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM16_CH1 |
mbed_official | 125:23cc3068a9e4 | 65 | {PB_5, PWM_17, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_10)}, // TIM17_CH1 |
mbed_official | 125:23cc3068a9e4 | 66 | {PB_6, PWM_16, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM16_CH1N |
mbed_official | 125:23cc3068a9e4 | 67 | {PB_7, PWM_17, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM17_CH1N |
mbed_official | 125:23cc3068a9e4 | 68 | {PB_8, PWM_16, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM16_CH1 |
mbed_official | 125:23cc3068a9e4 | 69 | {PB_9, PWM_17, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM17_CH1 |
mbed_official | 135:067cc8ba23da | 70 | // {PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM2_CH3 |
mbed_official | 135:067cc8ba23da | 71 | // {PB_11, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM2_CH4 |
mbed_official | 125:23cc3068a9e4 | 72 | {PB_13, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH1N |
mbed_official | 125:23cc3068a9e4 | 73 | {PB_14, PWM_15, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM15_CH1 |
mbed_official | 135:067cc8ba23da | 74 | // {PB_14, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH2N |
mbed_official | 125:23cc3068a9e4 | 75 | {PB_15, PWM_15, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)}, // TIM15_CH2 |
mbed_official | 135:067cc8ba23da | 76 | // {PB_15, PWM_15, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_2)}, // TIM15_CH1N |
mbed_official | 135:067cc8ba23da | 77 | // {PB_15, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_4)}, // TIM1_CH3N |
mbed_official | 135:067cc8ba23da | 78 | |
mbed_official | 125:23cc3068a9e4 | 79 | {PC_0, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_2)}, // TIM1_CH1 |
mbed_official | 125:23cc3068a9e4 | 80 | {PC_1, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_2)}, // TIM1_CH2 |
mbed_official | 125:23cc3068a9e4 | 81 | {PC_2, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_2)}, // TIM1_CH3 |
mbed_official | 125:23cc3068a9e4 | 82 | {PC_3, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_2)}, // TIM1_CH4 |
mbed_official | 125:23cc3068a9e4 | 83 | {PC_13, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_4)}, // TIM1_CH1N |
mbed_official | 135:067cc8ba23da | 84 | |
mbed_official | 125:23cc3068a9e4 | 85 | {PF_0, PWM_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_6)}, // TIM1_CH3N |
mbed_official | 135:067cc8ba23da | 86 | |
mbed_official | 125:23cc3068a9e4 | 87 | {NC, NC, 0} |
mbed_official | 125:23cc3068a9e4 | 88 | }; |
mbed_official | 125:23cc3068a9e4 | 89 | |
mbed_official | 135:067cc8ba23da | 90 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 125:23cc3068a9e4 | 91 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 125:23cc3068a9e4 | 92 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 135:067cc8ba23da | 93 | |
mbed_official | 125:23cc3068a9e4 | 94 | if (obj->pwm == (PWMName)NC) { |
mbed_official | 125:23cc3068a9e4 | 95 | error("PWM pinout mapping failed"); |
mbed_official | 125:23cc3068a9e4 | 96 | } |
mbed_official | 135:067cc8ba23da | 97 | |
mbed_official | 125:23cc3068a9e4 | 98 | // Enable TIM clock |
mbed_official | 125:23cc3068a9e4 | 99 | if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 100 | if (obj->pwm == PWM_15) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 101 | if (obj->pwm == PWM_16) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 102 | if (obj->pwm == PWM_17) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE); |
mbed_official | 135:067cc8ba23da | 103 | |
mbed_official | 125:23cc3068a9e4 | 104 | // Configure GPIO |
mbed_official | 125:23cc3068a9e4 | 105 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 135:067cc8ba23da | 106 | |
mbed_official | 125:23cc3068a9e4 | 107 | obj->pin = pin; |
mbed_official | 125:23cc3068a9e4 | 108 | obj->period = 0; |
mbed_official | 125:23cc3068a9e4 | 109 | obj->pulse = 0; |
mbed_official | 135:067cc8ba23da | 110 | |
mbed_official | 125:23cc3068a9e4 | 111 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 125:23cc3068a9e4 | 112 | } |
mbed_official | 125:23cc3068a9e4 | 113 | |
mbed_official | 125:23cc3068a9e4 | 114 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 216:577900467c9e | 115 | // Configure GPIO |
mbed_official | 216:577900467c9e | 116 | pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); |
mbed_official | 125:23cc3068a9e4 | 117 | } |
mbed_official | 125:23cc3068a9e4 | 118 | |
mbed_official | 125:23cc3068a9e4 | 119 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 125:23cc3068a9e4 | 120 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 125:23cc3068a9e4 | 121 | TIM_OCInitTypeDef TIM_OCInitStructure; |
mbed_official | 135:067cc8ba23da | 122 | |
mbed_official | 125:23cc3068a9e4 | 123 | if (value < (float)0.0) { |
mbed_official | 125:23cc3068a9e4 | 124 | value = (float)0.0; |
mbed_official | 125:23cc3068a9e4 | 125 | } else if (value > (float)1.0) { |
mbed_official | 125:23cc3068a9e4 | 126 | value = (float)1.0; |
mbed_official | 125:23cc3068a9e4 | 127 | } |
mbed_official | 135:067cc8ba23da | 128 | |
mbed_official | 125:23cc3068a9e4 | 129 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 125:23cc3068a9e4 | 130 | |
mbed_official | 135:067cc8ba23da | 131 | // Configure channels |
mbed_official | 125:23cc3068a9e4 | 132 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
mbed_official | 125:23cc3068a9e4 | 133 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
mbed_official | 125:23cc3068a9e4 | 134 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 125:23cc3068a9e4 | 135 | TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; |
mbed_official | 125:23cc3068a9e4 | 136 | TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; |
mbed_official | 125:23cc3068a9e4 | 137 | TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; |
mbed_official | 135:067cc8ba23da | 138 | |
mbed_official | 125:23cc3068a9e4 | 139 | switch (obj->pin) { |
mbed_official | 125:23cc3068a9e4 | 140 | // Channels 1 |
mbed_official | 135:067cc8ba23da | 141 | // case PA_0: |
mbed_official | 125:23cc3068a9e4 | 142 | case PA_2: |
mbed_official | 135:067cc8ba23da | 143 | // case PA_5: |
mbed_official | 125:23cc3068a9e4 | 144 | case PA_6: |
mbed_official | 125:23cc3068a9e4 | 145 | case PA_7: |
mbed_official | 125:23cc3068a9e4 | 146 | case PA_8: |
mbed_official | 125:23cc3068a9e4 | 147 | case PA_12: |
mbed_official | 135:067cc8ba23da | 148 | // case PA_15: |
mbed_official | 125:23cc3068a9e4 | 149 | case PB_4: |
mbed_official | 125:23cc3068a9e4 | 150 | case PB_5: |
mbed_official | 125:23cc3068a9e4 | 151 | case PB_8: |
mbed_official | 125:23cc3068a9e4 | 152 | case PB_9: |
mbed_official | 125:23cc3068a9e4 | 153 | case PB_14: |
mbed_official | 125:23cc3068a9e4 | 154 | case PC_0: |
mbed_official | 125:23cc3068a9e4 | 155 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 125:23cc3068a9e4 | 156 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 125:23cc3068a9e4 | 157 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 125:23cc3068a9e4 | 158 | break; |
mbed_official | 125:23cc3068a9e4 | 159 | // Channels 1N |
mbed_official | 125:23cc3068a9e4 | 160 | case PA_1: |
mbed_official | 135:067cc8ba23da | 161 | // case PA_7: |
mbed_official | 135:067cc8ba23da | 162 | // case PA_11: |
mbed_official | 125:23cc3068a9e4 | 163 | case PA_13: |
mbed_official | 125:23cc3068a9e4 | 164 | case PB_6: |
mbed_official | 125:23cc3068a9e4 | 165 | case PB_7: |
mbed_official | 125:23cc3068a9e4 | 166 | case PB_13: |
mbed_official | 135:067cc8ba23da | 167 | // case PB_15: |
mbed_official | 125:23cc3068a9e4 | 168 | case PC_13: |
mbed_official | 125:23cc3068a9e4 | 169 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 125:23cc3068a9e4 | 170 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 125:23cc3068a9e4 | 171 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 125:23cc3068a9e4 | 172 | break; |
mbed_official | 125:23cc3068a9e4 | 173 | // Channels 2 |
mbed_official | 135:067cc8ba23da | 174 | // case PA_1: |
mbed_official | 125:23cc3068a9e4 | 175 | case PA_3: |
mbed_official | 125:23cc3068a9e4 | 176 | case PA_9: |
mbed_official | 135:067cc8ba23da | 177 | // case PB_3: |
mbed_official | 125:23cc3068a9e4 | 178 | case PB_15: |
mbed_official | 125:23cc3068a9e4 | 179 | case PC_1: |
mbed_official | 125:23cc3068a9e4 | 180 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 125:23cc3068a9e4 | 181 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 125:23cc3068a9e4 | 182 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 125:23cc3068a9e4 | 183 | break; |
mbed_official | 125:23cc3068a9e4 | 184 | // Channels 2N |
mbed_official | 135:067cc8ba23da | 185 | // case PA_12: |
mbed_official | 125:23cc3068a9e4 | 186 | case PB_0: |
mbed_official | 135:067cc8ba23da | 187 | // case PB_14: |
mbed_official | 125:23cc3068a9e4 | 188 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 125:23cc3068a9e4 | 189 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 125:23cc3068a9e4 | 190 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 125:23cc3068a9e4 | 191 | break; |
mbed_official | 125:23cc3068a9e4 | 192 | // Channels 3 |
mbed_official | 135:067cc8ba23da | 193 | // case PA_9: |
mbed_official | 125:23cc3068a9e4 | 194 | case PA_10: |
mbed_official | 135:067cc8ba23da | 195 | // case PB_10: |
mbed_official | 125:23cc3068a9e4 | 196 | case PC_2: |
mbed_official | 125:23cc3068a9e4 | 197 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 125:23cc3068a9e4 | 198 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 125:23cc3068a9e4 | 199 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 125:23cc3068a9e4 | 200 | break; |
mbed_official | 125:23cc3068a9e4 | 201 | // Channels 3N |
mbed_official | 125:23cc3068a9e4 | 202 | case PB_1: |
mbed_official | 125:23cc3068a9e4 | 203 | case PF_0: |
mbed_official | 135:067cc8ba23da | 204 | // case PB_15: |
mbed_official | 125:23cc3068a9e4 | 205 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 125:23cc3068a9e4 | 206 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 125:23cc3068a9e4 | 207 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 125:23cc3068a9e4 | 208 | break; |
mbed_official | 125:23cc3068a9e4 | 209 | // Channels 4 |
mbed_official | 135:067cc8ba23da | 210 | // case PA_10: |
mbed_official | 125:23cc3068a9e4 | 211 | case PA_11: |
mbed_official | 135:067cc8ba23da | 212 | // case PB_11: |
mbed_official | 125:23cc3068a9e4 | 213 | case PC_3: |
mbed_official | 125:23cc3068a9e4 | 214 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 125:23cc3068a9e4 | 215 | TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 125:23cc3068a9e4 | 216 | TIM_OC4Init(tim, &TIM_OCInitStructure); |
mbed_official | 125:23cc3068a9e4 | 217 | break; |
mbed_official | 125:23cc3068a9e4 | 218 | default: |
mbed_official | 125:23cc3068a9e4 | 219 | return; |
mbed_official | 135:067cc8ba23da | 220 | } |
mbed_official | 125:23cc3068a9e4 | 221 | } |
mbed_official | 125:23cc3068a9e4 | 222 | |
mbed_official | 125:23cc3068a9e4 | 223 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 125:23cc3068a9e4 | 224 | float value = 0; |
mbed_official | 125:23cc3068a9e4 | 225 | if (obj->period > 0) { |
mbed_official | 125:23cc3068a9e4 | 226 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 125:23cc3068a9e4 | 227 | } |
mbed_official | 125:23cc3068a9e4 | 228 | return ((value > (float)1.0) ? ((float)1.0) : (value)); |
mbed_official | 125:23cc3068a9e4 | 229 | } |
mbed_official | 125:23cc3068a9e4 | 230 | |
mbed_official | 125:23cc3068a9e4 | 231 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 125:23cc3068a9e4 | 232 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 125:23cc3068a9e4 | 233 | } |
mbed_official | 125:23cc3068a9e4 | 234 | |
mbed_official | 125:23cc3068a9e4 | 235 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 125:23cc3068a9e4 | 236 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 125:23cc3068a9e4 | 237 | } |
mbed_official | 125:23cc3068a9e4 | 238 | |
mbed_official | 125:23cc3068a9e4 | 239 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 125:23cc3068a9e4 | 240 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 125:23cc3068a9e4 | 241 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
mbed_official | 125:23cc3068a9e4 | 242 | float dc = pwmout_read(obj); |
mbed_official | 125:23cc3068a9e4 | 243 | |
mbed_official | 135:067cc8ba23da | 244 | TIM_Cmd(tim, DISABLE); |
mbed_official | 135:067cc8ba23da | 245 | |
mbed_official | 125:23cc3068a9e4 | 246 | obj->period = us; |
mbed_official | 135:067cc8ba23da | 247 | |
mbed_official | 125:23cc3068a9e4 | 248 | TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); |
mbed_official | 125:23cc3068a9e4 | 249 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
mbed_official | 125:23cc3068a9e4 | 250 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 125:23cc3068a9e4 | 251 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
mbed_official | 125:23cc3068a9e4 | 252 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
mbed_official | 125:23cc3068a9e4 | 253 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
mbed_official | 125:23cc3068a9e4 | 254 | |
mbed_official | 125:23cc3068a9e4 | 255 | // Set duty cycle again |
mbed_official | 125:23cc3068a9e4 | 256 | pwmout_write(obj, dc); |
mbed_official | 135:067cc8ba23da | 257 | |
mbed_official | 125:23cc3068a9e4 | 258 | TIM_ARRPreloadConfig(tim, ENABLE); |
mbed_official | 135:067cc8ba23da | 259 | |
mbed_official | 125:23cc3068a9e4 | 260 | // Warning: Main Output must be enabled on TIM1, TIM8, TIM5, TIM6 and TIM17 |
mbed_official | 125:23cc3068a9e4 | 261 | if ((obj->pwm == PWM_1) || (obj->pwm == PWM_15) || (obj->pwm == PWM_16) || (obj->pwm == PWM_17)) { |
mbed_official | 125:23cc3068a9e4 | 262 | TIM_CtrlPWMOutputs(tim, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 263 | } |
mbed_official | 135:067cc8ba23da | 264 | |
mbed_official | 125:23cc3068a9e4 | 265 | TIM_Cmd(tim, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 266 | } |
mbed_official | 125:23cc3068a9e4 | 267 | |
mbed_official | 125:23cc3068a9e4 | 268 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 125:23cc3068a9e4 | 269 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 125:23cc3068a9e4 | 270 | } |
mbed_official | 125:23cc3068a9e4 | 271 | |
mbed_official | 125:23cc3068a9e4 | 272 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 125:23cc3068a9e4 | 273 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 125:23cc3068a9e4 | 274 | } |
mbed_official | 125:23cc3068a9e4 | 275 | |
mbed_official | 125:23cc3068a9e4 | 276 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 125:23cc3068a9e4 | 277 | float value = (float)us / (float)obj->period; |
mbed_official | 125:23cc3068a9e4 | 278 | pwmout_write(obj, value); |
mbed_official | 125:23cc3068a9e4 | 279 | } |
mbed_official | 162:937d965048d3 | 280 | |
mbed_official | 162:937d965048d3 | 281 | #endif |