rt-thread/bsp/nrf5x/libraries/drivers/drv_uart.c

254 lines
6.1 KiB
C
Raw Normal View History

2020-04-28 03:23:09 +08:00
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-04-28 xckhmf Modify for <nrfx>
*
*/
2020-04-14 22:17:27 +08:00
#include <rtdevice.h>
#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;
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;
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 = {
.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)
{
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;
/* rx_byte equal p_data */
//m_uart0_cfg.rx_byte = *(p_event->data.rxtx.p_data);
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
}
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);
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;
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)
{
config.hal_cfg.parity = NRF_UART_PARITY_EXCLUDED;
2020-04-14 22:17:27 +08:00
}
else
{
config.hal_cfg.parity = NRF_UART_PARITY_INCLUDED;
2020-04-14 22:17:27 +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-28 03:23:09 +08:00
nrfx_uart_init(&(instance->uart), &config, instance->event_handler);
nrfx_uart_rx(&(instance->uart),&(instance->rx_byte),1);
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;
}
_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:
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;
}
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;
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
}
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);
uart_putc_hook((rt_uint8_t *)&c);
while (!nrf_uart_event_check(instance->uart.p_reg, NRF_UART_EVENT_TXDRDY))
2020-04-14 22:17:27 +08:00
{
//wait for TXD send
}
return rtn;
2020-04-14 22:17:27 +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;
}
if(instance->rx_length)
{
ch = instance->rx_byte;
instance->rx_length--;
2020-04-14 22:17:27 +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
};
void rt_hw_uart_init(void)
{
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
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;
rt_hw_serial_register(&_serial_0, "uart0", \
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &m_uart0_cfg);
#endif /* BSP_USING_UART0 */
2020-04-14 22:17:27 +08:00
}
2020-04-28 03:23:09 +08:00
#endif /* BSP_USING_UART */