18
18
#include <zephyr/logging/log.h>
19
19
#include <zephyr/logging/log_ctrl.h>
20
20
#include <zephyr/sys/util.h>
21
+ #if defined(CONFIG_NUS_LPUARTE )
22
+ #include <bm_lpuarte.h>
23
+ #endif
21
24
22
25
#include <board-config.h>
23
26
@@ -30,16 +33,32 @@ BLE_QWR_DEF(ble_qwr); /* BLE QWR instance */
30
33
/** Handle of the current connection. */
31
34
static uint16_t conn_handle = BLE_CONN_HANDLE_INVALID ;
32
35
33
- /** NUS UARTE instance */
34
- static const nrfx_uarte_t uarte_inst = NRF_UARTE_INST_GET (BOARD_APP_UARTE_INST );
36
+ /** NUS UARTE instance and board config */
37
+ #if defined(CONFIG_NUS_LPUARTE )
38
+ #define NUS_UARTE_INST BOARD_APP_LPUARTE_INST
39
+ #define NUS_UARTE_PIN_TX BOARD_APP_LPUARTE_PIN_TX
40
+ #define NUS_UARTE_PIN_RX BOARD_APP_LPUARTE_PIN_RX
41
+ #define NUS_UARTE_PIN_RDY BOARD_APP_LPUARTE_PIN_RDY
42
+ #define NUS_UARTE_PIN_REQ BOARD_APP_LPUARTE_PIN_REQ
43
+
44
+ struct bm_lpuarte lpu ;
45
+ #else
46
+ #define NUS_UARTE_INST BOARD_APP_UARTE_INST
47
+ #define NUS_UARTE_PIN_TX BOARD_APP_UARTE_PIN_TX
48
+ #define NUS_UARTE_PIN_RX BOARD_APP_UARTE_PIN_RX
49
+ #define NUS_UARTE_PIN_CTS BOARD_APP_UARTE_PIN_CTS
50
+ #define NUS_UARTE_PIN_RTS BOARD_APP_UARTE_PIN_RTS
51
+
52
+ static const nrfx_uarte_t nus_uarte_inst = NRFX_UARTE_INSTANCE (NUS_UARTE_INST );
53
+ #endif /* CONFIG_NUS_LPUARTE */
35
54
36
55
/* Maximum length of data (in bytes) that can be transmitted to the peer by the
37
56
* Nordic UART service module.
38
57
*/
39
58
static volatile uint16_t ble_nus_max_data_len = BLE_NUS_MAX_DATA_LEN_CALC (BLE_GATT_ATT_MTU_DEFAULT );
40
59
41
- /* Receive buffer used in UART ISR callback */
42
- static uint8_t uarte_rx_buf [4 ];
60
+ /* Receive buffers used in UART ISR callback. */
61
+ static uint8_t uarte_rx_buf [CONFIG_NUS_UART_RX_BUF_SIZE ][ 2 ];
43
62
static int buf_idx ;
44
63
45
64
/**
@@ -48,6 +67,26 @@ static int buf_idx;
48
67
* @param[in] data Data received.
49
68
* @param[in] data_len Size of data.
50
69
*/
70
+ #if defined(CONFIG_NUS_LPUARTE )
71
+ static void lpuarte_rx_handler (char * data , size_t data_len )
72
+ {
73
+ int err ;
74
+ uint16_t len = data_len ;
75
+
76
+ LOG_INF ("Sending data over BLE NUS, len %d" , len );
77
+
78
+ do {
79
+ err = ble_nus_data_send (& ble_nus , data , & len , conn_handle );
80
+ if ((err != 0 ) &&
81
+ (err != - EPIPE ) &&
82
+ (err != - EAGAIN ) &&
83
+ (err != - EBADF )) {
84
+ LOG_ERR ("Failed to send NUS data, err %d" , err );
85
+ return ;
86
+ }
87
+ } while (err == - EAGAIN );
88
+ }
89
+ #else
51
90
static void uarte_rx_handler (char * data , size_t data_len )
52
91
{
53
92
int err ;
@@ -96,6 +135,7 @@ static void uarte_rx_handler(char *data, size_t data_len)
96
135
}
97
136
}
98
137
}
138
+ #endif
99
139
100
140
/**
101
141
* @brief UARTE event handler
@@ -107,18 +147,29 @@ static void uarte_evt_handler(nrfx_uarte_event_t const *event, void *ctx)
107
147
{
108
148
switch (event -> type ) {
109
149
case NRFX_UARTE_EVT_RX_DONE :
110
- LOG_DBG ("Received data from UART: %c" , event -> data .rx .p_buffer [0 ]);
150
+ LOG_DBG ("Received data from UART: %.*s (%d)" ,
151
+ event -> data .rx .length , event -> data .rx .p_buffer , event -> data .rx .length );
111
152
if (event -> data .rx .length > 0 ) {
153
+ #if defined(CONFIG_NUS_LPUARTE )
154
+ lpuarte_rx_handler (event -> data .rx .p_buffer , event -> data .rx .length );
155
+ #else
112
156
uarte_rx_handler (event -> data .rx .p_buffer , event -> data .rx .length );
157
+ #endif
113
158
}
114
159
115
- nrfx_uarte_rx_enable (& uarte_inst , 0 );
160
+ #if !defined(CONFIG_NUS_LPUARTE )
161
+ nrfx_uarte_rx_enable (& nus_uarte_inst , 0 );
162
+ #endif
116
163
break ;
117
164
case NRFX_UARTE_EVT_RX_BUF_REQUEST :
118
- nrfx_uarte_rx_buffer_set (& uarte_inst , & uarte_rx_buf [buf_idx ], 1 );
165
+ #if defined(CONFIG_NUS_LPUARTE )
166
+ bm_lpuarte_rx_buffer_set (& lpu , uarte_rx_buf [buf_idx ], CONFIG_NUS_UART_RX_BUF_SIZE );
167
+ #else
168
+ nrfx_uarte_rx_buffer_set (& nus_uarte_inst , uarte_rx_buf [buf_idx ],
169
+ CONFIG_NUS_UART_RX_BUF_SIZE );
170
+ #endif
119
171
120
- buf_idx ++ ;
121
- buf_idx = (buf_idx < sizeof (uarte_rx_buf )) ? buf_idx : 0 ;
172
+ buf_idx = buf_idx ? 0 : 1 ;
122
173
break ;
123
174
case NRFX_UARTE_EVT_ERROR :
124
175
LOG_ERR ("uarte error %#x" , event -> data .error .error_mask );
@@ -264,20 +315,36 @@ uint16_t ble_qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_evt *qwr_
264
315
static void ble_nus_evt_handler (const struct ble_nus_evt * evt )
265
316
{
266
317
const char newline = '\n' ;
318
+ uint32_t err ;
267
319
268
320
if (evt -> type != BLE_NUS_EVT_RX_DATA ) {
269
321
return ;
270
322
}
271
323
272
324
/* Handle incoming data */
273
- LOG_DBG ("Received data from BLE NUS: %s" , evt -> params .rx_data .data );
325
+ LOG_DBG ("Received data from BLE NUS: %.*s (%d)" ,
326
+ evt -> params .rx_data .length , evt -> params .rx_data .data , evt -> params .rx_data .length );
274
327
275
- for (uint32_t i = 0 ; i < evt -> params .rx_data .length ; i ++ ) {
276
- nrfx_uarte_tx (& uarte_inst , & evt -> params .rx_data .data [i ], 1 , NRFX_UARTE_TX_BLOCKING );
328
+ #if defined(CONFIG_NUS_LPUARTE )
329
+ err = bm_lpuarte_tx (& lpu , evt -> params .rx_data .data , evt -> params .rx_data .length , 3000 );
330
+ if (err != NRFX_SUCCESS ) {
331
+ LOG_ERR ("bm_lpuarte_tx failed, nrfx_err %#x" , err );
277
332
}
333
+ #else
334
+ err = nrfx_uarte_tx (& nus_uarte_inst , evt -> params .rx_data .data ,
335
+ evt -> params .rx_data .length , NRFX_UARTE_TX_BLOCKING );
336
+ if (err != NRFX_SUCCESS ) {
337
+ LOG_ERR ("nrfx_uarte_tx failed, nrfx_err %#x" , err );
338
+ }
339
+ #endif
340
+
278
341
279
342
if (evt -> params .rx_data .data [evt -> params .rx_data .length - 1 ] == '\r' ) {
280
- nrfx_uarte_tx (& uarte_inst , & newline , 1 , NRFX_UARTE_TX_BLOCKING );
343
+ #if defined(CONFIG_NUS_LPUARTE )
344
+ bm_lpuarte_tx (& lpu , & newline , 1 , 3000 );
345
+ #else
346
+ nrfx_uarte_tx (& nus_uarte_inst , & newline , 1 , NRFX_UARTE_TX_BLOCKING );
347
+ #endif
281
348
}
282
349
}
283
350
@@ -287,33 +354,62 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
287
354
static int uarte_init (void )
288
355
{
289
356
int err ;
357
+ nrfx_uarte_config_t * uarte_cfg ;
358
+ #if defined(CONFIG_NUS_LPUARTE )
359
+ struct bm_lpuarte_config lpu_cfg = {
360
+ .uarte_inst = NRFX_UARTE_INSTANCE (NUS_UARTE_INST ),
361
+ .uarte_cfg = NRFX_UARTE_DEFAULT_CONFIG (NUS_UARTE_PIN_TX ,
362
+ NUS_UARTE_PIN_RX ),
363
+ .req_pin = BOARD_APP_LPUARTE_PIN_REQ ,
364
+ .rdy_pin = BOARD_APP_LPUARTE_PIN_RDY ,
365
+ };
290
366
291
- nrfx_uarte_config_t uarte_config = NRFX_UARTE_DEFAULT_CONFIG (BOARD_APP_UARTE_PIN_TX ,
292
- BOARD_APP_UARTE_PIN_RX );
367
+ uarte_cfg = & lpu_cfg .uarte_cfg ;
368
+ #else
369
+ nrfx_uarte_config_t uarte_config = NRFX_UARTE_DEFAULT_CONFIG (NUS_UARTE_PIN_TX ,
370
+ NUS_UARTE_PIN_RX );
293
371
294
- #if defined(CONFIG_BLE_UART_HWFC )
295
- uarte_config .config .hwfc = NRF_UARTE_HWFC_ENABLED ;
296
- uarte_config .cts_pin = BOARD_APP_UARTE_PIN_CTS ;
297
- uarte_config .rts_pin = BOARD_APP_UARTE_PIN_RTS ;
298
- #endif
372
+ uarte_cfg = & uarte_config ;
373
+
374
+ #if defined(CONFIG_NUS_UART_HWFC )
375
+ uarte_cfg -> config .hwfc = NRF_UARTE_HWFC_ENABLED ;
376
+ uarte_cfg -> cts_pin = NUS_UARTE_PIN_CTS ;
377
+ uarte_cfg -> rts_pin = NUS_UARTE_PIN_RTS ;
378
+ #endif /* CONFIG_NUS_UART_HWFC */
379
+ #endif /* CONFIG_NUS_LPUARTE */
299
380
300
- #if defined(CONFIG_BLE_UART_PARITY )
301
- uarte_config . parity = NRF_UARTE_PARITY_INCLUDED ;
381
+ #if defined(CONFIG_NUS_UART_PARITY )
382
+ uarte_cfg -> parity = NRF_UARTE_PARITY_INCLUDED ;
302
383
#endif
303
384
304
- uarte_config . interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO ;
385
+ uarte_cfg -> interrupt_priority = CONFIG_NUS_UART_IRQ_PRIO ;
305
386
306
387
/** We need to connect the IRQ ourselves. */
307
- IRQ_CONNECT (NRFX_IRQ_NUMBER_GET (NRF_UARTE_INST_GET (BOARD_APP_UARTE_INST )), CONFIG_BLE_UART_IRQ_PRIO ,
308
- NRFX_UARTE_INST_HANDLER_GET (BOARD_APP_UARTE_INST ), 0 , 0 );
309
388
310
- irq_enable (NRFX_IRQ_NUMBER_GET (NRF_UARTE_INST_GET (BOARD_APP_UARTE_INST )));
389
+ IRQ_CONNECT (NRFX_IRQ_NUMBER_GET (NRF_UARTE_INST_GET (NUS_UARTE_INST )),
390
+ CONFIG_NUS_UART_IRQ_PRIO , NRFX_UARTE_INST_HANDLER_GET (NUS_UARTE_INST ), 0 , 0 );
311
391
312
- err = nrfx_uarte_init (& uarte_inst , & uarte_config , uarte_evt_handler );
392
+ irq_enable (NRFX_IRQ_NUMBER_GET (NRF_UARTE_INST_GET (NUS_UARTE_INST )));
393
+
394
+ #if defined(CONFIG_NUS_LPUARTE )
395
+ IRQ_CONNECT (NRFX_IRQ_NUMBER_GET (NRF_GPIOTE_INST_GET (20 )) + NRF_GPIOTE_IRQ_GROUP ,
396
+ CONFIG_GPIOTE_IRQ_PRIO , NRFX_GPIOTE_INST_HANDLER_GET (20 ), 0 , 0 );
397
+
398
+ IRQ_CONNECT (NRFX_IRQ_NUMBER_GET (NRF_GPIOTE_INST_GET (30 )) + NRF_GPIOTE_IRQ_GROUP ,
399
+ CONFIG_GPIOTE_IRQ_PRIO , NRFX_GPIOTE_INST_HANDLER_GET (30 ), 0 , 0 );
400
+
401
+ err = bm_lpuarte_init (& lpu , & lpu_cfg , uarte_evt_handler );
402
+ if (err != NRFX_SUCCESS ) {
403
+ LOG_ERR ("Failed to initialize UART, nrfx err %d" , err );
404
+ return err ;
405
+ }
406
+ #else
407
+ err = nrfx_uarte_init (& nus_uarte_inst , & uarte_config , uarte_evt_handler );
313
408
if (err != NRFX_SUCCESS ) {
314
409
LOG_ERR ("Failed to initialize UART, nrfx err %d" , err );
315
410
return err ;
316
411
}
412
+ #endif /* CONFIG_NUS_LPUARTE */
317
413
318
414
return 0 ;
319
415
}
@@ -395,18 +491,25 @@ int main(void)
395
491
goto idle ;
396
492
}
397
493
494
+ #if defined(CONFIG_NUS_LPUARTE )
495
+ err = bm_lpuarte_rx_enable (& lpu );
496
+ if (err != NRFX_SUCCESS ) {
497
+ LOG_ERR ("UART RX failed, nrfx err %d" , err );
498
+ }
499
+ #else
398
500
const uint8_t out [] = "UART started.\r\n" ;
399
501
400
- err = nrfx_uarte_tx (& uarte_inst , out , sizeof (out ), NRFX_UARTE_TX_BLOCKING );
502
+ err = nrfx_uarte_tx (& nus_uarte_inst , out , sizeof (out ), NRFX_UARTE_TX_BLOCKING );
401
503
if (err != NRFX_SUCCESS ) {
402
504
LOG_ERR ("UARTE TX failed, nrfx err %d" , err );
403
505
goto idle ;
404
506
}
405
507
406
- err = nrfx_uarte_rx_enable (& uarte_inst , 0 );
508
+ err = nrfx_uarte_rx_enable (& nus_uarte_inst , 0 );
407
509
if (err != NRFX_SUCCESS ) {
408
510
LOG_ERR ("UART RX failed, nrfx err %d" , err );
409
511
}
512
+ #endif
410
513
411
514
err = ble_adv_start (& ble_adv , BLE_ADV_MODE_FAST );
412
515
if (err ) {
@@ -415,9 +518,6 @@ int main(void)
415
518
}
416
519
417
520
LOG_INF ("Advertising as %s" , CONFIG_BLE_ADV_NAME );
418
- #if defined(CONFIG_SOC_SERIES_NRF54LX )
419
- LOG_INF ("The NUS service is handled at a separate uart instance" );
420
- #endif
421
521
422
522
idle :
423
523
while (true) {
0 commit comments