Skip to content

Commit bea08e8

Browse files
samples: bluettoth: ble_nus: add LPUARTE support
Add support for LPUARTE to NUS sample. Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent cf01cee commit bea08e8

File tree

4 files changed

+166
-31
lines changed

4 files changed

+166
-31
lines changed

samples/bluetooth/ble_nus/Kconfig

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ config BLE_UART_IRQ_PRIO
1010
int "BLE UART IRQ priority"
1111
default 3
1212

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

16-
config BLE_UART_HWFC
17-
bool "BLE UART use HWFC"
16+
config BLE_LPUART
17+
bool "BLE UART Low Power"
1818

19-
if BLE_UART_HWFC
19+
if BLE_LPUART
20+
21+
config GPIOTE_IRQ_PRIO
22+
int "GPIOTE IRQ priority"
23+
default 3
2024

21-
endif #BLE_UART_HWFC
25+
endif #BLE_LPUART
26+
27+
config BLE_UART_HWFC
28+
bool "BLE UART use HWFC"
29+
depends on !BLE_LPUART
2230

2331
endmenu # "BLE NUS sample"
2432

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_BLE_LPUART=y
3+
CONFIG_CLOCK_CONTROL=y
4+
CONFIG_BM_SW_LPUARTE=y
5+
CONFIG_BLE_UART_HWFC=n
6+
CONFIG_BM_TIMER=y
7+
# Set number of event handlers used by the LPUART driver
8+
CONFIG_NRFX_GPIOTE_NUM_OF_EVT_HANDLERS=2

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="lpuarte.conf"
2032
tags: ci_build

samples/bluetooth/ble_nus/src/main.c

Lines changed: 133 additions & 26 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 CONFIG_BLE_LPUART
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 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 */
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

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

4565
/**
@@ -48,6 +68,24 @@ static int buf_idx;
4868
* @param[in] data Data received.
4969
* @param[in] data_len Size of data.
5070
*/
71+
#if defined(CONFIG_BLE_LPUART)
72+
static void lpuarte_rx_handler(char *data, size_t data_len)
73+
{
74+
int err;
75+
uint16_t len = data_len;
76+
77+
do {
78+
err = ble_nus_data_send(&ble_nus, data, &len, conn_handle);
79+
if ((err != 0) &&
80+
(err != -EPIPE) &&
81+
(err != -EAGAIN) &&
82+
(err != -EBADF)) {
83+
LOG_ERR("Failed to send NUS data, err %d", err);
84+
return;
85+
}
86+
} while (err == -EAGAIN);
87+
}
88+
#else
5189
static void uarte_rx_handler(char *data, size_t data_len)
5290
{
5391
int err;
@@ -96,6 +134,7 @@ static void uarte_rx_handler(char *data, size_t data_len)
96134
}
97135
}
98136
}
137+
#endif
99138

100139
/**
101140
* @brief UARTE event handler
@@ -107,18 +146,28 @@ static void uarte_evt_handler(nrfx_uarte_event_t const *event, void *ctx)
107146
{
108147
switch (event->type) {
109148
case NRFX_UARTE_EVT_RX_DONE:
110-
LOG_DBG("Received data from UART: %c", event->data.rx.p_buffer[0]);
149+
LOG_DBG("Received data from UART: %.*s (%d)",
150+
event->data.rx.length, event->data.rx.p_buffer, event->data.rx.length);
111151
if (event->data.rx.length > 0) {
152+
#if defined(CONFIG_BLE_LPUART)
153+
lpuarte_rx_handler(event->data.rx.p_buffer, event->data.rx.length);
154+
#else
112155
uarte_rx_handler(event->data.rx.p_buffer, event->data.rx.length);
156+
#endif
113157
}
114158

115-
nrfx_uarte_rx_enable(&uarte_inst, 0);
159+
#if !defined(CONFIG_BLE_LPUART)
160+
nrfx_uarte_rx_enable(&nus_uarte_inst, 0);
161+
#endif
116162
break;
117163
case NRFX_UARTE_EVT_RX_BUF_REQUEST:
118-
nrfx_uarte_rx_buffer_set(&uarte_inst, &uarte_rx_buf[buf_idx], 1);
164+
#if defined(CONFIG_BLE_LPUART)
165+
bm_lpuarte_rx_buffer_set(&lpu, &uarte_rx_buf[buf_idx * 64], 64);
166+
#else
167+
nrfx_uarte_rx_buffer_set(&nus_uarte_inst, &uarte_rx_buf[buf_idx], 1);
168+
#endif
119169

120-
buf_idx++;
121-
buf_idx = (buf_idx < sizeof(uarte_rx_buf)) ? buf_idx : 0;
170+
buf_idx = buf_idx ? 0 : 1;
122171
break;
123172
case NRFX_UARTE_EVT_ERROR:
124173
LOG_ERR("uarte error %#x", event->data.error.error_mask);
@@ -264,20 +313,36 @@ uint16_t ble_qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_evt *qwr_
264313
static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
265314
{
266315
const char newline = '\n';
316+
uint32_t err;
267317

268318
if (evt->type != BLE_NUS_EVT_RX_DATA) {
269319
return;
270320
}
271321

272322
/* Handle incoming data */
273-
LOG_DBG("Received data from BLE NUS: %s", evt->params.rx_data.data);
323+
LOG_DBG("Received data from BLE NUS: %.*s (%d)",
324+
evt->params.rx_data.length, evt->params.rx_data.data, evt->params.rx_data.length);
274325

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);
326+
#if CONFIG_BLE_LPUART
327+
err = bm_lpuarte_tx(&lpu, evt->params.rx_data.data, evt->params.rx_data.length, 3000);
328+
if (err != NRFX_SUCCESS) {
329+
LOG_ERR("bm_lpuarte_tx failed, nrfx_err %#x", err);
277330
}
331+
#else
332+
err = nrfx_uarte_tx(&nus_uarte_inst, evt->params.rx_data.data,
333+
evt->params.rx_data.length, NRFX_UARTE_TX_BLOCKING);
334+
if (err != NRFX_SUCCESS) {
335+
LOG_ERR("nrfx_uarte_tx failed, nrfx_err %#x", err);
336+
}
337+
#endif
338+
278339

