Mbed for VNG board

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Nov 25 07:15:09 2014 +0000
Revision:
414:4ec4c5b614b0
Parent:
285:31249416b6f9
Synchronized with git revision fbc74e874ac089c6cb05add312fb5422be628886

Full URL: https://github.com/mbedmicro/mbed/commit/fbc74e874ac089c6cb05add312fb5422be628886/

Targets: NUCLEOs - Add PeripheralPins files for all nucleo targets

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 237:f3da66175598 1 /* mbed Microcontroller Library
mbed_official 237:f3da66175598 2 *******************************************************************************
mbed_official 237:f3da66175598 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 237:f3da66175598 4 * All rights reserved.
mbed_official 237:f3da66175598 5 *
mbed_official 237:f3da66175598 6 * Redistribution and use in source and binary forms, with or without
mbed_official 237:f3da66175598 7 * modification, are permitted provided that the following conditions are met:
mbed_official 237:f3da66175598 8 *
mbed_official 237:f3da66175598 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 237:f3da66175598 10 * this list of conditions and the following disclaimer.
mbed_official 237:f3da66175598 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 237:f3da66175598 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 237:f3da66175598 13 * and/or other materials provided with the distribution.
mbed_official 237:f3da66175598 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 237:f3da66175598 15 * may be used to endorse or promote products derived from this software
mbed_official 237:f3da66175598 16 * without specific prior written permission.
mbed_official 237:f3da66175598 17 *
mbed_official 237:f3da66175598 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 237:f3da66175598 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 237:f3da66175598 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 237:f3da66175598 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 237:f3da66175598 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 237:f3da66175598 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 237:f3da66175598 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 237:f3da66175598 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 237:f3da66175598 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 237:f3da66175598 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 237:f3da66175598 28 *******************************************************************************
mbed_official 237:f3da66175598 29 */
mbed_official 237:f3da66175598 30 #include "pwmout_api.h"
mbed_official 237:f3da66175598 31
mbed_official 237:f3da66175598 32 #if DEVICE_PWMOUT
mbed_official 237:f3da66175598 33
mbed_official 237:f3da66175598 34 #include "cmsis.h"
mbed_official 237:f3da66175598 35 #include "pinmap.h"
mbed_official 285:31249416b6f9 36 #include "mbed_error.h"
mbed_official 414:4ec4c5b614b0 37 #include "PeripheralPins.h"
mbed_official 237:f3da66175598 38
mbed_official 237:f3da66175598 39 static TIM_HandleTypeDef TimHandle;
mbed_official 237:f3da66175598 40
mbed_official 237:f3da66175598 41 void pwmout_init(pwmout_t* obj, PinName pin)
mbed_official 237:f3da66175598 42 {
mbed_official 237:f3da66175598 43 // Get the peripheral name from the pin and assign it to the object
mbed_official 237:f3da66175598 44 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 237:f3da66175598 45
mbed_official 237:f3da66175598 46 if (obj->pwm == (PWMName)NC) {
mbed_official 237:f3da66175598 47 error("PWM error: pinout mapping failed.");
mbed_official 237:f3da66175598 48 }
mbed_official 237:f3da66175598 49
mbed_official 237:f3da66175598 50 // Enable TIM clock
mbed_official 237:f3da66175598 51 if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
mbed_official 237:f3da66175598 52 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
mbed_official 237:f3da66175598 53 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
mbed_official 237:f3da66175598 54 if (obj->pwm == PWM_15) __TIM15_CLK_ENABLE();
mbed_official 237:f3da66175598 55 if (obj->pwm == PWM_16) __TIM16_CLK_ENABLE();
mbed_official 237:f3da66175598 56 if (obj->pwm == PWM_17) __TIM17_CLK_ENABLE();
mbed_official 237:f3da66175598 57
mbed_official 237:f3da66175598 58 // Configure GPIO
mbed_official 237:f3da66175598 59 pinmap_pinout(pin, PinMap_PWM);
mbed_official 237:f3da66175598 60
mbed_official 237:f3da66175598 61 obj->pin = pin;
mbed_official 237:f3da66175598 62 obj->period = 0;
mbed_official 237:f3da66175598 63 obj->pulse = 0;
mbed_official 237:f3da66175598 64
mbed_official 237:f3da66175598 65 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 237:f3da66175598 66 }
mbed_official 237:f3da66175598 67
mbed_official 237:f3da66175598 68 void pwmout_free(pwmout_t* obj)
mbed_official 237:f3da66175598 69 {
mbed_official 237:f3da66175598 70 // Configure GPIO
mbed_official 237:f3da66175598 71 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 237:f3da66175598 72 }
mbed_official 237:f3da66175598 73
mbed_official 237:f3da66175598 74 void pwmout_write(pwmout_t* obj, float value)
mbed_official 237:f3da66175598 75 {
mbed_official 237:f3da66175598 76 TIM_OC_InitTypeDef sConfig;
mbed_official 237:f3da66175598 77 int channel = 0;
mbed_official 237:f3da66175598 78 int complementary_channel = 0;
mbed_official 237:f3da66175598 79
mbed_official 237:f3da66175598 80 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 237:f3da66175598 81
mbed_official 237:f3da66175598 82 if (value < (float)0.0) {
mbed_official 237:f3da66175598 83 value = 0.0;
mbed_official 237:f3da66175598 84 } else if (value > (float)1.0) {
mbed_official 237:f3da66175598 85 value = 1.0;
mbed_official 237:f3da66175598 86 }
mbed_official 237:f3da66175598 87
mbed_official 237:f3da66175598 88 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 237:f3da66175598 89
mbed_official 237:f3da66175598 90 // Configure channels
mbed_official 237:f3da66175598 91 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 237:f3da66175598 92 sConfig.Pulse = obj->pulse;
mbed_official 237:f3da66175598 93 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 237:f3da66175598 94 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
mbed_official 237:f3da66175598 95 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 237:f3da66175598 96 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
mbed_official 237:f3da66175598 97 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
mbed_official 237:f3da66175598 98
mbed_official 237:f3da66175598 99 switch (obj->pin) {
mbed_official 237:f3da66175598 100
mbed_official 237:f3da66175598 101 // Channels 1
mbed_official 237:f3da66175598 102 case PA_2:
mbed_official 237:f3da66175598 103 case PA_6:
mbed_official 237:f3da66175598 104 case PA_7:
mbed_official 237:f3da66175598 105 case PA_8:
mbed_official 237:f3da66175598 106 case PA_12:
mbed_official 237:f3da66175598 107 case PB_4:
mbed_official 237:f3da66175598 108 case PB_5:
mbed_official 237:f3da66175598 109 case PB_8:
mbed_official 237:f3da66175598 110 case PB_9:
mbed_official 237:f3da66175598 111 case PB_14:
mbed_official 237:f3da66175598 112 case PC_0:
mbed_official 237:f3da66175598 113 case PC_6:
mbed_official 237:f3da66175598 114 channel = TIM_CHANNEL_1;
mbed_official 237:f3da66175598 115 break;
mbed_official 237:f3da66175598 116
mbed_official 237:f3da66175598 117 // Channels 1N
mbed_official 237:f3da66175598 118 case PA_1:
mbed_official 237:f3da66175598 119 case PA_13:
mbed_official 237:f3da66175598 120 case PB_6:
mbed_official 237:f3da66175598 121 case PB_13:
mbed_official 237:f3da66175598 122 case PC_13:
mbed_official 237:f3da66175598 123 channel = TIM_CHANNEL_1;
mbed_official 237:f3da66175598 124 complementary_channel = 1;
mbed_official 237:f3da66175598 125 break;
mbed_official 237:f3da66175598 126
mbed_official 237:f3da66175598 127 // Channels 2
mbed_official 237:f3da66175598 128 case PA_3:
mbed_official 237:f3da66175598 129 case PA_4:
mbed_official 237:f3da66175598 130 case PA_9:
mbed_official 237:f3da66175598 131 case PB_15:
mbed_official 237:f3da66175598 132 case PC_1:
mbed_official 237:f3da66175598 133 case PC_7:
mbed_official 237:f3da66175598 134 channel = TIM_CHANNEL_2;
mbed_official 237:f3da66175598 135 break;
mbed_official 237:f3da66175598 136
mbed_official 237:f3da66175598 137 // Channels 3
mbed_official 237:f3da66175598 138 case PA_10:
mbed_official 237:f3da66175598 139 case PB_0:
mbed_official 237:f3da66175598 140 case PC_2:
mbed_official 237:f3da66175598 141 case PC_8:
mbed_official 237:f3da66175598 142 channel = TIM_CHANNEL_3;
mbed_official 237:f3da66175598 143 break;
mbed_official 237:f3da66175598 144
mbed_official 237:f3da66175598 145 // Channels 3N
mbed_official 237:f3da66175598 146 case PF_0:
mbed_official 237:f3da66175598 147 channel = TIM_CHANNEL_3;
mbed_official 237:f3da66175598 148 complementary_channel = 1;
mbed_official 237:f3da66175598 149 break;
mbed_official 237:f3da66175598 150
mbed_official 237:f3da66175598 151 // Channels 4
mbed_official 237:f3da66175598 152 case PA_11:
mbed_official 237:f3da66175598 153 case PB_1:
mbed_official 237:f3da66175598 154 case PB_7:
mbed_official 237:f3da66175598 155 case PC_3:
mbed_official 237:f3da66175598 156 case PC_9:
mbed_official 237:f3da66175598 157 channel = TIM_CHANNEL_4;
mbed_official 237:f3da66175598 158 break;
mbed_official 237:f3da66175598 159
mbed_official 237:f3da66175598 160 default:
mbed_official 237:f3da66175598 161 return;
mbed_official 237:f3da66175598 162 }
mbed_official 237:f3da66175598 163
mbed_official 237:f3da66175598 164 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
mbed_official 237:f3da66175598 165
mbed_official 237:f3da66175598 166 if (complementary_channel) {
mbed_official 237:f3da66175598 167 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
mbed_official 237:f3da66175598 168 } else {
mbed_official 237:f3da66175598 169 HAL_TIM_PWM_Start(&TimHandle, channel);
mbed_official 237:f3da66175598 170 }
mbed_official 237:f3da66175598 171 }
mbed_official 237:f3da66175598 172
mbed_official 237:f3da66175598 173 float pwmout_read(pwmout_t* obj)
mbed_official 237:f3da66175598 174 {
mbed_official 237:f3da66175598 175 float value = 0;
mbed_official 237:f3da66175598 176 if (obj->period > 0) {
mbed_official 237:f3da66175598 177 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 237:f3da66175598 178 }
mbed_official 237:f3da66175598 179 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 237:f3da66175598 180 }
mbed_official 237:f3da66175598 181
mbed_official 237:f3da66175598 182 void pwmout_period(pwmout_t* obj, float seconds)
mbed_official 237:f3da66175598 183 {
mbed_official 237:f3da66175598 184 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 237:f3da66175598 185 }
mbed_official 237:f3da66175598 186
mbed_official 237:f3da66175598 187 void pwmout_period_ms(pwmout_t* obj, int ms)
mbed_official 237:f3da66175598 188 {
mbed_official 237:f3da66175598 189 pwmout_period_us(obj, ms * 1000);
mbed_official 237:f3da66175598 190 }
mbed_official 237:f3da66175598 191
mbed_official 237:f3da66175598 192 void pwmout_period_us(pwmout_t* obj, int us)
mbed_official 237:f3da66175598 193 {
mbed_official 237:f3da66175598 194 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 237:f3da66175598 195
mbed_official 237:f3da66175598 196 float dc = pwmout_read(obj);
mbed_official 237:f3da66175598 197
mbed_official 237:f3da66175598 198 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 237:f3da66175598 199
mbed_official 237:f3da66175598 200 // Update the SystemCoreClock variable
mbed_official 237:f3da66175598 201 SystemCoreClockUpdate();
mbed_official 237:f3da66175598 202
mbed_official 237:f3da66175598 203 TimHandle.Init.Period = us - 1;
mbed_official 237:f3da66175598 204 TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
mbed_official 237:f3da66175598 205 TimHandle.Init.ClockDivision = 0;
mbed_official 237:f3da66175598 206 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 237:f3da66175598 207 HAL_TIM_PWM_Init(&TimHandle);
mbed_official 237:f3da66175598 208
mbed_official 237:f3da66175598 209 // Set duty cycle again
mbed_official 237:f3da66175598 210 pwmout_write(obj, dc);
mbed_official 237:f3da66175598 211
mbed_official 237:f3da66175598 212 // Save for future use
mbed_official 237:f3da66175598 213 obj->period = us;
mbed_official 237:f3da66175598 214
mbed_official 237:f3da66175598 215 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 237:f3da66175598 216 }
mbed_official 237:f3da66175598 217
mbed_official 237:f3da66175598 218 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
mbed_official 237:f3da66175598 219 {
mbed_official 237:f3da66175598 220 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 237:f3da66175598 221 }
mbed_official 237:f3da66175598 222
mbed_official 237:f3da66175598 223 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
mbed_official 237:f3da66175598 224 {
mbed_official 237:f3da66175598 225 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 237:f3da66175598 226 }
mbed_official 237:f3da66175598 227
mbed_official 237:f3da66175598 228 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
mbed_official 237:f3da66175598 229 {
mbed_official 237:f3da66175598 230 float value = (float)us / (float)obj->period;
mbed_official 237:f3da66175598 231 pwmout_write(obj, value);
mbed_official 237:f3da66175598 232 }
mbed_official 237:f3da66175598 233
mbed_official 237:f3da66175598 234 #endif