mbed library sources

Dependents:   bare

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Mar 19 10:15:22 2014 +0000
Revision:
125:23cc3068a9e4
Synchronized with git revision ace35dfba3748c7cdc102eb38ec6b9e1067c3252

Full URL: https://github.com/mbedmicro/mbed/commit/ace35dfba3748c7cdc102eb38ec6b9e1067c3252/

[NUCLEO_F302R8] Add cmsis and hal files + change F401RE clock to 84MHz

Who changed what in which revision?

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