mbed library with additional peripherals for ST F401 board
Fork of mbed-src by
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
targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/pwmout_api.c
- Committer:
- mbed_official
- Date:
- 2014-02-10
- Revision:
- 88:81f18c97d490
- Parent:
- 87:085cde657901
- Child:
- 90:8818430cfa55
File content as of revision 88:81f18c97d490:
/* mbed Microcontroller Library ******************************************************************************* * Copyright (c) 2014, STMicroelectronics * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" #include "error.h" #include "stm32f4xx_hal.h" static const PinMap PinMap_PWM[] = { {PB_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH2 {PB_4, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1 {PB_6, PWM_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4)}, // TIM4_CH1 {NC, NC, 0} }; static TIM_HandleTypeDef TimHandle; void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); if (obj->pwm == (PWMName)NC) { error("PWM error: pinout mapping failed."); } // Enable TIM clock if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE(); // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; pwmout_period_us(obj, 20000); // 20 ms per default } void pwmout_free(pwmout_t* obj) { TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); HAL_TIM_PWM_DeInit(&TimHandle); pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } void pwmout_write(pwmout_t* obj, float value) { TIM_OC_InitTypeDef sConfig; TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); if (value < 0.0) { value = 0.0; } else if (value > 1.0) { value = 1.0; } obj->pulse = (uint32_t)((float)obj->period * value); sConfig.OCMode = TIM_OCMODE_PWM1; sConfig.OCFastMode = TIM_OCFAST_DISABLE; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.Pulse = obj->pulse; if (obj->pin == PB_3) { HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2); } if ((obj->pin == PB_4) || (obj->pin == PB_6)) { HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1); } } float pwmout_read(pwmout_t* obj) { float value = 0; if (obj->period > 0) { value = (float)(obj->pulse) / (float)(obj->period); } return ((value > 1.0) ? (1.0) : (value)); } void pwmout_period(pwmout_t* obj, float seconds) { pwmout_period_us(obj, seconds * 1000000.0f); } void pwmout_period_ms(pwmout_t* obj, int ms) { pwmout_period_us(obj, ms * 1000); } void pwmout_period_us(pwmout_t* obj, int us) { TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); float dc = pwmout_read(obj); __HAL_TIM_DISABLE(&TimHandle); TimHandle.Init.Period = us - 1; TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_PWM_Init(&TimHandle); // Set duty cycle again pwmout_write(obj, dc); // Save for future use obj->period = us; __HAL_TIM_ENABLE(&TimHandle); } void pwmout_pulsewidth(pwmout_t* obj, float seconds) { pwmout_pulsewidth_us(obj, seconds * 1000000.0f); } void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { pwmout_pulsewidth_us(obj, ms * 1000); } void pwmout_pulsewidth_us(pwmout_t* obj, int us) { float value = (float)us / (float)obj->period; pwmout_write(obj, value); }