|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2025 The Pybricks Authors |
| 3 | + |
| 4 | +// EV3 UART driver for BlueKitchen BTStack (stubs). |
| 5 | + |
| 6 | +#include <pbdrv/config.h> |
| 7 | + |
| 8 | +#if PBDRV_CONFIG_BLUETOOTH_BTSTACK_EV3_UART |
| 9 | + |
| 10 | +#include <stdint.h> |
| 11 | + |
| 12 | +#include <btstack.h> |
| 13 | +#include <btstack_uart_block.h> |
| 14 | +#include <pbdrv/uart.h> |
| 15 | +#include <pbio/os.h> |
| 16 | + |
| 17 | +#include "bluetooth_btstack_uart_block_ev3.h" |
| 18 | + |
| 19 | +// If a read has been requested, a pointer to the buffer and its length, else |
| 20 | +// null and zero. |
| 21 | +static uint8_t *read_buf; |
| 22 | +static int read_buf_len; |
| 23 | + |
| 24 | +// If a write has been requested, a pointer to the buffer and its length, else |
| 25 | +// null and zero. |
| 26 | +static const uint8_t *write_buf; |
| 27 | +static int write_buf_len; |
| 28 | + |
| 29 | +// Should the reader and writer processes shut down? |
| 30 | +static bool start_shutdown; |
| 31 | + |
| 32 | +// How many threads have processes have exited since the request to shut down? |
| 33 | +static int8_t threads_shutdown_complete; |
| 34 | + |
| 35 | +// Called when a block finishes sending. |
| 36 | +static void (*block_sent)(void); |
| 37 | +// Called when a block finishes being received. |
| 38 | +static void (*block_received)(void); |
| 39 | + |
| 40 | +// Processes for reading and writing blocks. |
| 41 | +static pbio_os_process_t reader_process; |
| 42 | +static pbio_os_process_t writer_process; |
| 43 | + |
| 44 | +static pbdrv_uart_dev_t *uart_device() { |
| 45 | + const pbdrv_bluetooth_btstack_uart_block_ev3_platform_data_t *pdata = |
| 46 | + &pbdrv_bluetooth_btstack_uart_block_ev3_platform_data; |
| 47 | + pbdrv_uart_dev_t *uart; |
| 48 | + pbio_error_t err = pbdrv_uart_get_instance(pdata->uart_id, &uart); |
| 49 | + if (err != PBIO_SUCCESS) { |
| 50 | + return NULL; |
| 51 | + } |
| 52 | + return uart; |
| 53 | +} |
| 54 | + |
| 55 | +static int btstack_uart_block_ev3_init(const btstack_uart_config_t *config) { |
| 56 | + pbdrv_uart_set_baud_rate(uart_device(), config->baudrate); |
| 57 | + // TODO: add parity, flow control APIs and obey them. |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +static pbio_error_t do_read_process(pbio_os_state_t *state, void *context) { |
| 63 | + pbdrv_uart_dev_t *const uart = uart_device(); |
| 64 | + |
| 65 | + pbio_os_state_t read_state; |
| 66 | + PBIO_OS_ASYNC_BEGIN(state); |
| 67 | + |
| 68 | + while (true) { |
| 69 | + PBIO_OS_AWAIT_UNTIL(state, read_buf || start_shutdown); |
| 70 | + if (start_shutdown) { |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + PBIO_OS_AWAIT(state, &read_state, |
| 75 | + pbdrv_uart_read(state, uart, read_buf, read_buf_len, /*timeout=*/ 0)); |
| 76 | + read_buf = NULL; |
| 77 | + read_buf_len = 0; |
| 78 | + if (block_received) { |
| 79 | + block_received(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + ++threads_shutdown_complete; |
| 84 | + PBIO_OS_ASYNC_END(PBIO_SUCCESS); |
| 85 | +} |
| 86 | + |
| 87 | +static pbio_error_t do_write_process(pbio_os_state_t *state, void *context) { |
| 88 | + pbdrv_uart_dev_t *const uart = uart_device(); |
| 89 | + |
| 90 | + pbio_os_state_t write_state; |
| 91 | + |
| 92 | + PBIO_OS_ASYNC_BEGIN(state); |
| 93 | + |
| 94 | + while (true) { |
| 95 | + PBIO_OS_AWAIT_UNTIL(state, write_buf || start_shutdown); |
| 96 | + if (start_shutdown) { |
| 97 | + break; |
| 98 | + } |
| 99 | + |
| 100 | + PBIO_OS_AWAIT(state, &write_state, |
| 101 | + pbdrv_uart_write(&write_state, uart, (uint8_t *)write_buf, write_buf_len, /*timeout=*/ 0)); |
| 102 | + write_buf = NULL; |
| 103 | + write_buf_len = 0; |
| 104 | + if (block_sent) { |
| 105 | + block_sent(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + ++threads_shutdown_complete; |
| 110 | + PBIO_OS_ASYNC_END(PBIO_SUCCESS); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +static int btstack_uart_block_ev3_open(void) { |
| 115 | + write_buf = NULL; |
| 116 | + write_buf_len = 0; |
| 117 | + read_buf = NULL; |
| 118 | + read_buf_len = 0; |
| 119 | + start_shutdown = false; |
| 120 | + threads_shutdown_complete = 0; |
| 121 | + block_received = NULL; |
| 122 | + block_sent = NULL; |
| 123 | + |
| 124 | + pbio_os_process_start(&reader_process, do_read_process, NULL); |
| 125 | + pbio_os_process_start(&writer_process, do_write_process, NULL); |
| 126 | + |
| 127 | + return 0; |
| 128 | +} |
| 129 | + |
| 130 | +static int btstack_uart_block_ev3_close(void) { |
| 131 | + start_shutdown = true; |
| 132 | + while (threads_shutdown_complete < 2) { |
| 133 | + pbio_os_run_processes_and_wait_for_event(); |
| 134 | + } |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
| 138 | + |
| 139 | +static void btstack_uart_block_ev3_set_block_received(void (*handler)(void)) { |
| 140 | + block_received = handler; |
| 141 | +} |
| 142 | + |
| 143 | +static void btstack_uart_block_ev3_set_block_sent(void (*handler)(void)) { |
| 144 | + block_sent = handler; |
| 145 | +} |
| 146 | + |
| 147 | +static int btstack_uart_block_ev3_set_baudrate(uint32_t baud) { |
| 148 | + pbdrv_uart_set_baud_rate(uart_device(), baud); |
| 149 | + return 0; |
| 150 | +} |
| 151 | + |
| 152 | +static int btstack_uart_block_ev3_set_parity(int parity) { |
| 153 | + // TODO: maybe implement the parity setting. |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +static void btstack_uart_block_ev3_receive_block(uint8_t *buffer, |
| 158 | + uint16_t len) { |
| 159 | + read_buf = buffer; |
| 160 | + read_buf_len = len; |
| 161 | + pbio_os_request_poll(); |
| 162 | +} |
| 163 | + |
| 164 | +static void btstack_uart_block_ev3_send_block(const uint8_t *data, |
| 165 | + uint16_t size) { |
| 166 | + write_buf = data; |
| 167 | + write_buf_len = size; |
| 168 | + pbio_os_request_poll(); |
| 169 | +} |
| 170 | + |
| 171 | +static const btstack_uart_block_t btstack_uart_block_ev3 = { |
| 172 | + .init = btstack_uart_block_ev3_init, |
| 173 | + .open = btstack_uart_block_ev3_open, |
| 174 | + .close = btstack_uart_block_ev3_close, |
| 175 | + .set_block_received = btstack_uart_block_ev3_set_block_received, |
| 176 | + .set_block_sent = btstack_uart_block_ev3_set_block_sent, |
| 177 | + .set_baudrate = btstack_uart_block_ev3_set_baudrate, |
| 178 | + .set_parity = btstack_uart_block_ev3_set_parity, |
| 179 | + .set_flowcontrol = NULL, |
| 180 | + .receive_block = btstack_uart_block_ev3_receive_block, |
| 181 | + .send_block = btstack_uart_block_ev3_send_block, |
| 182 | + .get_supported_sleep_modes = NULL, |
| 183 | + .set_sleep = NULL, |
| 184 | + .set_wakeup_handler = NULL, |
| 185 | +}; |
| 186 | + |
| 187 | +const btstack_uart_block_t *pbdrv_bluetooth_btstack_uart_block_ev3_instance( |
| 188 | + void) { |
| 189 | + return &btstack_uart_block_ev3; |
| 190 | +} |
| 191 | + |
| 192 | +#endif // PBDRV_CONFIG_BLUETOOTH_BTSTACK_EV3_UART |
0 commit comments