mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pwmout_api.c@76:aeb1df146756, 2014-01-27 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Jan 27 14:30:07 2014 +0000
- Revision:
- 76:aeb1df146756
- Child:
- 84:f54042cbc282
Synchronized with git revision a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd
Full URL: https://github.com/mbedmicro/mbed/commit/a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd/
Add NUCLEO_L152RE
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 | 76:aeb1df146756 | 32 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 33 | #include "pinmap.h" |
mbed_official | 76:aeb1df146756 | 34 | #include "error.h" |
mbed_official | 76:aeb1df146756 | 35 | |
mbed_official | 76:aeb1df146756 | 36 | static const PinMap PinMap_PWM[] = { |
mbed_official | 76:aeb1df146756 | 37 | {PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM2)}, // TIM2_CH2 |
mbed_official | 76:aeb1df146756 | 38 | {PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH1 |
mbed_official | 76:aeb1df146756 | 39 | //{PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM2)}, // TIM2_CH3 |
mbed_official | 76:aeb1df146756 | 40 | //{PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_TIM3)}, // TIM3_CH2 |
mbed_official | 76:aeb1df146756 | 41 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 42 | }; |
mbed_official | 76:aeb1df146756 | 43 | |
mbed_official | 76:aeb1df146756 | 44 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 76:aeb1df146756 | 45 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 76:aeb1df146756 | 46 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 76:aeb1df146756 | 47 | |
mbed_official | 76:aeb1df146756 | 48 | if (obj->pwm == (PWMName)NC) { |
mbed_official | 76:aeb1df146756 | 49 | error("PWM pinout mapping failed"); |
mbed_official | 76:aeb1df146756 | 50 | } |
mbed_official | 76:aeb1df146756 | 51 | |
mbed_official | 76:aeb1df146756 | 52 | // Enable TIM clock |
mbed_official | 76:aeb1df146756 | 53 | if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); |
mbed_official | 76:aeb1df146756 | 54 | if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); |
mbed_official | 76:aeb1df146756 | 55 | |
mbed_official | 76:aeb1df146756 | 56 | // Configure GPIO |
mbed_official | 76:aeb1df146756 | 57 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 76:aeb1df146756 | 58 | |
mbed_official | 76:aeb1df146756 | 59 | obj->pin = pin; |
mbed_official | 76:aeb1df146756 | 60 | obj->period = 0; |
mbed_official | 76:aeb1df146756 | 61 | obj->pulse = 0; |
mbed_official | 76:aeb1df146756 | 62 | |
mbed_official | 76:aeb1df146756 | 63 | pwmout_period_us(obj, 20000); // 20 ms per default |
mbed_official | 76:aeb1df146756 | 64 | } |
mbed_official | 76:aeb1df146756 | 65 | |
mbed_official | 76:aeb1df146756 | 66 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 76:aeb1df146756 | 67 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 76:aeb1df146756 | 68 | TIM_DeInit(tim); |
mbed_official | 76:aeb1df146756 | 69 | } |
mbed_official | 76:aeb1df146756 | 70 | |
mbed_official | 76:aeb1df146756 | 71 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 76:aeb1df146756 | 72 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 76:aeb1df146756 | 73 | TIM_OCInitTypeDef TIM_OCInitStructure; |
mbed_official | 76:aeb1df146756 | 74 | |
mbed_official | 76:aeb1df146756 | 75 | if (value < 0.0) { |
mbed_official | 76:aeb1df146756 | 76 | value = 0.0; |
mbed_official | 76:aeb1df146756 | 77 | } else if (value > 1.0) { |
mbed_official | 76:aeb1df146756 | 78 | value = 1.0; |
mbed_official | 76:aeb1df146756 | 79 | } |
mbed_official | 76:aeb1df146756 | 80 | |
mbed_official | 76:aeb1df146756 | 81 | //while(TIM_GetFlagStatus(tim, TIM_FLAG_Update) == RESET); |
mbed_official | 76:aeb1df146756 | 82 | //TIM_ClearFlag(tim, TIM_FLAG_Update); |
mbed_official | 76:aeb1df146756 | 83 | |
mbed_official | 76:aeb1df146756 | 84 | obj->pulse = (uint32_t)((float)obj->period * value); |
mbed_official | 76:aeb1df146756 | 85 | |
mbed_official | 76:aeb1df146756 | 86 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; |
mbed_official | 76:aeb1df146756 | 87 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; |
mbed_official | 76:aeb1df146756 | 88 | TIM_OCInitStructure.TIM_Pulse = obj->pulse; |
mbed_official | 76:aeb1df146756 | 89 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; |
mbed_official | 76:aeb1df146756 | 90 | |
mbed_official | 76:aeb1df146756 | 91 | // Configure channel 1 |
mbed_official | 76:aeb1df146756 | 92 | if (obj->pin == PB_4) { |
mbed_official | 76:aeb1df146756 | 93 | TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 76:aeb1df146756 | 94 | TIM_OC1Init(tim, &TIM_OCInitStructure); |
mbed_official | 76:aeb1df146756 | 95 | } |
mbed_official | 76:aeb1df146756 | 96 | |
mbed_official | 76:aeb1df146756 | 97 | // Configure channel 2 |
mbed_official | 76:aeb1df146756 | 98 | if (obj->pin == PB_3) { |
mbed_official | 76:aeb1df146756 | 99 | TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable); |
mbed_official | 76:aeb1df146756 | 100 | TIM_OC2Init(tim, &TIM_OCInitStructure); |
mbed_official | 76:aeb1df146756 | 101 | } |
mbed_official | 76:aeb1df146756 | 102 | } |
mbed_official | 76:aeb1df146756 | 103 | |
mbed_official | 76:aeb1df146756 | 104 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 76:aeb1df146756 | 105 | float value = 0; |
mbed_official | 76:aeb1df146756 | 106 | if (obj->period > 0) { |
mbed_official | 76:aeb1df146756 | 107 | value = (float)(obj->pulse) / (float)(obj->period); |
mbed_official | 76:aeb1df146756 | 108 | } |
mbed_official | 76:aeb1df146756 | 109 | return ((value > 1.0) ? (1.0) : (value)); |
mbed_official | 76:aeb1df146756 | 110 | } |
mbed_official | 76:aeb1df146756 | 111 | |
mbed_official | 76:aeb1df146756 | 112 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 76:aeb1df146756 | 113 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 76:aeb1df146756 | 114 | } |
mbed_official | 76:aeb1df146756 | 115 | |
mbed_official | 76:aeb1df146756 | 116 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 76:aeb1df146756 | 117 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 76:aeb1df146756 | 118 | } |
mbed_official | 76:aeb1df146756 | 119 | |
mbed_official | 76:aeb1df146756 | 120 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 76:aeb1df146756 | 121 | TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); |
mbed_official | 76:aeb1df146756 | 122 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; |
mbed_official | 76:aeb1df146756 | 123 | float dc = pwmout_read(obj); |
mbed_official | 76:aeb1df146756 | 124 | |
mbed_official | 76:aeb1df146756 | 125 | TIM_Cmd(tim, DISABLE); |
mbed_official | 76:aeb1df146756 | 126 | |
mbed_official | 76:aeb1df146756 | 127 | obj->period = us; |
mbed_official | 76:aeb1df146756 | 128 | |
mbed_official | 76:aeb1df146756 | 129 | TIM_TimeBaseStructure.TIM_Period = obj->period - 1; |
mbed_official | 76:aeb1df146756 | 130 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick |
mbed_official | 76:aeb1df146756 | 131 | TIM_TimeBaseStructure.TIM_ClockDivision = 0; |
mbed_official | 76:aeb1df146756 | 132 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; |
mbed_official | 76:aeb1df146756 | 133 | TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); |
mbed_official | 76:aeb1df146756 | 134 | |
mbed_official | 76:aeb1df146756 | 135 | // Set duty cycle again |
mbed_official | 76:aeb1df146756 | 136 | pwmout_write(obj, dc); |
mbed_official | 76:aeb1df146756 | 137 | |
mbed_official | 76:aeb1df146756 | 138 | TIM_ARRPreloadConfig(tim, ENABLE); |
mbed_official | 76:aeb1df146756 | 139 | TIM_Cmd(tim, ENABLE); |
mbed_official | 76:aeb1df146756 | 140 | } |
mbed_official | 76:aeb1df146756 | 141 | |
mbed_official | 76:aeb1df146756 | 142 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 76:aeb1df146756 | 143 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 76:aeb1df146756 | 144 | } |
mbed_official | 76:aeb1df146756 | 145 | |
mbed_official | 76:aeb1df146756 | 146 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 76:aeb1df146756 | 147 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 76:aeb1df146756 | 148 | } |
mbed_official | 76:aeb1df146756 | 149 | |
mbed_official | 76:aeb1df146756 | 150 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 76:aeb1df146756 | 151 | float value = (float)us / (float)obj->period; |
mbed_official | 76:aeb1df146756 | 152 | pwmout_write(obj, value); |
mbed_official | 76:aeb1df146756 | 153 | } |