mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Sep 30 17:00:09 2015 +0100
Revision:
635:a11c0372f0ba
Parent:
508:4f5903e025e6
Synchronized with git revision d29c98dae61be0946ddf3a3c641c7726056f9452

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

Added support for SAMW25

Who changed what in which revision?

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