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:
Mon Oct 13 08:00:07 2014 +0100
Revision:
344:ed24f9926218
Parent:
340:28d1f895c6fe
Child:
428:4ddf7f7eabbb
Synchronized with git revision 342790b0c3be997d6ed3744962729c4d9c6a9a39

Full URL: https://github.com/mbedmicro/mbed/commit/342790b0c3be997d6ed3744962729c4d9c6a9a39/

HAL: Disco_F429ZI - extensions of interrupt vectors, pin names and pin functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 340:28d1f895c6fe 1 /* mbed Microcontroller Library
mbed_official 340:28d1f895c6fe 2 *******************************************************************************
mbed_official 340:28d1f895c6fe 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 340:28d1f895c6fe 4 * All rights reserved.
mbed_official 340:28d1f895c6fe 5 *
mbed_official 340:28d1f895c6fe 6 * Redistribution and use in source and binary forms, with or without
mbed_official 340:28d1f895c6fe 7 * modification, are permitted provided that the following conditions are met:
mbed_official 340:28d1f895c6fe 8 *
mbed_official 340:28d1f895c6fe 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 340:28d1f895c6fe 10 * this list of conditions and the following disclaimer.
mbed_official 340:28d1f895c6fe 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 340:28d1f895c6fe 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 340:28d1f895c6fe 13 * and/or other materials provided with the distribution.
mbed_official 340:28d1f895c6fe 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 340:28d1f895c6fe 15 * may be used to endorse or promote products derived from this software
mbed_official 340:28d1f895c6fe 16 * without specific prior written permission.
mbed_official 340:28d1f895c6fe 17 *
mbed_official 340:28d1f895c6fe 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 340:28d1f895c6fe 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 340:28d1f895c6fe 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 340:28d1f895c6fe 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 340:28d1f895c6fe 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 340:28d1f895c6fe 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 340:28d1f895c6fe 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 340:28d1f895c6fe 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 340:28d1f895c6fe 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 340:28d1f895c6fe 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 340:28d1f895c6fe 28 *******************************************************************************
mbed_official 340:28d1f895c6fe 29 */
mbed_official 340:28d1f895c6fe 30 #include "pwmout_api.h"
mbed_official 340:28d1f895c6fe 31
mbed_official 340:28d1f895c6fe 32 #if DEVICE_PWMOUT
mbed_official 340:28d1f895c6fe 33
mbed_official 340:28d1f895c6fe 34 #include "cmsis.h"
mbed_official 340:28d1f895c6fe 35 #include "pinmap.h"
mbed_official 340:28d1f895c6fe 36 #include "mbed_error.h"
mbed_official 340:28d1f895c6fe 37
mbed_official 340:28d1f895c6fe 38 // TIM5 cannot be used because already used by the us_ticker
mbed_official 340:28d1f895c6fe 39 static const PinMap PinMap_PWM[] = {
mbed_official 344:ed24f9926218 40 /*
mbed_official 344:ed24f9926218 41 * The lines below show all combinations to conect a port pin with a timer. Commented
mbed_official 344:ed24f9926218 42 * lines are alternative possibilities not used per default. But they can be changed
mbed_official 344:ed24f9926218 43 * manually instead of the suggested configuration. For example you can see that on
mbed_official 344:ed24f9926218 44 * PA_5 you can have a PWM using either Timer2/Channel1 or Timer8/Channel1N. Today I
mbed_official 344:ed24f9926218 45 * have decided to use Timer2/Channel1. But you can also notice that Timer2/Channel1
mbed_official 344:ed24f9926218 46 * is also used on PA_0. That means that today you cannot output two different PWM
mbed_official 344:ed24f9926218 47 * signals on PA_0 and PA_5 at the same time. If someone wants this, he will need to
mbed_official 344:ed24f9926218 48 * change the timer that is used on PA_5. This is why the other possibilities are
mbed_official 344:ed24f9926218 49 * commented to make this change easier without looking deeply into the mcu datasheet.
mbed_official 344:ed24f9926218 50 */
mbed_official 340:28d1f895c6fe 51 {PA_0, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 340:28d1f895c6fe 52 {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 340:28d1f895c6fe 53 {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 340:28d1f895c6fe 54 // {PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1
mbed_official 340:28d1f895c6fe 55 {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4
mbed_official 340:28d1f895c6fe 56 // {PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2
mbed_official 340:28d1f895c6fe 57 {PA_5, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 344:ed24f9926218 58 // {PA_5, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM8)}, // TIM8_CH1N
mbed_official 340:28d1f895c6fe 59 {PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 344:ed24f9926218 60 {PA_7, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 344:ed24f9926218 61 // {PA_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 344:ed24f9926218 62 // {PA_7, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH1N
mbed_official 340:28d1f895c6fe 63 {PA_8, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1
mbed_official 340:28d1f895c6fe 64 {PA_9, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2
mbed_official 340:28d1f895c6fe 65 {PA_10, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3
mbed_official 340:28d1f895c6fe 66 {PA_11, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH4
mbed_official 340:28d1f895c6fe 67 {PA_15, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
mbed_official 340:28d1f895c6fe 68
mbed_official 340:28d1f895c6fe 69 {PB_0, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 340:28d1f895c6fe 70 // {PB_0, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 344:ed24f9926218 71 // {PB_0, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH2N
mbed_official 340:28d1f895c6fe 72 {PB_1, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 340:28d1f895c6fe 73 // {PB_1, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4
mbed_official 344:ed24f9926218 74 // {PB_1, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH3N
mbed_official 344:ed24f9926218 75 {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 344:ed24f9926218 76 {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 340:28d1f895c6fe 77 {PB_5, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 344:ed24f9926218 78 {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1
mbed_official 340:28d1f895c6fe 79 {PB_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2
mbed_official 340:28d1f895c6fe 80 {PB_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3
mbed_official 340:28d1f895c6fe 81 // {PB_8, PWM_10,STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10)}, // TIM10_CH1
mbed_official 340:28d1f895c6fe 82 {PB_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4
mbed_official 340:28d1f895c6fe 83 // {PB_9, PWM_11,STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11)}, // TIM11_CH1
mbed_official 344:ed24f9926218 84 {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
mbed_official 344:ed24f9926218 85 {PB_11, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4
mbed_official 340:28d1f895c6fe 86 {PB_13, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 340:28d1f895c6fe 87 {PB_14, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 344:ed24f9926218 88 // {PB_14, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH2N
mbed_official 340:28d1f895c6fe 89 {PB_15, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 344:ed24f9926218 90 // {PB_15, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM1_CH3N
mbed_official 340:28d1f895c6fe 91
mbed_official 340:28d1f895c6fe 92 {PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 344:ed24f9926218 93 // {PC_6, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH1
mbed_official 344:ed24f9926218 94 {PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
mbed_official 344:ed24f9926218 95 // {PC_7, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH2
mbed_official 340:28d1f895c6fe 96 {PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
mbed_official 344:ed24f9926218 97 // {PC_8, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH3
mbed_official 340:28d1f895c6fe 98 {PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4
mbed_official 344:ed24f9926218 99 // {PC_9, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH4
mbed_official 344:ed24f9926218 100
mbed_official 344:ed24f9926218 101 {PD_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1
mbed_official 344:ed24f9926218 102 {PD_7, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH2
mbed_official 344:ed24f9926218 103 {PD_8, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH3
mbed_official 344:ed24f9926218 104 {PD_9, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH4
mbed_official 344:ed24f9926218 105
mbed_official 344:ed24f9926218 106 {PE_5, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1
mbed_official 344:ed24f9926218 107 {PE_6, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2
mbed_official 344:ed24f9926218 108
mbed_official 344:ed24f9926218 109 {PE_8, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1N
mbed_official 344:ed24f9926218 110 {PE_9, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH1
mbed_official 344:ed24f9926218 111 {PE_10, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2N
mbed_official 344:ed24f9926218 112 {PE_11, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH2
mbed_official 344:ed24f9926218 113 {PE_12, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3N
mbed_official 344:ed24f9926218 114 {PE_13, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH3
mbed_official 344:ed24f9926218 115 {PE_14, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1)}, // TIM1_CH4
mbed_official 344:ed24f9926218 116
mbed_official 344:ed24f9926218 117 {PH_13, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH1N
mbed_official 344:ed24f9926218 118 {PH_14, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH2N
mbed_official 344:ed24f9926218 119 {PH_15, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH3N
mbed_official 344:ed24f9926218 120
mbed_official 344:ed24f9926218 121 {PI_2, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH4
mbed_official 344:ed24f9926218 122 {PI_5, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH1
mbed_official 344:ed24f9926218 123 {PI_6, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH2
mbed_official 344:ed24f9926218 124 {PI_7, PWM_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8)}, // TIM8_CH3
mbed_official 340:28d1f895c6fe 125
mbed_official 340:28d1f895c6fe 126 {NC, NC, 0}
mbed_official 340:28d1f895c6fe 127 };
mbed_official 340:28d1f895c6fe 128
mbed_official 340:28d1f895c6fe 129 static TIM_HandleTypeDef TimHandle;
mbed_official 340:28d1f895c6fe 130
mbed_official 340:28d1f895c6fe 131 void pwmout_init(pwmout_t* obj, PinName pin)
mbed_official 340:28d1f895c6fe 132 {
mbed_official 340:28d1f895c6fe 133 // Get the peripheral name from the pin and assign it to the object
mbed_official 340:28d1f895c6fe 134 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 340:28d1f895c6fe 135
mbed_official 340:28d1f895c6fe 136 if (obj->pwm == (PWMName)NC) {
mbed_official 340:28d1f895c6fe 137 error("PWM error: pinout mapping failed.");
mbed_official 340:28d1f895c6fe 138 }
mbed_official 340:28d1f895c6fe 139
mbed_official 340:28d1f895c6fe 140 // Enable TIM clock
mbed_official 340:28d1f895c6fe 141 if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 142 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 143 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 144 if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 145 if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 146 if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 147 if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 148
mbed_official 340:28d1f895c6fe 149 // Configure GPIO
mbed_official 340:28d1f895c6fe 150 pinmap_pinout(pin, PinMap_PWM);
mbed_official 340:28d1f895c6fe 151
mbed_official 340:28d1f895c6fe 152 obj->pin = pin;
mbed_official 340:28d1f895c6fe 153 obj->period = 0;
mbed_official 340:28d1f895c6fe 154 obj->pulse = 0;
mbed_official 340:28d1f895c6fe 155
mbed_official 340:28d1f895c6fe 156 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 340:28d1f895c6fe 157 }
mbed_official 340:28d1f895c6fe 158
mbed_official 340:28d1f895c6fe 159 void pwmout_free(pwmout_t* obj)
mbed_official 340:28d1f895c6fe 160 {
mbed_official 340:28d1f895c6fe 161 // Configure GPIO
mbed_official 340:28d1f895c6fe 162 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 340:28d1f895c6fe 163 }
mbed_official 340:28d1f895c6fe 164
mbed_official 340:28d1f895c6fe 165 void pwmout_write(pwmout_t* obj, float value)
mbed_official 340:28d1f895c6fe 166 {
mbed_official 340:28d1f895c6fe 167 TIM_OC_InitTypeDef sConfig;
mbed_official 340:28d1f895c6fe 168 int channel = 0;
mbed_official 340:28d1f895c6fe 169 int complementary_channel = 0;
mbed_official 340:28d1f895c6fe 170
mbed_official 340:28d1f895c6fe 171 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 340:28d1f895c6fe 172
mbed_official 340:28d1f895c6fe 173 if (value < (float)0.0) {
mbed_official 340:28d1f895c6fe 174 value = 0.0;
mbed_official 340:28d1f895c6fe 175 } else if (value > (float)1.0) {
mbed_official 340:28d1f895c6fe 176 value = 1.0;
mbed_official 340:28d1f895c6fe 177 }
mbed_official 340:28d1f895c6fe 178
mbed_official 340:28d1f895c6fe 179 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 340:28d1f895c6fe 180
mbed_official 340:28d1f895c6fe 181 // Configure channels
mbed_official 340:28d1f895c6fe 182 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 340:28d1f895c6fe 183 sConfig.Pulse = obj->pulse;
mbed_official 340:28d1f895c6fe 184 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 340:28d1f895c6fe 185 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
mbed_official 340:28d1f895c6fe 186 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 340:28d1f895c6fe 187 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
mbed_official 340:28d1f895c6fe 188 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
mbed_official 340:28d1f895c6fe 189
mbed_official 340:28d1f895c6fe 190 switch (obj->pin) {
mbed_official 340:28d1f895c6fe 191
mbed_official 340:28d1f895c6fe 192 // Channels 1
mbed_official 340:28d1f895c6fe 193 case PA_0:
mbed_official 340:28d1f895c6fe 194 case PA_5:
mbed_official 340:28d1f895c6fe 195 case PA_6:
mbed_official 340:28d1f895c6fe 196 case PA_8:
mbed_official 340:28d1f895c6fe 197 case PA_15:
mbed_official 340:28d1f895c6fe 198 case PB_4:
mbed_official 340:28d1f895c6fe 199 case PB_6:
mbed_official 340:28d1f895c6fe 200 case PC_6:
mbed_official 340:28d1f895c6fe 201 channel = TIM_CHANNEL_1;
mbed_official 340:28d1f895c6fe 202 break;
mbed_official 340:28d1f895c6fe 203
mbed_official 340:28d1f895c6fe 204 // Channels 1N
mbed_official 340:28d1f895c6fe 205 case PA_7:
mbed_official 340:28d1f895c6fe 206 case PB_13:
mbed_official 340:28d1f895c6fe 207 channel = TIM_CHANNEL_1;
mbed_official 340:28d1f895c6fe 208 complementary_channel = 1;
mbed_official 340:28d1f895c6fe 209 break;
mbed_official 340:28d1f895c6fe 210
mbed_official 340:28d1f895c6fe 211 // Channels 2
mbed_official 340:28d1f895c6fe 212 case PA_1:
mbed_official 340:28d1f895c6fe 213 case PA_9:
mbed_official 340:28d1f895c6fe 214 case PB_3:
mbed_official 340:28d1f895c6fe 215 case PB_5:
mbed_official 340:28d1f895c6fe 216 case PB_7:
mbed_official 340:28d1f895c6fe 217 case PC_7:
mbed_official 340:28d1f895c6fe 218 channel = TIM_CHANNEL_2;
mbed_official 340:28d1f895c6fe 219 break;
mbed_official 340:28d1f895c6fe 220
mbed_official 340:28d1f895c6fe 221 // Channels 2N
mbed_official 340:28d1f895c6fe 222 case PB_0:
mbed_official 340:28d1f895c6fe 223 case PB_14:
mbed_official 340:28d1f895c6fe 224 channel = TIM_CHANNEL_2;
mbed_official 340:28d1f895c6fe 225 complementary_channel = 1;
mbed_official 340:28d1f895c6fe 226 break;
mbed_official 340:28d1f895c6fe 227
mbed_official 340:28d1f895c6fe 228 // Channels 3
mbed_official 340:28d1f895c6fe 229 case PA_2:
mbed_official 340:28d1f895c6fe 230 case PA_10:
mbed_official 340:28d1f895c6fe 231 case PB_8:
mbed_official 340:28d1f895c6fe 232 case PB_10:
mbed_official 340:28d1f895c6fe 233 case PC_8:
mbed_official 340:28d1f895c6fe 234 channel = TIM_CHANNEL_3;
mbed_official 340:28d1f895c6fe 235 break;
mbed_official 340:28d1f895c6fe 236
mbed_official 340:28d1f895c6fe 237 // Channels 3N
mbed_official 340:28d1f895c6fe 238 case PB_1:
mbed_official 340:28d1f895c6fe 239 case PB_15:
mbed_official 340:28d1f895c6fe 240 channel = TIM_CHANNEL_3;
mbed_official 340:28d1f895c6fe 241 complementary_channel = 1;
mbed_official 340:28d1f895c6fe 242 break;
mbed_official 340:28d1f895c6fe 243
mbed_official 340:28d1f895c6fe 244 // Channels 4
mbed_official 340:28d1f895c6fe 245 case PA_3:
mbed_official 340:28d1f895c6fe 246 case PA_11:
mbed_official 340:28d1f895c6fe 247 case PB_9:
mbed_official 340:28d1f895c6fe 248 case PC_9:
mbed_official 340:28d1f895c6fe 249 channel = TIM_CHANNEL_4;
mbed_official 340:28d1f895c6fe 250 break;
mbed_official 340:28d1f895c6fe 251
mbed_official 340:28d1f895c6fe 252 default:
mbed_official 340:28d1f895c6fe 253 return;
mbed_official 340:28d1f895c6fe 254 }
mbed_official 340:28d1f895c6fe 255
mbed_official 340:28d1f895c6fe 256 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
mbed_official 340:28d1f895c6fe 257 if (complementary_channel) {
mbed_official 340:28d1f895c6fe 258 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
mbed_official 340:28d1f895c6fe 259 } else {
mbed_official 340:28d1f895c6fe 260 HAL_TIM_PWM_Start(&TimHandle, channel);
mbed_official 340:28d1f895c6fe 261 }
mbed_official 340:28d1f895c6fe 262 }
mbed_official 340:28d1f895c6fe 263
mbed_official 340:28d1f895c6fe 264 float pwmout_read(pwmout_t* obj)
mbed_official 340:28d1f895c6fe 265 {
mbed_official 340:28d1f895c6fe 266 float value = 0;
mbed_official 340:28d1f895c6fe 267 if (obj->period > 0) {
mbed_official 340:28d1f895c6fe 268 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 340:28d1f895c6fe 269 }
mbed_official 340:28d1f895c6fe 270 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 340:28d1f895c6fe 271 }
mbed_official 340:28d1f895c6fe 272
mbed_official 340:28d1f895c6fe 273 void pwmout_period(pwmout_t* obj, float seconds)
mbed_official 340:28d1f895c6fe 274 {
mbed_official 340:28d1f895c6fe 275 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 340:28d1f895c6fe 276 }
mbed_official 340:28d1f895c6fe 277
mbed_official 340:28d1f895c6fe 278 void pwmout_period_ms(pwmout_t* obj, int ms)
mbed_official 340:28d1f895c6fe 279 {
mbed_official 340:28d1f895c6fe 280 pwmout_period_us(obj, ms * 1000);
mbed_official 340:28d1f895c6fe 281 }
mbed_official 340:28d1f895c6fe 282
mbed_official 340:28d1f895c6fe 283 void pwmout_period_us(pwmout_t* obj, int us)
mbed_official 340:28d1f895c6fe 284 {
mbed_official 340:28d1f895c6fe 285 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 340:28d1f895c6fe 286
mbed_official 340:28d1f895c6fe 287 float dc = pwmout_read(obj);
mbed_official 340:28d1f895c6fe 288
mbed_official 340:28d1f895c6fe 289 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 340:28d1f895c6fe 290
mbed_official 340:28d1f895c6fe 291 // Update the SystemCoreClock variable
mbed_official 340:28d1f895c6fe 292 SystemCoreClockUpdate();
mbed_official 340:28d1f895c6fe 293
mbed_official 340:28d1f895c6fe 294 TimHandle.Init.Period = us - 1;
mbed_official 340:28d1f895c6fe 295 TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
mbed_official 340:28d1f895c6fe 296 TimHandle.Init.ClockDivision = 0;
mbed_official 340:28d1f895c6fe 297 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 340:28d1f895c6fe 298 HAL_TIM_PWM_Init(&TimHandle);
mbed_official 340:28d1f895c6fe 299
mbed_official 340:28d1f895c6fe 300 // Set duty cycle again
mbed_official 340:28d1f895c6fe 301 pwmout_write(obj, dc);
mbed_official 340:28d1f895c6fe 302
mbed_official 340:28d1f895c6fe 303 // Save for future use
mbed_official 340:28d1f895c6fe 304 obj->period = us;
mbed_official 340:28d1f895c6fe 305
mbed_official 340:28d1f895c6fe 306 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 340:28d1f895c6fe 307 }
mbed_official 340:28d1f895c6fe 308
mbed_official 340:28d1f895c6fe 309 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
mbed_official 340:28d1f895c6fe 310 {
mbed_official 340:28d1f895c6fe 311 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 340:28d1f895c6fe 312 }
mbed_official 340:28d1f895c6fe 313
mbed_official 340:28d1f895c6fe 314 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
mbed_official 340:28d1f895c6fe 315 {
mbed_official 340:28d1f895c6fe 316 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 340:28d1f895c6fe 317 }
mbed_official 340:28d1f895c6fe 318
mbed_official 340:28d1f895c6fe 319 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
mbed_official 340:28d1f895c6fe 320 {
mbed_official 340:28d1f895c6fe 321 float value = (float)us / (float)obj->period;
mbed_official 340:28d1f895c6fe 322 pwmout_write(obj, value);
mbed_official 340:28d1f895c6fe 323 }
mbed_official 340:28d1f895c6fe 324
mbed_official 340:28d1f895c6fe 325 #endif