2020-04-28 03:23:09 +08:00
|
|
|
/*
|
2021-03-14 15:18:33 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2020-04-28 03:23:09 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2020-04-28 xckhmf Modify for <nrfx>
|
2021-06-26 18:47:31 +08:00
|
|
|
* 2021-06-26 supperthomas fix rt_hw_uart_init
|
2020-04-28 03:23:09 +08:00
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
#ifdef BSP_USING_UART
|
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-28 03:23:09 +08:00
|
|
|
uint8_t rx_byte;
|
|
|
|
uint16_t rx_length;
|
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-28 03:23:09 +08:00
|
|
|
} drv_uart_cfg_t;
|
2020-04-14 22:17:27 +08:00
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
static struct rt_serial_device _serial_0;
|
|
|
|
static void uart0_event_hander(nrfx_uart_event_t const *p_event,void *p_context);
|
|
|
|
drv_uart_cfg_t m_uart0_cfg = {
|
2020-04-19 17:37:43 +08:00
|
|
|
.uart = NRFX_UART_INSTANCE(0),
|
2020-04-28 03:23:09 +08:00
|
|
|
.rx_byte = 0,
|
|
|
|
.rx_length = 0,
|
|
|
|
.rx_pin = BSP_UART0_RX_PIN,
|
|
|
|
.tx_pin = BSP_UART0_TX_PIN,
|
|
|
|
.event_handler = uart0_event_hander
|
2020-04-14 22:17:27 +08:00
|
|
|
};
|
2020-04-28 03:23:09 +08:00
|
|
|
#endif /* BSP_USING_UART0 */
|
2020-04-14 22:17:27 +08:00
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
#error <nrfx_uart> not support UART1. Use UART0 instead.
|
|
|
|
#endif /* BSP_USING_UART1 */
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
static void uart0_event_hander(nrfx_uart_event_t const *p_event,void *p_context)
|
2021-03-14 15:18:33 +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
|
|
|
{
|
2020-04-28 03:23:09 +08:00
|
|
|
if(p_event->data.rxtx.bytes == 1)
|
|
|
|
{
|
|
|
|
m_uart0_cfg.rx_length = p_event->data.rxtx.bytes;
|
2021-03-14 15:18:33 +08:00
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
/* rx_byte equal p_data */
|
2021-03-14 15:18:33 +08:00
|
|
|
//m_uart0_cfg.rx_byte = *(p_event->data.rxtx.p_data);
|
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
rt_hw_serial_isr(m_uart0_cfg.serial, RT_SERIAL_EVENT_RX_IND);
|
|
|
|
}
|
|
|
|
nrfx_uart_rx(&(m_uart0_cfg.uart),&m_uart0_cfg.rx_byte,1);
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
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-28 03:23:09 +08:00
|
|
|
/* @TODO:[RT_DEVICE_FLAG_INT_TX]*/
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 03:23:09 +08:00
|
|
|
#endif /* BSP_USING_UART0 */
|
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-28 03:23:09 +08:00
|
|
|
nrfx_uart_config_t config = NRFX_UART_DEFAULT_CONFIG(BSP_UART0_TX_PIN,BSP_UART0_RX_PIN);
|
|
|
|
drv_uart_cfg_t *instance = RT_NULL;
|
2020-04-14 22:17:27 +08:00
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
RT_ASSERT(cfg != RT_NULL);
|
2021-03-14 15:18:33 +08:00
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
if (serial->parent.user_data == RT_NULL)
|
2020-04-14 22:17:27 +08:00
|
|
|
{
|
2020-04-28 03:23:09 +08:00
|
|
|
return -RT_ERROR;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
2020-04-28 03:23:09 +08:00
|
|
|
instance = (drv_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;
|
2021-03-14 15:18:33 +08:00
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
nrfx_uart_init(&(instance->uart), &config, instance->event_handler);
|
|
|
|
nrfx_uart_rx(&(instance->uart),&(instance->rx_byte),1);
|
2020-04-19 17:37:43 +08:00
|
|
|
nrf_uart_int_disable(instance->uart.p_reg, NRF_UART_INT_MASK_TXDRDY);
|
2020-04-14 22:17:27 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _uart_ctrl(struct rt_serial_device *serial, int cmd, void *arg)
|
|
|
|
{
|
2020-04-28 03:23:09 +08:00
|
|
|
drv_uart_cfg_t *instance = NULL;
|
2020-04-14 22:17:27 +08:00
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
if (serial->parent.user_data == RT_NULL)
|
2020-04-14 22:17:27 +08:00
|
|
|
{
|
2020-04-28 03:23:09 +08:00
|
|
|
return -RT_ERROR;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
2020-04-28 03:23:09 +08:00
|
|
|
instance = (drv_uart_cfg_t*)serial->parent.user_data;
|
2020-04-14 22:17:27 +08:00
|
|
|
|
|
|
|
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:
|
2020-04-28 03:23:09 +08:00
|
|
|
_uart_cfg(instance->serial, &(instance->serial->config));
|
2020-04-14 22:17:27 +08:00
|
|
|
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;
|
|
|
|
}
|
2020-10-27 22:42:12 +08:00
|
|
|
RT_WEAK int uart_putc_hook(rt_uint8_t *ch)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2020-04-14 22:17:27 +08:00
|
|
|
static int _uart_putc(struct rt_serial_device *serial, char c)
|
|
|
|
{
|
2020-04-28 03:23:09 +08:00
|
|
|
drv_uart_cfg_t *instance = NULL;
|
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)
|
|
|
|
{
|
2020-04-28 03:23:09 +08:00
|
|
|
instance = (drv_uart_cfg_t*)serial->parent.user_data;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2020-10-27 22:42:12 +08:00
|
|
|
uart_putc_hook((rt_uint8_t *)&c);
|
2020-04-19 17:37:43 +08:00
|
|
|
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
|
2021-03-14 15:18:33 +08:00
|
|
|
}
|
2020-04-19 17:37:43 +08:00
|
|
|
return rtn;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2020-10-27 22:42:12 +08:00
|
|
|
RT_WEAK int uart_getc_hook(rt_uint8_t *ch)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2020-04-14 22:17:27 +08:00
|
|
|
static int _uart_getc(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
int ch = -1;
|
2020-04-28 03:23:09 +08:00
|
|
|
drv_uart_cfg_t *instance = NULL;
|
2020-04-14 22:17:27 +08:00
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
|
|
|
if (serial->parent.user_data != RT_NULL)
|
|
|
|
{
|
2020-04-28 03:23:09 +08:00
|
|
|
instance = (drv_uart_cfg_t*)serial->parent.user_data;
|
2021-03-14 15:18:33 +08:00
|
|
|
}
|
2020-04-28 03:23:09 +08:00
|
|
|
if(instance->rx_length)
|
|
|
|
{
|
|
|
|
ch = instance->rx_byte;
|
|
|
|
instance->rx_length--;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
2020-10-27 22:42:12 +08:00
|
|
|
|
|
|
|
if (-1 != ch)
|
|
|
|
{
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (-1 == uart_getc_hook((rt_uint8_t *)&ch))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct rt_uart_ops _uart_ops = {
|
|
|
|
_uart_cfg,
|
|
|
|
_uart_ctrl,
|
|
|
|
_uart_putc,
|
|
|
|
_uart_getc
|
|
|
|
};
|
|
|
|
|
2021-06-26 18:47:31 +08:00
|
|
|
int rt_hw_uart_init(void)
|
2020-04-14 22:17:27 +08:00
|
|
|
{
|
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
2021-09-19 00:10:31 +08:00
|
|
|
rt_err_t result = RT_EOK;
|
2020-04-14 22:17:27 +08:00
|
|
|
|
2020-04-28 03:23:09 +08:00
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
_serial_0.config = config;
|
|
|
|
_serial_0.ops = &_uart_ops;
|
|
|
|
m_uart0_cfg.serial = &_serial_0;
|
2021-09-19 00:10:31 +08:00
|
|
|
result = rt_hw_serial_register(&_serial_0, "uart0", \
|
2020-04-28 03:23:09 +08:00
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &m_uart0_cfg);
|
|
|
|
#endif /* BSP_USING_UART0 */
|
2021-09-19 00:10:31 +08:00
|
|
|
return result;
|
2020-04-14 22:17:27 +08:00
|
|
|
}
|
2020-04-28 03:23:09 +08:00
|
|
|
#endif /* BSP_USING_UART */
|