Skip to content

Commit 7047e7e

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 2f28886 commit 7047e7e

File tree

3 files changed

+121
-27
lines changed

3 files changed

+121
-27
lines changed

samples/bluetooth/ble_nus/Kconfig

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@ 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"
2229

2330
endmenu # "BLE NUS sample"
2431

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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_BM_TIMER=y

samples/bluetooth/ble_nus/src/main.c

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <bluetooth/services/ble_nus.h>
1616
#include <nrf_soc.h>
1717
#include <nrfx_uarte.h>
18+
#if CONFIG_BLE_LPUART
19+
#include <lpuarte.h>
20+
#endif
1821
#include <zephyr/logging/log.h>
1922
#include <zephyr/logging/log_ctrl.h>
2023
#include <zephyr/sys/util.h>
@@ -30,16 +33,34 @@ BLE_QWR_DEF(ble_qwr); /* BLE QWR instance */
3033
/** Handle of the current connection. */
3134
static uint16_t conn_handle = BLE_CONN_HANDLE_INVALID;
3235

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

3657
/* Maximum length of data (in bytes) that can be transmitted to the peer by the
3758
* Nordic UART service module.
3859
*/
3960
static volatile uint16_t ble_nus_max_data_len = BLE_NUS_MAX_DATA_LEN_CALC(BLE_GATT_ATT_MTU_DEFAULT);
4061

4162
/* 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];
4364
static int buf_idx;
4465

4566
/**
@@ -112,13 +133,18 @@ static void uarte_evt_handler(nrfx_uarte_event_t const *event, void *ctx)
112133
uarte_rx_handler(event->data.rx.p_buffer, event->data.rx.length);
113134
}
114135

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
116139
break;
117140
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
119146

120-
buf_idx++;
121-
buf_idx = (buf_idx < sizeof(uarte_rx_buf)) ? buf_idx : 0;
147+
buf_idx = buf_idx ? 0 : 1;
122148
break;
123149
case NRFX_UARTE_EVT_ERROR:
124150
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)
273299
LOG_DBG("Received data from BLE NUS: %s", evt->params.rx_data.data);
274300

275301
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
277312
}
278313

279314
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
281320
}
282321
}
283322

@@ -287,33 +326,62 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
287326
static int uarte_init(void)
288327
{
289328
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+
};
290338

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
293346

294347
#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;
298351
#endif
299352

300353
#if defined(CONFIG_BLE_UART_PARITY)
301-
uarte_config.parity = NRF_UARTE_PARITY_INCLUDED;
354+
uarte_cfg->parity = NRF_UARTE_PARITY_INCLUDED;
302355
#endif
303356

304-
uarte_config.interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
357+
uarte_cfg->interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
305358

306359
/** 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);
309360

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)));
311365

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);
313374
if (err != NRFX_SUCCESS) {
314375
LOG_ERR("Failed to initialize UART, nrfx err %d", err);
315376
return err;
316377
}
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 */
317385

318386
return 0;
319387
}
@@ -397,16 +465,30 @@ int main(void)
397465

398466
const uint8_t out[] = "UART started.\r\n";
399467

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);
401482
if (err != NRFX_SUCCESS) {
402483
LOG_ERR("UARTE TX failed, nrfx err %d", err);
403484
return -1;
404485
}
405486

406-
err = nrfx_uarte_rx_enable(&uarte_inst, 0);
487+
err = nrfx_uarte_rx_enable(&nus_uarte_inst, 0);
407488
if (err != NRFX_SUCCESS) {
408489
LOG_ERR("UART RX failed, nrfx err %d", err);
409490
}
491+
#endif
410492

411493
err = ble_adv_start(&ble_adv, BLE_ADV_MODE_FAST);
412494
if (err) {

0 commit comments

Comments
 (0)