mbed w/ spi bug fig

Dependents:   display-puck

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jun 27 07:30:09 2014 +0100
Revision:
242:7074e42da0b2
Parent:
227:7bd0639b8911
Synchronized with git revision 124ef5e3add9e74a3221347a3fbeea7c8b3cf353

Full URL: https://github.com/mbedmicro/mbed/commit/124ef5e3add9e74a3221347a3fbeea7c8b3cf353/

[DISCO_F407VG] HAL update.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 133:d4dda5c437f0 1 /* mbed Microcontroller Library
mbed_official 133:d4dda5c437f0 2 *******************************************************************************
mbed_official 133:d4dda5c437f0 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 133:d4dda5c437f0 4 * All rights reserved.
mbed_official 133:d4dda5c437f0 5 *
mbed_official 133:d4dda5c437f0 6 * Redistribution and use in source and binary forms, with or without
mbed_official 133:d4dda5c437f0 7 * modification, are permitted provided that the following conditions are met:
mbed_official 133:d4dda5c437f0 8 *
mbed_official 133:d4dda5c437f0 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 133:d4dda5c437f0 10 * this list of conditions and the following disclaimer.
mbed_official 133:d4dda5c437f0 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 133:d4dda5c437f0 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 133:d4dda5c437f0 13 * and/or other materials provided with the distribution.
mbed_official 133:d4dda5c437f0 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 133:d4dda5c437f0 15 * may be used to endorse or promote products derived from this software
mbed_official 133:d4dda5c437f0 16 * without specific prior written permission.
mbed_official 133:d4dda5c437f0 17 *
mbed_official 133:d4dda5c437f0 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 133:d4dda5c437f0 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 133:d4dda5c437f0 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 133:d4dda5c437f0 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 133:d4dda5c437f0 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 133:d4dda5c437f0 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 133:d4dda5c437f0 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 133:d4dda5c437f0 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 133:d4dda5c437f0 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 133:d4dda5c437f0 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 133:d4dda5c437f0 28 *******************************************************************************
mbed_official 133:d4dda5c437f0 29 */
mbed_official 133:d4dda5c437f0 30 #include "pwmout_api.h"
mbed_official 133:d4dda5c437f0 31
mbed_official 242:7074e42da0b2 32 #if DEVICE_PWMOUT
mbed_official 242:7074e42da0b2 33
mbed_official 133:d4dda5c437f0 34 #include "cmsis.h"
mbed_official 133:d4dda5c437f0 35 #include "pinmap.h"
mbed_official 133:d4dda5c437f0 36 #include "error.h"
mbed_official 133:d4dda5c437f0 37
mbed_official 133:d4dda5c437f0 38 // TIM5 cannot be used because already used by the us_ticker
mbed_official 133:d4dda5c437f0 39 static const PinMap PinMap_PWM[] = {
mbed_official 133:d4dda5c437f0 40 {PA_0, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 242:7074e42da0b2 41 // {PA_0, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH1
mbed_official 133:d4dda5c437f0 42 {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 242:7074e42da0b2 43 // {PA_1, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH2
mbed_official 133:d4dda5c437f0 44 {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 242:7074e42da0b2 45 // {PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3
mbed_official 242:7074e42da0b2 46 // {PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1
mbed_official 159:3b23f6d9ecb9 47 {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4
mbed_official 242:7074e42da0b2 48 // {PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4
mbed_official 242:7074e42da0b2 49 // {PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2
mbed_official 133:d4dda5c437f0 50 {PA_5, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 133:d4dda5c437f0 51 {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 52 {PA_7, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 242:7074e42da0b2 53 // {PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 54 {PA_8, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1
mbed_official 133:d4dda5c437f0 55 {PA_9, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2
mbed_official 133:d4dda5c437f0 56 {PA_10, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3
mbed_official 133:d4dda5c437f0 57 {PA_11, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH4
mbed_official 133:d4dda5c437f0 58 {PA_15, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 133:d4dda5c437f0 59
mbed_official 133:d4dda5c437f0 60 {PB_0, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 242:7074e42da0b2 61 // {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 133:d4dda5c437f0 62 {PB_1, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 242:7074e42da0b2 63 // {PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4
mbed_official 133:d4dda5c437f0 64 {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 133:d4dda5c437f0 65 {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 66 {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 67 {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1
mbed_official 133:d4dda5c437f0 68 {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2
mbed_official 133:d4dda5c437f0 69 {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3
mbed_official 242:7074e42da0b2 70 // {PB_8, PWM_10,STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1
mbed_official 133:d4dda5c437f0 71 {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4
mbed_official 242:7074e42da0b2 72 // {PB_9, PWM_11,STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1
mbed_official 133:d4dda5c437f0 73 {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 133:d4dda5c437f0 74 {PB_13, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 133:d4dda5c437f0 75 {PB_14, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 133:d4dda5c437f0 76 {PB_15, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 133:d4dda5c437f0 77
mbed_official 133:d4dda5c437f0 78 {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 133:d4dda5c437f0 79 {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 133:d4dda5c437f0 80 {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 159:3b23f6d9ecb9 81 {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4
mbed_official 133:d4dda5c437f0 82
mbed_official 133:d4dda5c437f0 83 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 84 };
mbed_official 133:d4dda5c437f0 85
mbed_official 133:d4dda5c437f0 86 static TIM_HandleTypeDef TimHandle;
mbed_official 242:7074e42da0b2 87 static uint8_t ClockDivider;
mbed_official 242:7074e42da0b2 88 /* 0, 1, 2, 3, 4, 5, 6, 7 */
mbed_official 242:7074e42da0b2 89 const uint8_t APBxPrescTable[]={ 0, 0, 0, 0, 1, 2, 3, 4 };
mbed_official 242:7074e42da0b2 90 extern const uint8_t AHBPrescTable[];
mbed_official 133:d4dda5c437f0 91
mbed_official 133:d4dda5c437f0 92 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 133:d4dda5c437f0 93 // Get the peripheral name from the pin and assign it to the object
mbed_official 133:d4dda5c437f0 94 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 242:7074e42da0b2 95
mbed_official 242:7074e42da0b2 96 if (obj->pwm == (PWMName)NC) {
mbed_official 242:7074e42da0b2 97 error("PWM error: pinout mapping failed.");
mbed_official 242:7074e42da0b2 98 }
mbed_official 227:7bd0639b8911 99
mbed_official 133:d4dda5c437f0 100 // Enable TIM clock
mbed_official 242:7074e42da0b2 101 /* APB1 Timers */
mbed_official 133:d4dda5c437f0 102 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 103 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 104 if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
mbed_official 242:7074e42da0b2 105 /* APB2 Timers */
mbed_official 242:7074e42da0b2 106 if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
mbed_official 242:7074e42da0b2 107 if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 108 if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 109 if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE();
mbed_official 242:7074e42da0b2 110 /* Decide APBx divider for timer base clock
mbed_official 242:7074e42da0b2 111 * Auto detect the Clockdivider value according to AHB,APB1 and APB2 prescaler register.
mbed_official 242:7074e42da0b2 112 */
mbed_official 242:7074e42da0b2 113 ClockDivider = AHBPrescTable[ ( ( RCC->CFGR & RCC_CFGR_HPRE ) >> 4 ) ];
mbed_official 242:7074e42da0b2 114 switch( obj->pwm ) {
mbed_official 242:7074e42da0b2 115 /* APB1 low clock */
mbed_official 242:7074e42da0b2 116 case PWM_2:
mbed_official 242:7074e42da0b2 117 case PWM_3:
mbed_official 242:7074e42da0b2 118 case PWM_4:
mbed_official 242:7074e42da0b2 119 ClockDivider += APBxPrescTable[ ( (RCC->CFGR & RCC_CFGR_PPRE1 ) >> 13 ) ];
mbed_official 242:7074e42da0b2 120 break;
mbed_official 242:7074e42da0b2 121 /* APB2 fast clock */
mbed_official 242:7074e42da0b2 122 case PWM_1:
mbed_official 242:7074e42da0b2 123 case PWM_9:
mbed_official 242:7074e42da0b2 124 case PWM_10:
mbed_official 242:7074e42da0b2 125 case PWM_11:
mbed_official 242:7074e42da0b2 126 ClockDivider += APBxPrescTable[ ( (RCC->CFGR & RCC_CFGR_PPRE2 ) >> 10 ) ];
mbed_official 242:7074e42da0b2 127 break;
mbed_official 242:7074e42da0b2 128 default:
mbed_official 242:7074e42da0b2 129 break;
mbed_official 242:7074e42da0b2 130 }
mbed_official 133:d4dda5c437f0 131
mbed_official 133:d4dda5c437f0 132 // Configure GPIO
mbed_official 133:d4dda5c437f0 133 pinmap_pinout(pin, PinMap_PWM);
mbed_official 133:d4dda5c437f0 134
mbed_official 133:d4dda5c437f0 135 obj->pin = pin;
mbed_official 133:d4dda5c437f0 136 obj->period = 0;
mbed_official 133:d4dda5c437f0 137 obj->pulse = 0;
mbed_official 133:d4dda5c437f0 138
mbed_official 133:d4dda5c437f0 139 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 133:d4dda5c437f0 140 }
mbed_official 133:d4dda5c437f0 141
mbed_official 133:d4dda5c437f0 142 void pwmout_free(pwmout_t* obj) {
mbed_official 242:7074e42da0b2 143 // Configure GPIO
mbed_official 133:d4dda5c437f0 144 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 133:d4dda5c437f0 145 }
mbed_official 133:d4dda5c437f0 146
mbed_official 133:d4dda5c437f0 147 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 133:d4dda5c437f0 148 TIM_OC_InitTypeDef sConfig;
mbed_official 133:d4dda5c437f0 149 int channel = 0;
mbed_official 133:d4dda5c437f0 150 int complementary_channel = 0;
mbed_official 133:d4dda5c437f0 151
mbed_official 133:d4dda5c437f0 152 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 153
mbed_official 133:d4dda5c437f0 154 if (value < (float)0.0) {
mbed_official 133:d4dda5c437f0 155 value = 0.0;
mbed_official 133:d4dda5c437f0 156 } else if (value > (float)1.0) {
mbed_official 133:d4dda5c437f0 157 value = 1.0;
mbed_official 133:d4dda5c437f0 158 }
mbed_official 133:d4dda5c437f0 159
mbed_official 133:d4dda5c437f0 160 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 133:d4dda5c437f0 161
mbed_official 133:d4dda5c437f0 162 // Configure channels
mbed_official 133:d4dda5c437f0 163 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 133:d4dda5c437f0 164 sConfig.Pulse = obj->pulse;
mbed_official 133:d4dda5c437f0 165 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 133:d4dda5c437f0 166 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
mbed_official 133:d4dda5c437f0 167 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 133:d4dda5c437f0 168 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
mbed_official 133:d4dda5c437f0 169 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
mbed_official 133:d4dda5c437f0 170
mbed_official 133:d4dda5c437f0 171 switch (obj->pin) {
mbed_official 133:d4dda5c437f0 172 // Channels 1
mbed_official 133:d4dda5c437f0 173 case PA_0:
mbed_official 133:d4dda5c437f0 174 case PA_5:
mbed_official 133:d4dda5c437f0 175 case PA_6:
mbed_official 133:d4dda5c437f0 176 case PA_8:
mbed_official 133:d4dda5c437f0 177 case PA_15:
mbed_official 133:d4dda5c437f0 178 case PB_4:
mbed_official 133:d4dda5c437f0 179 case PB_6:
mbed_official 133:d4dda5c437f0 180 case PC_6:
mbed_official 133:d4dda5c437f0 181 channel = TIM_CHANNEL_1;
mbed_official 133:d4dda5c437f0 182 break;
mbed_official 133:d4dda5c437f0 183 // Channels 1N
mbed_official 133:d4dda5c437f0 184 case PA_7:
mbed_official 133:d4dda5c437f0 185 case PB_13:
mbed_official 133:d4dda5c437f0 186 channel = TIM_CHANNEL_1;
mbed_official 133:d4dda5c437f0 187 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 188 break;
mbed_official 133:d4dda5c437f0 189 // Channels 2
mbed_official 133:d4dda5c437f0 190 case PA_1:
mbed_official 133:d4dda5c437f0 191 case PA_9:
mbed_official 133:d4dda5c437f0 192 case PB_3:
mbed_official 133:d4dda5c437f0 193 case PB_5:
mbed_official 133:d4dda5c437f0 194 case PB_7:
mbed_official 133:d4dda5c437f0 195 case PC_7:
mbed_official 133:d4dda5c437f0 196 channel = TIM_CHANNEL_2;
mbed_official 133:d4dda5c437f0 197 break;
mbed_official 133:d4dda5c437f0 198 // Channels 2N
mbed_official 133:d4dda5c437f0 199 case PB_0:
mbed_official 133:d4dda5c437f0 200 case PB_14:
mbed_official 133:d4dda5c437f0 201 channel = TIM_CHANNEL_2;
mbed_official 133:d4dda5c437f0 202 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 203 break;
mbed_official 133:d4dda5c437f0 204 // Channels 3
mbed_official 133:d4dda5c437f0 205 case PA_2:
mbed_official 133:d4dda5c437f0 206 case PA_10:
mbed_official 133:d4dda5c437f0 207 case PB_8:
mbed_official 133:d4dda5c437f0 208 case PB_10:
mbed_official 133:d4dda5c437f0 209 case PC_8:
mbed_official 133:d4dda5c437f0 210 channel = TIM_CHANNEL_3;
mbed_official 133:d4dda5c437f0 211 break;
mbed_official 133:d4dda5c437f0 212 // Channels 3N
mbed_official 133:d4dda5c437f0 213 case PB_1:
mbed_official 133:d4dda5c437f0 214 case PB_15:
mbed_official 133:d4dda5c437f0 215 channel = TIM_CHANNEL_3;
mbed_official 133:d4dda5c437f0 216 complementary_channel = 1;
mbed_official 133:d4dda5c437f0 217 break;
mbed_official 133:d4dda5c437f0 218 // Channels 4
mbed_official 159:3b23f6d9ecb9 219 case PA_3:
mbed_official 133:d4dda5c437f0 220 case PA_11:
mbed_official 133:d4dda5c437f0 221 case PB_9:
mbed_official 159:3b23f6d9ecb9 222 case PC_9:
mbed_official 133:d4dda5c437f0 223 channel = TIM_CHANNEL_4;
mbed_official 133:d4dda5c437f0 224 break;
mbed_official 133:d4dda5c437f0 225 default:
mbed_official 133:d4dda5c437f0 226 return;
mbed_official 133:d4dda5c437f0 227 }
mbed_official 133:d4dda5c437f0 228
mbed_official 133:d4dda5c437f0 229 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
mbed_official 133:d4dda5c437f0 230 if (complementary_channel) {
mbed_official 133:d4dda5c437f0 231 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
mbed_official 242:7074e42da0b2 232 } else {
mbed_official 133:d4dda5c437f0 233 HAL_TIM_PWM_Start(&TimHandle, channel);
mbed_official 133:d4dda5c437f0 234 }
mbed_official 133:d4dda5c437f0 235 }
mbed_official 133:d4dda5c437f0 236
mbed_official 133:d4dda5c437f0 237 float pwmout_read(pwmout_t* obj) {
mbed_official 133:d4dda5c437f0 238 float value = 0;
mbed_official 133:d4dda5c437f0 239 if (obj->period > 0) {
mbed_official 133:d4dda5c437f0 240 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 133:d4dda5c437f0 241 }
mbed_official 133:d4dda5c437f0 242 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 133:d4dda5c437f0 243 }
mbed_official 133:d4dda5c437f0 244
mbed_official 133:d4dda5c437f0 245 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 133:d4dda5c437f0 246 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 133:d4dda5c437f0 247 }
mbed_official 133:d4dda5c437f0 248
mbed_official 133:d4dda5c437f0 249 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 133:d4dda5c437f0 250 pwmout_period_us(obj, ms * 1000);
mbed_official 133:d4dda5c437f0 251 }
mbed_official 133:d4dda5c437f0 252
mbed_official 133:d4dda5c437f0 253 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 133:d4dda5c437f0 254 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 133:d4dda5c437f0 255
mbed_official 133:d4dda5c437f0 256 float dc = pwmout_read(obj);
mbed_official 133:d4dda5c437f0 257
mbed_official 133:d4dda5c437f0 258 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 133:d4dda5c437f0 259
mbed_official 242:7074e42da0b2 260 // Update the SystemCoreClock variable
mbed_official 242:7074e42da0b2 261 SystemCoreClockUpdate();
mbed_official 133:d4dda5c437f0 262 TimHandle.Init.Period = us - 1;
mbed_official 242:7074e42da0b2 263 TimHandle.Init.Prescaler = (uint16_t)( ( SystemCoreClock >> ClockDivider ) / 1000000) - 1; // 1 µs tick
mbed_official 133:d4dda5c437f0 264 TimHandle.Init.ClockDivision = 0;
mbed_official 133:d4dda5c437f0 265 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 133:d4dda5c437f0 266 HAL_TIM_PWM_Init(&TimHandle);
mbed_official 133:d4dda5c437f0 267
mbed_official 133:d4dda5c437f0 268 // Set duty cycle again
mbed_official 133:d4dda5c437f0 269 pwmout_write(obj, dc);
mbed_official 133:d4dda5c437f0 270
mbed_official 133:d4dda5c437f0 271 // Save for future use
mbed_official 133:d4dda5c437f0 272 obj->period = us;
mbed_official 133:d4dda5c437f0 273
mbed_official 133:d4dda5c437f0 274 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 133:d4dda5c437f0 275 }
mbed_official 133:d4dda5c437f0 276
mbed_official 133:d4dda5c437f0 277 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 133:d4dda5c437f0 278 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 133:d4dda5c437f0 279 }
mbed_official 133:d4dda5c437f0 280
mbed_official 133:d4dda5c437f0 281 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 133:d4dda5c437f0 282 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 133:d4dda5c437f0 283 }
mbed_official 133:d4dda5c437f0 284
mbed_official 133:d4dda5c437f0 285 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 133:d4dda5c437f0 286 float value = (float)us / (float)obj->period;
mbed_official 133:d4dda5c437f0 287 pwmout_write(obj, value);
mbed_official 133:d4dda5c437f0 288 }
mbed_official 242:7074e42da0b2 289
mbed_official 242:7074e42da0b2 290 #endif