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 <pthread.h>
28
29#include "py/runtime.h"
30#include "py/mperrno.h"
31#include "py/mphal.h"
32
33#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4
34
35#include "lib/btstack/chipset/zephyr/btstack_chipset_zephyr.h"
36
37#include "extmod/btstack/btstack_hci_uart.h"
38#include "extmod/btstack/modbluetooth_btstack.h"
39
40#include "mpbtstackport.h"
41
42#define DEBUG_printf(...) // printf(__VA_ARGS__)
43
44STATIC hci_transport_config_uart_t hci_transport_config_uart = {
45 HCI_TRANSPORT_CONFIG_UART,
46 1000000, // initial baudrate
47 0, // main baudrate
48 1, // flow control
49 NULL, // device name
50};
51
52void mp_bluetooth_hci_poll_h4(void) {
53 if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
54 mp_bluetooth_btstack_hci_uart_process();
55 }
56}
57
58void mp_bluetooth_btstack_port_init_h4(void) {
59 DEBUG_printf("mp_bluetooth_btstack_port_init_h4\n");
60
61 const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block);
62 hci_init(transport, &hci_transport_config_uart);
63
64 hci_set_chipset(btstack_chipset_zephyr_instance());
65}
66
67void mp_bluetooth_btstack_port_deinit(void) {
68 DEBUG_printf("mp_bluetooth_btstack_port_deinit\n");
69
70 hci_power_control(HCI_POWER_OFF);
71 hci_close();
72}
73
74void mp_bluetooth_btstack_port_start(void) {
75 DEBUG_printf("mp_bluetooth_btstack_port_start\n");
76
77 hci_power_control(HCI_POWER_ON);
78}
79
80#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4
81