mbed library sources. With a patch for the can_api

Fork of mbed-dev by mbed official

Committer:
DangerousElectrician
Date:
Mon Nov 14 04:39:23 2016 +0000
Revision:
151:91825d030f9b
Parent:
149:156823d33999
stuff changed?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /* mbed Microcontroller Library
<> 144:ef7eb2e8f9f7 2 *******************************************************************************
<> 144:ef7eb2e8f9f7 3 * Copyright (c) 2016, STMicroelectronics
<> 144:ef7eb2e8f9f7 4 * All rights reserved.
<> 144:ef7eb2e8f9f7 5 *
<> 144:ef7eb2e8f9f7 6 * Redistribution and use in source and binary forms, with or without
<> 144:ef7eb2e8f9f7 7 * modification, are permitted provided that the following conditions are met:
<> 144:ef7eb2e8f9f7 8 *
<> 144:ef7eb2e8f9f7 9 * 1. Redistributions of source code must retain the above copyright notice,
<> 144:ef7eb2e8f9f7 10 * this list of conditions and the following disclaimer.
<> 144:ef7eb2e8f9f7 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
<> 144:ef7eb2e8f9f7 12 * this list of conditions and the following disclaimer in the documentation
<> 144:ef7eb2e8f9f7 13 * and/or other materials provided with the distribution.
<> 144:ef7eb2e8f9f7 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
<> 144:ef7eb2e8f9f7 15 * may be used to endorse or promote products derived from this software
<> 144:ef7eb2e8f9f7 16 * without specific prior written permission.
<> 144:ef7eb2e8f9f7 17 *
<> 144:ef7eb2e8f9f7 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
<> 144:ef7eb2e8f9f7 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
<> 144:ef7eb2e8f9f7 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 144:ef7eb2e8f9f7 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
<> 144:ef7eb2e8f9f7 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
<> 144:ef7eb2e8f9f7 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
<> 144:ef7eb2e8f9f7 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
<> 144:ef7eb2e8f9f7 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
<> 144:ef7eb2e8f9f7 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
<> 144:ef7eb2e8f9f7 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 144:ef7eb2e8f9f7 28 *******************************************************************************
<> 144:ef7eb2e8f9f7 29 */
<> 144:ef7eb2e8f9f7 30 #include "pwmout_api.h"
<> 144:ef7eb2e8f9f7 31
<> 144:ef7eb2e8f9f7 32 #if DEVICE_PWMOUT
<> 144:ef7eb2e8f9f7 33
<> 144:ef7eb2e8f9f7 34 #include "cmsis.h"
<> 144:ef7eb2e8f9f7 35 #include "pinmap.h"
<> 144:ef7eb2e8f9f7 36 #include "mbed_error.h"
<> 144:ef7eb2e8f9f7 37 #include "PeripheralPins.h"
<> 144:ef7eb2e8f9f7 38
<> 144:ef7eb2e8f9f7 39 static TIM_HandleTypeDef TimHandle;
<> 144:ef7eb2e8f9f7 40
<> 144:ef7eb2e8f9f7 41 void pwmout_init(pwmout_t* obj, PinName pin)
<> 144:ef7eb2e8f9f7 42 {
<> 144:ef7eb2e8f9f7 43 // Get the peripheral name from the pin and assign it to the object
<> 144:ef7eb2e8f9f7 44 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 45 MBED_ASSERT(obj->pwm != (PWMName)NC);
<> 144:ef7eb2e8f9f7 46
<> 144:ef7eb2e8f9f7 47 // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object
<> 144:ef7eb2e8f9f7 48 uint32_t function = pinmap_function(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 49 MBED_ASSERT(function != (uint32_t)NC);
<> 144:ef7eb2e8f9f7 50 obj->channel = STM_PIN_CHANNEL(function);
<> 144:ef7eb2e8f9f7 51 obj->inverted = STM_PIN_INVERTED(function);
<> 144:ef7eb2e8f9f7 52
<> 144:ef7eb2e8f9f7 53 // Enable TIM clock
<> 144:ef7eb2e8f9f7 54 #if defined(TIM1_BASE)
<> 144:ef7eb2e8f9f7 55 if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 56 #endif
<> 144:ef7eb2e8f9f7 57 #if defined(TIM2_BASE)
<> 144:ef7eb2e8f9f7 58 if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 59 #endif
<> 144:ef7eb2e8f9f7 60 #if defined(TIM3_BASE)
<> 144:ef7eb2e8f9f7 61 if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 62 #endif
<> 144:ef7eb2e8f9f7 63 #if defined(TIM4_BASE)
<> 144:ef7eb2e8f9f7 64 if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 65 #endif
<> 144:ef7eb2e8f9f7 66 #if defined(TIM5_BASE)
<> 144:ef7eb2e8f9f7 67 if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 68 #endif
<> 144:ef7eb2e8f9f7 69 #if defined(TIM8_BASE)
<> 144:ef7eb2e8f9f7 70 if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 71 #endif
<> 144:ef7eb2e8f9f7 72 #if defined(TIM9_BASE)
<> 144:ef7eb2e8f9f7 73 if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 74 #endif
<> 144:ef7eb2e8f9f7 75 #if defined(TIM10_BASE)
<> 144:ef7eb2e8f9f7 76 if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 77 #endif
<> 144:ef7eb2e8f9f7 78 #if defined(TIM11_BASE)
<> 144:ef7eb2e8f9f7 79 if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 80 #endif
<> 144:ef7eb2e8f9f7 81 #if defined(TIM12_BASE)
<> 144:ef7eb2e8f9f7 82 if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 83 #endif
<> 144:ef7eb2e8f9f7 84 #if defined(TIM13_BASE)
<> 144:ef7eb2e8f9f7 85 if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 86 #endif
<> 144:ef7eb2e8f9f7 87 #if defined(TIM14_BASE)
<> 144:ef7eb2e8f9f7 88 if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE();
<> 144:ef7eb2e8f9f7 89 #endif
<> 144:ef7eb2e8f9f7 90
<> 144:ef7eb2e8f9f7 91 // Configure GPIO
<> 144:ef7eb2e8f9f7 92 pinmap_pinout(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 93
<> 144:ef7eb2e8f9f7 94 obj->pin = pin;
<> 144:ef7eb2e8f9f7 95 obj->period = 0;
<> 144:ef7eb2e8f9f7 96 obj->pulse = 0;
<> 144:ef7eb2e8f9f7 97
<> 144:ef7eb2e8f9f7 98 pwmout_period_us(obj, 20000); // 20 ms per default
<> 144:ef7eb2e8f9f7 99 }
<> 144:ef7eb2e8f9f7 100
<> 144:ef7eb2e8f9f7 101 void pwmout_free(pwmout_t* obj)
<> 144:ef7eb2e8f9f7 102 {
<> 144:ef7eb2e8f9f7 103 // Configure GPIO
<> 144:ef7eb2e8f9f7 104 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
<> 144:ef7eb2e8f9f7 105 }
<> 144:ef7eb2e8f9f7 106
<> 144:ef7eb2e8f9f7 107 void pwmout_write(pwmout_t* obj, float value)
<> 144:ef7eb2e8f9f7 108 {
<> 144:ef7eb2e8f9f7 109 TIM_OC_InitTypeDef sConfig;
<> 144:ef7eb2e8f9f7 110 int channel = 0;
<> 144:ef7eb2e8f9f7 111
<> 144:ef7eb2e8f9f7 112 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 144:ef7eb2e8f9f7 113
<> 144:ef7eb2e8f9f7 114 if (value < (float)0.0) {
<> 144:ef7eb2e8f9f7 115 value = 0.0;
<> 144:ef7eb2e8f9f7 116 } else if (value > (float)1.0) {
<> 144:ef7eb2e8f9f7 117 value = 1.0;
<> 144:ef7eb2e8f9f7 118 }
<> 144:ef7eb2e8f9f7 119
<> 144:ef7eb2e8f9f7 120 obj->pulse = (uint32_t)((float)obj->period * value);
<> 144:ef7eb2e8f9f7 121
<> 144:ef7eb2e8f9f7 122 // Configure channels
<> 144:ef7eb2e8f9f7 123 sConfig.OCMode = TIM_OCMODE_PWM1;
<> 144:ef7eb2e8f9f7 124 sConfig.Pulse = obj->pulse;
<> 144:ef7eb2e8f9f7 125 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
<> 144:ef7eb2e8f9f7 126 sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
<> 144:ef7eb2e8f9f7 127 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
<> 144:ef7eb2e8f9f7 128 sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
<> 144:ef7eb2e8f9f7 129 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
<> 144:ef7eb2e8f9f7 130
<> 144:ef7eb2e8f9f7 131 switch (obj->channel) {
<> 144:ef7eb2e8f9f7 132 case 1:
<> 144:ef7eb2e8f9f7 133 channel = TIM_CHANNEL_1;
<> 144:ef7eb2e8f9f7 134 break;
<> 144:ef7eb2e8f9f7 135 case 2:
<> 144:ef7eb2e8f9f7 136 channel = TIM_CHANNEL_2;
<> 144:ef7eb2e8f9f7 137 break;
<> 144:ef7eb2e8f9f7 138 case 3:
<> 144:ef7eb2e8f9f7 139 channel = TIM_CHANNEL_3;
<> 144:ef7eb2e8f9f7 140 break;
<> 144:ef7eb2e8f9f7 141 case 4:
<> 144:ef7eb2e8f9f7 142 channel = TIM_CHANNEL_4;
<> 144:ef7eb2e8f9f7 143 break;
<> 144:ef7eb2e8f9f7 144 default:
<> 144:ef7eb2e8f9f7 145 return;
<> 144:ef7eb2e8f9f7 146 }
<> 144:ef7eb2e8f9f7 147
<> 144:ef7eb2e8f9f7 148 if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
<> 144:ef7eb2e8f9f7 149 error("Cannot initialize PWM\n");
<> 144:ef7eb2e8f9f7 150 }
<> 144:ef7eb2e8f9f7 151
<> 144:ef7eb2e8f9f7 152 if (obj->inverted) {
<> 144:ef7eb2e8f9f7 153 HAL_TIMEx_PWMN_Start(&TimHandle, channel);
<> 144:ef7eb2e8f9f7 154 } else {
<> 144:ef7eb2e8f9f7 155 HAL_TIM_PWM_Start(&TimHandle, channel);
<> 144:ef7eb2e8f9f7 156 }
<> 144:ef7eb2e8f9f7 157 }
<> 144:ef7eb2e8f9f7 158
<> 144:ef7eb2e8f9f7 159 float pwmout_read(pwmout_t* obj)
<> 144:ef7eb2e8f9f7 160 {
<> 144:ef7eb2e8f9f7 161 float value = 0;
<> 144:ef7eb2e8f9f7 162 if (obj->period > 0) {
<> 144:ef7eb2e8f9f7 163 value = (float)(obj->pulse) / (float)(obj->period);
<> 144:ef7eb2e8f9f7 164 }
<> 144:ef7eb2e8f9f7 165 return ((value > (float)1.0) ? (float)(1.0) : (value));
<> 144:ef7eb2e8f9f7 166 }
<> 144:ef7eb2e8f9f7 167
<> 144:ef7eb2e8f9f7 168 void pwmout_period(pwmout_t* obj, float seconds)
<> 144:ef7eb2e8f9f7 169 {
<> 144:ef7eb2e8f9f7 170 pwmout_period_us(obj, seconds * 1000000.0f);
<> 144:ef7eb2e8f9f7 171 }
<> 144:ef7eb2e8f9f7 172
<> 144:ef7eb2e8f9f7 173 void pwmout_period_ms(pwmout_t* obj, int ms)
<> 144:ef7eb2e8f9f7 174 {
<> 144:ef7eb2e8f9f7 175 pwmout_period_us(obj, ms * 1000);
<> 144:ef7eb2e8f9f7 176 }
<> 144:ef7eb2e8f9f7 177
<> 144:ef7eb2e8f9f7 178 void pwmout_period_us(pwmout_t* obj, int us)
<> 144:ef7eb2e8f9f7 179 {
<> 144:ef7eb2e8f9f7 180 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
<> 144:ef7eb2e8f9f7 181 RCC_ClkInitTypeDef RCC_ClkInitStruct;
<> 144:ef7eb2e8f9f7 182 uint32_t PclkFreq;
<> 144:ef7eb2e8f9f7 183 uint32_t APBxCLKDivider;
<> 144:ef7eb2e8f9f7 184 float dc = pwmout_read(obj);
<> 144:ef7eb2e8f9f7 185
<> 144:ef7eb2e8f9f7 186 __HAL_TIM_DISABLE(&TimHandle);
<> 144:ef7eb2e8f9f7 187
<> 144:ef7eb2e8f9f7 188 // Get clock configuration
<> 144:ef7eb2e8f9f7 189 // Note: PclkFreq contains here the Latency (not used after)
<> 144:ef7eb2e8f9f7 190 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
<> 144:ef7eb2e8f9f7 191
<> 144:ef7eb2e8f9f7 192 // Get the PCLK and APBCLK divider related to the timer
<> 144:ef7eb2e8f9f7 193 switch (obj->pwm) {
<> 144:ef7eb2e8f9f7 194
<> 144:ef7eb2e8f9f7 195 // APB1 clock
<> 144:ef7eb2e8f9f7 196 #if defined(TIM2_BASE)
<> 144:ef7eb2e8f9f7 197 case PWM_2:
<> 144:ef7eb2e8f9f7 198 #endif
<> 144:ef7eb2e8f9f7 199 #if defined(TIM3_BASE)
<> 144:ef7eb2e8f9f7 200 case PWM_3:
<> 144:ef7eb2e8f9f7 201 #endif
<> 144:ef7eb2e8f9f7 202 #if defined(TIM4_BASE)
<> 144:ef7eb2e8f9f7 203 case PWM_4:
<> 144:ef7eb2e8f9f7 204 #endif
<> 144:ef7eb2e8f9f7 205 #if defined(TIM5_BASE)
<> 144:ef7eb2e8f9f7 206 case PWM_5:
<> 144:ef7eb2e8f9f7 207 #endif
<> 144:ef7eb2e8f9f7 208 #if defined(TIM12_BASE)
<> 144:ef7eb2e8f9f7 209 case PWM_12:
<> 144:ef7eb2e8f9f7 210 #endif
<> 144:ef7eb2e8f9f7 211 #if defined(TIM13_BASE)
<> 144:ef7eb2e8f9f7 212 case PWM_13:
<> 144:ef7eb2e8f9f7 213 #endif
<> 144:ef7eb2e8f9f7 214 #if defined(TIM14_BASE)
<> 144:ef7eb2e8f9f7 215 case PWM_14:
<> 144:ef7eb2e8f9f7 216 #endif
<> 144:ef7eb2e8f9f7 217 PclkFreq = HAL_RCC_GetPCLK1Freq();
<> 144:ef7eb2e8f9f7 218 APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider;
<> 144:ef7eb2e8f9f7 219 break;
<> 144:ef7eb2e8f9f7 220
<> 144:ef7eb2e8f9f7 221 // APB2 clock
<> 144:ef7eb2e8f9f7 222 #if defined(TIM1_BASE)
<> 144:ef7eb2e8f9f7 223 case PWM_1:
<> 144:ef7eb2e8f9f7 224 #endif
<> 144:ef7eb2e8f9f7 225 #if defined(TIM8_BASE)
<> 144:ef7eb2e8f9f7 226 case PWM_8:
<> 144:ef7eb2e8f9f7 227 #endif
<> 144:ef7eb2e8f9f7 228 #if defined(TIM9_BASE)
<> 144:ef7eb2e8f9f7 229 case PWM_9:
<> 144:ef7eb2e8f9f7 230 #endif
<> 144:ef7eb2e8f9f7 231 #if defined(TIM10_BASE)
<> 144:ef7eb2e8f9f7 232 case PWM_10:
<> 144:ef7eb2e8f9f7 233 #endif
<> 144:ef7eb2e8f9f7 234 #if defined(TIM11_BASE)
<> 144:ef7eb2e8f9f7 235 case PWM_11:
<> 144:ef7eb2e8f9f7 236 #endif
<> 144:ef7eb2e8f9f7 237 PclkFreq = HAL_RCC_GetPCLK2Freq();
<> 144:ef7eb2e8f9f7 238 APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider;
<> 144:ef7eb2e8f9f7 239 break;
<> 144:ef7eb2e8f9f7 240 default:
<> 144:ef7eb2e8f9f7 241 return;
<> 144:ef7eb2e8f9f7 242 }
<> 144:ef7eb2e8f9f7 243
<> 144:ef7eb2e8f9f7 244 TimHandle.Init.Period = us - 1;
<> 144:ef7eb2e8f9f7 245 // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
<> 144:ef7eb2e8f9f7 246 if (APBxCLKDivider == RCC_HCLK_DIV1)
<> 144:ef7eb2e8f9f7 247 TimHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
<> 144:ef7eb2e8f9f7 248 else
<> 144:ef7eb2e8f9f7 249 TimHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
<> 144:ef7eb2e8f9f7 250 TimHandle.Init.ClockDivision = 0;
<> 144:ef7eb2e8f9f7 251 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
<> 144:ef7eb2e8f9f7 252
<> 144:ef7eb2e8f9f7 253 if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
<> 144:ef7eb2e8f9f7 254 error("Cannot initialize PWM\n");
<> 144:ef7eb2e8f9f7 255 }
<> 144:ef7eb2e8f9f7 256
<> 144:ef7eb2e8f9f7 257 // Set duty cycle again
<> 144:ef7eb2e8f9f7 258 pwmout_write(obj, dc);
<> 144:ef7eb2e8f9f7 259
<> 144:ef7eb2e8f9f7 260 // Save for future use
<> 144:ef7eb2e8f9f7 261 obj->period = us;
<> 144:ef7eb2e8f9f7 262
<> 144:ef7eb2e8f9f7 263 __HAL_TIM_ENABLE(&TimHandle);
<> 144:ef7eb2e8f9f7 264 }
<> 144:ef7eb2e8f9f7 265
<> 144:ef7eb2e8f9f7 266 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
<> 144:ef7eb2e8f9f7 267 {
<> 144:ef7eb2e8f9f7 268 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
<> 144:ef7eb2e8f9f7 269 }
<> 144:ef7eb2e8f9f7 270
<> 144:ef7eb2e8f9f7 271 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
<> 144:ef7eb2e8f9f7 272 {
<> 144:ef7eb2e8f9f7 273 pwmout_pulsewidth_us(obj, ms * 1000);
<> 144:ef7eb2e8f9f7 274 }
<> 144:ef7eb2e8f9f7 275
<> 144:ef7eb2e8f9f7 276 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
<> 144:ef7eb2e8f9f7 277 {
<> 144:ef7eb2e8f9f7 278 float value = (float)us / (float)obj->period;
<> 144:ef7eb2e8f9f7 279 pwmout_write(obj, value);
<> 144:ef7eb2e8f9f7 280 }
<> 144:ef7eb2e8f9f7 281
<> 144:ef7eb2e8f9f7 282 #endif