Skip to content

Commit a5516fa

Browse files
committed
nrf/bluetooth: Add support for nimble based bluetooth.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 2e1cd87 commit a5516fa

File tree

22 files changed

+787
-42
lines changed

22 files changed

+787
-42
lines changed

extmod/nimble/logcfg/logcfg.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "modlog/modlog.h"
1010
#include "log_common/log_common.h"
1111

12-
#define MICROPY_PY_BLUETOOTH_DIAGNOSTIC_LOGGING (1)
12+
#ifndef MICROPY_PY_BLUETOOTH_DIAGNOSTIC_LOGGING
13+
#define MICROPY_PY_BLUETOOTH_DIAGNOSTIC_LOGGING (0)
14+
#endif
1315

1416
#if MICROPY_PY_BLUETOOTH_DIAGNOSTIC_LOGGING
1517
#define DFLT_LOG_DEBUG(...) MODLOG_DEBUG(4, __VA_ARGS__)

extmod/nimble/modbluetooth_nimble.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,24 +519,30 @@ static int central_gap_event_cb(struct ble_gap_event *event, void *arg) {
519519

520520
// On ports such as ESP32 where we only implement the bindings, then
521521
// the port must provide these functions.
522-
// But for STM32 / Unix-H4, we provide a default implementation of the
522+
// For STM32 / Unix-H4, we provide a default implementation of the
523523
// port-specific functionality.
524524
// TODO: In the future if a port ever needs to customise these functions
525525
// then investigate using MP_WEAK or splitting them out to another .c file.
526526

527+
#if !MICROPY_BLUETOOTH_NIMBLE_CONTROLLER
527528
#include "transport/uart/ble_hci_uart.h"
529+
#endif
528530

529531
void mp_bluetooth_nimble_port_hci_init(void) {
530532
DEBUG_printf("mp_bluetooth_nimble_port_hci_init (nimble default)\n");
533+
#if MYNEWT_VAL_BLE_HCI_TRANSPORT_UART
531534
// This calls mp_bluetooth_hci_uart_init (via ble_hci_uart_init --> hal_uart_config --> mp_bluetooth_hci_uart_init).
532535
ble_hci_uart_init();
536+
#endif
533537
mp_bluetooth_hci_controller_init();
534538
}
535539

536540
void mp_bluetooth_nimble_port_hci_deinit(void) {
537541
DEBUG_printf("mp_bluetooth_nimble_port_hci_deinit (nimble default)\n");
538542
mp_bluetooth_hci_controller_deinit();
543+
#if MYNEWT_VAL_BLE_HCI_TRANSPORT_UART
539544
mp_bluetooth_hci_uart_deinit();
545+
#endif
540546
}
541547

542548
void mp_bluetooth_nimble_port_start(void) {
@@ -600,17 +606,17 @@ int mp_bluetooth_init(void) {
600606
MP_STATE_PORT(bluetooth_nimble_memory) = NULL;
601607
#endif
602608

603-
// Allow port (ESP32) to override NimBLE's HCI init.
604-
// Otherwise default implementation above calls ble_hci_uart_init().
605-
mp_bluetooth_nimble_port_hci_init();
606-
607609
// Static initialization is complete, can start processing events.
608610
mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC;
609611

610612
// Initialise NimBLE memory and data structures.
611613
DEBUG_printf("mp_bluetooth_init: nimble_port_init\n");
612614
nimble_port_init();
613615

616+
// Allow port (ESP32) to override NimBLE's HCI init.
617+
// Otherwise default implementation above calls ble_hci_uart_init().
618+
mp_bluetooth_nimble_port_hci_init();
619+
614620
ble_hs_cfg.reset_cb = reset_cb;
615621
ble_hs_cfg.sync_cb = sync_cb;
616622
ble_hs_cfg.gatts_register_cb = gatts_register_cb;

extmod/nimble/nimble.mk

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ SRC_THIRDPARTY_C += $(addprefix $(NIMBLE_LIB_DIR)/, \
8686
ble_uuid.c \
8787
) \
8888
nimble/host/util/src/addr.c \
89-
nimble/transport/uart/src/ble_hci_uart.c \
9089
$(addprefix porting/nimble/src/, \
9190
endian.c \
9291
mem.c \
@@ -100,7 +99,6 @@ SRC_THIRDPARTY_C += $(addprefix $(NIMBLE_LIB_DIR)/, \
10099
101100
SRC_THIRDPARTY_C += $(addprefix $(NIMBLE_EXTMOD_DIR)/, \
102101
nimble/nimble_npl_os.c \
103-
hal/hal_uart.c \
104102
)
105103

106104
INC += -I$(TOP)/$(NIMBLE_EXTMOD_DIR)
@@ -112,11 +110,60 @@ INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/host/services/gatt/include
112110
INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/host/store/ram/include
113111
INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/host/util/include
114112
INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/include
115-
INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/transport/uart/include
116113
INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/porting/nimble/include
117114

118115
$(BUILD)/$(NIMBLE_LIB_DIR)/%.o: CFLAGS += -Wno-maybe-uninitialized -Wno-pointer-arith -Wno-unused-but-set-variable -Wno-format -Wno-sign-compare -Wno-old-style-declaration
119116

120117
endif
121118

119+
ifeq ($(MICROPY_BLUETOOTH_NIMBLE_CONTROLLER),1)
120+
121+
# Include controller layer to run entire stack on-chip
122+
CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_NIMBLE_CONTROLLER=1
123+
124+
INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/controller/include
125+
126+
SRC_THIRDPARTY_C += $(addprefix $(NIMBLE_LIB_DIR)/, \
127+
$(addprefix nimble/controller/src/, \
128+
ble_ll.c \
129+
ble_ll_adv.c \
130+
ble_ll_conn.c \
131+
ble_ll_conn_hci.c \
132+
ble_ll_ctrl.c \
133+
ble_ll_dtm.c \
134+
ble_ll_hci.c \
135+
ble_ll_hci_ev.c \
136+
ble_ll_iso.c \
137+
ble_ll_rand.c \
138+
ble_ll_resolv.c \
139+
ble_ll_rfmgmt.c \
140+
ble_ll_scan.c \
141+
ble_ll_sched.c \
142+
ble_ll_supp_cmd.c \
143+
ble_ll_sync.c \
144+
ble_ll_trace.c \
145+
ble_ll_utils.c \
146+
ble_ll_whitelist.c \
147+
) \
148+
)
149+
150+
SRC_THIRDPARTY_C += $(addprefix $(NIMBLE_LIB_DIR)/, \
151+
$(addprefix porting/nimble/src/, \
152+
hal_timer.c \
153+
os_cputime.c \
154+
os_cputime_pwr2.c \
155+
) \
156+
)
157+
158+
SRC_THIRDPARTY_C += $(NIMBLE_LIB_DIR)/nimble/transport/ram/src/ble_hci_ram.c
159+
INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/transport/ram/include
160+
161+
else # !MICROPY_BLUETOOTH_NIMBLE_CONTROLLER
162+
# External controller being used
163+
INC += -I$(TOP)/$(NIMBLE_LIB_DIR)/nimble/transport/uart/include
164+
SRC_THIRDPARTY_C += $(addprefix $(NIMBLE_EXTMOD_DIR)/, \
165+
hal/hal_uart.c \
166+
)
167+
SRC_THIRDPARTY_C += $(NIMBLE_LIB_DIR)/nimble/transport/uart/src/ble_hci_uart.c
168+
endif
122169
endif

extmod/nimble/nimble/nimble_npl_os.c

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "nimble/ble.h"
3131
#include "nimble/nimble_npl.h"
3232
#include "extmod/nimble/hal/hal_uart.h"
33+
#include "os/os_cputime.h"
34+
#include "hal/hal_timer.h"
3335

3436
#include "extmod/modbluetooth.h"
3537
#include "extmod/nimble/modbluetooth_nimble.h"
@@ -44,7 +46,7 @@
4446
#define DEBUG_TIME_printf(...) // printf(__VA_ARGS__)
4547
#define DEBUG_CRIT_printf(...) // printf(__VA_ARGS__)
4648

47-
bool ble_npl_os_started(void) {
49+
MP_WEAK bool ble_npl_os_started(void) {
4850
DEBUG_OS_printf("ble_npl_os_started\n");
4951
return true;
5052
}
@@ -175,6 +177,24 @@ int nimble_sprintf(char *str, const char *fmt, ...) {
175177
return 0;
176178
}
177179

180+
// Function to implement `strncat()` function in C
181+
char* strncat(char* destination, const char* source, size_t num)
182+
{
183+
// make `ptr` point to the end of the destination string
184+
char* ptr = destination + strlen(destination);
185+
186+
// Appends characters of the source to the destination string
187+
while (*source != '\0' && num--) {
188+
*ptr++ = *source++;
189+
}
190+
191+
// null terminate destination string
192+
*ptr = '\0';
193+
194+
// destination string is returned by standard `strncat()`
195+
return destination;
196+
}
197+
178198
/******************************************************************************/
179199
// EVENTQ
180200

@@ -278,6 +298,61 @@ void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev) {
278298
mp_bluetooth_hci_poll_now();
279299
}
280300

301+
struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo) {
302+
if (tmo != BLE_NPL_TIME_FOREVER && tmo != 0) {
303+
tmo += mp_hal_ticks_ms();
304+
}
305+
306+
struct ble_npl_event *ev = NULL;
307+
os_sr_t sr;
308+
do {
309+
ev = evq->head;
310+
if (ev) {
311+
OS_ENTER_CRITICAL(sr);
312+
// Remove this event from the queue.
313+
evq->head = ev->next;
314+
if (ev->next) {
315+
ev->next->prev = NULL;
316+
ev->next = NULL;
317+
}
318+
ev->prev = NULL;
319+
320+
ev->pending = false;
321+
322+
// Stop searching and execute this event.
323+
OS_EXIT_CRITICAL(sr);
324+
break;
325+
}
326+
} while (tmo != 0 && (tmo == BLE_NPL_TIME_FOREVER || tmo < mp_hal_ticks_ms()));
327+
328+
return ev;
329+
}
330+
331+
void ble_npl_event_run(struct ble_npl_event *ev) {
332+
// Run the event handler.
333+
DEBUG_EVENT_printf("ble_npl_event_run(%p)\n", ev);
334+
ev->fn(ev);
335+
}
336+
337+
inline bool ble_npl_event_is_queued(struct ble_npl_event *ev) {
338+
return ev->pending;
339+
}
340+
341+
void ble_npl_eventq_run(struct ble_npl_eventq *evq) {
342+
printf("ble_npl_eventq_run\n");
343+
assert(0);
344+
}
345+
346+
void ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev) {
347+
DEBUG_EVENT_printf("ble_npl_eventq_remove(%p, %p (%p, %p))\n", evq, ev, ev->prev, ev->next);
348+
os_sr_t sr;
349+
OS_ENTER_CRITICAL(sr);
350+
// Set the previous events next to this events next, so removing it from the chain
351+
ev->prev->next = ev->next;
352+
OS_EXIT_CRITICAL(sr);
353+
}
354+
355+
281356
void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn, void *arg) {
282357
DEBUG_EVENT_printf("ble_npl_event_init(%p, %p, %p)\n", ev, fn, arg);
283358
ev->fn = fn;
@@ -473,7 +548,7 @@ void ble_npl_callout_set_arg(struct ble_npl_callout *c, void *arg) {
473548
}
474549

475550
/******************************************************************************/
476-
// TIME
551+
// TIME (ticks in ms)
477552

478553
uint32_t ble_npl_time_get(void) {
479554
DEBUG_TIME_printf("ble_npl_time_get -> %u\n", (uint)mp_hal_ticks_ms());

extmod/nimble/nimble/nimble_npl_os.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ struct ble_npl_sem {
8888
// --- Called by the MicroPython port -----------------------------------------
8989

9090
void mp_bluetooth_nimble_os_eventq_run_all(void);
91+
void mp_bluetooth_nimble_eventq_run_all(struct ble_npl_eventq *eventq);
9192
void mp_bluetooth_nimble_os_callout_process(void);
93+
void mp_bluetooth_nimble_os_cputime_timer_poll(void);
9294

9395
// --- Must be provided by the MicroPython port -------------------------------
9496

0 commit comments

Comments
 (0)