2020-04-14 22:17:27 +08:00
|
|
|
#include <rtdevice.h>
|
2020-04-19 17:37:43 +08:00
|
|
|
#include <nrfx_uart.h>
|
|
|
|
#include "drv_uart.h"
|
2020-04-14 22:17:27 +08:00
|
|
|
|
|
|
|
static struct rt_serial_device _serial0_0;
|
2020-04-19 17:37:43 +08:00
|
|
|
static void uart_event_hander(nrfx_uart_event_t const *p_event,void *p_context);
|
2020-04-14 22:17:27 +08:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
struct rt_serial_device *serial;
|
2020-04-19 17:37:43 +08:00
|
|
|
nrfx_uart_t uart;
|
2020-04-14 22:17:27 +08:00
|
|
|
uint32_t rx_pin;
|
|
|
|
uint32_t tx_pin;
|
2020-04-19 17:37:43 +08:00
|
|
|
nrfx_uart_event_handler_t event_handler;
|
2020-04-14 22:17:27 +08:00
|
|
|
} UART_CFG_T;
|
|
|
|
|
2020-04-19 17:37:43 +08:00
|
|
|
|
2020-04-14 22:17:27 +08:00
|
|
|
UART_CFG_T uart0 = {
|
2020-04-19 17:37:43 +08:00
|
|
|
.uart = NRFX_UART_INSTANCE(0),
|
2020-04-14 22:17:27 +08:00
|
|
|
#ifdef RT_USING_CONSOLE
|
|
|
|
.rx_pin = 8,
|
2020-04-19 17:37:43 +08:00
|
|
|
.tx_pin = 6,
|
|
|
|
.event_handler = uart_event_hander,
|
2020-04-14 22:17:27 +08:00
|
|
|
#else
|
|
|
|
.rx_pin = 19,
|
|
|
|
.tx_pin = 20
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
UART_CFG_T *working_cfg = RT_NULL;
|
|
|
|
|
2020-04-19 17:37:43 +08:00
|
|
|
static void uart_event_hander(nrfx_uart_event_t const *p_event,void *p_context)
|
2020-04-14 22:17:27 +08:00
|
|
|
{
|
2020-04-19 17:37:43 +08:00
|
|
|
|
|
|
|
if (p_event->type == NRFX_UART_EVT_RX_DONE)
|
2020-04-14 22:17:27 +08:00
|
|
|
{
|
|
|
|
rt_hw_serial_isr(working_cfg->serial, RT_SERIAL_EVENT_RX_IND);
|
|
|
|
}
|
2020-04-19 17:37:43 +08:00
|
|
|
if (p_event->type == NRFX_UART_EVT_TX_DONE)
|
2020-04-14 22:17:27 +08:00
|
|
|
{
|
2020-04-19 17:37:43 +08:00
|
|
|
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _uart_cfg(struct rt_serial_device *serial, struct serial_configure *cfg)
|
|
|
|
{
|
2020-04-19 17:37:43 +08:00
|
|
|
nrfx_uart_config_t config = NRFX_UART_DEFAULT_CONFIG(uart0.tx_pin,uart0.rx_pin);
|
2020-04-14 22:17:27 +08:00
|
|
|
UART_CFG_T *instance = &uart0;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
RT_ASSERT(cfg != RT_NULL);
|
|
|
|
|
|
|
|
if (serial->parent.user_data != RT_NULL)
|
|
|
|
{
|
|
|
|
instance = (UART_CFG_T*)serial->parent.user_data;
|
|
|
|
}
|
|
|
|
|
2020-04-19 17:37:43 +08:00
|
|
|
nrfx_uart_uninit(&(instance->uart));
|
2020-04-14 22:17:27 +08:00
|
|
|
|
|
|
|
switch (cfg->baud_rate)
|
|
|
|
{
|
|
|
|
case 115200:
|
|
|
|
config.baudrate = NRF_UART_BAUDRATE_115200;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 9600:
|
|
|
|
config.baudrate = NRF_UART_BAUDRATE_9600;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
config.baudrate = NRF_UART_BAUDRATE_115200;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg->parity == PARITY_NONE)
|
|
|
|
{
|
2020-04-19 17:37:43 +08:00
|
|
|
config.hal_cfg.parity = NRF_UART_PARITY_EXCLUDED;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-19 17:37:43 +08:00
|
|
|
config.hal_cfg.parity = NRF_UART_PARITY_INCLUDED;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2020-04-19 17:37:43 +08:00
|
|
|
config.hal_cfg.hwfc = NRF_UART_HWFC_DISABLED;
|
2020-04-14 22:17:27 +08:00
|
|
|
config.pselrxd = instance->rx_pin;
|
|
|
|
config.pseltxd = instance->tx_pin;
|
|
|
|
|
2020-04-19 17:37:43 +08:00
|
|
|
nrfx_uart_init(&(instance->uart), &config, instance->event_handler);
|
|
|
|
|
|
|
|
nrf_uart_int_enable(instance->uart.p_reg, NRF_UART_INT_MASK_RXDRDY | NRF_UART_INT_MASK_RXTO | NRF_UART_INT_MASK_ERROR);
|
|
|
|
nrf_uart_int_disable(instance->uart.p_reg, NRF_UART_INT_MASK_TXDRDY);
|
|
|
|
|
|
|
|
nrfx_uart_rx_enable(&(instance->uart));
|
|
|
|
|
2020-04-14 22:17:27 +08:00
|
|
|
working_cfg = instance;
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _uart_ctrl(struct rt_serial_device *serial, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
UART_CFG_T *instance = working_cfg;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
|
|
|
if (serial->parent.user_data != RT_NULL)
|
|
|
|
{
|
|
|
|
instance = (UART_CFG_T*)serial->parent.user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_DEVICE_CTRL_CUSTOM:
|
|
|
|
if ((rt_uint32_t)(arg) == UART_CONFIG_BAUD_RATE_9600)
|
|
|
|
{
|
|
|
|
instance->serial->config.baud_rate = 9600;
|
|
|
|
}
|
|
|
|
else if ((rt_uint32_t)(arg) == UART_CONFIG_BAUD_RATE_115200)
|
|
|
|
{
|
|
|
|
instance->serial->config.baud_rate = 115200;
|
|
|
|
}
|
2020-04-19 17:37:43 +08:00
|
|
|
_uart_cfg(instance->serial, &(instance->serial->config));
|
2020-04-14 22:17:27 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_DEVICE_CTRL_PIN:
|
|
|
|
if (working_cfg != instance)
|
|
|
|
{
|
|
|
|
_uart_cfg(instance->serial, &(instance->serial->config));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_DEVICE_POWERSAVE:
|
2020-04-19 17:37:43 +08:00
|
|
|
nrfx_uart_uninit(&(instance->uart));
|
2020-04-14 22:17:27 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_DEVICE_WAKEUP:
|
|
|
|
_uart_cfg(instance->serial, &(instance->serial->config));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _uart_putc(struct rt_serial_device *serial, char c)
|
|
|
|
{
|
|
|
|
UART_CFG_T *instance = working_cfg;
|
2020-04-19 17:37:43 +08:00
|
|
|
int rtn = 1;
|
2020-04-14 22:17:27 +08:00
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
|
|
|
if (serial->parent.user_data != RT_NULL)
|
|
|
|
{
|
|
|
|
instance = (UART_CFG_T*)serial->parent.user_data;
|
|
|
|
}
|
|
|
|
|
2020-04-19 17:37:43 +08:00
|
|
|
nrf_uart_event_clear(instance->uart.p_reg, NRF_UART_EVENT_TXDRDY);
|
|
|
|
nrf_uart_task_trigger(instance->uart.p_reg, NRF_UART_TASK_STARTTX);
|
|
|
|
nrf_uart_txd_set(instance->uart.p_reg, (uint8_t)c);
|
|
|
|
while (!nrf_uart_event_check(instance->uart.p_reg, NRF_UART_EVENT_TXDRDY))
|
2020-04-14 22:17:27 +08:00
|
|
|
{
|
2020-04-19 17:37:43 +08:00
|
|
|
//wait for TXD send
|
|
|
|
}
|
|
|
|
return rtn;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2020-04-19 17:37:43 +08:00
|
|
|
/*
|
|
|
|
@note: this function is invaild ,the cause of the problem is [nrfx_uart.c - line 340]
|
|
|
|
*/
|
2020-04-14 22:17:27 +08:00
|
|
|
static int _uart_getc(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
int ch = -1;
|
|
|
|
UART_CFG_T *instance = working_cfg;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
|
|
|
if (serial->parent.user_data != RT_NULL)
|
|
|
|
{
|
|
|
|
instance = (UART_CFG_T*)serial->parent.user_data;
|
|
|
|
}
|
2020-04-19 17:37:43 +08:00
|
|
|
|
|
|
|
ch = (int)(nrf_uart_rxd_get(instance->uart.p_reg));
|
|
|
|
|
2020-04-14 22:17:27 +08:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rt_uart_ops _uart_ops = {
|
|
|
|
_uart_cfg,
|
|
|
|
_uart_ctrl,
|
|
|
|
_uart_putc,
|
|
|
|
_uart_getc
|
|
|
|
};
|
|
|
|
|
|
|
|
void rt_hw_uart_init(void)
|
|
|
|
{
|
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
|
|
|
|
|
|
|
config.bufsz = RT_SERIAL_RB_BUFSZ;
|
|
|
|
_serial0_0.config = config;
|
|
|
|
_serial0_0.ops = &_uart_ops;
|
|
|
|
uart0.serial = &_serial0_0;
|
|
|
|
|
2020-04-19 17:37:43 +08:00
|
|
|
rt_hw_serial_register(&_serial0_0, "uart0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &uart0);
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
|