|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 OpenMV LLC. |
| 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/mphal.h" |
| 28 | +#include "py/runtime.h" |
| 29 | +#include "extmod/mpbthci.h" |
| 30 | +#include "shared/runtime/softtimer.h" |
| 31 | +#include "mpbthciport.h" |
| 32 | + |
| 33 | +#if MICROPY_PY_BLUETOOTH |
| 34 | + |
| 35 | +uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; |
| 36 | + |
| 37 | +// Soft timer and scheduling node for scheduling a HCI poll. |
| 38 | +static soft_timer_entry_t mp_bluetooth_hci_soft_timer; |
| 39 | +static mp_sched_node_t mp_bluetooth_hci_sched_node; |
| 40 | + |
| 41 | +// This is called by soft_timer and executes at IRQ_PRI_PENDSV. |
| 42 | +static void mp_bluetooth_hci_soft_timer_callback(soft_timer_entry_t *self) { |
| 43 | + mp_bluetooth_hci_poll_now(); |
| 44 | +} |
| 45 | + |
| 46 | +void mp_bluetooth_hci_init(void) { |
| 47 | + soft_timer_static_init( |
| 48 | + &mp_bluetooth_hci_soft_timer, |
| 49 | + SOFT_TIMER_MODE_ONE_SHOT, |
| 50 | + 0, |
| 51 | + mp_bluetooth_hci_soft_timer_callback |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +static void mp_bluetooth_hci_start_polling(void) { |
| 56 | + mp_bluetooth_hci_poll_now(); |
| 57 | +} |
| 58 | + |
| 59 | +void mp_bluetooth_hci_poll_in_ms(uint32_t ms) { |
| 60 | + soft_timer_reinsert(&mp_bluetooth_hci_soft_timer, ms); |
| 61 | +} |
| 62 | + |
| 63 | +// For synchronous mode, we run all BLE stack code inside a scheduled task. |
| 64 | +// This task is scheduled periodically via a timer, or immediately after UART RX IRQ. |
| 65 | +static void run_events_scheduled_task(mp_sched_node_t *node) { |
| 66 | + (void)node; |
| 67 | + // This will process all buffered HCI UART data, and run any callouts or events. |
| 68 | + mp_bluetooth_hci_poll(); |
| 69 | +} |
| 70 | + |
| 71 | +// Called periodically (systick) or directly (e.g. UART RX IRQ) in order to |
| 72 | +// request that processing happens ASAP in the scheduler. |
| 73 | +void mp_bluetooth_hci_poll_now(void) { |
| 74 | + mp_sched_schedule_node(&mp_bluetooth_hci_sched_node, run_events_scheduled_task); |
| 75 | +} |
| 76 | + |
| 77 | +/******************************************************************************/ |
| 78 | +// HCI over UART |
| 79 | + |
| 80 | +#include "mpuart.h" |
| 81 | + |
| 82 | +static uint32_t hci_uart_id; |
| 83 | +static bool hci_uart_first_char; |
| 84 | +static uint8_t hci_rx_ringbuf_array[768]; |
| 85 | +static ringbuf_t hci_rx_ringbuf = { |
| 86 | + .buf = hci_rx_ringbuf_array, |
| 87 | + .size = sizeof(hci_rx_ringbuf_array), |
| 88 | + .iget = 0, |
| 89 | + .iput = 0, |
| 90 | +}; |
| 91 | + |
| 92 | +int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { |
| 93 | + hci_uart_id = port; |
| 94 | + hci_uart_first_char = true; |
| 95 | + |
| 96 | + // Initialise the UART. |
| 97 | + mp_uart_init(hci_uart_id, baudrate, pin_BT_UART_TX, pin_BT_UART_RX, &hci_rx_ringbuf); |
| 98 | + mp_uart_set_flow(hci_uart_id, pin_BT_UART_RTS, pin_BT_UART_CTS); |
| 99 | + mp_uart_set_irq_callback(hci_uart_id, mp_bluetooth_hci_poll_now); |
| 100 | + |
| 101 | + // Start the HCI polling to process any initial events/packets. |
| 102 | + mp_bluetooth_hci_start_polling(); |
| 103 | + |
| 104 | + return 0; |
| 105 | +} |
| 106 | + |
| 107 | +int mp_bluetooth_hci_uart_deinit(void) { |
| 108 | + mp_uart_deinit(hci_uart_id); |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { |
| 113 | + mp_uart_set_baudrate(hci_uart_id, baudrate); |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { |
| 118 | + mp_bluetooth_hci_controller_wakeup(); |
| 119 | + mp_uart_tx_data(hci_uart_id, (void *)buf, len); |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +// This function expects the controller to be in the wake state via a previous call |
| 124 | +// to mp_bluetooth_hci_controller_woken. |
| 125 | +int mp_bluetooth_hci_uart_readchar(void) { |
| 126 | + if (mp_uart_rx_any(hci_uart_id)) { |
| 127 | + int c = mp_uart_rx_char(hci_uart_id); |
| 128 | + if (hci_uart_first_char) { |
| 129 | + if (c == 0) { |
| 130 | + return -1; |
| 131 | + } |
| 132 | + hci_uart_first_char = false; |
| 133 | + } |
| 134 | + return c; |
| 135 | + } else { |
| 136 | + return -1; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#endif // MICROPY_PY_BLUETOOTH |
0 commit comments