19
19
#include <zephyr/logging/log_ctrl.h>
20
20
#include <zephyr/sys/util.h>
21
21
22
+ #if CONFIG_BLE_LPUART
23
+ #include <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,34 @@ 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 CONFIG_BLE_LPUART
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_CTS BOARD_APP_LPUARTE_PIN_CTS
43
+ #define NUS_UARTE_PIN_RTS BOARD_APP_LPUARTE_PIN_RTS
44
+ #define NUS_UARTE_PIN_RDY BOARD_APP_LPUARTE_PIN_RDY
45
+ #define NUS_UARTE_PIN_REQ BOARD_APP_LPUARTE_PIN_REQ
46
+
47
+ struct bm_lpuarte lpu ;
48
+ #else
49
+ #define NUS_UARTE_INST BOARD_APP_UARTE_INST
50
+ #define NUS_UARTE_PIN_TX BOARD_APP_UARTE_PIN_TX
51
+ #define NUS_UARTE_PIN_RX BOARD_APP_UARTE_PIN_RX
52
+ #define NUS_UARTE_PIN_CTS BOARD_APP_UARTE_PIN_CTS
53
+ #define NUS_UARTE_PIN_RTS BOARD_APP_UARTE_PIN_RTS
54
+
55
+ static const nrfx_uarte_t nus_uarte_inst = NRFX_UARTE_INSTANCE (NUS_UARTE_INST );
56
+ #endif /* CONFIG_BLE_LPUART */
35
57
36
58
/* Maximum length of data (in bytes) that can be transmitted to the peer by the
37
59
* Nordic UART service module.
38
60
*/
39
61
static volatile uint16_t ble_nus_max_data_len = BLE_NUS_MAX_DATA_LEN_CALC (BLE_GATT_ATT_MTU_DEFAULT );
40
62
41
63
/* Receive buffer used in UART ISR callback */
42
- static uint8_t uarte_rx_buf [4 ];
64
+ static uint8_t uarte_rx_buf [IS_ENABLED ( CONFIG_BLE_LPUART ) ? 128 : 2 ];
43
65
static int buf_idx ;
44
66
45
67
/**
@@ -112,13 +134,18 @@ static void uarte_evt_handler(nrfx_uarte_event_t const *event, void *ctx)
112
134
uarte_rx_handler (event -> data .rx .p_buffer , event -> data .rx .length );
113
135
}
114
136
115
- nrfx_uarte_rx_enable (& uarte_inst , 0 );
137
+ #if !defined(CONFIG_BLE_LPUART )
138
+ nrfx_uarte_rx_enable (& nus_uarte_inst , 0 );
139
+ #endif
116
140
break ;
117
141
case NRFX_UARTE_EVT_RX_BUF_REQUEST :
118
- nrfx_uarte_rx_buffer_set (& uarte_inst , & uarte_rx_buf [buf_idx ], 1 );
142
+ #if defined(CONFIG_BLE_LPUART )
143
+ bm_lpuarte_rx_buffer_set (& lpu , & uarte_rx_buf [buf_idx * 64 ], 64 );
144
+ #else
145
+ nrfx_uarte_rx_buffer_set (& nus_uarte_inst , & uarte_rx_buf [buf_idx ], 1 );
146
+ #endif
119
147
120
- buf_idx ++ ;
121
- buf_idx = (buf_idx < sizeof (uarte_rx_buf )) ? buf_idx : 0 ;
148
+ buf_idx = buf_idx ? 0 : 1 ;
122
149
break ;
123
150
case NRFX_UARTE_EVT_ERROR :
124
151
LOG_ERR ("uarte error %#x" , event -> data .error .error_mask );
@@ -273,11 +300,24 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
273
300
LOG_DBG ("Received data from BLE NUS: %s" , evt -> params .rx_data .data );
274
301
275
302
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 );
303
+ #if CONFIG_BLE_LPUART
304
+ bm_lpuarte_tx (& lpu , evt -> params .rx_data .data , evt -> params .rx_data .length , 0 ,
305
+ BM_TIMER_MS_TO_TICKS (3000 ));
306
+ while (bm_lpuarte_tx_in_progress (& lpu )) {
307
+ k_busy_wait (1 );
308
+ }
309
+ #else
310
+ nrfx_uarte_tx (& nus_uarte_inst , evt -> params .rx_data .data , evt -> params .rx_data .length ,
311
+ NRFX_UARTE_TX_BLOCKING );
312
+ #endif
277
313
}
278
314
279
315
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 );
316
+ #if CONFIG_BLE_LPUART
317
+ bm_lpuarte_tx (& lpu , & newline , 1 , 0 , BM_TIMER_MS_TO_TICKS (3000 ));
318
+ #else
319
+ nrfx_uarte_tx (& nus_uarte_inst , & newline , 1 , NRFX_UARTE_TX_BLOCKING );
320
+ #endif
281
321
}
282
322
}
283
323
@@ -287,33 +327,62 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
287
327
static int uarte_init (void )
288
328
{
289
329
int err ;
330
+ nrfx_uarte_config_t * uarte_cfg ;
331
+ #if CONFIG_BLE_LPUART
332
+ struct bm_lpuarte_config lpu_cfg = {
333
+ .uarte_inst = NRFX_UARTE_INSTANCE (NUS_UARTE_INST ),
334
+ .uarte_cfg = NRFX_UARTE_DEFAULT_CONFIG (NUS_UARTE_PIN_TX ,
335
+ NUS_UARTE_PIN_RX ),
336
+ .req_pin = BOARD_APP_LPUARTE_PIN_REQ ,
337
+ .rdy_pin = BOARD_APP_LPUARTE_PIN_RDY ,
338
+ };
339
+
340
+ uarte_cfg = & lpu_cfg .uarte_cfg ;
341
+ #else
342
+ nrfx_uarte_config_t uarte_config = NRFX_UARTE_DEFAULT_CONFIG (NUS_UARTE_PIN_TX ,
343
+ NUS_UARTE_PIN_RX );
290
344
291
- nrfx_uarte_config_t uarte_config = NRFX_UARTE_DEFAULT_CONFIG ( BOARD_APP_UARTE_PIN_TX ,
292
- BOARD_APP_UARTE_PIN_RX );
345
+ uarte_cfg = & uarte_config ;
346
+ #endif
293
347
294
348
#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 ;
349
+ uarte_cfg -> config .hwfc = NRF_UARTE_HWFC_ENABLED ;
350
+ uarte_cfg -> cts_pin = NUS_UARTE_PIN_CTS ;
351
+ uarte_cfg -> rts_pin = NUS_UARTE_PIN_RTS ;
298
352
#endif
299
353
300
354
#if defined(CONFIG_BLE_UART_PARITY )
301
- uarte_config . parity = NRF_UARTE_PARITY_INCLUDED ;
355
+ uarte_cfg -> parity = NRF_UARTE_PARITY_INCLUDED ;
302
356
#endif
303
357
304
- uarte_config . interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO ;
358
+ uarte_cfg -> interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO ;
305
359
306
360
/** 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
361
310
- irq_enable (NRFX_IRQ_NUMBER_GET (NRF_UARTE_INST_GET (BOARD_APP_UARTE_INST )));
362
+ IRQ_CONNECT (NRFX_IRQ_NUMBER_GET (NRF_UARTE_INST_GET (NUS_UARTE_INST )),
363
+ CONFIG_BLE_UART_IRQ_PRIO , NRFX_UARTE_INST_HANDLER_GET (NUS_UARTE_INST ), 0 , 0 );
364
+
365
+ irq_enable (NRFX_IRQ_NUMBER_GET (NRF_UARTE_INST_GET (NUS_UARTE_INST )));
366
+
367
+ #if CONFIG_BLE_LPUART
368
+ IRQ_CONNECT (NRFX_IRQ_NUMBER_GET (NRF_GPIOTE_INST_GET (20 )) + NRF_GPIOTE_IRQ_GROUP ,
369
+ CONFIG_GPIOTE_IRQ_PRIO , NRFX_GPIOTE_INST_HANDLER_GET (20 ), 0 , 0 );
370
+
371
+ IRQ_CONNECT (NRFX_IRQ_NUMBER_GET (NRF_GPIOTE_INST_GET (30 )) + NRF_GPIOTE_IRQ_GROUP ,
372
+ CONFIG_GPIOTE_IRQ_PRIO , NRFX_GPIOTE_INST_HANDLER_GET (30 ), 0 , 0 );
311
373
312
- err = nrfx_uarte_init ( & uarte_inst , & uarte_config , uarte_evt_handler );
374
+ err = bm_lpuarte_init ( & lpu , & lpu_cfg , uarte_evt_handler );
313
375
if (err != NRFX_SUCCESS ) {
314
376
LOG_ERR ("Failed to initialize UART, nrfx err %d" , err );
315
377
return err ;
316
378
}
379
+ #else
380
+ err = nrfx_uarte_init (& nus_uarte_inst , & uarte_config , uarte_evt_handler );
381
+ if (err != NRFX_SUCCESS ) {
382
+ LOG_ERR ("Failed to initialize UART, nrfx err %d" , err );
383
+ return err ;
384
+ }
385
+ #endif /* CONFIG_BLE_LPUART */
317
386
318
387
return 0 ;
319
388
}
@@ -397,16 +466,30 @@ int main(void)
397
466
398
467
const uint8_t out [] = "UART started.\r\n" ;
399
468
400
- err = nrfx_uarte_tx (& uarte_inst , out , sizeof (out ), NRFX_UARTE_TX_BLOCKING );
469
+ #if CONFIG_BLE_LPUART
470
+ err = bm_lpuarte_tx (& lpu , out , sizeof (out ), NRFX_UARTE_TX_BLOCKING ,
471
+ BM_TIMER_MS_TO_TICKS (3000 ));
401
472
if (err != NRFX_SUCCESS ) {
402
473
LOG_ERR ("UARTE TX failed, nrfx err %d" , err );
403
474
return -1 ;
404
475
}
405
476
406
- err = nrfx_uarte_rx_enable ( & uarte_inst , 0 );
477
+ err = bm_lpuarte_rx_enable ( & lpu , 0 );
407
478
if (err != NRFX_SUCCESS ) {
408
479
LOG_ERR ("UART RX failed, nrfx err %d" , err );
409
480
}
481
+ #else
482
+ err = nrfx_uarte_tx (& nus_uarte_inst , out , sizeof (out ), NRFX_UARTE_TX_BLOCKING );
483
+ if (err != NRFX_SUCCESS ) {
484
+ LOG_ERR ("UARTE TX failed, nrfx err %d" , err );
485
+ return -1 ;
486
+ }
487
+
488
+ err = nrfx_uarte_rx_enable (& nus_uarte_inst , 0 );
489
+ if (err != NRFX_SUCCESS ) {
490
+ LOG_ERR ("UART RX failed, nrfx err %d" , err );
491
+ }
492
+ #endif
410
493
411
494
err = ble_adv_start (& ble_adv , BLE_ADV_MODE_FAST );
412
495
if (err ) {
0 commit comments