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