|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018-2021 Damien P. George |
| 7 | + * Copyright (c) 2023 Ibrahim Abdelkader <[email protected]> |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "py/runtime.h" |
| 29 | +#include "py/stream.h" |
| 30 | +#include "py/mphal.h" |
| 31 | +#include "extmod/modbluetooth.h" |
| 32 | +#include "extmod/mpbthci.h" |
| 33 | +#include "shared/runtime/softtimer.h" |
| 34 | +#include "modmachine.h" |
| 35 | +#include "mpbthciport.h" |
| 36 | + |
| 37 | +#include "fsl_lpuart.h" |
| 38 | +#include CLOCK_CONFIG_H |
| 39 | + |
| 40 | +#if MICROPY_PY_BLUETOOTH |
| 41 | + |
| 42 | +#define DEBUG_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) |
| 43 | +#define ERROR_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) |
| 44 | + |
| 45 | +uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; |
| 46 | + |
| 47 | +STATIC mp_sched_node_t mp_bluetooth_hci_sched_node; |
| 48 | +STATIC soft_timer_entry_t mp_bluetooth_hci_soft_timer; |
| 49 | + |
| 50 | +STATIC void mp_bluetooth_hci_soft_timer_callback(soft_timer_entry_t *self) { |
| 51 | + mp_bluetooth_hci_poll_now(); |
| 52 | +} |
| 53 | + |
| 54 | +void mp_bluetooth_hci_init(void) { |
| 55 | + soft_timer_static_init( |
| 56 | + &mp_bluetooth_hci_soft_timer, |
| 57 | + SOFT_TIMER_MODE_ONE_SHOT, |
| 58 | + 0, |
| 59 | + mp_bluetooth_hci_soft_timer_callback |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +STATIC void mp_bluetooth_hci_start_polling(void) { |
| 64 | + mp_bluetooth_hci_poll_now(); |
| 65 | +} |
| 66 | + |
| 67 | +void mp_bluetooth_hci_poll_in_ms(uint32_t ms) { |
| 68 | + soft_timer_reinsert(&mp_bluetooth_hci_soft_timer, ms); |
| 69 | +} |
| 70 | + |
| 71 | +// For synchronous mode, we run all BLE stack code inside a scheduled task. |
| 72 | +STATIC void run_events_scheduled_task(mp_sched_node_t *node) { |
| 73 | + // This will process all buffered HCI UART data, and run any callouts or events. |
| 74 | + mp_bluetooth_hci_poll(); |
| 75 | +} |
| 76 | + |
| 77 | +// Called periodically (systick) or directly (e.g. UART RX IRQ) in order to |
| 78 | +// request that processing happens ASAP in the scheduler. |
| 79 | +void mp_bluetooth_hci_poll_now(void) { |
| 80 | + mp_sched_schedule_node(&mp_bluetooth_hci_sched_node, run_events_scheduled_task); |
| 81 | +} |
| 82 | + |
| 83 | +mp_obj_t mp_bthci_uart; |
| 84 | + |
| 85 | +int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { |
| 86 | + DEBUG_printf("mp_bluetooth_hci_uart_init\n"); |
| 87 | + |
| 88 | + mp_obj_t args[] = { |
| 89 | + MP_OBJ_NEW_SMALL_INT(port), |
| 90 | + MP_OBJ_NEW_QSTR(MP_QSTR_baudrate), MP_OBJ_NEW_SMALL_INT(baudrate), |
| 91 | + MP_OBJ_NEW_QSTR(MP_QSTR_timeout), MP_OBJ_NEW_SMALL_INT(200), |
| 92 | + MP_OBJ_NEW_QSTR(MP_QSTR_timeout_char), MP_OBJ_NEW_SMALL_INT(200), |
| 93 | + MP_OBJ_NEW_QSTR(MP_QSTR_txbuf), MP_OBJ_NEW_SMALL_INT(768), |
| 94 | + MP_OBJ_NEW_QSTR(MP_QSTR_rxbuf), MP_OBJ_NEW_SMALL_INT(768), |
| 95 | + }; |
| 96 | + |
| 97 | + mp_bthci_uart = MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, make_new)((mp_obj_t)&machine_uart_type, 1, 5, args); |
| 98 | + MP_STATE_PORT(mp_bthci_uart) = mp_bthci_uart; |
| 99 | + |
| 100 | + // Start the HCI polling to process any initial events/packets. |
| 101 | + mp_bluetooth_hci_start_polling(); |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +int mp_bluetooth_hci_uart_deinit(void) { |
| 106 | + DEBUG_printf("mp_bluetooth_hci_uart_deinit\n"); |
| 107 | + mp_bthci_uart = MP_OBJ_NULL; |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { |
| 112 | + DEBUG_printf("mp_bluetooth_hci_uart_set_baudrate(%lu)\n", baudrate); |
| 113 | + if (mp_bthci_uart != MP_OBJ_NULL) { |
| 114 | + // This struct is not public, so we use the base defined in board config files. |
| 115 | + // machine_uart_obj_t uart = (machine_uart_obj_t *) MP_PTR_FROM_OBJ(mp_bthci_uart); |
| 116 | + LPUART_SetBaudRate(MICROPY_HW_BLE_UART_BASE, baudrate, BOARD_BOOTCLOCKRUN_UART_CLK_ROOT); |
| 117 | + } |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +int mp_bluetooth_hci_uart_any(void) { |
| 122 | + int errcode = 0; |
| 123 | + const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol); |
| 124 | + |
| 125 | + mp_uint_t ret = proto->ioctl(mp_bthci_uart, MP_STREAM_POLL, MP_STREAM_POLL_RD, &errcode); |
| 126 | + if (errcode != 0) { |
| 127 | + ERROR_printf("Uart ioctl failed to poll UART %d\n", errcode); |
| 128 | + return -1; |
| 129 | + } |
| 130 | + return ret & MP_STREAM_POLL_RD; |
| 131 | +} |
| 132 | + |
| 133 | +int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { |
| 134 | + DEBUG_printf("mp_bluetooth_hci_uart_write\n"); |
| 135 | + |
| 136 | + int errcode = 0; |
| 137 | + const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol); |
| 138 | + |
| 139 | + mp_bluetooth_hci_controller_wakeup(); |
| 140 | + |
| 141 | + if (proto->write(mp_bthci_uart, (void *)buf, len, &errcode) < 0) { |
| 142 | + ERROR_printf("mp_bluetooth_hci_uart_write: failed to write to UART %d\n", errcode); |
| 143 | + } |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +// This function expects the controller to be in the wake state via a previous call |
| 148 | +// to mp_bluetooth_hci_controller_woken. |
| 149 | +int mp_bluetooth_hci_uart_readchar(void) { |
| 150 | + DEBUG_printf("mp_bluetooth_hci_uart_readchar\n"); |
| 151 | + if (mp_bluetooth_hci_uart_any()) { |
| 152 | + int errcode = 0; |
| 153 | + uint8_t buf = 0; |
| 154 | + const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol); |
| 155 | + if (proto->read(mp_bthci_uart, (void *)&buf, 1, &errcode) < 0) { |
| 156 | + ERROR_printf("mp_bluetooth_hci_uart_readchar: failed to read UART %d\n", errcode); |
| 157 | + return -1; |
| 158 | + } |
| 159 | + return buf; |
| 160 | + } else { |
| 161 | + DEBUG_printf("mp_bluetooth_hci_uart_readchar: not ready\n"); |
| 162 | + return -1; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// Default (weak) implementation of the HCI controller interface. |
| 167 | +// A driver (e.g. cywbt43.c) can override these for controller-specific |
| 168 | +// functionality (i.e. power management). |
| 169 | +MP_WEAK int mp_bluetooth_hci_controller_init(void) { |
| 170 | + DEBUG_printf("mp_bluetooth_hci_controller_init (default)\n"); |
| 171 | + return 0; |
| 172 | +} |
| 173 | + |
| 174 | +MP_WEAK int mp_bluetooth_hci_controller_deinit(void) { |
| 175 | + DEBUG_printf("mp_bluetooth_hci_controller_deinit (default)\n"); |
| 176 | + return 0; |
| 177 | +} |
| 178 | + |
| 179 | +MP_WEAK int mp_bluetooth_hci_controller_sleep_maybe(void) { |
| 180 | + DEBUG_printf("mp_bluetooth_hci_controller_sleep_maybe (default)\n"); |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
| 184 | +MP_WEAK bool mp_bluetooth_hci_controller_woken(void) { |
| 185 | + DEBUG_printf("mp_bluetooth_hci_controller_woken (default)\n"); |
| 186 | + return true; |
| 187 | +} |
| 188 | + |
| 189 | +MP_WEAK int mp_bluetooth_hci_controller_wakeup(void) { |
| 190 | + DEBUG_printf("mp_bluetooth_hci_controller_wakeup (default)\n"); |
| 191 | + return 0; |
| 192 | +} |
| 193 | + |
| 194 | +MP_REGISTER_ROOT_POINTER(mp_obj_t mp_bthci_uart); |
| 195 | + |
| 196 | +#endif // MICROPY_PY_BLUETOOTH |
0 commit comments