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