mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_DISCO_F051R8/pwmout_api.c@130:1dec54e4aec3, 2014-03-21 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Mar 21 11:45:09 2014 +0000
- Revision:
- 130:1dec54e4aec3
Synchronized with git revision e5c9ff6781a4e277a5a454e5a0b037f76e31739d
Full URL: https://github.com/mbedmicro/mbed/commit/e5c9ff6781a4e277a5a454e5a0b037f76e31739d/
STM32F0-Discovery (STM32F051R8) initial port
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 130:1dec54e4aec3 | 1 | /* mbed Microcontroller Library |
mbed_official | 130:1dec54e4aec3 | 2 | ******************************************************************************* |
mbed_official | 130:1dec54e4aec3 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 130:1dec54e4aec3 | 4 | * All rights reserved. |
mbed_official | 130:1dec54e4aec3 | 5 | * |
mbed_official | 130:1dec54e4aec3 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 130:1dec54e4aec3 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 130:1dec54e4aec3 | 8 | * |
mbed_official | 130:1dec54e4aec3 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 130:1dec54e4aec3 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 130:1dec54e4aec3 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 130:1dec54e4aec3 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 130:1dec54e4aec3 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 130:1dec54e4aec3 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 130:1dec54e4aec3 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 130:1dec54e4aec3 | 16 | * without specific prior written permission. |
mbed_official | 130:1dec54e4aec3 | 17 | * |
mbed_official | 130:1dec54e4aec3 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 130:1dec54e4aec3 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 130:1dec54e4aec3 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 130:1dec54e4aec3 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 130:1dec54e4aec3 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 130:1dec54e4aec3 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 130:1dec54e4aec3 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 130:1dec54e4aec3 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 130:1dec54e4aec3 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 130:1dec54e4aec3 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 130:1dec54e4aec3 | 28 | ******************************************************************************* |
mbed_official | 130:1dec54e4aec3 | 29 | */ |
mbed_official | 130:1dec54e4aec3 | 30 | #include "pwmout_api.h" |
mbed_official | 130:1dec54e4aec3 | 31 | |
mbed_official | 130:1dec54e4aec3 | 32 | #include "cmsis.h" |
mbed_official | 130:1dec54e4aec3 | 33 | #include "pinmap.h" |
mbed_official | 130:1dec54e4aec3 | 34 | #include "error.h" |
mbed_official | 130:1dec54e4aec3 | 35 | |
mbed_official | 130:1dec54e4aec3 | 36 | static const PinMap PinMap_PWM[] = { |
mbed_official | 130:1dec54e4aec3 | 37 | {PA_7, TIM_14, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_4)}, // TIM14_CH1 |
mbed_official | 130:1dec54e4aec3 | 38 | {PC_7, TIM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, // TIM3_CH2 |
mbed_official | 130:1dec54e4aec3 | 39 | {PB_6, TIM_16, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_2)}, // TIM16_CH1N --> FAIL |
mbed_official | 130:1dec54e4aec3 | 40 | {NC, NC, 0} |
mbed_official | 130:1dec54e4aec3 | 41 | }; |
mbed_official | 130:1dec54e4aec3 | 42 | |
mbed_official | 130:1dec54e4aec3 | 43 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 130:1dec54e4aec3 | 44 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 130:1dec54e4aec3 | 45 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 130:1dec54e4aec3 | 46 | |
mbed_official | 130:1dec54e4aec3 | 47 | if (obj->pwm == (PWMName)NC) { |
mbed_official | 130:1dec54e4aec3 | 48 | error("PWM pinout mapping failed"); |
mbed_official | 130:1dec54e4aec3 | 49 | } |
mbed_official | 130:1dec54e4aec3 | 50 | |
mbed_official | 130:1dec54e4aec3 | 51 | // Enable TIM clock |
mbed_official | 130:1dec54e4aec3 | 52 | if (obj->pwm == TIM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 53 | if (obj->pwm == TIM_14) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 54 | if (obj->pwm == TIM_16) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 55 | |
mbed_official | 130:1dec54e4aec3 | 56 | // Configure GPIO |
mbed_official | 130:1dec54e4aec3 | 57 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 130:1dec54e4aec3 | 58 | //pin_mode(pin, PullUp); |
mbed_official | 130:1dec54e4aec3 | 59 | |
mbed_official | 130:1dec54e4aec3 | 60 | obj->pin = pin; |
mbed_official | 130:1dec54e4aec3 | 61 | obj->period = 0; |
mbed_official | 130:1dec54e4aec3 | 62 | obj->pulse = 0; |
mbed_official | 130:1dec54e4aec3 | 63 | |
mbed_official | 130:1dec54e4aec3 | 64 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 130:1dec54e4aec3 | 65 | } |
mbed_official | 130:1dec54e4aec3 | 66 | |
mbed_official | 130:1dec54e4aec3 | 67 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 130:1dec54e4aec3 | 68 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 130:1dec54e4aec3 | 69 | TIM_DeInit(tim); |
mbed_official | 130:1dec54e4aec3 | 70 | } |
mbed_official | 130:1dec54e4aec3 | 71 | |
mbed_official | 130:1dec54e4aec3 | 72 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 130:1dec54e4aec3 | 73 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 130:1dec54e4aec3 | 74 | TIM_OCInitTypeDef TIM_OCInitStructure; |
mbed_official | 130:1dec54e4aec3 | 75 | |
mbed_official | 130:1dec54e4aec3 | 76 | if (value < 0.0) { |
mbed_official | 130:1dec54e4aec3 | 77 | value = 0.0; |
mbed_official | 130:1dec54e4aec3 | 78 | } else if (value > 1.0) { |
mbed_official | 130:1dec54e4aec3 | 79 | value = 1.0; |
mbed_official | 130:1dec54e4aec3 | 80 | } |
mbed_official | 130:1dec54e4aec3 | 81 | |
mbed_official | 130:1dec54e4aec3 | 82 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 130:1dec54e4aec3 | 83 | |
mbed_official | 130:1dec54e4aec3 | 84 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
mbed_official | 130:1dec54e4aec3 | 85 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
mbed_official | 130:1dec54e4aec3 | 86 | |
mbed_official | 130:1dec54e4aec3 | 87 | // Configure channel 1 |
mbed_official | 130:1dec54e4aec3 | 88 | if (obj->pin == PA_7) { |
mbed_official | 130:1dec54e4aec3 | 89 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 130:1dec54e4aec3 | 90 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 130:1dec54e4aec3 | 91 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 130:1dec54e4aec3 | 92 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 130:1dec54e4aec3 | 93 | } |
mbed_official | 130:1dec54e4aec3 | 94 | |
mbed_official | 130:1dec54e4aec3 | 95 | // Configure channel 1N |
mbed_official | 130:1dec54e4aec3 | 96 | if (obj->pin == PB_6) { |
mbed_official | 130:1dec54e4aec3 | 97 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; |
mbed_official | 130:1dec54e4aec3 | 98 | TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; |
mbed_official | 130:1dec54e4aec3 | 99 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 130:1dec54e4aec3 | 100 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 130:1dec54e4aec3 | 101 | } |
mbed_official | 130:1dec54e4aec3 | 102 | |
mbed_official | 130:1dec54e4aec3 | 103 | // Configure channel 2 |
mbed_official | 130:1dec54e4aec3 | 104 | if (obj->pin == PC_7) { |
mbed_official | 130:1dec54e4aec3 | 105 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 130:1dec54e4aec3 | 106 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 130:1dec54e4aec3 | 107 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 130:1dec54e4aec3 | 108 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 130:1dec54e4aec3 | 109 | } |
mbed_official | 130:1dec54e4aec3 | 110 | } |
mbed_official | 130:1dec54e4aec3 | 111 | |
mbed_official | 130:1dec54e4aec3 | 112 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 130:1dec54e4aec3 | 113 | float value = 0; |
mbed_official | 130:1dec54e4aec3 | 114 | if (obj->period > 0) { |
mbed_official | 130:1dec54e4aec3 | 115 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 130:1dec54e4aec3 | 116 | } |
mbed_official | 130:1dec54e4aec3 | 117 | return ((value > 1.0) ? (1.0) : (value)); |
mbed_official | 130:1dec54e4aec3 | 118 | } |
mbed_official | 130:1dec54e4aec3 | 119 | |
mbed_official | 130:1dec54e4aec3 | 120 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 130:1dec54e4aec3 | 121 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 130:1dec54e4aec3 | 122 | } |
mbed_official | 130:1dec54e4aec3 | 123 | |
mbed_official | 130:1dec54e4aec3 | 124 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 130:1dec54e4aec3 | 125 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 130:1dec54e4aec3 | 126 | } |
mbed_official | 130:1dec54e4aec3 | 127 | |
mbed_official | 130:1dec54e4aec3 | 128 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 130:1dec54e4aec3 | 129 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 130:1dec54e4aec3 | 130 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
mbed_official | 130:1dec54e4aec3 | 131 | float dc = pwmout_read(obj); |
mbed_official | 130:1dec54e4aec3 | 132 | |
mbed_official | 130:1dec54e4aec3 | 133 | TIM_Cmd(tim, DISABLE); |
mbed_official | 130:1dec54e4aec3 | 134 | |
mbed_official | 130:1dec54e4aec3 | 135 | obj->period = us; |
mbed_official | 130:1dec54e4aec3 | 136 | |
mbed_official | 130:1dec54e4aec3 | 137 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
mbed_official | 130:1dec54e4aec3 | 138 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 130:1dec54e4aec3 | 139 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
mbed_official | 130:1dec54e4aec3 | 140 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
mbed_official | 130:1dec54e4aec3 | 141 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
mbed_official | 130:1dec54e4aec3 | 142 | |
mbed_official | 130:1dec54e4aec3 | 143 | // Set duty cycle again |
mbed_official | 130:1dec54e4aec3 | 144 | pwmout_write(obj, dc); |
mbed_official | 130:1dec54e4aec3 | 145 | |
mbed_official | 130:1dec54e4aec3 | 146 | TIM_ARRPreloadConfig(tim, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 147 | |
mbed_official | 130:1dec54e4aec3 | 148 | TIM_Cmd(tim, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 149 | } |
mbed_official | 130:1dec54e4aec3 | 150 | |
mbed_official | 130:1dec54e4aec3 | 151 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 130:1dec54e4aec3 | 152 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 130:1dec54e4aec3 | 153 | } |
mbed_official | 130:1dec54e4aec3 | 154 | |
mbed_official | 130:1dec54e4aec3 | 155 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 130:1dec54e4aec3 | 156 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 130:1dec54e4aec3 | 157 | } |
mbed_official | 130:1dec54e4aec3 | 158 | |
mbed_official | 130:1dec54e4aec3 | 159 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 130:1dec54e4aec3 | 160 | float value = (float)us / (float)obj->period; |
mbed_official | 130:1dec54e4aec3 | 161 | pwmout_write(obj, value); |
mbed_official | 130:1dec54e4aec3 | 162 | } |