Skip to content

Commit 773ffa1

Browse files
samples: bluetooth: ble_nus: add LPUARTE support
Add support for LPUARTE to NUS sample. Co-authored-by: Andreas Moltumyr <[email protected]> Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent 74c1a0a commit 773ffa1

File tree

6 files changed

+187
-41
lines changed

6 files changed

+187
-41
lines changed

samples/bluetooth/ble_nus/Kconfig

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,33 @@
66

77
menu "BLE NUS sample"
88

9-
config BLE_UART_IRQ_PRIO
10-
int "BLE UART IRQ priority"
9+
config NUS_UART_IRQ_PRIO
10+
int "NUS UART IRQ priority"
1111
default 3
1212

13-
config BLE_UART_PARITY
13+
config NUS_UART_PARITY
1414
bool "BLE UART use parity"
1515

16-
config BLE_UART_HWFC
17-
bool "BLE UART use HWFC"
16+
config NUS_LPUARTE
17+
bool "NUS Low Power UARTE"
1818

19-
if BLE_UART_HWFC
19+
if NUS_LPUARTE
2020

21-
endif #BLE_UART_HWFC
21+
config GPIOTE_IRQ_PRIO
22+
int "GPIOTE IRQ priority"
23+
default 3
24+
25+
endif # BLE_LPUART
26+
27+
config NUS_UART_HWFC
28+
bool "NUS UART use HWFC"
29+
depends on !NUS_LPUARTE
30+
default y
31+
32+
config NUS_UART_RX_BUF_SIZE
33+
int "NUS UART receive buffer size"
34+
default 128 if NUS_LPUARTE
35+
default 1
2236

2337
endmenu # "BLE NUS sample"
2438

samples/bluetooth/ble_nus/README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,24 @@ This sample can be found under :file:`samples/bluetooth/ble_nus/` in the |BMshor
6060

6161
.. include:: /includes/program_sample.txt
6262

63+
Building and running with LPUARTE
64+
*********************************
65+
66+
The :file:`overlay-lpuarte.conf` file configures the sample to use the :ref:`LPUARTE <driver_lpuarte>` driver for the NUS Service.
67+
This is useful for reducing the power consumption.
68+
The overlay must be added to the build configuration is VS Code or as an extra argument to west: ``-DEXTRA_CONF_FILE="overlay-lpuarte.conf"``.
69+
70+
To test the NUS sample with the :ref:`LPUARTE <driver_lpuarte>` driver in loopback mode, connect pin ``P1.08`` (REQ) with ``P1.09`` (RDY) and ``P1.10`` (RX) with ``P1.11`` (TX) on the nRF54L15DK, as given in the :file:`board-config.h`.
71+
It is also possible to test between two devices running NUS with LPUART by connecting the above mentioned pins and ``GND`` between the devices.
72+
Make sure the ``REQ`` pin on one board is connected to the ``RDY`` on the other board, and vice versa.
73+
6374
Testing
6475
=======
6576

6677
1. Compile and program the application.
6778
#. Connect the device to the computer to access UART 0 and UART 1.
6879
If you use a development kit, UART 0 and 1 are forwarded as COM ports (Windows) or ttyACM devices (Linux) after you connect the development kit over USB.
80+
One instance is used for logging (if enabled), the other for the NUS service.
6981
#. Connect to the kit with a terminal emulator (for example, the `Serial Terminal app`_) to both UARTs.
7082
#. Reset the kit.
7183
#. Observe that the device is advertising under the default name ``nRF_BM_NUS``.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file enables Low Power UARTE (LPUARTE) driver for the NUS UART instance.
2+
CONFIG_BM_SW_LPUARTE=y
3+
CONFIG_BM_TIMER=y
4+
5+
CONFIG_NUS_LPUARTE=y
6+
7+
# Set number of event handlers used by the LPUART driver
8+
CONFIG_NRFX_GPIOTE_NUM_OF_EVT_HANDLERS=2

samples/bluetooth/ble_nus/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ CONFIG_BLE_CONN_PARAMS=y
1414

1515
# Nordic UART service
1616
CONFIG_BLE_NUS=y
17-
CONFIG_BLE_UART_HWFC=y
1817

1918
# NUS depends on the Queued Writes module
2019
CONFIG_BLE_QWR=y

samples/bluetooth/ble_nus/sample.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@ tests:
44
sample.ble_nus:
55
build_only: true
66
integration_platforms:
7+
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot
8+
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice
9+
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice
10+
platform_allow:
711
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice
812
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice
913
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice
1014
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot
1115
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot
1216
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot
17+
tags: ci_build
18+
sample.ble_nus.lpuarte:
19+
build_only: true
20+
integration_platforms:
21+
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot
22+
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice
23+
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice
1324
platform_allow:
1425
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice
1526
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice
1627
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice
1728
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot
1829
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot
1930
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot
31+
extra_args: EXTRA_CONF_FILE="overlay-lpuarte.conf"
2032
tags: ci_build

samples/bluetooth/ble_nus/src/main.c

