mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

This mbed LIB has additional peripherals for ST F401 board

  • UART2 : PA_3 rx, PA_2 tx
  • UART3 : PC_7 rx, PC_6 tx
  • I2C2 : PB_3 SDA, PB_10 SCL
  • I2C3 : PB_4 SDA, PA_8 SCL
Committer:
mbed_official
Date:
Mon Feb 10 19:00:06 2014 +0000
Revision:
88:81f18c97d490
Parent:
87:085cde657901
Child:
90:8818430cfa55
Synchronized with git revision 82b58af42c386d68aa6f12d8cfcae87f0f448b73

Full URL: https://github.com/mbedmicro/mbed/commit/82b58af42c386d68aa6f12d8cfcae87f0f448b73/

[NUCLEO_F401RE] Corrections in uvision exporter, GPIOs setting and SPI.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 87:085cde657901 1 /* mbed Microcontroller Library
mbed_official 87:085cde657901 2 *******************************************************************************
mbed_official 87:085cde657901 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 87:085cde657901 4 * All rights reserved.
mbed_official 87:085cde657901 5 *
mbed_official 87:085cde657901 6 * Redistribution and use in source and binary forms, with or without
mbed_official 87:085cde657901 7 * modification, are permitted provided that the following conditions are met:
mbed_official 87:085cde657901 8 *
mbed_official 87:085cde657901 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 87:085cde657901 10 * this list of conditions and the following disclaimer.
mbed_official 87:085cde657901 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 87:085cde657901 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 87:085cde657901 13 * and/or other materials provided with the distribution.
mbed_official 87:085cde657901 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 87:085cde657901 15 * may be used to endorse or promote products derived from this software
mbed_official 87:085cde657901 16 * without specific prior written permission.
mbed_official 87:085cde657901 17 *
mbed_official 87:085cde657901 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 87:085cde657901 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 87:085cde657901 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 87:085cde657901 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 87:085cde657901 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 87:085cde657901 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 87:085cde657901 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 87:085cde657901 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 87:085cde657901 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 87:085cde657901 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 87:085cde657901 28 *******************************************************************************
mbed_official 87:085cde657901 29 */
mbed_official 87:085cde657901 30 #include "pwmout_api.h"
mbed_official 87:085cde657901 31
mbed_official 87:085cde657901 32 #include "cmsis.h"
mbed_official 87:085cde657901 33 #include "pinmap.h"
mbed_official 87:085cde657901 34 #include "error.h"
mbed_official 87:085cde657901 35 #include "stm32f4xx_hal.h"
mbed_official 87:085cde657901 36
mbed_official 87:085cde657901 37 static const PinMap PinMap_PWM[] = {
mbed_official 88:81f18c97d490 38 {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2
mbed_official 88:81f18c97d490 39 {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
mbed_official 88:81f18c97d490 40 {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1
mbed_official 87:085cde657901 41 {NC, NC, 0}
mbed_official 87:085cde657901 42 };
mbed_official 87:085cde657901 43
mbed_official 87:085cde657901 44 static TIM_HandleTypeDef TimHandle;
mbed_official 87:085cde657901 45
mbed_official 87:085cde657901 46 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 87:085cde657901 47 // Get the peripheral name from the pin and assign it to the object
mbed_official 87:085cde657901 48 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 87:085cde657901 49
mbed_official 87:085cde657901 50 if (obj->pwm == (PWMName)NC) {
mbed_official 87:085cde657901 51 error("PWM error: pinout mapping failed.");
mbed_official 87:085cde657901 52 }
mbed_official 87:085cde657901 53
mbed_official 87:085cde657901 54 // Enable TIM clock
mbed_official 87:085cde657901 55 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
mbed_official 87:085cde657901 56 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
mbed_official 87:085cde657901 57 if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
mbed_official 87:085cde657901 58
mbed_official 87:085cde657901 59 // Configure GPIO
mbed_official 87:085cde657901 60 pinmap_pinout(pin, PinMap_PWM);
mbed_official 87:085cde657901 61
mbed_official 87:085cde657901 62 obj->pin = pin;
mbed_official 87:085cde657901 63 obj->period = 0;
mbed_official 87:085cde657901 64 obj->pulse = 0;
mbed_official 87:085cde657901 65
mbed_official 87:085cde657901 66 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 87:085cde657901 67 }
mbed_official 87:085cde657901 68
mbed_official 87:085cde657901 69 void pwmout_free(pwmout_t* obj) {
mbed_official 87:085cde657901 70 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 87:085cde657901 71
mbed_official 87:085cde657901 72 HAL_TIM_PWM_DeInit(&TimHandle);
mbed_official 87:085cde657901 73
mbed_official 88:81f18c97d490 74 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 87:085cde657901 75 }
mbed_official 87:085cde657901 76
mbed_official 87:085cde657901 77 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 87:085cde657901 78 TIM_OC_InitTypeDef sConfig;
mbed_official 87:085cde657901 79
mbed_official 87:085cde657901 80 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 87:085cde657901 81
mbed_official 87:085cde657901 82 if (value < 0.0) {
mbed_official 87:085cde657901 83 value = 0.0;
mbed_official 87:085cde657901 84 } else if (value > 1.0) {
mbed_official 87:085cde657901 85 value = 1.0;
mbed_official 87:085cde657901 86 }
mbed_official 87:085cde657901 87
mbed_official 87:085cde657901 88 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 87:085cde657901 89
mbed_official 87:085cde657901 90 sConfig.OCMode = TIM_OCMODE_PWM1;
mbed_official 87:085cde657901 91 sConfig.OCFastMode = TIM_OCFAST_DISABLE;
mbed_official 87:085cde657901 92 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
mbed_official 87:085cde657901 93 sConfig.Pulse = obj->pulse;
mbed_official 87:085cde657901 94
mbed_official 87:085cde657901 95 if (obj->pin == PB_3) {
mbed_official 87:085cde657901 96 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
mbed_official 87:085cde657901 97 HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2);
mbed_official 87:085cde657901 98 }
mbed_official 87:085cde657901 99
mbed_official 87:085cde657901 100 if ((obj->pin == PB_4) || (obj->pin == PB_6)) {
mbed_official 87:085cde657901 101 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
mbed_official 87:085cde657901 102 HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1);
mbed_official 87:085cde657901 103 }
mbed_official 87:085cde657901 104 }
mbed_official 87:085cde657901 105
mbed_official 87:085cde657901 106 float pwmout_read(pwmout_t* obj) {
mbed_official 87:085cde657901 107 float value = 0;
mbed_official 87:085cde657901 108 if (obj->period > 0) {
mbed_official 87:085cde657901 109 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 87:085cde657901 110 }
mbed_official 87:085cde657901 111 return ((value > 1.0) ? (1.0) : (value));
mbed_official 87:085cde657901 112 }
mbed_official 87:085cde657901 113
mbed_official 87:085cde657901 114 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 87:085cde657901 115 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 87:085cde657901 116 }
mbed_official 87:085cde657901 117
mbed_official 87:085cde657901 118 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 87:085cde657901 119 pwmout_period_us(obj, ms * 1000);
mbed_official 87:085cde657901 120 }
mbed_official 87:085cde657901 121
mbed_official 87:085cde657901 122 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 87:085cde657901 123 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
mbed_official 87:085cde657901 124
mbed_official 87:085cde657901 125 float dc = pwmout_read(obj);
mbed_official 87:085cde657901 126
mbed_official 87:085cde657901 127 __HAL_TIM_DISABLE(&TimHandle);
mbed_official 87:085cde657901 128
mbed_official 87:085cde657901 129 TimHandle.Init.Period = us - 1;
mbed_official 87:085cde657901 130 TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
mbed_official 87:085cde657901 131 TimHandle.Init.ClockDivision = 0;
mbed_official 87:085cde657901 132 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mbed_official 87:085cde657901 133 HAL_TIM_PWM_Init(&TimHandle);
mbed_official 87:085cde657901 134
mbed_official 87:085cde657901 135 // Set duty cycle again
mbed_official 87:085cde657901 136 pwmout_write(obj, dc);
mbed_official 87:085cde657901 137
mbed_official 87:085cde657901 138 // Save for future use
mbed_official 87:085cde657901 139 obj->period = us;
mbed_official 87:085cde657901 140
mbed_official 87:085cde657901 141 __HAL_TIM_ENABLE(&TimHandle);
mbed_official 87:085cde657901 142 }
mbed_official 87:085cde657901 143
mbed_official 87:085cde657901 144 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 87:085cde657901 145 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 87:085cde657901 146 }
mbed_official 87:085cde657901 147
mbed_official 87:085cde657901 148 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 87:085cde657901 149 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 87:085cde657901 150 }
mbed_official 87:085cde657901 151
mbed_official 87:085cde657901 152 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 87:085cde657901 153 float value = (float)us / (float)obj->period;
mbed_official 87:085cde657901 154 pwmout_write(obj, value);
mbed_official 87:085cde657901 155 }