Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more
targets/hal/TARGET_STM/TARGET_STM32F7/pwmout_api.c@610:813dcc80987e, 2015-08-14 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 14 13:15:17 2015 +0100
- Revision:
- 610:813dcc80987e
- Parent:
- 596:abfb2833c9f5
Synchronized with git revision 6d84db41c6833e0b9b024741eb0616a5f62d5599
Full URL: https://github.com/mbedmicro/mbed/commit/6d84db41c6833e0b9b024741eb0616a5f62d5599/
DISCO_F746NG - Improvements
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 573:ad23fe03a082 | 1 | /* mbed Microcontroller Library |
| mbed_official | 573:ad23fe03a082 | 2 | ******************************************************************************* |
| mbed_official | 573:ad23fe03a082 | 3 | * Copyright (c) 2015, STMicroelectronics |
| mbed_official | 573:ad23fe03a082 | 4 | * All rights reserved. |
| mbed_official | 573:ad23fe03a082 | 5 | * |
| mbed_official | 573:ad23fe03a082 | 6 | * Redistribution and use in source and binary forms, with or without |
| mbed_official | 573:ad23fe03a082 | 7 | * modification, are permitted provided that the following conditions are met: |
| mbed_official | 573:ad23fe03a082 | 8 | * |
| mbed_official | 573:ad23fe03a082 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
| mbed_official | 573:ad23fe03a082 | 10 | * this list of conditions and the following disclaimer. |
| mbed_official | 573:ad23fe03a082 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| mbed_official | 573:ad23fe03a082 | 12 | * this list of conditions and the following disclaimer in the documentation |
| mbed_official | 573:ad23fe03a082 | 13 | * and/or other materials provided with the distribution. |
| mbed_official | 573:ad23fe03a082 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| mbed_official | 573:ad23fe03a082 | 15 | * may be used to endorse or promote products derived from this software |
| mbed_official | 573:ad23fe03a082 | 16 | * without specific prior written permission. |
| mbed_official | 573:ad23fe03a082 | 17 | * |
| mbed_official | 573:ad23fe03a082 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| mbed_official | 573:ad23fe03a082 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| mbed_official | 573:ad23fe03a082 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| mbed_official | 573:ad23fe03a082 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| mbed_official | 573:ad23fe03a082 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| mbed_official | 573:ad23fe03a082 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| mbed_official | 573:ad23fe03a082 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| mbed_official | 573:ad23fe03a082 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| mbed_official | 573:ad23fe03a082 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| mbed_official | 573:ad23fe03a082 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| mbed_official | 573:ad23fe03a082 | 28 | ******************************************************************************* |
| mbed_official | 573:ad23fe03a082 | 29 | */ |
| mbed_official | 573:ad23fe03a082 | 30 | #include "pwmout_api.h" |
| mbed_official | 573:ad23fe03a082 | 31 | |
| mbed_official | 573:ad23fe03a082 | 32 | #if DEVICE_PWMOUT |
| mbed_official | 573:ad23fe03a082 | 33 | |
| mbed_official | 573:ad23fe03a082 | 34 | #include "cmsis.h" |
| mbed_official | 573:ad23fe03a082 | 35 | #include "pinmap.h" |
| mbed_official | 573:ad23fe03a082 | 36 | #include "mbed_error.h" |
| mbed_official | 573:ad23fe03a082 | 37 | #include "PeripheralPins.h" |
| mbed_official | 573:ad23fe03a082 | 38 | |
| mbed_official | 573:ad23fe03a082 | 39 | static TIM_HandleTypeDef TimHandle; |
| mbed_official | 573:ad23fe03a082 | 40 | |
| mbed_official | 573:ad23fe03a082 | 41 | void pwmout_init(pwmout_t* obj, PinName pin) |
| mbed_official | 573:ad23fe03a082 | 42 | { |
| mbed_official | 573:ad23fe03a082 | 43 | // Get the peripheral name from the pin and assign it to the object |
| mbed_official | 573:ad23fe03a082 | 44 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
| mbed_official | 610:813dcc80987e | 45 | MBED_ASSERT(obj->pwm != (PWMName)NC); |
| mbed_official | 573:ad23fe03a082 | 46 | |
| mbed_official | 573:ad23fe03a082 | 47 | // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object |
| mbed_official | 573:ad23fe03a082 | 48 | uint32_t function = pinmap_function(pin, PinMap_PWM); |
| mbed_official | 573:ad23fe03a082 | 49 | MBED_ASSERT(function != (uint32_t)NC); |
| mbed_official | 610:813dcc80987e | 50 | obj->channel = STM_PIN_CHANNEL(function); |
| mbed_official | 573:ad23fe03a082 | 51 | obj->inverted = STM_PIN_INVERTED(function); |
| mbed_official | 573:ad23fe03a082 | 52 | |
| mbed_official | 573:ad23fe03a082 | 53 | // Enable TIM clock |
| mbed_official | 610:813dcc80987e | 54 | if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE(); |
| mbed_official | 610:813dcc80987e | 55 | if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE(); |
| mbed_official | 610:813dcc80987e | 56 | if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE(); |
| mbed_official | 610:813dcc80987e | 57 | if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE(); |
| mbed_official | 610:813dcc80987e | 58 | if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE(); |
| mbed_official | 610:813dcc80987e | 59 | if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE(); |
| mbed_official | 582:a89625bcd809 | 60 | if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE(); |
| mbed_official | 582:a89625bcd809 | 61 | if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE(); |
| mbed_official | 596:abfb2833c9f5 | 62 | if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE(); |
| mbed_official | 582:a89625bcd809 | 63 | if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE(); |
| mbed_official | 582:a89625bcd809 | 64 | if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE(); |
| mbed_official | 573:ad23fe03a082 | 65 | |
| mbed_official | 573:ad23fe03a082 | 66 | // Configure GPIO |
| mbed_official | 573:ad23fe03a082 | 67 | pinmap_pinout(pin, PinMap_PWM); |
| mbed_official | 573:ad23fe03a082 | 68 | |
| mbed_official | 573:ad23fe03a082 | 69 | obj->pin = pin; |
| mbed_official | 573:ad23fe03a082 | 70 | obj->period = 0; |
| mbed_official | 573:ad23fe03a082 | 71 | obj->pulse = 0; |
| mbed_official | 573:ad23fe03a082 | 72 | |
| mbed_official | 573:ad23fe03a082 | 73 | pwmout_period_us(obj, 20000); // 20 ms per default |
| mbed_official | 573:ad23fe03a082 | 74 | } |
| mbed_official | 573:ad23fe03a082 | 75 | |
| mbed_official | 573:ad23fe03a082 | 76 | void pwmout_free(pwmout_t* obj) |
| mbed_official | 573:ad23fe03a082 | 77 | { |
| mbed_official | 573:ad23fe03a082 | 78 | // Configure GPIO |
| mbed_official | 573:ad23fe03a082 | 79 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
| mbed_official | 573:ad23fe03a082 | 80 | } |
| mbed_official | 573:ad23fe03a082 | 81 | |
| mbed_official | 573:ad23fe03a082 | 82 | void pwmout_write(pwmout_t* obj, float value) |
| mbed_official | 573:ad23fe03a082 | 83 | { |
| mbed_official | 573:ad23fe03a082 | 84 | TIM_OC_InitTypeDef sConfig; |
| mbed_official | 573:ad23fe03a082 | 85 | int channel = 0; |
| mbed_official | 573:ad23fe03a082 | 86 | |
| mbed_official | 573:ad23fe03a082 | 87 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
| mbed_official | 573:ad23fe03a082 | 88 | |
| mbed_official | 573:ad23fe03a082 | 89 | if (value < (float)0.0) { |
| mbed_official | 573:ad23fe03a082 | 90 | value = 0.0; |
| mbed_official | 573:ad23fe03a082 | 91 | } else if (value > (float)1.0) { |
| mbed_official | 573:ad23fe03a082 | 92 | value = 1.0; |
| mbed_official | 573:ad23fe03a082 | 93 | } |
| mbed_official | 573:ad23fe03a082 | 94 | |
| mbed_official | 573:ad23fe03a082 | 95 | obj->pulse = (uint32_t)((float)obj->period * value); |
| mbed_official | 573:ad23fe03a082 | 96 | |
| mbed_official | 573:ad23fe03a082 | 97 | // Configure channels |
| mbed_official | 573:ad23fe03a082 | 98 | sConfig.OCMode = TIM_OCMODE_PWM1; |
| mbed_official | 573:ad23fe03a082 | 99 | sConfig.Pulse = obj->pulse; |
| mbed_official | 573:ad23fe03a082 | 100 | sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
| mbed_official | 573:ad23fe03a082 | 101 | sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
| mbed_official | 573:ad23fe03a082 | 102 | sConfig.OCFastMode = TIM_OCFAST_DISABLE; |
| mbed_official | 573:ad23fe03a082 | 103 | sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; |
| mbed_official | 573:ad23fe03a082 | 104 | sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
| mbed_official | 573:ad23fe03a082 | 105 | |
| mbed_official | 573:ad23fe03a082 | 106 | switch (obj->channel) { |
| mbed_official | 573:ad23fe03a082 | 107 | case 1: |
| mbed_official | 573:ad23fe03a082 | 108 | channel = TIM_CHANNEL_1; |
| mbed_official | 573:ad23fe03a082 | 109 | break; |
| mbed_official | 573:ad23fe03a082 | 110 | case 2: |
| mbed_official | 573:ad23fe03a082 | 111 | channel = TIM_CHANNEL_2; |
| mbed_official | 573:ad23fe03a082 | 112 | break; |
| mbed_official | 573:ad23fe03a082 | 113 | case 3: |
| mbed_official | 573:ad23fe03a082 | 114 | channel = TIM_CHANNEL_3; |
| mbed_official | 573:ad23fe03a082 | 115 | break; |
| mbed_official | 573:ad23fe03a082 | 116 | case 4: |
| mbed_official | 573:ad23fe03a082 | 117 | channel = TIM_CHANNEL_4; |
| mbed_official | 573:ad23fe03a082 | 118 | break; |
| mbed_official | 573:ad23fe03a082 | 119 | default: |
| mbed_official | 573:ad23fe03a082 | 120 | return; |
| mbed_official | 573:ad23fe03a082 | 121 | } |
| mbed_official | 573:ad23fe03a082 | 122 | |
| mbed_official | 582:a89625bcd809 | 123 | if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { |
| mbed_official | 610:813dcc80987e | 124 | error("Cannot configure PWM channel\n"); |
| mbed_official | 582:a89625bcd809 | 125 | } |
| mbed_official | 582:a89625bcd809 | 126 | |
| mbed_official | 582:a89625bcd809 | 127 | if (obj->inverted) { |
| mbed_official | 573:ad23fe03a082 | 128 | HAL_TIMEx_PWMN_Start(&TimHandle, channel); |
| mbed_official | 573:ad23fe03a082 | 129 | } else { |
| mbed_official | 573:ad23fe03a082 | 130 | HAL_TIM_PWM_Start(&TimHandle, channel); |
| mbed_official | 573:ad23fe03a082 | 131 | } |
| mbed_official | 573:ad23fe03a082 | 132 | } |
| mbed_official | 573:ad23fe03a082 | 133 | |
| mbed_official | 573:ad23fe03a082 | 134 | float pwmout_read(pwmout_t* obj) |
| mbed_official | 573:ad23fe03a082 | 135 | { |
| mbed_official | 573:ad23fe03a082 | 136 | float value = 0; |
| mbed_official | 573:ad23fe03a082 | 137 | if (obj->period > 0) { |
| mbed_official | 573:ad23fe03a082 | 138 | value = (float)(obj->pulse) / (float)(obj->period); |
| mbed_official | 573:ad23fe03a082 | 139 | } |
| mbed_official | 573:ad23fe03a082 | 140 | return ((value > (float)1.0) ? (float)(1.0) : (value)); |
| mbed_official | 573:ad23fe03a082 | 141 | } |
| mbed_official | 573:ad23fe03a082 | 142 | |
| mbed_official | 573:ad23fe03a082 | 143 | void pwmout_period(pwmout_t* obj, float seconds) |
| mbed_official | 573:ad23fe03a082 | 144 | { |
| mbed_official | 573:ad23fe03a082 | 145 | pwmout_period_us(obj, seconds * 1000000.0f); |
| mbed_official | 573:ad23fe03a082 | 146 | } |
| mbed_official | 573:ad23fe03a082 | 147 | |
| mbed_official | 573:ad23fe03a082 | 148 | void pwmout_period_ms(pwmout_t* obj, int ms) |
| mbed_official | 573:ad23fe03a082 | 149 | { |
| mbed_official | 573:ad23fe03a082 | 150 | pwmout_period_us(obj, ms * 1000); |
| mbed_official | 573:ad23fe03a082 | 151 | } |
| mbed_official | 573:ad23fe03a082 | 152 | |
| mbed_official | 573:ad23fe03a082 | 153 | void pwmout_period_us(pwmout_t* obj, int us) |
| mbed_official | 573:ad23fe03a082 | 154 | { |
| mbed_official | 573:ad23fe03a082 | 155 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
| mbed_official | 610:813dcc80987e | 156 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| mbed_official | 573:ad23fe03a082 | 157 | uint32_t PclkFreq; |
| mbed_official | 610:813dcc80987e | 158 | uint32_t APBxCLKDivider; |
| mbed_official | 573:ad23fe03a082 | 159 | float dc = pwmout_read(obj); |
| mbed_official | 573:ad23fe03a082 | 160 | |
| mbed_official | 573:ad23fe03a082 | 161 | __HAL_TIM_DISABLE(&TimHandle); |
| mbed_official | 573:ad23fe03a082 | 162 | |
| mbed_official | 610:813dcc80987e | 163 | // Get clock configuration |
| mbed_official | 610:813dcc80987e | 164 | // Note: PclkFreq contains here the Latency (not used after) |
| mbed_official | 610:813dcc80987e | 165 | HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); |
| mbed_official | 610:813dcc80987e | 166 | |
| mbed_official | 610:813dcc80987e | 167 | // Get the PCLK and APBCLK divider related to the timer |
| mbed_official | 573:ad23fe03a082 | 168 | switch (obj->pwm) { |
| mbed_official | 610:813dcc80987e | 169 | |
| mbed_official | 610:813dcc80987e | 170 | // APB1 clock |
| mbed_official | 573:ad23fe03a082 | 171 | case PWM_2: |
| mbed_official | 573:ad23fe03a082 | 172 | case PWM_3: |
| mbed_official | 573:ad23fe03a082 | 173 | case PWM_4: |
| mbed_official | 582:a89625bcd809 | 174 | case PWM_5: |
| mbed_official | 582:a89625bcd809 | 175 | case PWM_12: |
| mbed_official | 582:a89625bcd809 | 176 | case PWM_13: |
| mbed_official | 582:a89625bcd809 | 177 | case PWM_14: |
| mbed_official | 582:a89625bcd809 | 178 | PclkFreq = HAL_RCC_GetPCLK1Freq(); |
| mbed_official | 610:813dcc80987e | 179 | APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider; |
| mbed_official | 573:ad23fe03a082 | 180 | break; |
| mbed_official | 610:813dcc80987e | 181 | |
| mbed_official | 610:813dcc80987e | 182 | // APB2 clock |
| mbed_official | 582:a89625bcd809 | 183 | case PWM_1: |
| mbed_official | 582:a89625bcd809 | 184 | case PWM_8: |
| mbed_official | 573:ad23fe03a082 | 185 | case PWM_9: |
| mbed_official | 573:ad23fe03a082 | 186 | case PWM_10: |
| mbed_official | 573:ad23fe03a082 | 187 | case PWM_11: |
| mbed_official | 582:a89625bcd809 | 188 | PclkFreq = HAL_RCC_GetPCLK2Freq(); |
| mbed_official | 610:813dcc80987e | 189 | APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider; |
| mbed_official | 573:ad23fe03a082 | 190 | break; |
| mbed_official | 573:ad23fe03a082 | 191 | default: |
| mbed_official | 573:ad23fe03a082 | 192 | return; |
| mbed_official | 573:ad23fe03a082 | 193 | } |
| mbed_official | 573:ad23fe03a082 | 194 | |
| mbed_official | 573:ad23fe03a082 | 195 | TimHandle.Init.Period = us - 1; |
| mbed_official | 610:813dcc80987e | 196 | // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx |
| mbed_official | 610:813dcc80987e | 197 | if (APBxCLKDivider == RCC_HCLK_DIV1) |
| mbed_official | 610:813dcc80987e | 198 | TimHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 µs tick |
| mbed_official | 610:813dcc80987e | 199 | else |
| mbed_official | 610:813dcc80987e | 200 | TimHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 µs tick |
| mbed_official | 573:ad23fe03a082 | 201 | TimHandle.Init.ClockDivision = 0; |
| mbed_official | 573:ad23fe03a082 | 202 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
| mbed_official | 582:a89625bcd809 | 203 | |
| mbed_official | 582:a89625bcd809 | 204 | if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { |
| mbed_official | 610:813dcc80987e | 205 | error("Cannot initialize PWM\n"); |
| mbed_official | 582:a89625bcd809 | 206 | } |
| mbed_official | 573:ad23fe03a082 | 207 | |
| mbed_official | 573:ad23fe03a082 | 208 | // Set duty cycle again |
| mbed_official | 573:ad23fe03a082 | 209 | pwmout_write(obj, dc); |
| mbed_official | 573:ad23fe03a082 | 210 | |
| mbed_official | 573:ad23fe03a082 | 211 | // Save for future use |
| mbed_official | 573:ad23fe03a082 | 212 | obj->period = us; |
| mbed_official | 573:ad23fe03a082 | 213 | |
| mbed_official | 573:ad23fe03a082 | 214 | __HAL_TIM_ENABLE(&TimHandle); |
| mbed_official | 573:ad23fe03a082 | 215 | } |
| mbed_official | 573:ad23fe03a082 | 216 | |
| mbed_official | 573:ad23fe03a082 | 217 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
| mbed_official | 573:ad23fe03a082 | 218 | { |
| mbed_official | 573:ad23fe03a082 | 219 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
| mbed_official | 573:ad23fe03a082 | 220 | } |
| mbed_official | 573:ad23fe03a082 | 221 | |
| mbed_official | 573:ad23fe03a082 | 222 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
| mbed_official | 573:ad23fe03a082 | 223 | { |
| mbed_official | 573:ad23fe03a082 | 224 | pwmout_pulsewidth_us(obj, ms * 1000); |
| mbed_official | 573:ad23fe03a082 | 225 | } |
| mbed_official | 573:ad23fe03a082 | 226 | |
| mbed_official | 573:ad23fe03a082 | 227 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
| mbed_official | 573:ad23fe03a082 | 228 | { |
| mbed_official | 573:ad23fe03a082 | 229 | float value = (float)us / (float)obj->period; |
| mbed_official | 573:ad23fe03a082 | 230 | pwmout_write(obj, value); |
| mbed_official | 573:ad23fe03a082 | 231 | } |
| mbed_official | 573:ad23fe03a082 | 232 | |
| mbed_official | 573:ad23fe03a082 | 233 | #endif |

