2023-03-19 14:41:18 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2022/12/25 flyingcys first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include "drv_uart.h"
|
|
|
|
|
2023-04-26 08:50:03 +08:00
|
|
|
#ifdef RT_USING_SMART
|
|
|
|
#include <ioremap.h>
|
|
|
|
#endif
|
|
|
|
|
2023-03-19 14:41:18 +08:00
|
|
|
#define DBG_TAG "DRV.UART"
|
|
|
|
#define DBG_LVL DBG_WARNING
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
#define UART_DEFAULT_BAUDRATE 2000000
|
|
|
|
|
|
|
|
struct device_uart
|
|
|
|
{
|
|
|
|
struct rt_serial_device serial;
|
|
|
|
struct bflb_device_s *bflb_device;
|
|
|
|
};
|
|
|
|
|
2023-04-26 08:50:03 +08:00
|
|
|
static void uart_irq_handler(int irq, void *arg)
|
2023-03-19 14:41:18 +08:00
|
|
|
{
|
|
|
|
struct rt_serial_device *serial = (struct rt_serial_device *)arg;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
|
|
|
struct device_uart *uart = serial->parent.user_data;
|
|
|
|
RT_ASSERT(uart != RT_NULL);
|
|
|
|
RT_ASSERT(uart->bflb_device != RT_NULL);
|
|
|
|
|
|
|
|
uint32_t intstatus = bflb_uart_get_intstatus(uart->bflb_device);
|
|
|
|
if (intstatus & UART_INTSTS_RX_FIFO)
|
|
|
|
{
|
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intstatus & UART_INTSTS_RTO)
|
|
|
|
{
|
|
|
|
bflb_uart_int_clear(uart->bflb_device, UART_INTCLR_RTO);
|
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _uart_configure(struct rt_serial_device *serial, struct serial_configure *serial_cfg)
|
|
|
|
{
|
|
|
|
struct device_uart *uart = RT_NULL;
|
|
|
|
struct bflb_uart_config_s uart_cfg;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
RT_ASSERT(serial_cfg != RT_NULL);
|
|
|
|
|
|
|
|
uart = serial->parent.user_data;
|
|
|
|
RT_ASSERT(uart != RT_NULL);
|
|
|
|
RT_ASSERT(uart->bflb_device != RT_NULL);
|
|
|
|
|
|
|
|
uart_cfg.baudrate = UART_DEFAULT_BAUDRATE;
|
|
|
|
uart_cfg.data_bits = UART_DATA_BITS_8;
|
|
|
|
uart_cfg.stop_bits = UART_STOP_BITS_1;
|
|
|
|
uart_cfg.parity = UART_PARITY_NONE;
|
|
|
|
uart_cfg.flow_ctrl = 0;
|
|
|
|
uart_cfg.tx_fifo_threshold = 7;
|
|
|
|
uart_cfg.rx_fifo_threshold = 7;
|
|
|
|
|
|
|
|
uart_cfg.baudrate = serial_cfg->baud_rate;
|
|
|
|
|
|
|
|
switch (serial_cfg->data_bits)
|
|
|
|
{
|
|
|
|
case DATA_BITS_5:
|
|
|
|
uart_cfg.data_bits = UART_DATA_BITS_5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_BITS_6:
|
|
|
|
uart_cfg.data_bits = UART_DATA_BITS_6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_BITS_7:
|
|
|
|
uart_cfg.data_bits = UART_DATA_BITS_7;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_BITS_8:
|
|
|
|
uart_cfg.data_bits = UART_DATA_BITS_8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
uart_cfg.data_bits = UART_DATA_BITS_8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (serial_cfg->stop_bits)
|
|
|
|
{
|
|
|
|
case STOP_BITS_1:
|
|
|
|
uart_cfg.stop_bits = UART_STOP_BITS_1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STOP_BITS_2:
|
|
|
|
uart_cfg.stop_bits = UART_STOP_BITS_2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
uart_cfg.stop_bits = UART_STOP_BITS_1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (serial_cfg->parity)
|
|
|
|
{
|
|
|
|
case PARITY_NONE:
|
|
|
|
uart_cfg.parity = UART_PARITY_NONE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARITY_ODD:
|
|
|
|
uart_cfg.parity = UART_PARITY_ODD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARITY_EVEN:
|
|
|
|
uart_cfg.parity = UART_PARITY_EVEN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
uart_cfg.parity = UART_PARITY_NONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bflb_uart_init(uart->bflb_device, &uart_cfg);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _uart_control(struct rt_serial_device *serial, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct device_uart *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
|
|
|
uart = serial->parent.user_data;
|
|
|
|
RT_ASSERT(uart != RT_NULL);
|
|
|
|
RT_ASSERT(uart->bflb_device != RT_NULL);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
2023-04-26 08:50:03 +08:00
|
|
|
bflb_uart_rxint_mask(uart->bflb_device, true);
|
|
|
|
#ifdef BL808_CORE_D0
|
|
|
|
rt_hw_interrupt_mask(uart->bflb_device->irq_num);
|
|
|
|
rt_hw_interrupt_install(uart->bflb_device->irq_num, RT_NULL, serial, RT_NULL);
|
|
|
|
#else
|
2023-03-19 14:41:18 +08:00
|
|
|
bflb_irq_disable(uart->bflb_device->irq_num);
|
2023-04-26 08:50:03 +08:00
|
|
|
bflb_irq_attach(uart->bflb_device->irq_num, RT_NULL, RT_NULL);
|
|
|
|
#endif
|
2023-03-19 14:41:18 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
|
|
|
bflb_uart_rxint_mask(uart->bflb_device, false);
|
2023-04-26 08:50:03 +08:00
|
|
|
#ifdef BL808_CORE_D0
|
|
|
|
rt_hw_interrupt_install(uart->bflb_device->irq_num, uart_irq_handler, serial, RT_NULL);
|
|
|
|
rt_hw_interrupt_umask(uart->bflb_device->irq_num);
|
|
|
|
#else
|
|
|
|
bflb_irq_attach(uart->bflb_device->irq_num, uart_irq_handler, serial);
|
2023-03-19 14:41:18 +08:00
|
|
|
bflb_irq_enable(uart->bflb_device->irq_num);
|
2023-04-26 08:50:03 +08:00
|
|
|
#endif
|
2023-03-19 14:41:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _uart_putc(struct rt_serial_device *serial, char c)
|
|
|
|
{
|
|
|
|
struct device_uart *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
|
|
|
uart = serial->parent.user_data;
|
|
|
|
RT_ASSERT(uart != RT_NULL);
|
|
|
|
RT_ASSERT(uart->bflb_device != RT_NULL);
|
|
|
|
|
|
|
|
bflb_uart_putchar(uart->bflb_device, c);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _uart_getc(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
int ch = -1;
|
|
|
|
struct device_uart *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = serial->parent.user_data;
|
|
|
|
RT_ASSERT(uart != RT_NULL);
|
|
|
|
RT_ASSERT(uart->bflb_device != RT_NULL);
|
|
|
|
|
|
|
|
ch = bflb_uart_getchar(uart->bflb_device);
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_uart_ops _uart_ops =
|
|
|
|
{
|
|
|
|
.configure = _uart_configure,
|
|
|
|
.control = _uart_control,
|
|
|
|
.putc = _uart_putc,
|
|
|
|
.getc = _uart_getc,
|
|
|
|
.dma_transmit = RT_NULL
|
|
|
|
};
|
|
|
|
|
2023-04-26 08:50:03 +08:00
|
|
|
static void _uart_init(const char *name, struct device_uart *uart, rt_uint32_t flag)
|
|
|
|
{
|
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
|
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
|
|
|
struct rt_serial_device *serial;
|
|
|
|
|
|
|
|
serial = &uart->serial;
|
|
|
|
|
|
|
|
serial->ops = &_uart_ops;
|
|
|
|
serial->config = config;
|
|
|
|
serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
|
|
|
|
|
|
|
|
/* register USART device */
|
|
|
|
result = rt_hw_serial_register(serial,
|
|
|
|
name,
|
|
|
|
flag,
|
|
|
|
uart);
|
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
}
|
2023-03-19 14:41:18 +08:00
|
|
|
/*
|
|
|
|
* UART Initiation
|
|
|
|
*/
|
|
|
|
int rt_hw_uart_init(void)
|
|
|
|
{
|
2023-03-25 19:05:33 +08:00
|
|
|
struct bflb_device_s *gpio;
|
2023-04-26 08:50:03 +08:00
|
|
|
rt_uint32_t flag;
|
2023-03-19 14:41:18 +08:00
|
|
|
|
|
|
|
gpio = bflb_device_get_by_name("gpio");
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
static struct device_uart bl_uart0;
|
2023-04-26 08:50:03 +08:00
|
|
|
bl_uart0.bflb_device = bflb_device_get_by_name("uart0");
|
2023-03-19 14:41:18 +08:00
|
|
|
|
|
|
|
bflb_gpio_uart_init(gpio, UART0_GPIO_TX, GPIO_UART_FUNC_UART0_TX);
|
|
|
|
bflb_gpio_uart_init(gpio, UART0_GPIO_RX, GPIO_UART_FUNC_UART0_RX);
|
|
|
|
|
2023-04-26 08:50:03 +08:00
|
|
|
flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
|
|
|
|
|
|
|
|
_uart_init("uart0", &bl_uart0, flag);
|
2023-03-19 14:41:18 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
static struct device_uart bl_uart1;
|
2023-04-26 08:50:03 +08:00
|
|
|
bl_uart1.bflb_device = bflb_device_get_by_name("uart1");
|
2023-03-19 14:41:18 +08:00
|
|
|
|
|
|
|
bflb_gpio_uart_init(gpio, UART1_GPIO_TX, GPIO_UART_FUNC_UART1_TX);
|
|
|
|
bflb_gpio_uart_init(gpio, UART1_GPIO_RX, GPIO_UART_FUNC_UART1_RX);
|
|
|
|
|
2023-04-26 08:50:03 +08:00
|
|
|
flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
|
|
|
|
|
|
|
|
_uart_init("uart1", &bl_uart1, flag);
|
2023-03-19 14:41:18 +08:00
|
|
|
#endif
|
2023-03-25 19:05:33 +08:00
|
|
|
|
|
|
|
#ifdef BSP_USING_UART2
|
|
|
|
static struct device_uart bl_uart2;
|
2023-04-26 08:50:03 +08:00
|
|
|
bl_uart2.bflb_device = bflb_device_get_by_name("uart2");
|
2023-03-25 19:05:33 +08:00
|
|
|
|
|
|
|
bflb_gpio_uart_init(gpio, UART2_GPIO_TX, GPIO_UART_FUNC_UART2_TX);
|
|
|
|
bflb_gpio_uart_init(gpio, UART2_GPIO_RX, GPIO_UART_FUNC_UART2_RX);
|
|
|
|
|
2023-04-26 08:50:03 +08:00
|
|
|
flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
|
|
|
|
|
|
|
|
_uart_init("uart2", &bl_uart2, flag);
|
2023-03-25 19:05:33 +08:00
|
|
|
#endif
|
2023-04-26 08:50:03 +08:00
|
|
|
|
|
|
|
#ifdef BSP_USING_UART3
|
|
|
|
static struct device_uart bl_uart3;
|
|
|
|
bl_uart3.bflb_device = bflb_device_get_by_name("uart3");
|
|
|
|
|
|
|
|
bflb_gpio_init(gpio, UART3_GPIO_TX, 21 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
|
|
|
|
bflb_gpio_init(gpio, UART3_GPIO_RX, 21 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
|
|
|
|
|
|
|
|
flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
|
|
|
|
if(strcmp("uart3", RT_CONSOLE_DEVICE_NAME) == 0)
|
|
|
|
flag |= RT_DEVICE_FLAG_STREAM;
|
|
|
|
|
|
|
|
_uart_init("uart3", &bl_uart3, flag);
|
|
|
|
#endif
|
|
|
|
|
2023-03-19 14:41:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|