|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Ibrahim Abdelkader <[email protected]> |
| 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/stream.h" |
| 29 | +#include "py/mphal.h" |
| 30 | +#include "extmod/modbluetooth.h" |
| 31 | +#include "extmod/mpbthci.h" |
| 32 | +#include "modmachine.h" |
| 33 | +#include "mpbthciport.h" |
| 34 | +#include "pico/stdlib.h" |
| 35 | + |
| 36 | +#if MICROPY_PY_BLUETOOTH |
| 37 | + |
| 38 | +#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) |
| 39 | +#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) |
| 40 | + |
| 41 | +// Poll timer ID. |
| 42 | +static alarm_id_t poll_timer_id = 0; |
| 43 | + |
| 44 | +uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; |
| 45 | + |
| 46 | +// Prevent double-enqueuing of the scheduled task. |
| 47 | +STATIC volatile bool events_task_is_scheduled; |
| 48 | + |
| 49 | +void mp_bluetooth_hci_init(void) { |
| 50 | + events_task_is_scheduled = false; |
| 51 | +} |
| 52 | + |
| 53 | +STATIC void mp_bluetooth_hci_start_polling(void) { |
| 54 | + events_task_is_scheduled = false; |
| 55 | + mp_bluetooth_hci_poll_now(); |
| 56 | +} |
| 57 | + |
| 58 | +static int64_t mp_bluetooth_hci_timer_callback(alarm_id_t id, void *user_data) { |
| 59 | + poll_timer_id = 0; |
| 60 | + mp_bluetooth_hci_poll_now(); |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +void mp_bluetooth_hci_poll_in_ms(uint32_t ms) { |
| 65 | + poll_timer_id = add_alarm_in_ms(ms, mp_bluetooth_hci_timer_callback, NULL, true); |
| 66 | +} |
| 67 | + |
| 68 | +// For synchronous mode, we run all BLE stack code inside a scheduled task. |
| 69 | +// This task is scheduled periodically via a timer, or immediately after UART RX IRQ. |
| 70 | +STATIC mp_obj_t run_events_scheduled_task(mp_obj_t none_in) { |
| 71 | + (void)none_in; |
| 72 | + events_task_is_scheduled = false; |
| 73 | + // This will process all buffered HCI UART data, and run any callouts or events. |
| 74 | + mp_bluetooth_hci_poll(); |
| 75 | + return mp_const_none; |
| 76 | +} |
| 77 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(run_events_scheduled_task_obj, run_events_scheduled_task); |
| 78 | + |
| 79 | +// Called periodically (systick) or directly (e.g. UART RX IRQ) in order to |
| 80 | +// request that processing happens ASAP in the scheduler. |
| 81 | +void mp_bluetooth_hci_poll_now(void) { |
| 82 | + if (!events_task_is_scheduled) { |
| 83 | + events_task_is_scheduled = mp_sched_schedule(MP_OBJ_FROM_PTR(&run_events_scheduled_task_obj), mp_const_none); |
| 84 | + if (!events_task_is_scheduled) { |
| 85 | + // The schedule queue is full, set callback to try again soon. |
| 86 | + mp_bluetooth_hci_poll_in_ms(5); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +mp_obj_t mp_bthci_uart; |
| 92 | + |
| 93 | +int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { |
| 94 | + debug_printf("mp_bluetooth_hci_uart_init\n"); |
| 95 | + |
| 96 | + mp_obj_t args[] = { |
| 97 | + MP_OBJ_NEW_SMALL_INT(MICROPY_HW_BLE_UART_ID), |
| 98 | + MP_OBJ_NEW_SMALL_INT(MICROPY_HW_BLE_UART_BAUDRATE), |
| 99 | + MP_OBJ_NEW_QSTR(MP_QSTR_flow), MP_OBJ_NEW_SMALL_INT((1 | 2)), |
| 100 | + MP_OBJ_NEW_QSTR(MP_QSTR_timeout), MP_OBJ_NEW_SMALL_INT(1000), |
| 101 | + }; |
| 102 | + |
| 103 | + mp_bthci_uart = machine_uart_type.make_new((mp_obj_t)&machine_uart_type, 2, 2, args); |
| 104 | + MP_STATE_PORT(mp_bthci_uart) = mp_bthci_uart; |
| 105 | + |
| 106 | + // Start the HCI polling to process any initial events/packets. |
| 107 | + mp_bluetooth_hci_start_polling(); |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +int mp_bluetooth_hci_uart_deinit(void) { |
| 112 | + debug_printf("mp_bluetooth_hci_uart_deinit\n"); |
| 113 | + |
| 114 | + // If a poll callback is set cancel it now. |
| 115 | + if (poll_timer_id > 0) { |
| 116 | + cancel_alarm(poll_timer_id); |
| 117 | + } |
| 118 | + poll_timer_id = 0; |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { |
| 123 | + debug_printf("mp_bluetooth_hci_uart_set_baudrate(%lu)\n", baudrate); |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +int mp_bluetooth_hci_uart_any(void) { |
| 128 | + int errcode = 0; |
| 129 | + const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; |
| 130 | + |
| 131 | + mp_uint_t ret = proto->ioctl(mp_bthci_uart, MP_STREAM_POLL, MP_STREAM_POLL_RD, &errcode); |
| 132 | + if (errcode != 0) { |
| 133 | + error_printf("Uart ioctl failed to poll UART %d\n", errcode); |
| 134 | + return -1; |
| 135 | + } |
| 136 | + return ret & MP_STREAM_POLL_RD; |
| 137 | +} |
| 138 | + |
| 139 | +int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { |
| 140 | + debug_printf("mp_bluetooth_hci_uart_write\n"); |
| 141 | + |
| 142 | + int errcode = 0; |
| 143 | + const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; |
| 144 | + |
| 145 | + mp_bluetooth_hci_controller_wakeup(); |
| 146 | + |
| 147 | + if (proto->write(mp_bthci_uart, (void *)buf, len, &errcode) < 0) { |
| 148 | + error_printf("mp_bluetooth_hci_uart_write: failed to write to UART %d\n", errcode); |
| 149 | + } |
| 150 | + return 0; |
| 151 | +} |
| 152 | + |
| 153 | +// This function expects the controller to be in the wake state via a previous call |
| 154 | +// to mp_bluetooth_hci_controller_woken. |
| 155 | +int mp_bluetooth_hci_uart_readchar(void) { |
| 156 | + debug_printf("mp_bluetooth_hci_uart_readchar\n"); |
| 157 | + if (mp_bluetooth_hci_uart_any()) { |
| 158 | + int errcode = 0; |
| 159 | + uint8_t buf = 0; |
| 160 | + const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; |
| 161 | + if (proto->read(mp_bthci_uart, (void *)&buf, 1, &errcode) < 0) { |
| 162 | + error_printf("mp_bluetooth_hci_uart_readchar: failed to read UART %d\n", errcode); |
| 163 | + return -1; |
| 164 | + } |
| 165 | + return buf; |
| 166 | + } else { |
| 167 | + debug_printf("mp_bluetooth_hci_uart_readchar: not ready\n"); |
| 168 | + return -1; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// Default (weak) implementation of the HCI controller interface. |
| 173 | +// A driver (e.g. cywbt43.c) can override these for controller-specific |
| 174 | +// functionality (i.e. power management). |
| 175 | +MP_WEAK int mp_bluetooth_hci_controller_init(void) { |
| 176 | + debug_printf("mp_bluetooth_hci_controller_init (default)\n"); |
| 177 | + return 0; |
| 178 | +} |
| 179 | + |
| 180 | +MP_WEAK int mp_bluetooth_hci_controller_deinit(void) { |
| 181 | + debug_printf("mp_bluetooth_hci_controller_deinit (default)\n"); |
| 182 | + return 0; |
| 183 | +} |
| 184 | + |
| 185 | +MP_WEAK int mp_bluetooth_hci_controller_sleep_maybe(void) { |
| 186 | + debug_printf("mp_bluetooth_hci_controller_sleep_maybe (default)\n"); |
| 187 | + return 0; |
| 188 | +} |
| 189 | + |
| 190 | +MP_WEAK bool mp_bluetooth_hci_controller_woken(void) { |
| 191 | + debug_printf("mp_bluetooth_hci_controller_woken (default)\n"); |
| 192 | + return true; |
| 193 | +} |
| 194 | + |
| 195 | +MP_WEAK int mp_bluetooth_hci_controller_wakeup(void) { |
| 196 | + debug_printf("mp_bluetooth_hci_controller_wakeup (default)\n"); |
| 197 | + return 0; |
| 198 | +} |
| 199 | + |
| 200 | +#endif // MICROPY_PY_BLUETOOTH |
0 commit comments