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