44 * The MIT License (MIT)
55 *
66 * Copyright (c) 2020 Damien P. George
7+ * Copyright (c) 2020 Jim Mussared
78 *
89 * Permission is hereby granted, free of charge, to any person obtaining a copy
910 * of this software and associated documentation files (the "Software"), to deal
3132#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
3233
3334#include "lib/btstack/src/btstack.h"
34- #include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h"
35- #include "lib/btstack/platform/embedded/hal_cpu.h"
36- #include "lib/btstack/platform/embedded/hal_time_ms.h"
3735
38- #include "extmod/modbluetooth_hci.h"
39- #include "extmod/btstack/modbluetooth_btstack.h"
36+ #include "extmod/mpbthci.h"
37+ #include "extmod/btstack/btstack_hci_uart.h"
38+
39+ #include "mpbtstackport.h"
40+
41+ // Implements a btstack btstack_uart_block_t on top of the mphciuart.h
42+ // interface to an HCI UART provided by the port.
4043
4144// We pass the bytes directly to the UART during a send, but then notify btstack in the next poll.
4245STATIC bool send_done ;
@@ -48,42 +51,29 @@ STATIC size_t recv_len;
4851STATIC size_t recv_idx ;
4952STATIC void (* recv_handler )(void );
5053
51- // The IRQ functionality in btstack_run_loop_embedded.c is not used, so the
52- // following three functions are empty.
53-
54- void hal_cpu_disable_irqs (void ) {
55- }
56-
57- void hal_cpu_enable_irqs (void ) {
58- }
59-
60- void hal_cpu_enable_irqs_and_sleep (void ) {
61- }
62-
63- uint32_t hal_time_ms (void ) {
64- return mp_hal_ticks_ms ();
65- }
66-
6754STATIC int btstack_uart_init (const btstack_uart_config_t * uart_config ) {
55+ (void )uart_config ;
56+
6857 send_done = false;
6958 recv_len = 0 ;
7059 recv_idx = 0 ;
7160 recv_handler = NULL ;
7261 send_handler = NULL ;
7362
74- // Set up the UART periperhal.
75- mp_bluetooth_hci_uart_init (MICROPY_HW_BLE_UART_ID );
63+ // Set up the UART peripheral, attach IRQ and power up the HCI controller.
64+ // We haven't been told the baud rate yet, so defer that until btstack_uart_set_baudrate.
65+ mp_bluetooth_hci_uart_init (MICROPY_HW_BLE_UART_ID , 0 );
66+ mp_bluetooth_hci_controller_init ();
7667
7768 return 0 ;
7869}
7970
8071STATIC int btstack_uart_open (void ) {
81- // Attach IRQ and power up the HCI controller.
82- mp_bluetooth_hci_uart_activate ();
8372 return 0 ;
8473}
8574
8675STATIC int btstack_uart_close (void ) {
76+ mp_bluetooth_hci_controller_deinit ();
8777 return 0 ;
8878}
8979
@@ -101,10 +91,12 @@ STATIC int btstack_uart_set_baudrate(uint32_t baudrate) {
10191}
10292
10393STATIC int btstack_uart_set_parity (int parity ) {
94+ (void )parity ;
10495 return 0 ;
10596}
10697
10798STATIC int btstack_uart_set_flowcontrol (int flowcontrol ) {
99+ (void )flowcontrol ;
108100 return 0 ;
109101}
110102
@@ -123,14 +115,16 @@ STATIC int btstack_uart_get_supported_sleep_modes(void) {
123115}
124116
125117STATIC void btstack_uart_set_sleep (btstack_uart_sleep_mode_t sleep_mode ) {
118+ (void )sleep_mode ;
126119 // printf("btstack_uart_set_sleep %u\n", sleep_mode);
127120}
128121
129122STATIC void btstack_uart_set_wakeup_handler (void (* wakeup_handler )(void )) {
123+ (void )wakeup_handler ;
130124 // printf("btstack_uart_set_wakeup_handler\n");
131125}
132126
133- STATIC const btstack_uart_block_t btstack_uart_block = {
127+ const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block = {
134128 & btstack_uart_init ,
135129 & btstack_uart_open ,
136130 & btstack_uart_close ,
@@ -146,15 +140,9 @@ STATIC const btstack_uart_block_t btstack_uart_block = {
146140 & btstack_uart_set_wakeup_handler ,
147141};
148142
149- STATIC const hci_transport_config_uart_t hci_transport_config_uart = {
150- HCI_TRANSPORT_CONFIG_UART ,
151- MICROPY_HW_BLE_UART_BAUDRATE ,
152- 3000000 ,
153- 0 ,
154- NULL ,
155- };
143+ void mp_bluetooth_btstack_hci_uart_process (void ) {
144+ bool host_wake = mp_bluetooth_hci_controller_woken ();
156145
157- STATIC void btstack_uart_process (void ) {
158146 if (send_done ) {
159147 // If we'd done a TX in the last interval, notify btstack that it's complete.
160148 send_done = false;
@@ -176,48 +164,10 @@ STATIC void btstack_uart_process(void) {
176164 }
177165 }
178166 }
179- }
180-
181- void mp_bluetooth_hci_poll (void ) {
182- if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_OFF ) {
183- return ;
184- }
185167
186- // Process uart data.
187- bool host_wake = mp_bluetooth_hci_controller_woken ();
188- btstack_uart_process ();
189168 if (host_wake ) {
190169 mp_bluetooth_hci_controller_sleep_maybe ();
191170 }
192-
193- // Call the BTstack run loop.
194- btstack_run_loop_embedded_execute_once ();
195- }
196-
197- void mp_bluetooth_btstack_port_init (void ) {
198- static bool run_loop_init = false;
199- if (!run_loop_init ) {
200- run_loop_init = true;
201- btstack_run_loop_init (btstack_run_loop_embedded_get_instance ());
202- } else {
203- btstack_run_loop_embedded_get_instance ()-> init ();
204- }
205-
206- // hci_dump_open(NULL, HCI_DUMP_STDOUT);
207- const hci_transport_t * transport = hci_transport_h4_instance (& btstack_uart_block );
208- hci_init (transport , & hci_transport_config_uart );
209-
210- // TODO: Probably not necessary for BCM (we have our own firmware loader),
211- // but might be worth investigating for other controllers in the future.
212- // hci_set_chipset(btstack_chipset_bcm_instance());
213- }
214-
215- void mp_bluetooth_btstack_port_deinit (void ) {
216- hci_power_control (HCI_POWER_OFF );
217- }
218-
219- void mp_bluetooth_btstack_port_start (void ) {
220- hci_power_control (HCI_POWER_ON );
221171}
222172
223173#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
0 commit comments