mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_STM32L1/pwmout_api.c@496:543871686697, 2015-03-24 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Mar 24 09:00:08 2015 +0000
- Revision:
- 496:543871686697
- Parent:
- targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pwmout_api.c@414:4ec4c5b614b0
Synchronized with git revision ea01d61fa18430564b78226045b196bb6bf6b66a
Full URL: https://github.com/mbedmicro/mbed/commit/ea01d61fa18430564b78226045b196bb6bf6b66a/
NUCLEO_L152RE - reorg hal
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 76:aeb1df146756 | 1 | /* mbed Microcontroller Library |
mbed_official | 76:aeb1df146756 | 2 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 76:aeb1df146756 | 4 | * All rights reserved. |
mbed_official | 76:aeb1df146756 | 5 | * |
mbed_official | 76:aeb1df146756 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 76:aeb1df146756 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 76:aeb1df146756 | 8 | * |
mbed_official | 76:aeb1df146756 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 76:aeb1df146756 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 76:aeb1df146756 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 76:aeb1df146756 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 76:aeb1df146756 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 76:aeb1df146756 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 76:aeb1df146756 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 76:aeb1df146756 | 16 | * without specific prior written permission. |
mbed_official | 76:aeb1df146756 | 17 | * |
mbed_official | 76:aeb1df146756 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 76:aeb1df146756 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 76:aeb1df146756 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 76:aeb1df146756 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 76:aeb1df146756 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 76:aeb1df146756 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 76:aeb1df146756 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 76:aeb1df146756 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 76:aeb1df146756 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 76:aeb1df146756 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 76:aeb1df146756 | 28 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 29 | */ |
mbed_official | 76:aeb1df146756 | 30 | #include "pwmout_api.h" |
mbed_official | 76:aeb1df146756 | 31 | |
mbed_official | 174:8bb9f3a33240 | 32 | #if DEVICE_PWMOUT |
mbed_official | 174:8bb9f3a33240 | 33 | |
mbed_official | 76:aeb1df146756 | 34 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 35 | #include "pinmap.h" |
mbed_official | 354:e67efb2aab0e | 36 | #include "mbed_error.h" |
mbed_official | 414:4ec4c5b614b0 | 37 | #include "PeripheralPins.h" |
mbed_official | 76:aeb1df146756 | 38 | |
mbed_official | 354:e67efb2aab0e | 39 | static TIM_HandleTypeDef TimHandle; |
mbed_official | 354:e67efb2aab0e | 40 | |
mbed_official | 354:e67efb2aab0e | 41 | void pwmout_init(pwmout_t* obj, PinName pin) |
mbed_official | 354:e67efb2aab0e | 42 | { |
mbed_official | 76:aeb1df146756 | 43 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 76:aeb1df146756 | 44 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 354:e67efb2aab0e | 45 | |
mbed_official | 354:e67efb2aab0e | 46 | if (obj->pwm == (PWMName)NC) { |
mbed_official | 354:e67efb2aab0e | 47 | error("PWM error: pinout mapping failed."); |
mbed_official | 354:e67efb2aab0e | 48 | } |
mbed_official | 174:8bb9f3a33240 | 49 | |
mbed_official | 76:aeb1df146756 | 50 | // Enable TIM clock |
mbed_official | 354:e67efb2aab0e | 51 | if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); |
mbed_official | 354:e67efb2aab0e | 52 | if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); |
mbed_official | 354:e67efb2aab0e | 53 | if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE(); |
mbed_official | 354:e67efb2aab0e | 54 | if (obj->pwm == PWM_5) __TIM5_CLK_ENABLE(); |
mbed_official | 354:e67efb2aab0e | 55 | if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE(); |
mbed_official | 354:e67efb2aab0e | 56 | if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE(); |
mbed_official | 354:e67efb2aab0e | 57 | if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE(); |
mbed_official | 174:8bb9f3a33240 | 58 | |
mbed_official | 76:aeb1df146756 | 59 | // Configure GPIO |
mbed_official | 76:aeb1df146756 | 60 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 174:8bb9f3a33240 | 61 | |
mbed_official | 76:aeb1df146756 | 62 | obj->pin = pin; |
mbed_official | 76:aeb1df146756 | 63 | obj->period = 0; |
mbed_official | 76:aeb1df146756 | 64 | obj->pulse = 0; |
mbed_official | 174:8bb9f3a33240 | 65 | |
mbed_official | 76:aeb1df146756 | 66 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 76:aeb1df146756 | 67 | } |
mbed_official | 76:aeb1df146756 | 68 | |
mbed_official | 354:e67efb2aab0e | 69 | void pwmout_free(pwmout_t* obj) |
mbed_official | 354:e67efb2aab0e | 70 | { |
mbed_official | 354:e67efb2aab0e | 71 | // Configure GPIO |
mbed_official | 354:e67efb2aab0e | 72 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 76:aeb1df146756 | 73 | } |
mbed_official | 76:aeb1df146756 | 74 | |
mbed_official | 354:e67efb2aab0e | 75 | void pwmout_write(pwmout_t* obj, float value) |
mbed_official | 354:e67efb2aab0e | 76 | { |
mbed_official | 354:e67efb2aab0e | 77 | TIM_OC_InitTypeDef sConfig; |
mbed_official | 354:e67efb2aab0e | 78 | int channel = 0; |
mbed_official | 129:0182c99221bc | 79 | |
mbed_official | 354:e67efb2aab0e | 80 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 354:e67efb2aab0e | 81 | |
mbed_official | 354:e67efb2aab0e | 82 | if (value < (float)0.0) { |
mbed_official | 76:aeb1df146756 | 83 | value = 0.0; |
mbed_official | 354:e67efb2aab0e | 84 | } else if (value > (float)1.0) { |
mbed_official | 76:aeb1df146756 | 85 | value = 1.0; |
mbed_official | 76:aeb1df146756 | 86 | } |
mbed_official | 174:8bb9f3a33240 | 87 | |
mbed_official | 76:aeb1df146756 | 88 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 174:8bb9f3a33240 | 89 | |
mbed_official | 354:e67efb2aab0e | 90 | // Configure channels |
mbed_official | 354:e67efb2aab0e | 91 | sConfig.OCMode = TIM_OCMODE_PWM1; |
mbed_official | 354:e67efb2aab0e | 92 | sConfig.Pulse = obj->pulse; |
mbed_official | 354:e67efb2aab0e | 93 | sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
mbed_official | 354:e67efb2aab0e | 94 | sConfig.OCFastMode = TIM_OCFAST_ENABLE; |
mbed_official | 76:aeb1df146756 | 95 | |
mbed_official | 129:0182c99221bc | 96 | switch (obj->pin) { |
mbed_official | 129:0182c99221bc | 97 | // Channels 1 |
mbed_official | 129:0182c99221bc | 98 | case PA_6: |
mbed_official | 129:0182c99221bc | 99 | case PB_4: |
mbed_official | 129:0182c99221bc | 100 | case PB_6: |
mbed_official | 129:0182c99221bc | 101 | case PB_12: |
mbed_official | 129:0182c99221bc | 102 | case PB_13: |
mbed_official | 129:0182c99221bc | 103 | case PB_15: |
mbed_official | 129:0182c99221bc | 104 | case PC_6: |
mbed_official | 354:e67efb2aab0e | 105 | channel = TIM_CHANNEL_1; |
mbed_official | 129:0182c99221bc | 106 | break; |
mbed_official | 129:0182c99221bc | 107 | // Channels 2 |
mbed_official | 129:0182c99221bc | 108 | case PA_1: |
mbed_official | 129:0182c99221bc | 109 | case PA_7: |
mbed_official | 129:0182c99221bc | 110 | case PB_3: |
mbed_official | 129:0182c99221bc | 111 | case PB_5: |
mbed_official | 129:0182c99221bc | 112 | case PB_7: |
mbed_official | 129:0182c99221bc | 113 | case PB_14: |
mbed_official | 129:0182c99221bc | 114 | case PC_7: |
mbed_official | 354:e67efb2aab0e | 115 | channel = TIM_CHANNEL_2; |
mbed_official | 129:0182c99221bc | 116 | break; |
mbed_official | 129:0182c99221bc | 117 | // Channels 3 |
mbed_official | 129:0182c99221bc | 118 | case PA_2: |
mbed_official | 129:0182c99221bc | 119 | case PB_0: |
mbed_official | 129:0182c99221bc | 120 | case PB_8: |
mbed_official | 129:0182c99221bc | 121 | case PB_10: |
mbed_official | 129:0182c99221bc | 122 | case PC_8: |
mbed_official | 354:e67efb2aab0e | 123 | channel = TIM_CHANNEL_3; |
mbed_official | 129:0182c99221bc | 124 | break; |
mbed_official | 129:0182c99221bc | 125 | // Channels 4 |
mbed_official | 129:0182c99221bc | 126 | case PA_3: |
mbed_official | 129:0182c99221bc | 127 | case PB_1: |
mbed_official | 129:0182c99221bc | 128 | case PB_9: |
mbed_official | 129:0182c99221bc | 129 | case PB_11: |
mbed_official | 129:0182c99221bc | 130 | case PC_9: |
mbed_official | 354:e67efb2aab0e | 131 | channel = TIM_CHANNEL_4; |
mbed_official | 174:8bb9f3a33240 | 132 | break; |
mbed_official | 129:0182c99221bc | 133 | default: |
mbed_official | 129:0182c99221bc | 134 | return; |
mbed_official | 76:aeb1df146756 | 135 | } |
mbed_official | 354:e67efb2aab0e | 136 | |
mbed_official | 354:e67efb2aab0e | 137 | HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel); |
mbed_official | 354:e67efb2aab0e | 138 | HAL_TIM_PWM_Start(&TimHandle, channel); |
mbed_official | 76:aeb1df146756 | 139 | } |
mbed_official | 76:aeb1df146756 | 140 | |
mbed_official | 354:e67efb2aab0e | 141 | float pwmout_read(pwmout_t* obj) |
mbed_official | 354:e67efb2aab0e | 142 | { |
mbed_official | 76:aeb1df146756 | 143 | float value = 0; |
mbed_official | 76:aeb1df146756 | 144 | if (obj->period > 0) { |
mbed_official | 76:aeb1df146756 | 145 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 76:aeb1df146756 | 146 | } |
mbed_official | 354:e67efb2aab0e | 147 | return ((value > (float)1.0) ? (float)(1.0) : (value)); |
mbed_official | 76:aeb1df146756 | 148 | } |
mbed_official | 76:aeb1df146756 | 149 | |
mbed_official | 354:e67efb2aab0e | 150 | void pwmout_period(pwmout_t* obj, float seconds) |
mbed_official | 354:e67efb2aab0e | 151 | { |
mbed_official | 76:aeb1df146756 | 152 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 76:aeb1df146756 | 153 | } |
mbed_official | 76:aeb1df146756 | 154 | |
mbed_official | 354:e67efb2aab0e | 155 | void pwmout_period_ms(pwmout_t* obj, int ms) |
mbed_official | 354:e67efb2aab0e | 156 | { |
mbed_official | 76:aeb1df146756 | 157 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 76:aeb1df146756 | 158 | } |
mbed_official | 76:aeb1df146756 | 159 | |
mbed_official | 354:e67efb2aab0e | 160 | void pwmout_period_us(pwmout_t* obj, int us) |
mbed_official | 354:e67efb2aab0e | 161 | { |
mbed_official | 354:e67efb2aab0e | 162 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 354:e67efb2aab0e | 163 | |
mbed_official | 76:aeb1df146756 | 164 | float dc = pwmout_read(obj); |
mbed_official | 76:aeb1df146756 | 165 | |
mbed_official | 354:e67efb2aab0e | 166 | __HAL_TIM_DISABLE(&TimHandle); |
mbed_official | 174:8bb9f3a33240 | 167 | |
mbed_official | 354:e67efb2aab0e | 168 | SystemCoreClockUpdate(); |
mbed_official | 174:8bb9f3a33240 | 169 | |
mbed_official | 354:e67efb2aab0e | 170 | TimHandle.Init.Period = us - 1; |
mbed_official | 354:e67efb2aab0e | 171 | TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 us tick |
mbed_official | 354:e67efb2aab0e | 172 | TimHandle.Init.ClockDivision = 0; |
mbed_official | 354:e67efb2aab0e | 173 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
mbed_official | 354:e67efb2aab0e | 174 | HAL_TIM_PWM_Init(&TimHandle); |
mbed_official | 76:aeb1df146756 | 175 | |
mbed_official | 76:aeb1df146756 | 176 | // Set duty cycle again |
mbed_official | 76:aeb1df146756 | 177 | pwmout_write(obj, dc); |
mbed_official | 174:8bb9f3a33240 | 178 | |
mbed_official | 354:e67efb2aab0e | 179 | // Save for future use |
mbed_official | 354:e67efb2aab0e | 180 | obj->period = us; |
mbed_official | 354:e67efb2aab0e | 181 | |
mbed_official | 354:e67efb2aab0e | 182 | __HAL_TIM_ENABLE(&TimHandle); |
mbed_official | 76:aeb1df146756 | 183 | } |
mbed_official | 76:aeb1df146756 | 184 | |
mbed_official | 354:e67efb2aab0e | 185 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
mbed_official | 354:e67efb2aab0e | 186 | { |
mbed_official | 76:aeb1df146756 | 187 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 76:aeb1df146756 | 188 | } |
mbed_official | 76:aeb1df146756 | 189 | |
mbed_official | 354:e67efb2aab0e | 190 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
mbed_official | 354:e67efb2aab0e | 191 | { |
mbed_official | 76:aeb1df146756 | 192 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 76:aeb1df146756 | 193 | } |
mbed_official | 76:aeb1df146756 | 194 | |
mbed_official | 354:e67efb2aab0e | 195 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
mbed_official | 354:e67efb2aab0e | 196 | { |
mbed_official | 76:aeb1df146756 | 197 | float value = (float)us / (float)obj->period; |
mbed_official | 76:aeb1df146756 | 198 | pwmout_write(obj, value); |
mbed_official | 76:aeb1df146756 | 199 | } |
mbed_official | 174:8bb9f3a33240 | 200 | |
mbed_official | 174:8bb9f3a33240 | 201 | #endif |