Skip to content

Commit 907f8a7

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 72a1583 commit 907f8a7

File tree

6 files changed

+128
-50
lines changed

6 files changed

+128
-50
lines changed

drivers/lpuarte/Kconfig

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,15 @@
66
config BM_SW_LPUARTE
77
bool "Low Power UARTE using REQ/RDY lines"
88
help
9-
Low power UARTE implements UARTE API and extends standard UARTE
10-
communication with 2 pins protocol for receiver wake up and flow control.
9+
Low Power UARTE implements UARTE API and extends standard UARTE
10+
communication with a 2-pins protocol for receiver wake-up and flow control.
1111

1212
if BM_SW_LPUARTE
1313

14-
# Set number of event handlers used by the lpuart driver
14+
# Set number of event handlers used by the LPUART driver
1515
config NRFX_GPIOTE_NUM_OF_EVT_HANDLERS
1616
default 2
1717

18-
config BM_SW_LPUARTE_MAX_PACKET_SIZE
19-
int "Maximum RX packet size"
20-
default 128
21-
help
22-
If interrupt driven API is enabled then internal RX buffer of that
23-
size is created.
24-
2518
config BM_SW_LPUARTE_INT_DRV_TX_BUF_SIZE
2619
int "Tx buffer size"
2720
default 128
@@ -32,7 +25,7 @@ config BM_SW_LPUARTE_INT_DRV_TX_BUF_SIZE
3225
packet.
3326

3427
module = BM_SW_LPUARTE
35-
module-str = low power uart
28+
module-str = Low Power UARTE
3629
source "subsys/logging/Kconfig.template.log_config"
3730

3831
endif

drivers/lpuarte/lpuarte.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7-
87
#include <zephyr/logging/log.h>
98
#include <bm_lpuarte.h>
109
#include <nrfx_gpiote.h>
@@ -113,9 +112,8 @@ static nrfx_err_t req_pin_init(struct bm_lpuarte *lpu, nrfx_gpiote_pin_t pin)
113112

114113
lpu->req_pin = pin;
115114

116-
/* Make gpiote driver think that pin is input but then reconfigure to
117-
* output. Which will be at some point reconfigured back to input for
118-
* a moment.
115+
/* Set the request pin in idle state to indicate to the receiver that there is no pending
116+
* transmission.
119117
*/
120118
req_pin_idle(lpu);
121119

