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