279340
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);
341+
#if CONFIG_BLE_LPUART
342+
bm_lpuarte_tx(&lpu, &newline, 1, 3000);
343+
#else
344+
nrfx_uarte_tx(&nus_uarte_inst, &newline, 1, NRFX_UARTE_TX_BLOCKING);
345+
#endif
281346
}
282347
}
283348

@@ -287,33 +352,62 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
287352
static int uarte_init(void)
288353
{
289354
int err;
355+
nrfx_uarte_config_t *uarte_cfg;
356+
#if CONFIG_BLE_LPUART
357+
struct bm_lpuarte_config lpu_cfg = {
358+
.uarte_inst = NRFX_UARTE_INSTANCE(NUS_UARTE_INST),
359+
.uarte_cfg = NRFX_UARTE_DEFAULT_CONFIG(NUS_UARTE_PIN_TX,
360+
NUS_UARTE_PIN_RX),
361+
.req_pin = BOARD_APP_LPUARTE_PIN_REQ,
362+
.rdy_pin = BOARD_APP_LPUARTE_PIN_RDY,
363+
};
290364

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

294372
#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->config.hwfc = NRF_UARTE_HWFC_ENABLED;
374+
uarte_cfg->cts_pin = NUS_UARTE_PIN_CTS;
375+
uarte_cfg->rts_pin = NUS_UARTE_PIN_RTS;
376+
#endif /* CONFIG_BLE_UART_HWFC */
377+
#endif /* CONFIG_BLE_LPUART */
299378

300379
#if defined(CONFIG_BLE_UART_PARITY)
301-
uarte_config.parity = NRF_UARTE_PARITY_INCLUDED;
380+
uarte_cfg->parity = NRF_UARTE_PARITY_INCLUDED;
302381
#endif
303382

304-
uarte_config.interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
383+
uarte_cfg->interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
305384

306385
/** 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);
309386

310-
irq_enable(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(BOARD_APP_UARTE_INST)));
387+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(NUS_UARTE_INST)),
388+
CONFIG_BLE_UART_IRQ_PRIO, NRFX_UARTE_INST_HANDLER_GET(NUS_UARTE_INST), 0, 0);
389+
390+
irq_enable(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(NUS_UARTE_INST)));
391+
392+
#if CONFIG_BLE_LPUART
393+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE_INST_GET(20)) + NRF_GPIOTE_IRQ_GROUP,
394+
CONFIG_GPIOTE_IRQ_PRIO, NRFX_GPIOTE_INST_HANDLER_GET(20), 0, 0);
395+
396+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE_INST_GET(30)) + NRF_GPIOTE_IRQ_GROUP,
397+
CONFIG_GPIOTE_IRQ_PRIO, NRFX_GPIOTE_INST_HANDLER_GET(30), 0, 0);
311398

312-
err = nrfx_uarte_init(&uarte_inst, &uarte_config, uarte_evt_handler);
399+
err = bm_lpuarte_init(&lpu, &lpu_cfg, uarte_evt_handler);
400+
if (err != NRFX_SUCCESS) {
401+
LOG_ERR("Failed to initialize UART, nrfx err %d", err);
402+
return err;
403+
}
404+
#else
405+
err = nrfx_uarte_init(&nus_uarte_inst, &uarte_config, uarte_evt_handler);
313406
if (err != NRFX_SUCCESS) {
314407
LOG_ERR("Failed to initialize UART, nrfx err %d", err);
315408
return err;
316409
}
410+
#endif /* CONFIG_BLE_LPUART */
317411

318412
return 0;
319413
}
@@ -397,16 +491,29 @@ int main(void)
397491

398492
const uint8_t out[] = "UART started.\r\n";
399493

400-
err = nrfx_uarte_tx(&uarte_inst, out, sizeof(out), NRFX_UARTE_TX_BLOCKING);
494+
#if CONFIG_BLE_LPUART
495+
err = bm_lpuarte_tx(&lpu, out, sizeof(out), 3000);
401496
if (err != NRFX_SUCCESS) {
402497
LOG_ERR("UARTE TX failed, nrfx err %d", err);
403498
goto idle;
404499
}
405500

406-
err = nrfx_uarte_rx_enable(&uarte_inst, 0);
501+
err = bm_lpuarte_rx_enable(&lpu);
407502
if (err != NRFX_SUCCESS) {
408503
LOG_ERR("UART RX failed, nrfx err %d", err);
409504
}
505+
#else
506+
err = nrfx_uarte_tx(&nus_uarte_inst, out, sizeof(out), NRFX_UARTE_TX_BLOCKING);
507+
if (err != NRFX_SUCCESS) {
508+
LOG_ERR("UARTE TX failed, nrfx err %d", err);
509+
return -1;
510+
}
511+
512+
err = nrfx_uarte_rx_enable(&nus_uarte_inst, 0);
513+
if (err != NRFX_SUCCESS) {
514+
LOG_ERR("UART RX failed, nrfx err %d", err);
515+
}
516+
#endif
410517

411518
err = ble_adv_start(&ble_adv, BLE_ADV_MODE_FAST);
412519
if (err) {

0 commit comments

Comments
 (0)