mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Jul 29 19:00:07 2014 +0100
Revision:
268:402bcc0c870b
Parent:
251:de9a1e4ffd79
Child:
285:31249416b6f9
Synchronized with git revision 490d1a6606b3138f165c5edf2f2370ca616587c0

Full URL: https://github.com/mbedmicro/mbed/commit/490d1a6606b3138f165c5edf2f2370ca616587c0/

[LPC1114] Sleep fix + some device.h settings

Who changed what in which revision?

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