mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_DISCO_F303VC/pwmout_api.c@251:de9a1e4ffd79, 2014-07-08 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jul 08 14:15:07 2014 +0100
- Revision:
- 251:de9a1e4ffd79
- Parent:
- 250:a49055e7a707
- Child:
- 285:31249416b6f9
Synchronized with git revision b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63
Full URL: https://github.com/mbedmicro/mbed/commit/b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63/
Revert "error.h -> mbed_error.h"
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 | 157:90e3acc479a2 | 30 | #include "pwmout_api.h" |
mbed_official | 157:90e3acc479a2 | 31 | |
mbed_official | 241:ffe41b0c8126 | 32 | #if DEVICE_PWMOUT |
mbed_official | 241:ffe41b0c8126 | 33 | |
mbed_official | 157:90e3acc479a2 | 34 | #include "cmsis.h" |
mbed_official | 157:90e3acc479a2 | 35 | #include "pinmap.h" |
mbed_official | 251:de9a1e4ffd79 | 36 | #include "error.h" |
mbed_official | 157:90e3acc479a2 | 37 | |
mbed_official | 157:90e3acc479a2 | 38 | // TIM2 cannot be used because already used by the us_ticker |
mbed_official | 157:90e3acc479a2 | 39 | static const PinMap PinMap_PWM[] = { |
mbed_official | 157:90e3acc479a2 | 40 | // {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 | 41 | // {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 | 42 | {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 | 43 | {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 | 44 | {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 | 45 | // {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 | 46 | {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 | 47 | {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 | 48 | // {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 | 49 | {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 | 50 | {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 | 51 | // {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 | 52 | {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 | 53 | // {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 | 54 | {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 | 55 | // {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 | 56 | {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 | 57 | // {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 | 58 | {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 | 59 | // {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 | 60 | |
mbed_official | 157:90e3acc479a2 | 61 | {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 | 62 | {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 | 63 | // {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 | 64 | {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 | 65 | {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 | 66 | {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 | 67 | {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 | 68 | {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 | 69 | {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 | 70 | // {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 | 71 | // {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 | 72 | {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 | 73 | {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 | 74 | // {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 | 75 | {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 | 76 | // {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 | 77 | // {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 | 78 | |
mbed_official | 157:90e3acc479a2 | 79 | {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 | 80 | {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 | 81 | {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 | 82 | {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 | 83 | {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 | 84 | |
mbed_official | 157:90e3acc479a2 | 85 | {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 | 86 | |
mbed_official | 157:90e3acc479a2 | 87 | {NC, NC, 0} |
mbed_official | 157:90e3acc479a2 | 88 | }; |
mbed_official | 157:90e3acc479a2 | 89 | |
mbed_official | 227:7bd0639b8911 | 90 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 157:90e3acc479a2 | 91 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 157:90e3acc479a2 | 92 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 227:7bd0639b8911 | 93 | MBED_ASSERT(obj->pwm != (PWMName)NC); |
mbed_official | 157:90e3acc479a2 | 94 | |
mbed_official | 157:90e3acc479a2 | 95 | // Enable TIM clock |
mbed_official | 157:90e3acc479a2 | 96 | if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); |
mbed_official | 157:90e3acc479a2 | 97 | if (obj->pwm == PWM_15) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE); |
mbed_official | 157:90e3acc479a2 | 98 | if (obj->pwm == PWM_16) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE); |
mbed_official | 157:90e3acc479a2 | 99 | if (obj->pwm == PWM_17) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE); |
mbed_official | 157:90e3acc479a2 | 100 | |
mbed_official | 157:90e3acc479a2 | 101 | // Configure GPIO |
mbed_official | 157:90e3acc479a2 | 102 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 157:90e3acc479a2 | 103 | |
mbed_official | 157:90e3acc479a2 | 104 | obj->pin = pin; |
mbed_official | 157:90e3acc479a2 | 105 | obj->period = 0; |
mbed_official | 157:90e3acc479a2 | 106 | obj->pulse = 0; |
mbed_official | 157:90e3acc479a2 | 107 | |
mbed_official | 157:90e3acc479a2 | 108 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 157:90e3acc479a2 | 109 | } |
mbed_official | 157:90e3acc479a2 | 110 | |
mbed_official | 157:90e3acc479a2 | 111 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 157:90e3acc479a2 | 112 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 157:90e3acc479a2 | 113 | TIM_DeInit(tim); |
mbed_official | 157:90e3acc479a2 | 114 | } |
mbed_official | 157:90e3acc479a2 | 115 | |
mbed_official | 157:90e3acc479a2 | 116 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 157:90e3acc479a2 | 117 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 157:90e3acc479a2 | 118 | TIM_OCInitTypeDef TIM_OCInitStructure; |
mbed_official | 157:90e3acc479a2 | 119 | |
mbed_official | 157:90e3acc479a2 | 120 | if (value < (float)0.0) { |
mbed_official | 157:90e3acc479a2 | 121 | value = (float)0.0; |
mbed_official | 157:90e3acc479a2 | 122 | } else if (value > (float)1.0) { |
mbed_official | 157:90e3acc479a2 | 123 | value = (float)1.0; |
mbed_official | 157:90e3acc479a2 | 124 | } |
mbed_official | 157:90e3acc479a2 | 125 | |
mbed_official | 157:90e3acc479a2 | 126 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 157:90e3acc479a2 | 127 | |
mbed_official | 227:7bd0639b8911 | 128 | // Configure channels |
mbed_official | 157:90e3acc479a2 | 129 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
mbed_official | 157:90e3acc479a2 | 130 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
mbed_official | 157:90e3acc479a2 | 131 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 157:90e3acc479a2 | 132 | TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; |
mbed_official | 157:90e3acc479a2 | 133 | TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; |
mbed_official | 157:90e3acc479a2 | 134 | TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; |
mbed_official | 157:90e3acc479a2 | 135 | |
mbed_official | 157:90e3acc479a2 | 136 | switch (obj->pin) { |
mbed_official | 157:90e3acc479a2 | 137 | // Channels 1 |
mbed_official | 157:90e3acc479a2 | 138 | // case PA_0: |
mbed_official | 157:90e3acc479a2 | 139 | case PA_2: |
mbed_official | 157:90e3acc479a2 | 140 | // case PA_5: |
mbed_official | 157:90e3acc479a2 | 141 | case PA_6: |
mbed_official | 157:90e3acc479a2 | 142 | case PA_7: |
mbed_official | 157:90e3acc479a2 | 143 | case PA_8: |
mbed_official | 157:90e3acc479a2 | 144 | case PA_12: |
mbed_official | 157:90e3acc479a2 | 145 | // case PA_15: |
mbed_official | 157:90e3acc479a2 | 146 | case PB_4: |
mbed_official | 157:90e3acc479a2 | 147 | case PB_5: |
mbed_official | 157:90e3acc479a2 | 148 | case PB_8: |
mbed_official | 157:90e3acc479a2 | 149 | case PB_9: |
mbed_official | 157:90e3acc479a2 | 150 | case PB_14: |
mbed_official | 157:90e3acc479a2 | 151 | case PC_0: |
mbed_official | 157:90e3acc479a2 | 152 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 157:90e3acc479a2 | 153 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 157:90e3acc479a2 | 154 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 157:90e3acc479a2 | 155 | break; |
mbed_official | 157:90e3acc479a2 | 156 | // Channels 1N |
mbed_official | 157:90e3acc479a2 | 157 | case PA_1: |
mbed_official | 157:90e3acc479a2 | 158 | // case PA_7: |
mbed_official | 157:90e3acc479a2 | 159 | // case PA_11: |
mbed_official | 157:90e3acc479a2 | 160 | case PA_13: |
mbed_official | 157:90e3acc479a2 | 161 | case PB_6: |
mbed_official | 157:90e3acc479a2 | 162 | case PB_7: |
mbed_official | 157:90e3acc479a2 | 163 | case PB_13: |
mbed_official | 157:90e3acc479a2 | 164 | // case PB_15: |
mbed_official | 157:90e3acc479a2 | 165 | case PC_13: |
mbed_official | 157:90e3acc479a2 | 166 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 157:90e3acc479a2 | 167 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 157:90e3acc479a2 | 168 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 157:90e3acc479a2 | 169 | break; |
mbed_official | 157:90e3acc479a2 | 170 | // Channels 2 |
mbed_official | 157:90e3acc479a2 | 171 | // case PA_1: |
mbed_official | 157:90e3acc479a2 | 172 | case PA_3: |
mbed_official | 157:90e3acc479a2 | 173 | case PA_9: |
mbed_official | 157:90e3acc479a2 | 174 | // case PB_3: |
mbed_official | 157:90e3acc479a2 | 175 | case PB_15: |
mbed_official | 157:90e3acc479a2 | 176 | case PC_1: |
mbed_official | 157:90e3acc479a2 | 177 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 157:90e3acc479a2 | 178 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 157:90e3acc479a2 | 179 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 157:90e3acc479a2 | 180 | break; |
mbed_official | 157:90e3acc479a2 | 181 | // Channels 2N |
mbed_official | 157:90e3acc479a2 | 182 | // case PA_12: |
mbed_official | 157:90e3acc479a2 | 183 | case PB_0: |
mbed_official | 157:90e3acc479a2 | 184 | // case PB_14: |
mbed_official | 157:90e3acc479a2 | 185 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 157:90e3acc479a2 | 186 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 157:90e3acc479a2 | 187 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 157:90e3acc479a2 | 188 | break; |
mbed_official | 157:90e3acc479a2 | 189 | // Channels 3 |
mbed_official | 157:90e3acc479a2 | 190 | // case PA_9: |
mbed_official | 157:90e3acc479a2 | 191 | case PA_10: |
mbed_official | 157:90e3acc479a2 | 192 | // case PB_10: |
mbed_official | 157:90e3acc479a2 | 193 | case PC_2: |
mbed_official | 157:90e3acc479a2 | 194 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 157:90e3acc479a2 | 195 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 157:90e3acc479a2 | 196 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 157:90e3acc479a2 | 197 | break; |
mbed_official | 157:90e3acc479a2 | 198 | // Channels 3N |
mbed_official | 157:90e3acc479a2 | 199 | case PB_1: |
mbed_official | 157:90e3acc479a2 | 200 | case PF_0: |
mbed_official | 157:90e3acc479a2 | 201 | // case PB_15: |
mbed_official | 157:90e3acc479a2 | 202 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 157:90e3acc479a2 | 203 | TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 157:90e3acc479a2 | 204 | TIM_OC3Init(tim, &TIM_OCInitStructure); |
mbed_official | 157:90e3acc479a2 | 205 | break; |
mbed_official | 157:90e3acc479a2 | 206 | // Channels 4 |
mbed_official | 157:90e3acc479a2 | 207 | // case PA_10: |
mbed_official | 157:90e3acc479a2 | 208 | case PA_11: |
mbed_official | 157:90e3acc479a2 | 209 | // case PB_11: |
mbed_official | 157:90e3acc479a2 | 210 | case PC_3: |
mbed_official | 157:90e3acc479a2 | 211 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 157:90e3acc479a2 | 212 | TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 157:90e3acc479a2 | 213 | TIM_OC4Init(tim, &TIM_OCInitStructure); |
mbed_official | 157:90e3acc479a2 | 214 | break; |
mbed_official | 157:90e3acc479a2 | 215 | default: |
mbed_official | 157:90e3acc479a2 | 216 | return; |
mbed_official | 227:7bd0639b8911 | 217 | } |
mbed_official | 157:90e3acc479a2 | 218 | } |
mbed_official | 157:90e3acc479a2 | 219 | |
mbed_official | 157:90e3acc479a2 | 220 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 157:90e3acc479a2 | 221 | float value = 0; |
mbed_official | 157:90e3acc479a2 | 222 | if (obj->period > 0) { |
mbed_official | 157:90e3acc479a2 | 223 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 157:90e3acc479a2 | 224 | } |
mbed_official | 157:90e3acc479a2 | 225 | return ((value > (float)1.0) ? ((float)1.0) : (value)); |
mbed_official | 157:90e3acc479a2 | 226 | } |
mbed_official | 157:90e3acc479a2 | 227 | |
mbed_official | 157:90e3acc479a2 | 228 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 157:90e3acc479a2 | 229 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 157:90e3acc479a2 | 230 | } |
mbed_official | 157:90e3acc479a2 | 231 | |
mbed_official | 157:90e3acc479a2 | 232 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 157:90e3acc479a2 | 233 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 157:90e3acc479a2 | 234 | } |
mbed_official | 157:90e3acc479a2 | 235 | |
mbed_official | 157:90e3acc479a2 | 236 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 157:90e3acc479a2 | 237 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 157:90e3acc479a2 | 238 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
mbed_official | 157:90e3acc479a2 | 239 | float dc = pwmout_read(obj); |
mbed_official | 157:90e3acc479a2 | 240 | |
mbed_official | 227:7bd0639b8911 | 241 | TIM_Cmd(tim, DISABLE); |
mbed_official | 157:90e3acc479a2 | 242 | |
mbed_official | 157:90e3acc479a2 | 243 | obj->period = us; |
mbed_official | 157:90e3acc479a2 | 244 | |
mbed_official | 157:90e3acc479a2 | 245 | TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); |
mbed_official | 157:90e3acc479a2 | 246 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
mbed_official | 157:90e3acc479a2 | 247 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 157:90e3acc479a2 | 248 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
mbed_official | 157:90e3acc479a2 | 249 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
mbed_official | 157:90e3acc479a2 | 250 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
mbed_official | 157:90e3acc479a2 | 251 | |
mbed_official | 157:90e3acc479a2 | 252 | // Set duty cycle again |
mbed_official | 157:90e3acc479a2 | 253 | pwmout_write(obj, dc); |
mbed_official | 157:90e3acc479a2 | 254 | |
mbed_official | 157:90e3acc479a2 | 255 | TIM_ARRPreloadConfig(tim, ENABLE); |
mbed_official | 157:90e3acc479a2 | 256 | |
mbed_official | 157:90e3acc479a2 | 257 | // Warning: Main Output must be enabled on TIM1, TIM8, TIM5, TIM6 and TIM17 |
mbed_official | 157:90e3acc479a2 | 258 | if ((obj->pwm == PWM_1) || (obj->pwm == PWM_15) || (obj->pwm == PWM_16) || (obj->pwm == PWM_17)) { |
mbed_official | 157:90e3acc479a2 | 259 | TIM_CtrlPWMOutputs(tim, ENABLE); |
mbed_official | 157:90e3acc479a2 | 260 | } |
mbed_official | 157:90e3acc479a2 | 261 | |
mbed_official | 157:90e3acc479a2 | 262 | TIM_Cmd(tim, ENABLE); |
mbed_official | 157:90e3acc479a2 | 263 | } |
mbed_official | 157:90e3acc479a2 | 264 | |
mbed_official | 157:90e3acc479a2 | 265 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 157:90e3acc479a2 | 266 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 157:90e3acc479a2 | 267 | } |
mbed_official | 157:90e3acc479a2 | 268 | |
mbed_official | 157:90e3acc479a2 | 269 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 157:90e3acc479a2 | 270 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 157:90e3acc479a2 | 271 | } |
mbed_official | 157:90e3acc479a2 | 272 | |
mbed_official | 157:90e3acc479a2 | 273 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 157:90e3acc479a2 | 274 | float value = (float)us / (float)obj->period; |
mbed_official | 157:90e3acc479a2 | 275 | pwmout_write(obj, value); |
mbed_official | 157:90e3acc479a2 | 276 | } |
mbed_official | 241:ffe41b0c8126 | 277 | |
mbed_official | 241:ffe41b0c8126 | 278 | #endif |