Skip to content

Commit 9da0082

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 0972c94 commit 9da0082

File tree

3 files changed

+122
-28
lines changed

3 files changed

+122
-28
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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
6+
# Set number of event handlers used by the LPUART driver
7+
CONFIG_NRFX_GPIOTE_NUM_OF_EVT_HANDLERS=2

samples/bluetooth/ble_nus/src/main.c

Lines changed: 102 additions & 23 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
/**
@@ -112,13 +132,18 @@ static void uarte_evt_handler(nrfx_uarte_event_t const *event, void *ctx)
112132
uarte_rx_handler(event->data.rx.p_buffer, event->data.rx.length);
113133
}
114134

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
116138
break;
117139
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
119145

120-
buf_idx++;
121-
buf_idx = (buf_idx < sizeof(uarte_rx_buf)) ? buf_idx : 0;
146+
buf_idx = buf_idx ? 0 : 1;
122147
break;
123148
case NRFX_UARTE_EVT_ERROR:
124149
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)
273298
LOG_DBG("Received data from BLE NUS: %s", evt->params.rx_data.data);
274299

275300
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
277310
}
278311

279312
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
281318
}
282319
}
283320

@@ -287,33 +324,62 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
287324
static int uarte_init(void)
288325
{
289326
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);
290341

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;
293343

294344
#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 */
299350

300351
#if defined(CONFIG_BLE_UART_PARITY)
301-
uarte_config.parity = NRF_UARTE_PARITY_INCLUDED;
352+
uarte_cfg->parity = NRF_UARTE_PARITY_INCLUDED;
302353
#endif
303354

304-
uarte_config.interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
355+
uarte_cfg->interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
305356

306357
/** 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);
309358

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

312-
err = nrfx_uarte_init(&uarte_inst, &uarte_config, uarte_evt_handler);
371+
err = bm_lpuarte_init(&lpu, &lpu_cfg, uarte_evt_handler);
313372
if (err != NRFX_SUCCESS) {
314373
LOG_ERR("Failed to initialize UART, nrfx err %d", err);
315374
return err;
316375
}
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 */
317383

318384
return 0;
319385
}
@@ -397,16 +463,29 @@ int main(void)
397463

398464
const uint8_t out[] = "UART started.\r\n";
399465

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);
401468
if (err != NRFX_SUCCESS) {
402469
LOG_ERR("UARTE TX failed, nrfx err %d", err);
403470
return -1;
404471
}
405472

406-
err = nrfx_uarte_rx_enable(&uarte_inst, 0);
473+
err = bm_lpuarte_rx_enable(&lpu, 0);
407474
if (err != NRFX_SUCCESS) {
408475
LOG_ERR("UART RX failed, nrfx err %d", err);
409476
}
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
410489

411490
err = ble_adv_start(&ble_adv, BLE_ADV_MODE_FAST);
412491
if (err) {

0 commit comments

Comments
 (0)