mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/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:
- 181:a4cbdfbbd2f4
- 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 | 181:a4cbdfbbd2f4 | 1 | /* mbed Microcontroller Library |
mbed_official | 181:a4cbdfbbd2f4 | 2 | ******************************************************************************* |
mbed_official | 181:a4cbdfbbd2f4 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 181:a4cbdfbbd2f4 | 4 | * All rights reserved. |
mbed_official | 181:a4cbdfbbd2f4 | 5 | * |
mbed_official | 181:a4cbdfbbd2f4 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 181:a4cbdfbbd2f4 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 181:a4cbdfbbd2f4 | 8 | * |
mbed_official | 181:a4cbdfbbd2f4 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 181:a4cbdfbbd2f4 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 181:a4cbdfbbd2f4 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 181:a4cbdfbbd2f4 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 181:a4cbdfbbd2f4 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 181:a4cbdfbbd2f4 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 181:a4cbdfbbd2f4 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 181:a4cbdfbbd2f4 | 16 | * without specific prior written permission. |
mbed_official | 181:a4cbdfbbd2f4 | 17 | * |
mbed_official | 181:a4cbdfbbd2f4 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 181:a4cbdfbbd2f4 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 181:a4cbdfbbd2f4 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 181:a4cbdfbbd2f4 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 181:a4cbdfbbd2f4 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 181:a4cbdfbbd2f4 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 181:a4cbdfbbd2f4 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 181:a4cbdfbbd2f4 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 181:a4cbdfbbd2f4 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 181:a4cbdfbbd2f4 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 181:a4cbdfbbd2f4 | 28 | ******************************************************************************* |
mbed_official | 181:a4cbdfbbd2f4 | 29 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 30 | #include "pwmout_api.h" |
mbed_official | 181:a4cbdfbbd2f4 | 31 | |
mbed_official | 181:a4cbdfbbd2f4 | 32 | #if DEVICE_PWMOUT |
mbed_official | 181:a4cbdfbbd2f4 | 33 | |
mbed_official | 181:a4cbdfbbd2f4 | 34 | #include "cmsis.h" |
mbed_official | 181:a4cbdfbbd2f4 | 35 | #include "pinmap.h" |
mbed_official | 250:a49055e7a707 | 36 | #include "mbed_error.h" |
mbed_official | 181:a4cbdfbbd2f4 | 37 | |
mbed_official | 181:a4cbdfbbd2f4 | 38 | // TIM21 cannot be used because already used by the us_ticker |
mbed_official | 181:a4cbdfbbd2f4 | 39 | static const PinMap PinMap_PWM[] = { |
mbed_official | 181:a4cbdfbbd2f4 | 40 | {PA_0, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM2)}, // TIM2_CH1 |
mbed_official | 181:a4cbdfbbd2f4 | 41 | {PA_1, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM2)}, // TIM2_CH2 |
mbed_official | 181:a4cbdfbbd2f4 | 42 | // {PA_2, PWM_21, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_TIM21)}, // TIM21_CH1 |
mbed_official | 181:a4cbdfbbd2f4 | 43 | // {PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM2)}, // TIM2_CH3 - used by STDIO TX |
mbed_official | 181:a4cbdfbbd2f4 | 44 | // {PA_3, PWM_21, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_TIM21)}, // TIM21_CH2 |
mbed_official | 181:a4cbdfbbd2f4 | 45 | // {PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM2)}, // TIM2_CH4 - used by STDIO RX |
mbed_official | 181:a4cbdfbbd2f4 | 46 | {PA_5, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_TIM2)}, // TIM2_CH1 - used also to drive the LED |
mbed_official | 181:a4cbdfbbd2f4 | 47 | {PA_6, PWM_22, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_TIM22)}, // TIM22_CH1 |
mbed_official | 181:a4cbdfbbd2f4 | 48 | {PA_7, PWM_22, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_TIM22)}, // TIM22_CH2 |
mbed_official | 181:a4cbdfbbd2f4 | 49 | {PA_15, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_TIM2)}, // TIM2_CH1 |
mbed_official | 181:a4cbdfbbd2f4 | 50 | {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM2)}, // TIM2_CH2 |
mbed_official | 181:a4cbdfbbd2f4 | 51 | {PB_4, PWM_22, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_TIM22)}, // TIM22_CH1 |
mbed_official | 181:a4cbdfbbd2f4 | 52 | {PB_5, PWM_22, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_TIM22)}, // TIM22_CH2 |
mbed_official | 181:a4cbdfbbd2f4 | 53 | {PB_10, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM2)}, // TIM2_CH3 |
mbed_official | 181:a4cbdfbbd2f4 | 54 | {PB_11, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM2)}, // TIM2_CH4 |
mbed_official | 181:a4cbdfbbd2f4 | 55 | // {PB_13, PWM_21, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_TIM21)}, // TIM21_CH1 |
mbed_official | 181:a4cbdfbbd2f4 | 56 | // {PB_14, PWM_21, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_TIM21)}, // TIM21_CH2 |
mbed_official | 181:a4cbdfbbd2f4 | 57 | {PC_6, PWM_22, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_TIM22)}, // TIM22_CH1 |
mbed_official | 181:a4cbdfbbd2f4 | 58 | {PC_7, PWM_22, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_TIM22)}, // TIM22_CH2 |
mbed_official | 181:a4cbdfbbd2f4 | 59 | {NC, NC, 0} |
mbed_official | 181:a4cbdfbbd2f4 | 60 | }; |
mbed_official | 181:a4cbdfbbd2f4 | 61 | |
mbed_official | 181:a4cbdfbbd2f4 | 62 | static TIM_HandleTypeDef TimHandle; |
mbed_official | 181:a4cbdfbbd2f4 | 63 | |
mbed_official | 181:a4cbdfbbd2f4 | 64 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 181:a4cbdfbbd2f4 | 65 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 181:a4cbdfbbd2f4 | 66 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 181:a4cbdfbbd2f4 | 67 | |
mbed_official | 181:a4cbdfbbd2f4 | 68 | if (obj->pwm == (PWMName)NC) { |
mbed_official | 181:a4cbdfbbd2f4 | 69 | error("PWM error: pinout mapping failed."); |
mbed_official | 181:a4cbdfbbd2f4 | 70 | } |
mbed_official | 181:a4cbdfbbd2f4 | 71 | |
mbed_official | 181:a4cbdfbbd2f4 | 72 | // Enable TIM clock |
mbed_official | 181:a4cbdfbbd2f4 | 73 | if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); |
mbed_official | 181:a4cbdfbbd2f4 | 74 | if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE(); |
mbed_official | 181:a4cbdfbbd2f4 | 75 | if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE(); |
mbed_official | 181:a4cbdfbbd2f4 | 76 | |
mbed_official | 181:a4cbdfbbd2f4 | 77 | // Configure GPIO |
mbed_official | 181:a4cbdfbbd2f4 | 78 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 181:a4cbdfbbd2f4 | 79 | |
mbed_official | 181:a4cbdfbbd2f4 | 80 | obj->pin = pin; |
mbed_official | 181:a4cbdfbbd2f4 | 81 | obj->period = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 82 | obj->pulse = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 83 | |
mbed_official | 181:a4cbdfbbd2f4 | 84 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 181:a4cbdfbbd2f4 | 85 | } |
mbed_official | 181:a4cbdfbbd2f4 | 86 | |
mbed_official | 181:a4cbdfbbd2f4 | 87 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 181:a4cbdfbbd2f4 | 88 | // Configure GPIO |
mbed_official | 181:a4cbdfbbd2f4 | 89 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 181:a4cbdfbbd2f4 | 90 | } |
mbed_official | 181:a4cbdfbbd2f4 | 91 | |
mbed_official | 181:a4cbdfbbd2f4 | 92 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 181:a4cbdfbbd2f4 | 93 | TIM_OC_InitTypeDef sConfig; |
mbed_official | 181:a4cbdfbbd2f4 | 94 | int channel = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 95 | |
mbed_official | 181:a4cbdfbbd2f4 | 96 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 181:a4cbdfbbd2f4 | 97 | |
mbed_official | 181:a4cbdfbbd2f4 | 98 | if (value < (float)0.0) { |
mbed_official | 181:a4cbdfbbd2f4 | 99 | value = 0.0; |
mbed_official | 181:a4cbdfbbd2f4 | 100 | } else if (value > (float)1.0) { |
mbed_official | 181:a4cbdfbbd2f4 | 101 | value = 1.0; |
mbed_official | 181:a4cbdfbbd2f4 | 102 | } |
mbed_official | 181:a4cbdfbbd2f4 | 103 | |
mbed_official | 181:a4cbdfbbd2f4 | 104 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 181:a4cbdfbbd2f4 | 105 | |
mbed_official | 181:a4cbdfbbd2f4 | 106 | // Configure channels |
mbed_official | 181:a4cbdfbbd2f4 | 107 | sConfig.OCMode = TIM_OCMODE_PWM1; |
mbed_official | 181:a4cbdfbbd2f4 | 108 | sConfig.Pulse = obj->pulse; |
mbed_official | 181:a4cbdfbbd2f4 | 109 | sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
mbed_official | 181:a4cbdfbbd2f4 | 110 | sConfig.OCFastMode = TIM_OCFAST_ENABLE; |
mbed_official | 181:a4cbdfbbd2f4 | 111 | |
mbed_official | 181:a4cbdfbbd2f4 | 112 | switch (obj->pin) { |
mbed_official | 181:a4cbdfbbd2f4 | 113 | // Channels 1 |
mbed_official | 181:a4cbdfbbd2f4 | 114 | case PA_0: |
mbed_official | 181:a4cbdfbbd2f4 | 115 | // case PA_2: |
mbed_official | 181:a4cbdfbbd2f4 | 116 | case PA_5: |
mbed_official | 181:a4cbdfbbd2f4 | 117 | case PA_6: |
mbed_official | 181:a4cbdfbbd2f4 | 118 | case PA_15: |
mbed_official | 181:a4cbdfbbd2f4 | 119 | case PB_4: |
mbed_official | 181:a4cbdfbbd2f4 | 120 | // case PB_13: |
mbed_official | 181:a4cbdfbbd2f4 | 121 | case PC_6: |
mbed_official | 181:a4cbdfbbd2f4 | 122 | channel = TIM_CHANNEL_1; |
mbed_official | 181:a4cbdfbbd2f4 | 123 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 124 | // Channels 2 |
mbed_official | 181:a4cbdfbbd2f4 | 125 | case PA_1: |
mbed_official | 181:a4cbdfbbd2f4 | 126 | // case PA_3: |
mbed_official | 181:a4cbdfbbd2f4 | 127 | case PA_7: |
mbed_official | 181:a4cbdfbbd2f4 | 128 | case PB_3: |
mbed_official | 181:a4cbdfbbd2f4 | 129 | case PB_5: |
mbed_official | 181:a4cbdfbbd2f4 | 130 | // case PB_14: |
mbed_official | 181:a4cbdfbbd2f4 | 131 | case PC_7: |
mbed_official | 181:a4cbdfbbd2f4 | 132 | channel = TIM_CHANNEL_2; |
mbed_official | 181:a4cbdfbbd2f4 | 133 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 134 | // Channels 3 |
mbed_official | 181:a4cbdfbbd2f4 | 135 | case PA_2: |
mbed_official | 181:a4cbdfbbd2f4 | 136 | case PB_10: |
mbed_official | 181:a4cbdfbbd2f4 | 137 | channel = TIM_CHANNEL_3; |
mbed_official | 181:a4cbdfbbd2f4 | 138 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 139 | // Channels 4 |
mbed_official | 181:a4cbdfbbd2f4 | 140 | case PA_3: |
mbed_official | 181:a4cbdfbbd2f4 | 141 | case PB_11: |
mbed_official | 181:a4cbdfbbd2f4 | 142 | channel = TIM_CHANNEL_4; |
mbed_official | 181:a4cbdfbbd2f4 | 143 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 144 | default: |
mbed_official | 181:a4cbdfbbd2f4 | 145 | return; |
mbed_official | 181:a4cbdfbbd2f4 | 146 | } |
mbed_official | 181:a4cbdfbbd2f4 | 147 | |
mbed_official | 181:a4cbdfbbd2f4 | 148 | HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel); |
mbed_official | 181:a4cbdfbbd2f4 | 149 | HAL_TIM_PWM_Start(&TimHandle, channel); |
mbed_official | 181:a4cbdfbbd2f4 | 150 | } |
mbed_official | 181:a4cbdfbbd2f4 | 151 | |
mbed_official | 181:a4cbdfbbd2f4 | 152 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 181:a4cbdfbbd2f4 | 153 | float value = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 154 | if (obj->period > 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 155 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 181:a4cbdfbbd2f4 | 156 | } |
mbed_official | 181:a4cbdfbbd2f4 | 157 | return ((value > (float)1.0) ? (float)(1.0) : (value)); |
mbed_official | 181:a4cbdfbbd2f4 | 158 | } |
mbed_official | 181:a4cbdfbbd2f4 | 159 | |
mbed_official | 181:a4cbdfbbd2f4 | 160 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 181:a4cbdfbbd2f4 | 161 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 181:a4cbdfbbd2f4 | 162 | } |
mbed_official | 181:a4cbdfbbd2f4 | 163 | |
mbed_official | 181:a4cbdfbbd2f4 | 164 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 181:a4cbdfbbd2f4 | 165 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 181:a4cbdfbbd2f4 | 166 | } |
mbed_official | 181:a4cbdfbbd2f4 | 167 | |
mbed_official | 181:a4cbdfbbd2f4 | 168 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 181:a4cbdfbbd2f4 | 169 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 181:a4cbdfbbd2f4 | 170 | |
mbed_official | 181:a4cbdfbbd2f4 | 171 | float dc = pwmout_read(obj); |
mbed_official | 181:a4cbdfbbd2f4 | 172 | |
mbed_official | 181:a4cbdfbbd2f4 | 173 | __HAL_TIM_DISABLE(&TimHandle); |
mbed_official | 181:a4cbdfbbd2f4 | 174 | |
mbed_official | 181:a4cbdfbbd2f4 | 175 | TimHandle.Init.Period = us - 1; |
mbed_official | 181:a4cbdfbbd2f4 | 176 | TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 181:a4cbdfbbd2f4 | 177 | TimHandle.Init.ClockDivision = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 178 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
mbed_official | 181:a4cbdfbbd2f4 | 179 | HAL_TIM_PWM_Init(&TimHandle); |
mbed_official | 181:a4cbdfbbd2f4 | 180 | |
mbed_official | 181:a4cbdfbbd2f4 | 181 | // Set duty cycle again |
mbed_official | 181:a4cbdfbbd2f4 | 182 | pwmout_write(obj, dc); |
mbed_official | 181:a4cbdfbbd2f4 | 183 | |
mbed_official | 181:a4cbdfbbd2f4 | 184 | // Save for future use |
mbed_official | 181:a4cbdfbbd2f4 | 185 | obj->period = us; |
mbed_official | 181:a4cbdfbbd2f4 | 186 | |
mbed_official | 181:a4cbdfbbd2f4 | 187 | __HAL_TIM_ENABLE(&TimHandle); |
mbed_official | 181:a4cbdfbbd2f4 | 188 | } |
mbed_official | 181:a4cbdfbbd2f4 | 189 | |
mbed_official | 181:a4cbdfbbd2f4 | 190 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 181:a4cbdfbbd2f4 | 191 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 181:a4cbdfbbd2f4 | 192 | } |
mbed_official | 181:a4cbdfbbd2f4 | 193 | |
mbed_official | 181:a4cbdfbbd2f4 | 194 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 181:a4cbdfbbd2f4 | 195 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 181:a4cbdfbbd2f4 | 196 | } |
mbed_official | 181:a4cbdfbbd2f4 | 197 | |
mbed_official | 181:a4cbdfbbd2f4 | 198 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 181:a4cbdfbbd2f4 | 199 | float value = (float)us / (float)obj->period; |
mbed_official | 181:a4cbdfbbd2f4 | 200 | pwmout_write(obj, value); |
mbed_official | 181:a4cbdfbbd2f4 | 201 | } |
mbed_official | 181:a4cbdfbbd2f4 | 202 | |
mbed_official | 181:a4cbdfbbd2f4 | 203 | #endif |