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