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