2021-08-06 18:35:58 +08:00
|
|
|
/*
|
2022-12-20 17:49:37 +08:00
|
|
|
* serial.c UART driver
|
2021-08-06 18:35:58 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2022-12-20 17:49:37 +08:00
|
|
|
* 2013-03-30 Bernard the first verion
|
2021-08-06 18:35:58 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
#include "board.h"
|
2022-12-20 17:49:37 +08:00
|
|
|
#include "mmu.h"
|
2021-08-06 18:35:58 +08:00
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
struct hw_uart_device
|
2021-08-06 18:35:58 +08:00
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
rt_size_t hw_base;
|
|
|
|
rt_size_t irqno;
|
|
|
|
};
|
2021-08-06 18:35:58 +08:00
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
#define UART_DR(base) __REG32(base + 0x00)
|
|
|
|
#define UART_FR(base) __REG32(base + 0x18)
|
|
|
|
#define UART_CR(base) __REG32(base + 0x30)
|
|
|
|
#define UART_IMSC(base) __REG32(base + 0x38)
|
|
|
|
#define UART_ICR(base) __REG32(base + 0x44)
|
2021-08-06 18:35:58 +08:00
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
#define UARTFR_RXFE 0x10
|
|
|
|
#define UARTFR_TXFF 0x20
|
|
|
|
#define UARTIMSC_RXIM 0x10
|
|
|
|
#define UARTIMSC_TXIM 0x20
|
|
|
|
#define UARTICR_RXIC 0x10
|
|
|
|
#define UARTICR_TXIC 0x20
|
|
|
|
|
|
|
|
static void rt_hw_uart_isr(int irqno, void *param)
|
2021-08-06 18:35:58 +08:00
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
struct rt_serial_device *serial = (struct rt_serial_device *)param;
|
|
|
|
|
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
|
|
|
|
}
|
2021-08-06 18:35:58 +08:00
|
|
|
|
|
|
|
static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct hw_uart_device *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = (struct hw_uart_device *)serial->parent.user_data;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
|
|
|
/* disable rx irq */
|
|
|
|
UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
|
|
|
/* enable rx irq */
|
|
|
|
UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
|
|
|
|
rt_hw_interrupt_umask(uart->irqno);
|
|
|
|
break;
|
2023-10-21 08:41:55 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
2021-08-06 18:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uart_putc(struct rt_serial_device *serial, char c)
|
|
|
|
{
|
|
|
|
struct hw_uart_device *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = (struct hw_uart_device *)serial->parent.user_data;
|
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
while (UART_FR(uart->hw_base) & UARTFR_TXFF);
|
|
|
|
UART_DR(uart->hw_base) = c;
|
2021-08-06 18:35:58 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uart_getc(struct rt_serial_device *serial)
|
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
int ch;
|
2021-08-06 18:35:58 +08:00
|
|
|
struct hw_uart_device *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = (struct hw_uart_device *)serial->parent.user_data;
|
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
ch = -1;
|
|
|
|
if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
|
2021-08-06 18:35:58 +08:00
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
ch = UART_DR(uart->hw_base) & 0xff;
|
2021-08-06 18:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_uart_ops _uart_ops =
|
|
|
|
{
|
|
|
|
uart_configure,
|
|
|
|
uart_control,
|
|
|
|
uart_putc,
|
|
|
|
uart_getc,
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART0
|
|
|
|
/* UART device driver structure */
|
|
|
|
static struct hw_uart_device _uart0_device =
|
|
|
|
{
|
|
|
|
PL011_UART0_BASE,
|
2021-09-22 17:57:45 +08:00
|
|
|
PL011_UART0_IRQNUM,
|
2021-08-06 18:35:58 +08:00
|
|
|
};
|
|
|
|
static struct rt_serial_device _serial0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int rt_hw_uart_init(void)
|
|
|
|
{
|
|
|
|
struct hw_uart_device *uart;
|
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART0
|
2022-12-20 17:49:37 +08:00
|
|
|
_uart0_device.hw_base = (rt_size_t)rt_ioremap((void*)_uart0_device.hw_base, PL011_UART0_SIZE);
|
2021-08-06 18:35:58 +08:00
|
|
|
uart = &_uart0_device;
|
|
|
|
|
|
|
|
_serial0.ops = &_uart_ops;
|
|
|
|
_serial0.config = config;
|
|
|
|
|
|
|
|
/* register UART1 device */
|
|
|
|
rt_hw_serial_register(&_serial0, "uart0",
|
2022-12-20 17:49:37 +08:00
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
|
|
|
|
uart);
|
2021-08-06 18:35:58 +08:00
|
|
|
rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
|
2022-12-20 17:49:37 +08:00
|
|
|
/* enable Rx and Tx of UART */
|
|
|
|
UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
|
2021-08-06 18:35:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|