mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Nov 03 10:30:07 2014 +0000
Revision:
381:5460fc57b6e4
Synchronized with git revision 02478cd1f27fc7b9643486472635eb515b2bca81

Full URL: https://github.com/mbedmicro/mbed/commit/02478cd1f27fc7b9643486472635eb515b2bca81/

Target: LPC1549 - Fix serial interrupt issues (issue report #616)

Who changed what in which revision?

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