Merge pull request #12917 from aabadie/pr/cpu/fe310_uart_rework

cpu/fe310: slightly rework the uart driver
This commit is contained in:
Francisco 2019-12-20 16:20:18 +01:00 committed by GitHub
commit c9955f9c74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 147 additions and 115 deletions

View File

@ -117,12 +117,6 @@ void board_init(void)
cpu_init(); cpu_init();
board_init_clock(); board_init_clock();
/* Configure pin muxing for UART0 */
GPIO_REG(GPIO_OUTPUT_VAL) |= IOF0_UART0_MASK;
GPIO_REG(GPIO_OUTPUT_EN) |= IOF0_UART0_MASK;
GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK;
GPIO_REG(GPIO_IOF_EN) |= IOF0_UART0_MASK;
/* Configure GPIOs for LEDs */ /* Configure GPIOs for LEDs */
gpio_init(LED0_PIN, GPIO_OUT); gpio_init(LED0_PIN, GPIO_OUT);
gpio_init(LED1_PIN, GPIO_OUT); gpio_init(LED1_PIN, GPIO_OUT);

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2017 Ken Rabold * Copyright (C) 2017 Ken Rabold
* 2019 Inria
* *
* 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 General
* Public License v2.1. See the file LICENSE in the top level directory for more * Public License v2.1. See the file LICENSE in the top level directory for more
@ -19,6 +20,8 @@
#ifndef PERIPH_CONF_H #ifndef PERIPH_CONF_H
#define PERIPH_CONF_H #define PERIPH_CONF_H
#include "periph_cpu.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -49,6 +52,28 @@ extern "C" {
#define TIMER_NUMOF (1) #define TIMER_NUMOF (1)
/** @} */ /** @} */
/**
* @name UART configuration
* @{
*/
static const uart_conf_t uart_config[] = {
{
.addr = UART0_CTRL_ADDR,
.rx = GPIO_PIN(0, 16),
.tx = GPIO_PIN(0, 17),
.isr_num = INT_UART0_BASE,
},
{
.addr = UART1_CTRL_ADDR,
.rx = GPIO_PIN(0, 18),
.tx = GPIO_PIN(0, 23),
.isr_num = INT_UART1_BASE,
},
};
#define UART_NUMOF ARRAY_SIZE(uart_config)
/** @} */
/** /**
* @name RTT/RTC configuration * @name RTT/RTC configuration
* *
@ -76,16 +101,6 @@ extern "C" {
#define PWM_NUMOF (3) #define PWM_NUMOF (3)
/** @} */ /** @} */
/**
* @name UART configuration
*
* @{
*/
#define UART_NUMOF (2)
#define UART0_RX_INTR_PRIORITY (2)
#define UART1_RX_INTR_PRIORITY (2)
/** @} */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -139,12 +139,6 @@ void board_init(void)
board_init_clock(); board_init_clock();
// board_init_flash(); // board_init_flash();
/* Configure pin muxing for UART0 */
GPIO_REG(GPIO_OUTPUT_VAL) |= IOF0_UART0_MASK;
GPIO_REG(GPIO_OUTPUT_EN) |= IOF0_UART0_MASK;
GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK;
GPIO_REG(GPIO_IOF_EN) |= IOF0_UART0_MASK;
/* Configure GPIOs for LEDs */ /* Configure GPIOs for LEDs */
gpio_init(LED0_PIN, GPIO_OUT); gpio_init(LED0_PIN, GPIO_OUT);
gpio_init(LED1_PIN, GPIO_OUT); gpio_init(LED1_PIN, GPIO_OUT);

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2019 Ken Rabold * Copyright (C) 2019 Ken Rabold
* 2019 Inria
* *
* 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 General
* Public License v2.1. See the file LICENSE in the top level directory for more * Public License v2.1. See the file LICENSE in the top level directory for more
@ -14,11 +15,14 @@
* @brief Peripheral specific definitions for the HiFive1b RISC-V board * @brief Peripheral specific definitions for the HiFive1b RISC-V board
* *
* @author Ken Rabold * @author Ken Rabold
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
*/ */
#ifndef PERIPH_CONF_H #ifndef PERIPH_CONF_H
#define PERIPH_CONF_H #define PERIPH_CONF_H
#include "periph_cpu.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -49,6 +53,28 @@ extern "C" {
#define TIMER_NUMOF (1) #define TIMER_NUMOF (1)
/** @} */ /** @} */
/**
* @name UART configuration
* @{
*/
static const uart_conf_t uart_config[] = {
{
.addr = UART0_CTRL_ADDR,
.rx = GPIO_PIN(0, 16),
.tx = GPIO_PIN(0, 17),
.isr_num = INT_UART0_BASE,
},
{
.addr = UART1_CTRL_ADDR,
.rx = GPIO_PIN(0, 18),
.tx = GPIO_PIN(0, 23),
.isr_num = INT_UART1_BASE,
},
};
#define UART_NUMOF ARRAY_SIZE(uart_config)
/** @} */
/** /**
* @name RTT/RTC configuration * @name RTT/RTC configuration
* *
@ -76,16 +102,6 @@ extern "C" {
#define PWM_NUMOF (3) #define PWM_NUMOF (3)
/** @} */ /** @} */
/**
* @name UART configuration
*
* @{
*/
#define UART_NUMOF (2)
#define UART0_RX_INTR_PRIORITY (2)
#define UART1_RX_INTR_PRIORITY (2)
/** @} */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -25,8 +25,13 @@
#ifndef CPU_H #ifndef CPU_H
#define CPU_H #define CPU_H
#include <inttypes.h>
#include "thread.h" #include "thread.h"
#include "vendor/platform.h"
#include "vendor/plic_driver.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -19,6 +19,10 @@
#ifndef PERIPH_CPU_H #ifndef PERIPH_CPU_H
#define PERIPH_CPU_H #define PERIPH_CPU_H
#include <inttypes.h>
#include "cpu.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -28,6 +32,39 @@ extern "C" {
*/ */
#define CPUID_LEN (12U) #define CPUID_LEN (12U)
#ifndef DOXYGEN
/**
* @brief Overwrite the default gpio_t type definition
*/
#define HAVE_GPIO_T
typedef uint8_t gpio_t;
#endif
/**
* @brief Definition of a fitting UNDEF value
*/
#define GPIO_UNDEF (0xff)
/**
* @brief Define a CPU specific GPIO pin generator macro
*/
#define GPIO_PIN(x, y) (x | y)
/**
* @brief Structure for UART configuration data
*/
typedef struct {
uint32_t addr; /**< UART control register address */
gpio_t rx; /**< RX pin */
gpio_t tx; /**< TX pin */
plic_source isr_num; /**< ISR source number */
} uart_conf_t;
/**
* @brief UART interrupt priority
*/
#define UART_ISR_PRIO (2)
/** /**
* @brief Prevent shared timer functions from being used * @brief Prevent shared timer functions from being used
*/ */

