2021-05-18 09:57:25 +08:00
|
|
|
/*
|
2022-12-03 12:07:44 +08:00
|
|
|
* Copyright (c) 2006-2020, RT-Thread Development Team
|
2021-05-18 09:57:25 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-05-21 17:03:30 +08:00
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2021-05-18 09:57:25 +08:00
|
|
|
*/
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2021-05-18 09:57:25 +08:00
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtdevice.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <rtthread.h>
|
2021-05-18 09:57:25 +08:00
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include "drv_uart.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <ioremap.h>
|
|
|
|
#endif
|
2021-05-18 09:57:25 +08:00
|
|
|
#include "sbi.h"
|
|
|
|
|
|
|
|
struct device_uart
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_ubase_t hw_base;
|
2021-05-18 09:57:25 +08:00
|
|
|
rt_uint32_t irqno;
|
|
|
|
};
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
void *uart0_base = (void*)0x10000000;
|
|
|
|
struct rt_serial_device serial0;
|
|
|
|
struct device_uart uart0;
|
2021-05-18 09:57:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
void uart_init(void)
|
2021-05-18 09:57:25 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_uint32_t div = UART_REFERENCE_CLOCK / (UART_DEFAULT_BAUDRATE * 16);
|
2021-05-18 09:57:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
write8_uart0(UART_IER, 0x00);
|
|
|
|
write8_uart0(UART_LCR, UART_LCR_BAUD_LATCH);
|
2021-05-18 09:57:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
// LSB
|
|
|
|
write8_uart0(0, div & 0xff);
|
|
|
|
// MSB
|
|
|
|
write8_uart0(1, (div >> 8) & 0xff);
|
2021-05-18 09:57:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
// set word length to 8 bits, no parity
|
|
|
|
write8_uart0(UART_LCR, UART_LCR_EIGHT_BITS);
|
|
|
|
|
|
|
|
write8_uart0(UART_FCR, UART_FCR_FIFO_ENABLE | UART_FCR_FIFO_CLEAR);
|
|
|
|
|
|
|
|
return;
|
2021-05-18 09:57:25 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static rt_err_t _uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
|
2021-05-18 09:57:25 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
uart_init();
|
|
|
|
return (RT_EOK);
|
|
|
|
}
|
2021-05-18 09:57:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static rt_err_t _uart_control(struct rt_serial_device *serial, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct device_uart *uart = (struct device_uart*)serial->parent.user_data;
|
2021-05-18 09:57:25 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
2022-12-03 12:07:44 +08:00
|
|
|
if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
|
|
|
|
{
|
|
|
|
rt_uint8_t value = read8_uart0(UART_IER);
|
|
|
|
write8_uart0(UART_IER, value & ~UART_IER_RX_ENABLE);
|
|
|
|
}
|
2021-05-18 09:57:25 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
2022-12-03 12:07:44 +08:00
|
|
|
if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
|
|
|
|
{
|
|
|
|
rt_uint8_t value = read8_uart0(UART_IER);
|
|
|
|
write8_uart0(UART_IER, value | UART_IER_RX_ENABLE);
|
|
|
|
}
|
2021-05-18 09:57:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (RT_EOK);
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static int _uart_putc(struct rt_serial_device *serial, char c)
|
2021-05-21 18:39:41 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
struct device_uart *uart;
|
|
|
|
uart = (struct device_uart*)serial->parent.user_data;
|
|
|
|
|
|
|
|
// wait for Transmit Holding Empty to be set in LSR.
|
|
|
|
while((read8_uart0(UART_LSR) & UART_LSR_TX_IDLE) == 0)
|
|
|
|
;
|
|
|
|
write8_uart0(UART_THR, c);
|
|
|
|
|
|
|
|
return (1);
|
2021-05-18 09:57:25 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static int _uart_getc(struct rt_serial_device *serial)
|
2021-05-18 09:57:25 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
struct device_uart *uart;
|
|
|
|
volatile rt_uint32_t lsr;
|
|
|
|
int ch = -1;
|
|
|
|
|
|
|
|
uart = (struct device_uart*)serial->parent.user_data;
|
|
|
|
lsr = read8_uart0(UART_LSR);
|
|
|
|
|
|
|
|
if (lsr & UART_LSR_RX_READY)
|
|
|
|
{
|
|
|
|
ch = read8_uart0(UART_RHR);
|
2021-05-21 17:03:30 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
return ch;
|
2021-05-18 09:57:25 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
const struct rt_uart_ops _uart_ops = {
|
|
|
|
_uart_configure,
|
|
|
|
_uart_control,
|
|
|
|
_uart_putc,
|
|
|
|
_uart_getc,
|
|
|
|
// TODO: add DMA support
|
|
|
|
RT_NULL};
|
|
|
|
|
2021-05-21 17:03:30 +08:00
|
|
|
static void rt_hw_uart_isr(int irqno, void *param)
|
2021-05-18 09:57:25 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_ubase_t level = rt_hw_interrupt_disable();
|
2021-05-18 09:57:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
struct rt_serial_device *serial = (struct rt_serial_device *)param;
|
2021-05-18 09:57:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
|
|
|
|
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
2021-05-18 09:57:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* UART Initiation
|
|
|
|
*/
|
|
|
|
int rt_hw_uart_init(void)
|
|
|
|
{
|
|
|
|
struct rt_serial_device *serial;
|
2022-12-03 12:07:44 +08:00
|
|
|
struct device_uart *uart;
|
2021-05-18 09:57:25 +08:00
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-03 12:07:44 +08:00
|
|
|
uart0_base = rt_ioremap(uart0_base, 4096);
|
|
|
|
#endif
|
|
|
|
// register device
|
|
|
|
serial = &serial0;
|
|
|
|
uart = &uart0;
|
|
|
|
|
|
|
|
serial->ops = &_uart_ops;
|
|
|
|
serial->config = config;
|
|
|
|
serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
|
|
|
|
uart->hw_base = (rt_ubase_t)uart0_base;
|
|
|
|
uart->irqno = 0x0a;
|
|
|
|
|
|
|
|
rt_hw_serial_register(serial,
|
|
|
|
RT_CONSOLE_DEVICE_NAME,
|
|
|
|
RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
|
|
|
|
uart);
|
|
|
|
rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, RT_CONSOLE_DEVICE_NAME);
|
2022-12-10 22:16:42 +08:00
|
|
|
rt_hw_interrupt_umask(uart->irqno);
|
2021-05-18 09:57:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
/* WEAK for SDK 0.5.6 */
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_weak void uart_debug_init(int uart_channel)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
}
|