cpu/stm32f4: significantly optimized UART driver

This commit is contained in:
Hauke Petersen 2015-10-28 11:46:08 +01:00
parent b13f5cf801
commit a2247a3400
2 changed files with 262 additions and 196 deletions

View File

@ -16,11 +16,10 @@
* @author Hauke Petersen <hauke.peterse@fu-berlin.de> * @author Hauke Petersen <hauke.peterse@fu-berlin.de>
*/ */
#ifndef CPU_PERIPH_H_ #ifndef PERIPH_CPU_H
#define CPU_PERIPH_H_ #define PERIPH_CPU_H
#include "cpu.h" #include "cpu.h"
#include "periph/dev_enums.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -44,6 +43,15 @@ typedef uint32_t gpio_t;
*/ */
#define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y) #define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y)
/**
* @brief declare needed generic SPI functions
* @{
*/
#define PERIPH_SPI_NEEDS_TRANSFER_BYTES
#define PERIPH_SPI_NEEDS_TRANSFER_REG
#define PERIPH_SPI_NEEDS_TRANSFER_REGS
/** @} */
/** /**
* @brief Available ports on the STM32F4 family * @brief Available ports on the STM32F4 family
*/ */
@ -80,6 +88,22 @@ typedef enum {
GPIO_AF14 /**< use alternate function 14 */ GPIO_AF14 /**< use alternate function 14 */
} gpio_af_t; } gpio_af_t;
/**
* @brief Structure for UART configuration data
* @{
*/
typedef struct {
USART_TypeDef *dev; /**< UART device base register address */
uint32_t rcc_mask; /**< bit in clock enable register */
gpio_t rx_pin; /**< RX pin */
gpio_t tx_pin; /**< TX pin */
gpio_af_t af; /**< alternate pin function to use */
uint8_t irqn; /**< IRQ channel */
uint8_t dma_stream; /**< DMA stream used for TX */
uint8_t dma_chan; /**< DMA channel used for TX */
} uart_conf_t;
/** @} */
/** /**
* @brief Configure the alternate function for the given pin * @brief Configure the alternate function for the given pin
* *
@ -91,17 +115,98 @@ typedef enum {
void gpio_init_af(gpio_t pin, gpio_af_t af); void gpio_init_af(gpio_t pin, gpio_af_t af);
/** /**
* @brief declare needed generic SPI functions * @brief Power on the DMA device the given stream belongs to
* @{ *
* @param[in] stream logical DMA stream
*/ */
#define PERIPH_SPI_NEEDS_TRANSFER_BYTES static inline void dma_poweron(int stream)
#define PERIPH_SPI_NEEDS_TRANSFER_REG {
#define PERIPH_SPI_NEEDS_TRANSFER_REGS if (stream < 8) {
/** @} */ RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN;
} else {
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
}
}
/**
* @brief Get DMA base register
*
* For simplifying DMA stream handling, we map the DMA channels transparently to
* one integer number, such that DMA1 stream0 equals 0, DMA2 stream0 equals 8,
* DMA2 stream 7 equals 15 and so on.
*
* @param[in] stream logical DMA stream
*/
static inline DMA_TypeDef *dma_base(int stream)
{
return (stream < 8) ? DMA1 : DMA2;
}
/**
* @brief Get the DMA stream base address
*
* @param[in] stream logical DMA stream
*
* @return base address for the selected DMA stream
*/
static inline DMA_Stream_TypeDef *dma_stream(int stream)
{
uint32_t base = (uint32_t)dma_base(stream);
return (DMA_Stream_TypeDef *)(base + (0x10 + (0x18 * (stream & 0x7))));
}
/**
* @brief Select high or low DMA interrupt register based on stream number
*
* @param[in] stream logical DMA stream
*
* @return 0 for streams 0-3, 1 for streams 3-7
*/
static inline int dma_hl(int stream)
{
return ((stream & 0x4) >> 2);
}
/**
* @brief Get the interrupt flag clear bit position in the DMA LIFCR register
*
* @param[in] stream logical DMA stream
*/
static inline uint32_t dma_ifc(int stream)
{
switch (stream & 0x3) {
case 0:
return (1 << 5);
case 1:
return (1 << 11);
case 2:
return (1 << 21);
case 3:
return (1 << 27);
default:
return 0;
}
}
static inline void dma_isr_enable(int stream)
{
if (stream < 7) {
NVIC_EnableIRQ((IRQn_Type)((int)DMA1_Stream0_IRQn + stream));
}
else if (stream == 8) {
NVIC_EnableIRQ(DMA1_Stream7_IRQn);
}
else if (stream < 14) {
NVIC_EnableIRQ((IRQn_Type)((int)DMA2_Stream0_IRQn + stream));
}
else if (stream < 17) {
NVIC_EnableIRQ((IRQn_Type)((int)DMA2_Stream5_IRQn + stream));
}
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* CPU_PERIPH_H_ */ #endif /* PERIPH_CPU_H */
/** @} */ /** @} */

View File

@ -1,9 +1,9 @@
/* /*
* Copyright (C) 2014-2015 Freie Universität Berlin * Copyright (C) 2014-2015 Freie Universität Berlin
* *
* This file is subject to the terms and conditions of the GNU Lesser General * This file is subject to the terms and conditions of the GNU Lesser
* Public License v2.1. See the file LICENSE in the top level directory for more * General Public License v2.1. See the file LICENSE in the top level
* details. * directory for more details.
*/ */
/** /**
@ -22,250 +22,211 @@
#include "cpu.h" #include "cpu.h"
#include "thread.h" #include "thread.h"
#include "sched.h" #include "sched.h"
#include "periph_conf.h" #include "mutex.h"
#include "periph/uart.h" #include "periph/uart.h"
#include "periph/gpio.h"
/** /**
* @brief Allocate memory to store the callback functions. * @brief Allocate memory to store the callback functions
*/ */
static uart_isr_ctx_t uart_config[UART_NUMOF]; static uart_isr_ctx_t uart_ctx[UART_NUMOF];
/** /**
* @todo Remodel this UART driver and merge init functions... * @brief Get the base register for the given UART device
*/ */
static int init_base(uart_t uart, uint32_t baudrate); static inline USART_TypeDef *_dev(uart_t uart)
{
return uart_config[uart].dev;
}
/**
* @brief Transmission locks
*/
static mutex_t tx_sync[UART_NUMOF];
/**
* @brief Find out which peripheral bus the UART device is connected to
*
* @return 1: APB1
* @return 2: APB2
*/
static inline int _bus(uart_t uart)
{
return (uart_config[uart].rcc_mask < RCC_APB1ENR_USART2EN) ? 2 : 1;
}
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
{ {
/* do basic initialization */ USART_TypeDef *dev;
int res = init_base(uart, baudrate); DMA_Stream_TypeDef *stream;
if (res < 0) { float divider;
return res;
}
/* remember callback addresses */
uart_config[uart].rx_cb = rx_cb;
uart_config[uart].arg = arg;
/* enable receive interrupt */
switch (uart) {
#if UART_0_EN
case UART_0:
NVIC_SetPriority(UART_0_IRQ_CHAN, UART_IRQ_PRIO);
NVIC_EnableIRQ(UART_0_IRQ_CHAN);
UART_0_DEV->CR1 |= USART_CR1_RXNEIE;
break;
#endif
#if UART_1_EN
case UART_1:
NVIC_SetPriority(UART_1_IRQ_CHAN, UART_IRQ_PRIO);
NVIC_EnableIRQ(UART_1_IRQ_CHAN);
UART_1_DEV->CR1 |= USART_CR1_RXNEIE;
break;
#endif
#if UART_2_EN
case UART_2:
NVIC_SetPriority(UART_2_IRQ_CHAN, UART_IRQ_PRIO);
NVIC_EnableIRQ(UART_2_IRQ_CHAN);
UART_2_DEV->CR1 |= USART_CR1_RXNEIE;
break;
#endif
}
return 0;
}
static int init_base(uart_t uart, uint32_t baudrate)
{
USART_TypeDef *dev = 0;
GPIO_TypeDef *port = 0;
uint32_t tx_pin = 0;
uint32_t rx_pin = 0;
uint8_t af = 0;
uint32_t clk = 0;
uint16_t mantissa; uint16_t mantissa;
uint8_t fraction; uint8_t fraction;
switch (uart) { /* check if given UART device does exist */
#if UART_0_EN if (uart < 0 || uart >= UART_NUMOF) {
case UART_0:
dev = UART_0_DEV;
port = UART_0_PORT;
clk = UART_0_CLK;
tx_pin = UART_0_TX_PIN;
rx_pin = UART_0_RX_PIN;
af = UART_0_AF;
UART_0_CLKEN();
UART_0_PORT_CLKEN();
break;
#endif
#if UART_1_EN
case UART_1:
dev = UART_1_DEV;
port = UART_1_PORT;
clk = UART_1_CLK;
tx_pin = UART_1_TX_PIN;
rx_pin = UART_1_RX_PIN;
af = UART_1_AF;
UART_1_CLKEN();
UART_1_PORT_CLKEN();
break;
#endif
#if UART_2_EN
case UART_2:
dev = UART_2_DEV;
port = UART_2_PORT;
clk = UART_2_CLK;
tx_pin = UART_2_TX_PIN;
rx_pin = UART_2_RX_PIN;
af = UART_2_AF;
UART_2_CLKEN();
UART_2_PORT_CLKEN();
break;
#endif
default:
return -1; return -1;
} }
/* configure pp mode with no pull for RX and TX pins */ /* get UART base address */
port->OTYPER &= ~(1 << rx_pin | 1 << tx_pin); dev = _dev(uart);
port->PUPDR &= ~(3 << (rx_pin * 2) | 3 << (tx_pin * 2)); /* remember callback addresses and argument */
uart_ctx[uart].rx_cb = rx_cb;
uart_ctx[uart].arg = arg;
/* init tx lock */
mutex_init(&tx_sync[uart]);
mutex_lock(&tx_sync[uart]);
/* configure RX and TX pins, set pin to use alternative function mode */ /* configure pins */
port->MODER &= ~(3 << (rx_pin * 2) | 3 << (tx_pin * 2)); gpio_init(uart_config[uart].rx_pin, GPIO_DIR_IN, GPIO_NOPULL);
port->MODER |= 2 << (rx_pin * 2) | 2 << (tx_pin * 2); gpio_init(uart_config[uart].tx_pin, GPIO_DIR_OUT, GPIO_NOPULL);
/* and assign alternative function */ gpio_init_af(uart_config[uart].rx_pin, uart_config[uart].af);
if (rx_pin < 8) { gpio_init_af(uart_config[uart].tx_pin, uart_config[uart].af);
port->AFR[0] &= ~(0xf << (rx_pin * 4)); /* enable UART clock */
port->AFR[0] |= af << (rx_pin * 4); uart_poweron(uart);
/* calculate and set baudrate */
if (_bus(uart) == 1) {
divider = CLOCK_APB1 / (16 * baudrate);
} }
else { else {
port->AFR[1] &= ~(0xf << ((rx_pin - 8) * 4)); divider = CLOCK_APB2 / (16 * baudrate);
port->AFR[1] |= af << ((rx_pin - 8) * 4);
} }
if (tx_pin < 8) { mantissa = (uint16_t)divider;
port->AFR[0] &= ~(0xf << (tx_pin * 4)); fraction = (uint8_t)((divider - mantissa) * 16);
port->AFR[0] |= af << (tx_pin * 4);
}
else {
port->AFR[1] &= ~(0xf << ((tx_pin - 8) * 4));
port->AFR[1] |= af << ((tx_pin - 8) * 4);
}
/* configure UART to mode 8N1 with given baudrate */
clk /= baudrate;
mantissa = (uint16_t)(clk / 16);
fraction = (uint8_t)(clk - (mantissa * 16));
dev->BRR = ((mantissa & 0x0fff) << 4) | (0x0f & fraction); dev->BRR = ((mantissa & 0x0fff) << 4) | (0x0f & fraction);
/* configure UART to 8N1 and enable receive and transmit mode */
/* enable receive and transmit mode */ dev->CR3 = USART_CR3_DMAT;
dev->CR3 = 0;
dev->CR2 = 0; dev->CR2 = 0;
dev->CR1 = USART_CR1_UE | USART_CR1_TE | USART_CR1_RE; dev->CR1 = USART_CR1_UE | USART_CR1_TE | USART_CR1_RE;
/* configure the DMA stream for transmission */
dma_poweron(uart_config[uart].dma_stream);
stream = dma_stream(uart_config[uart].dma_stream);
stream->CR = ((uart_config[uart].dma_chan << 25) |
DMA_SxCR_PL_0 |
DMA_SxCR_MINC |
DMA_SxCR_DIR_0 |
DMA_SxCR_TCIE);
stream->PAR = (uint32_t)&(dev->DR);
stream->FCR = 0;
/* enable global and receive interrupts */
NVIC_EnableIRQ(uart_config[uart].irqn);
dma_isr_enable(uart_config[uart].dma_stream);
dev->CR1 |= USART_CR1_RXNEIE;
return 0; return 0;
} }
void uart_write(uart_t uart, const uint8_t *data, size_t len) void uart_write(uart_t uart, const uint8_t *data, size_t len)
{ {
USART_TypeDef *dev = 0; /* in case we are inside an ISR, we need to send blocking */
if (inISR()) {
switch (uart) { /* send data by active waiting on the TXE flag */
#if UART_0_EN USART_TypeDef *dev = _dev(uart);
case UART_0: for (int i = 0; i < len; i++) {
dev = UART_0_DEV;
break;
#endif
#if UART_1_EN
case UART_1:
dev = UART_1_DEV;
break;
#endif
#if UART_2_EN
case UART_2:
dev = UART_2_DEV;
break;
#endif
default:
return;
}
for (size_t i = 0; i < len; i++) {
while (!(dev->SR & USART_SR_TXE)); while (!(dev->SR & USART_SR_TXE));
dev->DR = data[i]; dev->DR = data[i];
} }
}
else {
DMA_Stream_TypeDef *stream = dma_stream(uart_config[uart].dma_stream);
/* configure and start DMA transfer */
stream->M0AR = (uint32_t)data;
stream->NDTR = (uint16_t)len;
stream->CR |= DMA_SxCR_EN;
/* wait for transfer to complete */
mutex_lock(&tx_sync[uart]);
}
} }
void uart_poweron(uart_t uart) void uart_poweron(uart_t uart)
{ {
switch (uart) { if (_bus(uart) == 1) {
#if UART_0_EN RCC->APB1ENR |= uart_config[uart].rcc_mask;
case UART_0: }
UART_0_CLKEN(); else {
break; RCC->APB2ENR |= uart_config[uart].rcc_mask;
#endif
#if UART_1_EN
case UART_1:
UART_1_CLKEN();
break;
#endif
#if UART_2_EN
case UART_2:
UART_2_CLKEN();
break;
#endif
} }
} }
void uart_poweroff(uart_t uart) void uart_poweroff(uart_t uart)
{ {
switch (uart) { if (_bus(uart) == 1) {
#if UART_0_EN RCC->APB1ENR &= ~(uart_config[uart].rcc_mask);
case UART_0: }
UART_0_CLKDIS(); else {
break; RCC->APB2ENR &= ~(uart_config[uart].rcc_mask);
#endif
#if UART_1_EN
case UART_1:
UART_1_CLKDIS();
break;
#endif
#if UART_2_EN
case UART_2:
UART_2_CLKDIS();
break;
#endif
} }
} }
static inline void irq_handler(uint8_t uartnum, USART_TypeDef *dev) static inline void irq_handler(int uart, USART_TypeDef *dev)
{ {
if (dev->SR & USART_SR_RXNE) { if (dev->SR & USART_SR_RXNE) {
char data = (char)dev->DR; char data = (char)dev->DR;
uart_config[uartnum].rx_cb(uart_config[uartnum].arg, data); uart_ctx[uart].rx_cb(uart_ctx[uart].arg, data);
} }
if (sched_context_switch_request) { if (sched_context_switch_request) {
thread_yield(); thread_yield();
} }
} }
#if UART_0_EN static inline void dma_handler(int uart, int stream)
{
/* clear DMA done flag */
dma_base(stream)->IFCR[dma_hl(stream)] = dma_ifc(stream);
mutex_unlock(&tx_sync[uart]);
if (sched_context_switch_request) {
thread_yield();
}
}
#ifdef UART_0_ISR
void UART_0_ISR(void) void UART_0_ISR(void)
{ {
irq_handler(UART_0, UART_0_DEV); irq_handler(0, uart_config[0].dev);
}
void UART_0_DMA_ISR(void)
{
dma_handler(0, uart_config[0].dma_stream);
} }
#endif #endif
#if UART_1_EN #ifdef UART_1_ISR
void UART_1_ISR(void) void UART_1_ISR(void)
{ {
irq_handler(UART_1, UART_1_DEV); irq_handler(1, uart_config[1].dev);
}
void UART_1_DMA_ISR(void)
{
dma_handler(1, uart_config[1].dma_stream);
} }
#endif #endif
#if UART_2_EN #ifdef UART_2_ISR
void UART_2_ISR(void) void UART_2_ISR(void)
{ {
irq_handler(UART_2, UART_2_DEV); irq_handler(2, uart_config[2].dev);
}
#endif
#ifdef UART_3_ISR
void UART_3_ISR(void)
{
irq_handler(3, uart_config[3].dev);
}
#endif
#ifdef UART_4_ISR
void UART_4_ISR(void)
{
irq_handler(4, uart_config[4].dev);
}
#endif
#ifdef UART_5_ISR
void UART_5_ISR(void)
{
irq_handler(5, uart_config[5].dev);
} }
#endif #endif