include/bm_lpuarte.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,14 @@ extern "C" {
2727
enum bm_lpuarte_rx_state {
2828
/* RX is disabled. */
2929
RX_OFF,
30-
3130
/* RX is in low power, idle state with pin detection armed. */
3231
RX_IDLE,
33-
3432
/* RX request is pending, receiver is in preparation. */
3533
RX_PREPARE,
36-
3734
/* RX is in active state, receiver is running. */
3835
RX_ACTIVE,
39-
4036
/* RX is transitioning to from active idle state. */
4137
RX_TO_IDLE,
42-
43-
/* RX is waiting for a new buffer. */
44-
RX_BLOCKED,
45-
4638
/* RX is transitioning to off state. */
4739
RX_TO_OFF,
4840
};

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: 105 additions & 22 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 <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,34 @@ 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_CTS BOARD_APP_LPUARTE_PIN_CTS
43+
#define NUS_UARTE_PIN_RTS BOARD_APP_LPUARTE_PIN_RTS
44+
#define NUS_UARTE_PIN_RDY BOARD_APP_LPUARTE_PIN_RDY
45+
#define NUS_UARTE_PIN_REQ BOARD_APP_LPUARTE_PIN_REQ
46+
47+
struct bm_lpuarte lpu;
48+
#else
49+
#define NUS_UARTE_INST BOARD_APP_UARTE_INST
50+
#define NUS_UARTE_PIN_TX BOARD_APP_UARTE_PIN_TX
51+
#define NUS_UARTE_PIN_RX BOARD_APP_UARTE_PIN_RX
52+
#define NUS_UARTE_PIN_CTS BOARD_APP_UARTE_PIN_CTS
53+
#define NUS_UARTE_PIN_RTS BOARD_APP_UARTE_PIN_RTS
54+
55+
static const nrfx_uarte_t nus_uarte_inst = NRFX_UARTE_INSTANCE(NUS_UARTE_INST);
56+
#endif /* CONFIG_BLE_LPUART */
3557

3658
/* Maximum length of data (in bytes) that can be transmitted to the peer by the
3759
* Nordic UART service module.
3860
*/
3961
static volatile uint16_t ble_nus_max_data_len = BLE_NUS_MAX_DATA_LEN_CALC(BLE_GATT_ATT_MTU_DEFAULT);
4062

4163
/* Receive buffer used in UART ISR callback */
42-
static uint8_t uarte_rx_buf[4];
64+
static uint8_t uarte_rx_buf[IS_ENABLED(CONFIG_BLE_LPUART) ? 128 : 2];
4365
static int buf_idx;
4466

4567
/**
@@ -112,13 +134,18 @@ static void uarte_evt_handler(nrfx_uarte_event_t const *event, void *ctx)
112134
uarte_rx_handler(event->data.rx.p_buffer, event->data.rx.length);
113135
}
114136

115-
nrfx_uarte_rx_enable(&uarte_inst, 0);
137+
#if !defined(CONFIG_BLE_LPUART)
138+
nrfx_uarte_rx_enable(&nus_uarte_inst, 0);
139+
#endif
116140
break;
117141
case NRFX_UARTE_EVT_RX_BUF_REQUEST:
118-
nrfx_uarte_rx_buffer_set(&uarte_inst, &uarte_rx_buf[buf_idx], 1);
142+
#if defined(CONFIG_BLE_LPUART)
143+
bm_lpuarte_rx_buffer_set(&lpu, &uarte_rx_buf[buf_idx * 64], 64);
144+
#else
145+
nrfx_uarte_rx_buffer_set(&nus_uarte_inst, &uarte_rx_buf[buf_idx], 1);
146+
#endif
119147

120-
buf_idx++;
121-
buf_idx = (buf_idx < sizeof(uarte_rx_buf)) ? buf_idx : 0;
148+
buf_idx = buf_idx ? 0 : 1;
122149
break;
123150
case NRFX_UARTE_EVT_ERROR:
124151
LOG_ERR("uarte error %#x", event->data.error.error_mask);
@@ -273,11 +300,24 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
273300
LOG_DBG("Received data from BLE NUS: %s", evt->params.rx_data.data);
274301

275302
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);
303+
#if CONFIG_BLE_LPUART
304+
bm_lpuarte_tx(&lpu, evt->params.rx_data.data, evt->params.rx_data.length, 0,
305+
BM_TIMER_MS_TO_TICKS(3000));
306+
while (bm_lpuarte_tx_in_progress(&lpu)) {
307+
k_busy_wait(1);
308+
}
309+
#else
310+
nrfx_uarte_tx(&nus_uarte_inst, evt->params.rx_data.data, evt->params.rx_data.length,
311+
NRFX_UARTE_TX_BLOCKING);
312+
#endif
277313
}
278314

279315
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);
316+
#if CONFIG_BLE_LPUART
317+
bm_lpuarte_tx(&lpu, &newline, 1, 0, BM_TIMER_MS_TO_TICKS(3000));
318+
#else
319+
nrfx_uarte_tx(&nus_uarte_inst, &newline, 1, NRFX_UARTE_TX_BLOCKING);
320+
#endif
281321
}
282322
}
283323

@@ -287,33 +327,62 @@ static void ble_nus_evt_handler(const struct ble_nus_evt *evt)
287327
static int uarte_init(void)
288328
{
289329
int err;
330+
nrfx_uarte_config_t *uarte_cfg;
331+
#if CONFIG_BLE_LPUART
332+
struct bm_lpuarte_config lpu_cfg = {
333+
.uarte_inst = NRFX_UARTE_INSTANCE(NUS_UARTE_INST),
334+
.uarte_cfg = NRFX_UARTE_DEFAULT_CONFIG(NUS_UARTE_PIN_TX,
335+
NUS_UARTE_PIN_RX),
336+
.req_pin = BOARD_APP_LPUARTE_PIN_REQ,
337+
.rdy_pin = BOARD_APP_LPUARTE_PIN_RDY,
338+
};
339+
340+
uarte_cfg = &lpu_cfg.uarte_cfg;
341+
#else
342+
nrfx_uarte_config_t uarte_config = NRFX_UARTE_DEFAULT_CONFIG(NUS_UARTE_PIN_TX,
343+
NUS_UARTE_PIN_RX);
290344

291-
nrfx_uarte_config_t uarte_config = NRFX_UARTE_DEFAULT_CONFIG(BOARD_APP_UARTE_PIN_TX,
292-
BOARD_APP_UARTE_PIN_RX);
345+
uarte_cfg = &uarte_config;
346+
#endif
293347

294348
#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;
349+
uarte_cfg->config.hwfc = NRF_UARTE_HWFC_ENABLED;
350+
uarte_cfg->cts_pin = NUS_UARTE_PIN_CTS;
351+
uarte_cfg->rts_pin = NUS_UARTE_PIN_RTS;
298352
#endif
299353

300354
#if defined(CONFIG_BLE_UART_PARITY)
301-
uarte_config.parity = NRF_UARTE_PARITY_INCLUDED;
355+
uarte_cfg->parity = NRF_UARTE_PARITY_INCLUDED;
302356
#endif
303357

304-
uarte_config.interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
358+
uarte_cfg->interrupt_priority = CONFIG_BLE_UART_IRQ_PRIO;
305359

306360
/** 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);
309361

310-
irq_enable(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(BOARD_APP_UARTE_INST)));
362+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(NUS_UARTE_INST)),
363+
CONFIG_BLE_UART_IRQ_PRIO, NRFX_UARTE_INST_HANDLER_GET(NUS_UARTE_INST), 0, 0);
364+
365+
irq_enable(NRFX_IRQ_NUMBER_GET(NRF_UARTE_INST_GET(NUS_UARTE_INST)));
366+
367+
#if CONFIG_BLE_LPUART
368+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE_INST_GET(20)) + NRF_GPIOTE_IRQ_GROUP,
369+
CONFIG_GPIOTE_IRQ_PRIO, NRFX_GPIOTE_INST_HANDLER_GET(20), 0, 0);
370+
371+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE_INST_GET(30)) + NRF_GPIOTE_IRQ_GROUP,
372+
CONFIG_GPIOTE_IRQ_PRIO, NRFX_GPIOTE_INST_HANDLER_GET(30), 0, 0);
311373

312-
err = nrfx_uarte_init(&uarte_inst, &uarte_config, uarte_evt_handler);
374+
err = bm_lpuarte_init(&lpu, &lpu_cfg, uarte_evt_handler);
313375
if (err != NRFX_SUCCESS) {
314376
LOG_ERR("Failed to initialize UART, nrfx err %d", err);
315377
return err;
316378
}
379+
#else
380+
err = nrfx_uarte_init(&nus_uarte_inst, &uarte_config, uarte_evt_handler);
381+
if (err != NRFX_SUCCESS) {
382+
LOG_ERR("Failed to initialize UART, nrfx err %d", err);
383+
return err;
384+
}
385+
#endif /* CONFIG_BLE_LPUART */
317386

318387
return 0;
319388
}
@@ -397,16 +466,30 @@ int main(void)
397466

