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