[NUCLEO_L053R8] Alignment with STM32Cube driver + preparation for future L0 devices

pull/961/head
bcostm 2015-03-10 14:06:30 +01:00
parent 1561dd146a
commit 2eb529ef2d
4 changed files with 148 additions and 29 deletions

View File

@ -39,30 +39,44 @@
static DAC_HandleTypeDef DacHandle;
// These variables are used for the "free" function
static int pa4_used = 0;
static int pa5_used = 0;
void analogout_init(dac_t *obj, PinName pin)
{
DAC_ChannelConfTypeDef sConfig;
DacHandle.Instance = DAC;
// Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
// Get the peripheral name from the pin and assign it to the object
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
MBED_ASSERT(obj->dac != (DACName)NC);
// Configure GPIO
pinmap_pinout(pin, PinMap_DAC);
// Save the channel for future use
// Save the pin for future use
obj->pin = pin;
// Enable DAC clock
__DAC_CLK_ENABLE();
// Configure DAC
DacHandle.Instance = DAC;
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
if (pin == PA_4) {
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
pa4_used = 1;
}
#if defined(DAC_CHANNEL_2)
if (pin == PA_5) {
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2);
pa5_used = 1;
}
#endif
analogout_write_u16(obj, 0);
}
@ -70,9 +84,14 @@ void analogout_init(dac_t *obj, PinName pin)
void analogout_free(dac_t *obj)
{
// Reset DAC and disable clock
__DAC_FORCE_RESET();
__DAC_RELEASE_RESET();
__DAC_CLK_DISABLE();
if (obj->pin == PA_4) pa4_used = 0;
if (obj->pin == PA_5) pa5_used = 0;
if ((pa4_used == 0) && (pa5_used == 0)) {
__DAC_FORCE_RESET();
__DAC_RELEASE_RESET();
__DAC_CLK_DISABLE();
}
// Configure GPIO
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
@ -80,13 +99,32 @@ void analogout_free(dac_t *obj)
static inline void dac_write(dac_t *obj, uint16_t value)
{
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
if (obj->pin == PA_4) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
}
#if defined(DAC_CHANNEL_2)
if (obj->pin == PA_5) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
}
#endif
}
static inline int dac_read(dac_t *obj)
{
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
if (obj->pin == PA_4) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
}
#if defined(DAC_CHANNEL_2)
else if (obj->pin == PA_5) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
}
#endif
else {
return 0;
}
}
void analogout_write(dac_t *obj, float value)

View File

@ -44,11 +44,14 @@
I2C_HandleTypeDef I2cHandle;
int i2c1_inited = 0;
int i2c2_inited = 0;
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
static int i2c1_inited = 0;
static int i2c2_inited = 0;
#if defined(I2C3_BASE)
static int i2c3_inited = 0;
#endif
// Determine the I2C to use
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
@ -67,6 +70,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
pin_mode(sda, OpenDrain);
pin_mode(scl, OpenDrain);
}
// Enable I2C2 clock and pinout if not done
if ((obj->i2c == I2C_2) && !i2c2_inited) {
i2c2_inited = 1;
@ -78,6 +82,19 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
pin_mode(scl, OpenDrain);
}
#if defined(I2C3_BASE)
// Enable I2C3 clock and pinout if not done
if ((obj->i2c == I2C_3) && !i2c3_inited) {
i2c3_inited = 1;
__I2C3_CLK_ENABLE();
// Configure I2C pins
pinmap_pinout(sda, PinMap_I2C_SDA);
pinmap_pinout(scl, PinMap_I2C_SCL);
pin_mode(sda, OpenDrain);
pin_mode(scl, OpenDrain);
}
#endif
// Reset to clear pending flags if any
i2c_reset(obj);

View File

@ -49,6 +49,9 @@ void pwmout_init(pwmout_t* obj, PinName pin)
// Enable TIM clock
if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
#if defined(TIM3_BASE)
if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
#endif
if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE();
if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE();
@ -109,12 +112,16 @@ void pwmout_write(pwmout_t* obj, float value)
break;
// Channels 3
case PA_2:
case PB_0:
case PB_10:
case PC_8:
channel = TIM_CHANNEL_3;
break;
// Channels 4
case PA_3:
case PB_1:
case PB_11:
case PC_9:
channel = TIM_CHANNEL_4;
break;
default:

View File

@ -37,9 +37,9 @@
#include <string.h>
#include "PeripheralPins.h"
#define UART_NUM (3)
#define UART_NUM (5)
static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0};
static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0, 0, 0};
static uart_irq_handler irq_handler;
@ -52,7 +52,6 @@ static void init_uart(serial_t *obj)
{
UartHandle.Instance = (USART_TypeDef *)(obj->uart);
// [TODO] Workaround to be removed after HAL driver is corrected
if (obj->uart == LPUART_1) {
UartHandle.Init.BaudRate = obj->baudrate >> 1;
} else {
@ -90,20 +89,34 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
// Enable UART clock
if (obj->uart == UART_1) {
__USART1_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
obj->index = 0;
}
if (obj->uart == UART_2) {
__USART2_CLK_ENABLE();
__HAL_RCC_USART2_CLK_ENABLE();
obj->index = 1;
}
if (obj->uart == LPUART_1) {
__LPUART1_CLK_ENABLE();
__HAL_RCC_LPUART1_CLK_ENABLE();
obj->index = 2;
}
#if defined(USART4_BASE)
if (obj->uart == UART_4) {
__HAL_RCC_USART4_CLK_ENABLE();
obj->index = 3;
}
#endif
#if defined(USART5_BASE)
if (obj->uart == UART_5) {
__HAL_RCC_USART5_CLK_ENABLE();
obj->index = 4;
}
#endif
// Configure the UART pins
pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);
@ -135,23 +148,39 @@ void serial_free(serial_t *obj)
{
// Reset UART and disable clock
if (obj->uart == UART_1) {
__USART1_FORCE_RESET();
__USART1_RELEASE_RESET();
__USART1_CLK_DISABLE();
__HAL_RCC_USART1_FORCE_RESET();
__HAL_RCC_USART1_RELEASE_RESET();
__HAL_RCC_USART1_CLK_DISABLE();
}
if (obj->uart == UART_2) {
__USART2_FORCE_RESET();
__USART2_RELEASE_RESET();
__USART2_CLK_DISABLE();
__HAL_RCC_USART2_FORCE_RESET();
__HAL_RCC_USART2_RELEASE_RESET();
__HAL_RCC_USART2_CLK_DISABLE();
}
if (obj->uart == LPUART_1) {
__LPUART1_FORCE_RESET();
__LPUART1_RELEASE_RESET();
__LPUART1_CLK_DISABLE();
__HAL_RCC_LPUART1_FORCE_RESET();
__HAL_RCC_LPUART1_RELEASE_RESET();
__HAL_RCC_LPUART1_CLK_DISABLE();
}
#if defined(USART4_BASE)
if (obj->uart == UART_4) {
__HAL_RCC_USART4_FORCE_RESET();
__HAL_RCC_USART4_RELEASE_RESET();
__HAL_RCC_USART4_CLK_DISABLE();
}
#endif
#if defined(USART5_BASE)
if (obj->uart == UART_5) {
__HAL_RCC_USART5_FORCE_RESET();
__HAL_RCC_USART5_RELEASE_RESET();
__HAL_RCC_USART5_CLK_DISABLE();
}
#endif
// Configure GPIOs
pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
@ -230,6 +259,20 @@ static void lpuart1_irq(void)
uart_irq(LPUART_1, 2);
}
#if defined(USART4_BASE)
static void uart4_irq(void)
{
uart_irq(UART_4, 3);
}
#endif
#if defined(USART5_BASE)
static void uart5_irq(void)
{
uart_irq(UART_5, 4);
}
#endif
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
{
irq_handler = handler;
@ -258,6 +301,20 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
vector = (uint32_t)&lpuart1_irq;
}
#if defined(USART4_BASE)
if (obj->uart == UART_4) {
irq_n = USART4_5_IRQn;
vector = (uint32_t)&uart4_irq;
}
#endif
#if defined(USART5_BASE)
if (obj->uart == UART_5) {
irq_n = USART4_5_IRQn;
vector = (uint32_t)&uart5_irq;
}
#endif
if (enable) {
if (irq == RxIrq) {