View File

@ -19,11 +19,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h>
#include <inttypes.h>
#include "irq.h" #include "irq.h"
#include "cpu.h" #include "cpu.h"
#include "periph_cpu.h"
#include "periph_conf.h"
#include "periph/uart.h" #include "periph/uart.h"
#include "vendor/encoding.h" #include "vendor/encoding.h"
#include "vendor/platform.h" #include "vendor/platform.h"
@ -35,29 +35,28 @@
*/ */
static uart_isr_ctx_t isr_ctx[UART_NUMOF]; static uart_isr_ctx_t isr_ctx[UART_NUMOF];
static inline void _uart_isr(uart_t dev)
{
uint32_t data = _REG32(uart_config[dev].addr, UART_REG_RXFIFO);
/* Intr cleared automatically when data is read */
while ((data & UART_RXFIFO_EMPTY) != (uint32_t)UART_RXFIFO_EMPTY) {
isr_ctx[dev].rx_cb(isr_ctx[dev].arg, (uint8_t)(data & 0xff));
data = _REG32(uart_config[dev].addr, UART_REG_RXFIFO);
}
}
void uart_isr(int num) void uart_isr(int num)
{ {
uint32_t data; switch (num) {
case INT_UART0_BASE:
/* Invoke callback function */ _uart_isr(0);
if (num == INT_UART0_BASE) { break;
data = UART0_REG(UART_REG_RXFIFO); case INT_UART1_BASE:
_uart_isr(1);
/* Intr cleared automatically when data is read */ break;
while ((data & UART_RXFIFO_EMPTY) != (uint32_t)UART_RXFIFO_EMPTY) { default:
isr_ctx[0].rx_cb(isr_ctx[0].arg, (uint8_t) data); break;
data = UART0_REG(UART_REG_RXFIFO);
}
}
if (num == INT_UART1_BASE) {
data = UART1_REG(UART_REG_RXFIFO);
/* Intr cleared automatically when data is read */
while ((data & UART_RXFIFO_EMPTY) != (uint32_t)UART_RXFIFO_EMPTY) {
isr_ctx[1].rx_cb(isr_ctx[1].arg, (uint8_t) data);
data = UART1_REG(UART_REG_RXFIFO);
}
} }
} }
@ -66,9 +65,7 @@ int uart_init(uart_t dev, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
uint32_t uartDiv; uint32_t uartDiv;
/* Check for valid UART dev */ /* Check for valid UART dev */
if (dev >= UART_NUMOF) { assert(dev < UART_NUMOF);
return UART_NODEV;
}
/* Save interrupt callback context */ /* Save interrupt callback context */
isr_ctx[dev].rx_cb = rx_cb; isr_ctx[dev].rx_cb = rx_cb;
@ -77,7 +74,7 @@ int uart_init(uart_t dev, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
/* Power on the device */ /* Power on the device */
uart_poweron(dev); uart_poweron(dev);
/* Calculate buadrate divisor given current CPU clk rate /* Calculate baudrate divisor given current CPU clk rate
* Ignore the first run (icache needs to be warm) */ * Ignore the first run (icache needs to be warm) */
uartDiv = PRCI_measure_mcycle_freq(1000, RTC_FREQ); uartDiv = PRCI_measure_mcycle_freq(1000, RTC_FREQ);
/* cppcheck-suppress redundantAssignment /* cppcheck-suppress redundantAssignment
@ -86,48 +83,37 @@ int uart_init(uart_t dev, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
uartDiv = uartDiv / baudrate; uartDiv = uartDiv / baudrate;
/* Enable UART 8-N-1 at given baudrate */ /* Enable UART 8-N-1 at given baudrate */
if (dev == 0) { _REG32(uart_config[dev].addr, UART_REG_DIV) = uartDiv;
/* Config UART */
UART0_REG(UART_REG_DIV) = uartDiv;
UART0_REG(UART_REG_TXCTRL) = UART_TXEN;
/* Enable RX intr if there is a callback */ /* Select IOF0 */
if (rx_cb != NULL) { GPIO_REG(GPIO_IOF_SEL) &= ~(1 << uart_config[dev].tx);
/* Disable ext interrupts when setting up */ /* Enable IOF */
clear_csr(mie, MIP_MEIP); GPIO_REG(GPIO_IOF_EN) |= (1 << uart_config[dev].tx);
/* Configure UART ISR with PLIC */ /* Enable TX */
set_external_isr_cb(INT_UART0_BASE, uart_isr); _REG32(uart_config[dev].addr, UART_REG_TXCTRL) = UART_TXEN;
PLIC_enable_interrupt(INT_UART0_BASE);
PLIC_set_priority(INT_UART0_BASE, UART0_RX_INTR_PRIORITY);
UART0_REG(UART_REG_IE) = UART_IP_RXWM;
UART0_REG(UART_REG_RXCTRL) = UART_RXEN;
/* Re-eanble ext interrupts */ /* Enable RX intr if there is a callback */
set_csr(mie, MIP_MEIP); if (rx_cb) {
} /* Select IOF0 */
} GPIO_REG(GPIO_IOF_SEL) &= ~(1 << uart_config[dev].rx);
/* Enable IOF */
GPIO_REG(GPIO_IOF_EN) |= (1 << uart_config[dev].rx);
if (dev == 1) { /* Disable ext interrupts when setting up */
/* Config UART */ clear_csr(mie, MIP_MEIP);
UART1_REG(UART_REG_DIV) = uartDiv;
UART1_REG(UART_REG_TXCTRL) = UART_TXEN;
/* Enable RX intr if there is a callback */ /* Configure UART ISR with PLIC */
if (rx_cb != NULL) { set_external_isr_cb(uart_config[dev].isr_num, uart_isr);
/* Disable ext interrupts when setting up */ PLIC_enable_interrupt(uart_config[dev].isr_num);
clear_csr(mie, MIP_MEIP); PLIC_set_priority(uart_config[dev].isr_num, UART_ISR_PRIO);
_REG32(uart_config[dev].addr, UART_REG_IE) = UART_IP_RXWM;
/* Configure UART ISR with PLIC */ /* Enable RX */
set_external_isr_cb(INT_UART1_BASE, uart_isr); _REG32(uart_config[dev].addr, UART_REG_RXCTRL) = UART_RXEN;
PLIC_enable_interrupt(INT_UART1_BASE);
PLIC_set_priority(INT_UART1_BASE, UART1_RX_INTR_PRIORITY);
UART1_REG(UART_REG_IE) = UART_IP_RXWM;
UART1_REG(UART_REG_RXCTRL) = UART_RXEN;
/* Re-eanble ext interrupts */ /* Re-enable ext interrupts */
set_csr(mie, MIP_MEIP); set_csr(mie, MIP_MEIP);
}
} }
return UART_OK; return UART_OK;
@ -135,28 +121,13 @@ int uart_init(uart_t dev, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
void uart_write(uart_t dev, const uint8_t *data, size_t len) void uart_write(uart_t dev, const uint8_t *data, size_t len)
{ {
if (dev == 0) { for (size_t i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) { /* Wait for FIFO to empty */
/* Wait for FIFO to empty */ while ((_REG32(uart_config[dev].addr, UART_REG_TXFIFO) & UART_TXFIFO_FULL)
while ((UART0_REG(UART_REG_TXFIFO) & UART_TXFIFO_FULL) == (uint32_t)UART_TXFIFO_FULL) {};
== (uint32_t)UART_TXFIFO_FULL)
;
/* Write a byte */ /* Write a byte */
UART0_REG(UART_REG_TXFIFO) = data[i]; _REG32(uart_config[dev].addr, UART_REG_TXFIFO) = data[i];
}
}
if (dev == 1) {
for (size_t i = 0; i < len; i++) {
/* Wait for FIFO to empty */
while ((UART1_REG(UART_REG_TXFIFO) & UART_TXFIFO_FULL)
== (uint32_t)UART_TXFIFO_FULL)
;
/* Write a byte */
UART1_REG(UART_REG_TXFIFO) = data[i];
}
} }
} }