1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-04-28 xckhmf Modify for <nrfx>
* 2021-06-26 supperthomas fix rt_hw_uart_init
*
*/
#include <rtdevice.h>
#include <nrfx_uart.h>
#include "drv_uart.h"
#ifdef BSP_USING_UART
typedef struct
{
struct rt_serial_device *serial;
nrfx_uart_t uart;
uint8_t rx_byte;
uint16_t rx_length;
uint32_t rx_pin;
uint32_t tx_pin;
nrfx_uart_event_handler_t event_handler;
} drv_uart_cfg_t;
#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),
.rx_byte = 0,
.rx_length = 0,
.rx_pin = BSP_UART0_RX_PIN,
.tx_pin = BSP_UART0_TX_PIN,
.event_handler = uart0_event_hander
};
#endif /* BSP_USING_UART0 */
#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)
{
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);
}
if (p_event->type == NRFX_UART_EVT_TX_DONE)
{
/* @TODO:[RT_DEVICE_FLAG_INT_TX]*/
}
}
#endif /* BSP_USING_UART0 */
static rt_err_t _uart_cfg(struct rt_serial_device *serial, struct serial_configure *cfg)
{
nrfx_uart_config_t config = NRFX_UART_DEFAULT_CONFIG(BSP_UART0_TX_PIN,BSP_UART0_RX_PIN);
drv_uart_cfg_t *instance = RT_NULL;
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
if (serial->parent.user_data == RT_NULL)
{
return -RT_ERROR;
}
instance = (drv_uart_cfg_t*)serial->parent.user_data;
nrfx_uart_uninit(&(instance->uart));
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;
}
else
{
config.hal_cfg.parity = NRF_UART_PARITY_INCLUDED;
}
config.hal_cfg.hwfc = NRF_UART_HWFC_DISABLED;
config.pselrxd = instance->rx_pin;
config.pseltxd = instance->tx_pin;
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);
return RT_EOK;
}
static rt_err_t _uart_ctrl(struct rt_serial_device *serial, int cmd, void *arg)
{
drv_uart_cfg_t *instance = NULL;
RT_ASSERT(serial != RT_NULL);
if (serial->parent.user_data == RT_NULL)
{
return -RT_ERROR;
}
instance = (drv_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;
}
_uart_cfg(instance->serial, &(instance->serial->config));
break;
case RT_DEVICE_CTRL_PIN:
_uart_cfg(instance->serial, &(instance->serial->config));
break;
case RT_DEVICE_POWERSAVE:
nrfx_uart_uninit(&(instance->uart));
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;
}
static int _uart_putc(struct rt_serial_device *serial, char c)
{
drv_uart_cfg_t *instance = NULL;
int rtn = 1;
RT_ASSERT(serial != RT_NULL);
if (serial->parent.user_data != RT_NULL)
{
instance = (drv_uart_cfg_t*)serial->parent.user_data;
}
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))
{
/* wait for TXD send */
}
return rtn;
}
rt_weak int uart_getc_hook(rt_uint8_t *ch)
{
return -1;
};
static int _uart_getc(struct rt_serial_device *serial)
{
int ch = -1;
drv_uart_cfg_t *instance = NULL;
RT_ASSERT(serial != RT_NULL);
if (serial->parent.user_data != RT_NULL)
{
instance = (drv_uart_cfg_t*)serial->parent.user_data;
}
if(instance->rx_length)
{
ch = instance->rx_byte;
instance->rx_length--;
}
if (-1 != ch)
{
return ch;
}
else
{
if (-1 == uart_getc_hook((rt_uint8_t *)&ch))
{
return -1;
}
else
{
return ch;
}
}
}
static struct rt_uart_ops _uart_ops = {
_uart_cfg,
_uart_ctrl,
_uart_putc,
_uart_getc
};
int rt_hw_uart_init(void)
{
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
rt_err_t result = RT_EOK;
#ifdef BSP_USING_UART0
_serial_0.config = config;
_serial_0.ops = &_uart_ops;
m_uart0_cfg.serial = &_serial_0;
result = rt_hw_serial_register(&_serial_0, "uart0", \
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &m_uart0_cfg);
#endif /* BSP_USING_UART0 */
return result;
}
#endif /* BSP_USING_UART */
|