8000 esp32/usb: Wake main thread when usb receives data. by andrewleech · Pull Request #12845 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

esp32/usb: Wake main thread when usb receives data. #12845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ports/esp32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ uint64_t mp_hal_time_ns(void) {
return ns;
}

// Wake up the main task if it is sleeping
// Wake up the main task if it is sleeping.
void mp_hal_wake_main_task(void) {
xTaskNotifyGive(mp_main_task_handle);
}

// Wake up the main task if it is sleeping, to be called from an ISR.
void mp_hal_wake_main_task_from_isr(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(mp_main_task_handle, &xHigherPriorityTaskWoken);
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/mphalport.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ uint32_t mp_hal_get_cpu_freq(void);
#define mp_hal_quiet_timing_exit(irq_state) MICROPY_END_ATOMIC_SECTION(irq_state)

// Wake up the main task if it is sleeping
void mp_hal_wake_main_task(void);
void mp_hal_wake_main_task_from_isr(void);

// C-level pin HAL
Expand Down
5 changes: 4 additions & 1 deletion ports/esp32/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@

static uint8_t usb_rx_buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE];

// This is called from FreeRTOS task "tusb_tsk" in espressif__esp_tinyusb (not an ISR).
static void usb_callback_rx(int itf, cdcacm_event_t *event) {
// TODO: what happens if more chars come in during this function, are they lost?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a comment (on line 42 just before the function), that it's called from a separate FreeRTOS task (and not an ISR).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, done

// espressif__esp_tinyusb places tinyusb rx data onto freertos ringbuffer which
// this function forwards onto our stdin_ringbuf.
for (;;) {
size_t len = 0;
esp_err_t ret = tinyusb_cdcacm_read(itf, usb_rx_buf, sizeof(usb_rx_buf), &len);
Expand All @@ -58,6 +60,7 @@ static void usb_callback_rx(int itf, cdcacm_event_t *event) {
ringbuf_put(&stdin_ringbuf, usb_rx_buf[i]);
}
}
mp_hal_wake_main_task();
}
}

Expand Down
0