Lines changed: 134 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include <zephyr/logging/log_ctrl.h>
2020
#include <zephyr/sys/util.h>
2121

22+
#if defined(CONFIG_NUS_LPUARTE)
23+
#include <bm_lpuarte.h>
24+
#endif
25+
2226
#include <board-config.h>
2327

2428
LOG_MODULE_REGISTER(app, CONFIG_BLE_NUS_SAMPLE_LOG_LEVEL);
@@ -30,16 +34,32 @@ BLE_QWR_DEF(ble_qwr); /* BLE QWR instance */
3034
/** Handle of the current connection. */
3135
static uint16_t conn_handle = BLE_CONN_HANDLE_INVALID;
3236

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 */
3555

3656
/* Maximum length of data (in bytes) that can be transmitted to the peer by the
3757
* Nordic UART service module.
3858
*/
3959
static volatile uint16_t ble_nus_max_data_len = BLE_NUS_MAX_DATA_LEN_CALC(BLE_GATT_ATT_MTU_DEFAULT);
4060

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];
4363
static int buf_idx;
4464

4565
/**
@@ -48,6 +68,26 @@ static int buf_idx;
4868
* @param[in] data Data received.
4969
* @param[in] data_len Size of data.
5070
*/
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
5191
static void uarte_rx_handler(char *data, size_t data_len)
5292
{
5393
int err;
@@ -96,6 +136,7 @@ static void uarte_rx_handler(char *data, size_t data_len)
96136
}
97137
}
98138
}
139+
#endif
99140

100141
/**
101142
* @brief UARTE event handler
@@ -107,18 +148,29 @@ static void uarte_evt_handler(nrfx_uarte_event_t const *event, void *ctx)
107148
{
108149
switch (event->type) {
109150
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);
111153
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
112157
uarte_rx_handler(event->data.rx.p_buffer, event->data.rx.length);
158+
#endif
113159
}
114160

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
116164
break;
117165
case NRFX_UARTE_EVT_RX_BUF_REQUEST:
118-
nrfx_uarte_rx_buffer_set(&uarte_inst, &uarte_rx_buf[buf_idx], 1);
166+
#if defined(CONFIG_NUS_LPUARTE)
167+
bm_lpuarte_rx_buffer_set(&lpu, uarte_rx_buf[buf_idx], CONFIG_NUS_UART_RX_BUF_SIZE);
168+
#else
169+
nrfx_uarte_rx_buffer_set(&nus_uarte_inst, uarte_rx_buf[buf_idx],
170+
CONFIG_NUS_UART_RX_BUF_SIZE);
171+
#endif
119172

120-
buf_idx++;
121-
buf_idx = (buf_idx < sizeof(uarte_rx_buf)) ? buf_idx : 0;
173+
buf_idx = buf_idx ? 0 : 1;
122174
break;
123175
case NRFX_UARTE_EVT_ERROR:
124176
LOG_ERR("uarte error %#x", event->data.error.error_mask);
@@ -264,20 +316,36 @@ uint16_t ble_qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_evt *qwr_
264316
static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
265317
{
266318
const char newline = '\n';
319+
uint32_t err;
267320

268321
if (evt->type != BLE_NUS_EVT_RX_DATA) {
269322
return;
270323
}
271324

272325
/* Handle incoming data */
273-
LOG_DBG("Received data from BLE NUS: %s", evt->params.rx_data.data);
326+
LOG_DBG("Received data from BLE NUS: %.*s (%d)",
327+
evt->params.rx_data.length, evt->params.rx_data.data, evt->params.rx_data.length);
274328

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);
329+
#if defined(CONFIG_NUS_LPUARTE)
330+
err = bm_lpuarte_tx(&lpu, evt->params.rx_data.data, evt->params.rx_data.length, 3000);
331+
if (err != NRFX_SUCCESS) {
332+
LOG_ERR("bm_lpuarte_tx failed, nrfx_err %#x", err);
277333
}
334+
#else
335+
err = nrfx_uarte_tx(&nus_uarte_inst, evt->params.rx_data.data,
336+
evt->params.rx_data.length, NRFX_UARTE_TX_BLOCKING);
337+
if (err != NRFX_SUCCESS) {
338+
LOG_ERR("nrfx_uarte_tx failed, nrfx_err %#x", err);
339+
}
340+
#endif
341+
278342

279343
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);
344+
#if defined(CONFIG_NUS_LPUARTE)
345+
bm_lpuarte_tx(&lpu, &newline, 1, 3000);
346+
#else
347+
nrfx_uarte_tx(&nus_uarte_inst, &newline, 1, NRFX_UARTE_TX_BLOCKING);
348+
#endif
281349
}
282350
}
283351

