1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2020 Jim Mussared
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include "py/runtime.h"
28#include "py/mperrno.h"
29#include "py/mphal.h"
30
31#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
32
33#include "lib/btstack/src/btstack.h"
34
35#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h"
36#include "lib/btstack/platform/embedded/hal_cpu.h"
37#include "lib/btstack/platform/embedded/hal_time_ms.h"
38
39#include "extmod/btstack/modbluetooth_btstack.h"
40
41#include "mpbtstackport.h"
42
43// Called by the UART polling thread in mpbthciport.c, or by the USB polling thread in mpbtstackport_usb.c.
44bool mp_bluetooth_hci_poll(void) {
45 if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_HALTING) {
46 // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time).
47 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
48 #if MICROPY_BLUETOOTH_BTSTACK_H4
49 mp_bluetooth_hci_poll_h4();
50 #endif
51 btstack_run_loop_embedded_execute_once();
52 MICROPY_END_ATOMIC_SECTION(atomic_state);
53
54 return true;
55 }
56
57 return false;
58}
59
60bool mp_bluetooth_hci_active(void) {
61 return mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_OFF
62 && mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT;
63}
64
65// The IRQ functionality in btstack_run_loop_embedded.c is not used, so the
66// following three functions are empty.
67
68void hal_cpu_disable_irqs(void) {
69}
70
71void hal_cpu_enable_irqs(void) {
72}
73
74void hal_cpu_enable_irqs_and_sleep(void) {
75}
76
77uint32_t hal_time_ms(void) {
78 return mp_hal_ticks_ms();
79}
80
81void mp_bluetooth_btstack_port_init(void) {
82 static bool run_loop_init = false;
83 if (!run_loop_init) {
84 run_loop_init = true;
85 btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
86 } else {
87 btstack_run_loop_embedded_get_instance()->init();
88 }
89
90 // hci_dump_open(NULL, HCI_DUMP_STDOUT);
91
92 #if MICROPY_BLUETOOTH_BTSTACK_H4
93 mp_bluetooth_btstack_port_init_h4();
94 #endif
95
96 #if MICROPY_BLUETOOTH_BTSTACK_USB
97 mp_bluetooth_btstack_port_init_usb();
98 #endif
99}
100
101#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
102