2021-02-11 04:45:50 +08:00
|
|
|
/*
|
2021-02-11 16:18:33 +08:00
|
|
|
* Copyright (c) 2006-2020, RT-Thread Development Team
|
2021-02-11 04:45:50 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2021-02-11 16:18:33 +08:00
|
|
|
* 2021-02-11 supperthomas first version
|
|
|
|
*
|
2021-02-11 04:45:50 +08:00
|
|
|
*/
|
|
|
|
|
2021-02-11 16:18:33 +08:00
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
#include "board.h"
|
|
|
|
#include "uart.h"
|
|
|
|
#include "rtdevice.h"
|
2021-02-11 16:18:33 +08:00
|
|
|
#ifdef RT_USING_SERIAL
|
2021-02-11 04:45:50 +08:00
|
|
|
|
2021-02-12 00:28:44 +08:00
|
|
|
//#define DRV_DEBUG
|
|
|
|
//#define LOG_TAG "drv.usart"
|
|
|
|
//#include <drv_log.h>
|
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
#define UART0_CONFIG \
|
|
|
|
{ \
|
|
|
|
.name = "uart0", \
|
2021-02-12 00:28:44 +08:00
|
|
|
.Instance = MXC_UART_GET_UART(0), \
|
|
|
|
.irq_type = MXC_UART_GET_IRQ(0), \
|
2021-02-11 04:45:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define UART1_CONFIG \
|
|
|
|
{ \
|
|
|
|
.name = "uart1", \
|
2021-02-12 00:28:44 +08:00
|
|
|
.Instance = MXC_UART_GET_UART(1), \
|
|
|
|
.irq_type = MXC_UART_GET_IRQ(1), \
|
2021-02-11 04:45:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct mcu_uart_config
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
mxc_uart_regs_t *Instance;
|
|
|
|
IRQn_Type irq_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mcu_uart
|
|
|
|
{
|
|
|
|
mxc_uart_regs_t *handle;
|
|
|
|
struct mcu_uart_config *config;
|
|
|
|
|
|
|
|
rt_uint16_t uart_dma_flag;
|
|
|
|
struct rt_serial_device serial;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-02-11 16:18:33 +08:00
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
|
|
|
|
#if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1)
|
|
|
|
|
|
|
|
#error "Please define at least one BSP_USING_UARTx"
|
|
|
|
/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
UART0_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
UART1_INDEX,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mcu_uart_config uart_config[] =
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
UART0_CONFIG,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
UART1_CONFIG,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mcu_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
void UART1_IRQHandler(void)
|
|
|
|
{
|
|
|
|
rt_interrupt_enter();
|
2021-02-11 10:57:00 +08:00
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
rt_hw_serial_isr(&(uart_obj[UART1_INDEX].serial), RT_SERIAL_EVENT_RX_IND);
|
2021-02-11 10:57:00 +08:00
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
uint32_t intst = 0;
|
|
|
|
intst = MXC_UART1->int_fl;
|
|
|
|
MXC_UART1->int_fl = intst;
|
2021-02-12 00:28:44 +08:00
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
void UART0_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
2021-02-11 10:57:00 +08:00
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
rt_hw_serial_isr(&(uart_obj[UART0_INDEX].serial), RT_SERIAL_EVENT_RX_IND);
|
2021-02-11 10:57:00 +08:00
|
|
|
/* clear flags */
|
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
uint32_t intst = 0;
|
|
|
|
intst = MXC_UART0->int_fl;
|
|
|
|
MXC_UART0->int_fl = intst;
|
2021-02-11 10:57:00 +08:00
|
|
|
|
2021-02-11 04:45:50 +08:00
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static rt_err_t mcu_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
|
|
|
|
{
|
2021-02-11 10:57:00 +08:00
|
|
|
int error;
|
2021-02-11 04:45:50 +08:00
|
|
|
struct mcu_uart *uart;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
RT_ASSERT(cfg != RT_NULL);
|
2021-02-11 10:57:00 +08:00
|
|
|
const sys_cfg_uart_t sys_uart_cfg =
|
|
|
|
{
|
2021-02-11 04:45:50 +08:00
|
|
|
MAP_A,
|
|
|
|
UART_FLOW_DISABLE,
|
|
|
|
};
|
|
|
|
uart = rt_container_of(serial, struct mcu_uart, serial);
|
|
|
|
uart_cfg_t mcu_cfg;
|
|
|
|
uart->handle = uart->config->Instance;
|
|
|
|
mcu_cfg.baud = cfg->baud_rate;
|
|
|
|
mcu_cfg.stop = UART_STOP_1;
|
|
|
|
mcu_cfg.parity = UART_PARITY_DISABLE;
|
|
|
|
mcu_cfg.size = UART_DATA_SIZE_8_BITS;
|
|
|
|
mcu_cfg.flow = UART_FLOW_CTRL_EN;
|
|
|
|
mcu_cfg.pol = UART_FLOW_POL_EN;
|
2021-02-11 10:57:00 +08:00
|
|
|
|
|
|
|
error = UART_Init(uart->handle, &mcu_cfg, &sys_uart_cfg);
|
2021-02-12 00:28:44 +08:00
|
|
|
if (error != E_NO_ERROR)
|
|
|
|
{
|
2021-02-11 10:57:00 +08:00
|
|
|
rt_kprintf("Error initializing UART %d\n", error);
|
2021-02-12 00:28:44 +08:00
|
|
|
while (1) {}
|
2021-02-11 10:57:00 +08:00
|
|
|
}
|
2021-02-11 04:45:50 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t mcu_control(struct rt_serial_device *serial, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct mcu_uart *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = rt_container_of(serial, struct mcu_uart, serial);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
|
|
|
/* disable rx irq */
|
|
|
|
NVIC_ClearPendingIRQ(uart->config->irq_type);
|
|
|
|
NVIC_DisableIRQ(uart->config->irq_type);
|
|
|
|
/* disable interrupt */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
|
|
|
/* enable rx irq */
|
2021-02-11 10:57:00 +08:00
|
|
|
NVIC_SetPriority(uart->config->irq_type, 1);
|
|
|
|
NVIC_EnableIRQ(uart->config->irq_type);
|
|
|
|
/* enable interrupt */
|
|
|
|
uart->handle->ctrl |= 0x05 << MXC_F_UART_CTRL_RX_TO_POS;
|
|
|
|
uart->handle->int_en |= MXC_F_UART_INT_EN_RX_FIFO_THRESH | \
|
|
|
|
MXC_F_UART_INT_EN_RX_TIMEOUT;
|
2021-02-12 00:28:44 +08:00
|
|
|
|
2021-02-11 10:57:00 +08:00
|
|
|
uart->handle->int_en |= MXC_F_UART_INT_EN_RX_FRAME_ERROR | \
|
|
|
|
MXC_F_UART_INT_EN_RX_PARITY_ERROR | \
|
|
|
|
MXC_F_UART_INT_EN_RX_OVERRUN ;
|
2021-02-11 04:45:50 +08:00
|
|
|
|
2021-02-11 10:57:00 +08:00
|
|
|
uart->handle->thresh_ctrl = MXC_UART_FIFO_DEPTH <<
|
|
|
|
MXC_F_UART_THRESH_CTRL_RX_FIFO_THRESH_POS;
|
2021-02-11 04:45:50 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_DEVICE_CTRL_CLOSE:
|
2021-02-11 10:57:00 +08:00
|
|
|
UART_Shutdown(uart->handle);
|
2021-02-11 04:45:50 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcu_putc(struct rt_serial_device *serial, char c)
|
|
|
|
{
|
|
|
|
struct mcu_uart *uart;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
|
|
|
uart = rt_container_of(serial, struct mcu_uart, serial);
|
|
|
|
UART_WriteByte(uart->handle, c);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcu_getc(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
struct mcu_uart *uart;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = rt_container_of(serial, struct mcu_uart, serial);
|
|
|
|
|
|
|
|
ch = -1;
|
|
|
|
|
2021-02-11 10:57:00 +08:00
|
|
|
if (UART_NumReadAvail(uart->handle))
|
2021-02-11 04:45:50 +08:00
|
|
|
{
|
2021-02-11 10:57:00 +08:00
|
|
|
ch = UART_ReadByte(uart->handle);
|
2021-02-11 04:45:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_uart_ops mcu_uart_ops =
|
|
|
|
{
|
|
|
|
.configure = mcu_configure,
|
|
|
|
.control = mcu_control,
|
|
|
|
.putc = mcu_putc,
|
|
|
|
.getc = mcu_getc,
|
|
|
|
};
|
|
|
|
|
|
|
|
int rt_hw_usart_init(void)
|
|
|
|
{
|
|
|
|
rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct mcu_uart);
|
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
|
|
|
rt_err_t result = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < obj_num; i++)
|
|
|
|
{
|
|
|
|
/* init UART object */
|
|
|
|
uart_obj[i].config = &uart_config[i];
|
|
|
|
uart_obj[i].serial.ops = &mcu_uart_ops;
|
|
|
|
uart_obj[i].serial.config = config;
|
|
|
|
|
|
|
|
/* register UART device */
|
|
|
|
result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
|
|
|
|
RT_DEVICE_FLAG_RDWR
|
|
|
|
| RT_DEVICE_FLAG_INT_RX
|
|
|
|
| RT_DEVICE_FLAG_INT_TX
|
2021-10-13 11:02:01 +08:00
|
|
|
, RT_NULL);
|
2021-02-11 04:45:50 +08:00
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
//INIT_BOARD_EXPORT(rt_hw_usart_init);
|
|
|
|
#endif /* RT_USING_SERIAL */
|