398467
const uint8_t out[] = "UART started.\r\n";
399468

400-
err = nrfx_uarte_tx(&uarte_inst, out, sizeof(out), NRFX_UARTE_TX_BLOCKING);
469+
#if CONFIG_BLE_LPUART
470+
err = bm_lpuarte_tx(&lpu, out, sizeof(out), NRFX_UARTE_TX_BLOCKING,
471+
BM_TIMER_MS_TO_TICKS(3000));
401472
if (err != NRFX_SUCCESS) {
402473
LOG_ERR("UARTE TX failed, nrfx err %d", err);
403474
return -1;
404475
}
405476

406-
err = nrfx_uarte_rx_enable(&uarte_inst, 0);
477+
err = bm_lpuarte_rx_enable(&lpu, 0);
407478
if (err != NRFX_SUCCESS) {
408479
LOG_ERR("UART RX failed, nrfx err %d", err);
409480
}
481+
#else
482+
err = nrfx_uarte_tx(&nus_uarte_inst, out, sizeof(out), NRFX_UARTE_TX_BLOCKING);
483+
if (err != NRFX_SUCCESS) {
484+
LOG_ERR("UARTE TX failed, nrfx err %d", err);
485+
return -1;
486+
}
487+
488+
err = nrfx_uarte_rx_enable(&nus_uarte_inst, 0);
489+
if (err != NRFX_SUCCESS) {
490+
LOG_ERR("UART RX failed, nrfx err %d", err);
491+
}
492+
#endif
410493

411494
err = ble_adv_start(&ble_adv, BLE_ADV_MODE_FAST);
412495
if (err) {

0 commit comments

Comments
 (0)