@@ -287,33 +355,62 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
287355
static int uarte_init(void)
288356
{
289357
int err;
358+
nrfx_uarte_config_t *uarte_cfg;
359+
#if defined(CONFIG_NUS_LPUARTE)
360+
struct bm_lpuarte_config lpu_cfg = {
361+
.uarte_inst = NRFX_UARTE_INSTANCE(NUS_UARTE_INST),
362+
.uarte_cfg = NRFX_UARTE_DEFAULT_CONFIG(NUS_UARTE_PIN_TX,
363+
NUS_UARTE_PIN_RX),
364+
.req_pin = BOARD_APP_LPUARTE_PIN_REQ,
365+
.rdy_pin = BOARD_APP_LPUARTE_PIN_RDY,
366+
};
290367

291-
nrfx_uarte_config_t uarte_config = NRFX_UARTE_DEFAULT_CONFIG(BOARD_APP_UARTE_PIN_TX,
292-
BOARD_APP_UARTE_PIN_RX);
368+
uarte_cfg = &lpu_cfg.uarte_cfg;
369+
#else
370+
nrfx_uarte_config_t uarte_config = NRFX_UARTE_DEFAULT_CONFIG(NUS_UARTE_PIN_TX,
371+
NUS_UARTE_PIN_RX);
293372

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
373+
uarte_cfg = &uarte_config;
299374

300-
#if defined(CONFIG_BLE_UART_PARITY)
301-
uarte_config.parity = NRF_UARTE_PARITY_INCLUDED;
375+
#if defined(CONFIG_NUS_UART_HWFC)
376+
uarte_cfg->config.hwfc = NRF_UARTE_HWFC_ENABLED;
377+
uarte_cfg->cts_pin = NUS_UARTE_PIN_CTS;
378+
uarte_cfg->rts_pin = NUS_UARTE_PIN_RTS;
379+
#endif /* CONFIG_NUS_UART_HWFC */
380+
#endif /* CONFIG_NUS_LPUARTE */
381+
382+
#if defined(CONFIG_NUS_UART_PARITY)
383+
uarte_cfg->parity = NRF_UARTE_PARITY_INCLUDED;
302384
#endif
303385

304-
uarte_config.interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
386+
uarte_cfg->interrupt_priority = CONFIG_NUS_UART_IRQ_PRIO;
305387

306388
/** 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);
309389

310-
irq_enable(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(BOARD_APP_UARTE_INST)));
390+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(NUS_UARTE_INST)),
391+
CONFIG_NUS_UART_IRQ_PRIO, NRFX_UARTE_INST_HANDLER_GET(NUS_UARTE_INST), 0, 0);
392+
393+
irq_enable(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(NUS_UARTE_INST)));
311394

312-
err = nrfx_uarte_init(&uarte_inst, &uarte_config, uarte_evt_handler);
395+
#if defined(CONFIG_NUS_LPUARTE)
396+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE_INST_GET(20)) + NRF_GPIOTE_IRQ_GROUP,
397+
CONFIG_GPIOTE_IRQ_PRIO, NRFX_GPIOTE_INST_HANDLER_GET(20), 0, 0);
398+
399+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE_INST_GET(30)) + NRF_GPIOTE_IRQ_GROUP,
400+
CONFIG_GPIOTE_IRQ_PRIO, NRFX_GPIOTE_INST_HANDLER_GET(30), 0, 0);
401+
402+
err = bm_lpuarte_init(&lpu, &lpu_cfg, uarte_evt_handler);
313403
if (err != NRFX_SUCCESS) {
314404
LOG_ERR("Failed to initialize UART, nrfx err %d", err);
315405
return err;
316406
}
407+
#else
408+
err = nrfx_uarte_init(&nus_uarte_inst, &uarte_config, uarte_evt_handler);
409+
if (err != NRFX_SUCCESS) {
410+
LOG_ERR("Failed to initialize UART, nrfx err %d", err);
411+
return err;
412+
}
413+
#endif /* CONFIG_NUS_LPUARTE */
317414

318415
return 0;
319416
}
@@ -395,18 +492,25 @@ int main(void)
395492
goto idle;
396493
}
397494

495+
#if defined(CONFIG_NUS_LPUARTE)
496+
err = bm_lpuarte_rx_enable(&lpu);
497+
if (err != NRFX_SUCCESS) {
498+
LOG_ERR("UART RX failed, nrfx err %d", err);
499+
}
500+
#else
398501
const uint8_t out[] = "UART started.\r\n";
399502

400-
err = nrfx_uarte_tx(&uarte_inst, out, sizeof(out), NRFX_UARTE_TX_BLOCKING);
503+
err = nrfx_uarte_tx(&nus_uarte_inst, out, sizeof(out), NRFX_UARTE_TX_BLOCKING);
401504
if (err != NRFX_SUCCESS) {
402505
LOG_ERR("UARTE TX failed, nrfx err %d", err);
403-
goto idle;
506+
return -1;
404507
}
405508

406-
err = nrfx_uarte_rx_enable(&uarte_inst, 0);
509+
err = nrfx_uarte_rx_enable(&nus_uarte_inst, 0);
407510
if (err != NRFX_SUCCESS) {
408511
LOG_ERR("UART RX failed, nrfx err %d", err);
409512
}
513+
#endif
410514

411515
err = ble_adv_start(&ble_adv, BLE_ADV_MODE_FAST);
412516
if (err) {
@@ -415,9 +519,6 @@ int main(void)
415519
}
416520

417521
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
421522

422523
idle:
423524
while (true) {

0 commit comments

Comments
 (0)