|
30 | 30 | #include "components/driver/include/driver/uart.h"
|
31 | 31 |
|
32 | 32 | #include "mpconfigport.h"
|
| 33 | +#include "shared/readline/readline.h" |
33 | 34 | #include "shared/runtime/interrupt_char.h"
|
34 | 35 | #include "py/gc.h"
|
35 | 36 | #include "py/mperrno.h"
|
36 | 37 | #include "py/runtime.h"
|
37 | 38 | #include "py/stream.h"
|
| 39 | +#include "supervisor/port.h" |
38 | 40 | #include "supervisor/shared/translate.h"
|
39 | 41 | #include "supervisor/shared/tick.h"
|
40 | 42 |
|
41 | 43 | uint8_t never_reset_uart_mask = 0;
|
42 | 44 |
|
| 45 | +static void uart_event_task(void *param) { |
| 46 | + busio_uart_obj_t *self = param; |
| 47 | + uart_event_t event; |
| 48 | + while (true) { |
| 49 | + if (xQueueReceive(self->event_queue, &event, portMAX_DELAY)) { |
| 50 | + switch (event.type) { |
| 51 | + case UART_PATTERN_DET: |
| 52 | + // When the debug uart receives CTRL+C, wake the main task and schedule a keyboard interrupt |
| 53 | + if (self->is_debug) { |
| 54 | + port_wake_main_task(); |
| 55 | + if (mp_interrupt_char == CHAR_CTRL_C) { |
| 56 | + uart_flush(self->uart_num); |
| 57 | + mp_sched_keyboard_interrupt(); |
| 58 | + } |
| 59 | + } |
| 60 | + break; |
| 61 | + case UART_DATA: |
| 62 | + // When the debug uart receives any key, wake the main task |
| 63 | + if (self->is_debug) { |
| 64 | + port_wake_main_task(); |
| 65 | + } |
| 66 | + break; |
| 67 | + default: |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
43 | 74 | void uart_reset(void) {
|
44 | 75 | for (uart_port_t num = 0; num < UART_NUM_MAX; num++) {
|
45 | 76 | // Ignore the UART used by the IDF.
|
@@ -125,10 +156,26 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
|
125 | 156 |
|
126 | 157 | uint8_t rx_threshold = UART_FIFO_LEN - 8;
|
127 | 158 | // Install the driver before we change the settings.
|
128 |
| - if (uart_driver_install(self->uart_num, receiver_buffer_size, 0, 0, NULL, 0) != ESP_OK || |
| 159 | + if (uart_driver_install(self->uart_num, receiver_buffer_size, 0, 20, &self->event_queue, 0) != ESP_OK || |
129 | 160 | uart_set_mode(self->uart_num, mode) != ESP_OK) {
|
130 | 161 | mp_raise_ValueError(translate("Could not initialize UART"));
|
131 | 162 | }
|
| 163 | + // On the debug uart, enable pattern detection to look for CTRL+C |
| 164 | + #ifdef CIRCUITPY_DEBUG_UART_RX |
| 165 | + if (rx == CIRCUITPY_DEBUG_UART_RX) { |
| 166 | + self->is_debug = true; |
| 167 | + uart_enable_pattern_det_baud_intr(self->uart_num, CHAR_CTRL_C, 1, 1, 0, 0); |
| 168 | + } |
| 169 | + #endif |
| 170 | + // Start a task to listen for uart events |
| 171 | + xTaskCreatePinnedToCore( |
| 172 | + uart_event_task, |
| 173 | + "uart_event_task", |
| 174 | + configMINIMAL_STACK_SIZE, |
| 175 | + self, |
| 176 | + CONFIG_PTHREAD_TASK_PRIO_DEFAULT, |
| 177 | + &self->event_task, |
| 178 | + xPortGetCoreID()); |
132 | 179 | uart_set_hw_flow_ctrl(self->uart_num, flow_control, rx_threshold);
|
133 | 180 |
|
134 | 181 | // Set baud rate
|
@@ -230,6 +277,7 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
|
230 | 277 | if (common_hal_busio_uart_deinited(self)) {
|
231 | 278 | return;
|
232 | 279 | }
|
| 280 | + vTaskDelete(self->event_task); |
233 | 281 | uart_driver_delete(self->uart_num);
|
234 | 282 |
|
235 | 283 | common_hal_reset_pin(self->rx_pin);
|
|
0 commit comments