mbed-src updated for BMD-200 evaluation board. Just pin numbers are updated.

Dependents:   mbed_blinky-bmd-200 bmd-200_accel_demo firstRig

Fork of mbed-src by mbed official

Replacement for the "mbed" or "mbed-src" library when using the BMD-200 Evaluation kit. This library only remaps the pin names (i.e. LED1 points to p0.01 instead of p0.18, etc) as used by the BMD-200 Evaluation board (select the nRF51822_mkit platform). All other code is untouched.

Committer:
mbed_official
Date:
Thu May 21 09:45:08 2015 +0100
Revision:
548:1af5f1c39e80
Synchronized with git revision 36a43ec36bdf0cf71b7f374965abb942f32132fa

Full URL: https://github.com/mbedmicro/mbed/commit/36a43ec36bdf0cf71b7f374965abb942f32132fa/

STM32F3xx - Refactor analogin, analogout and pwmout

Who changed what in which revision?

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