Skip to content

Commit db8273d

Browse files
iabdalkaderdpgeorge
authored andcommitted
shared/tinyusb: Add macro to override TinyUSB callbacks.
Add wrapper macros that by default expand to the callback name. Users can define this macro to add a prefix (e.g., mp_) to callback implementations, to redirect or completely override MicroPython's TinyUSB callbacks. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent a686410 commit db8273d

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

shared/tinyusb/mp_usbd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void mp_usbd_task_callback(mp_sched_node_t *node) {
4444
#endif // !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
4545

4646
// Schedule the TinyUSB task on demand, when there is a new USB device event
47-
TU_ATTR_FAST_FUNC void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr) {
47+
TU_ATTR_FAST_FUNC void MICROPY_WRAP_TUD_EVENT_HOOK_CB(tud_event_hook_cb)(uint8_t rhport, uint32_t eventid, bool in_isr) {
4848
mp_usbd_schedule_task();
4949
mp_hal_wake_main_task_from_isr();
5050
}

shared/tinyusb/mp_usbd.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@
2929

3030
#include "py/mpconfig.h"
3131

32+
#ifndef MICROPY_WRAP_TUD_SOF_CB
33+
#define MICROPY_WRAP_TUD_SOF_CB(name) name
34+
#endif
35+
36+
#ifndef MICROPY_WRAP_TUD_CDC_RX_CB
37+
#define MICROPY_WRAP_TUD_CDC_RX_CB(name) name
38+
#endif
39+
40+
#ifndef MICROPY_WRAP_TUD_CDC_LINE_STATE_CB
41+
#define MICROPY_WRAP_TUD_CDC_LINE_STATE_CB(name) name
42+
#endif
43+
44+
#ifndef MICROPY_WRAP_TUD_EVENT_HOOK_CB
45+
#define MICROPY_WRAP_TUD_EVENT_HOOK_CB(name) name
46+
#endif
47+
3248
#if MICROPY_HW_ENABLE_USBDEV
3349

3450
#include "py/obj.h"

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) {
6868
return ret;
6969
}
7070

71-
void tud_cdc_rx_cb(uint8_t itf) {
71+
void MICROPY_WRAP_TUD_CDC_RX_CB(tud_cdc_rx_cb)(uint8_t itf) {
7272
// consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
7373
// in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
7474
cdc_itf_pending &= ~(1 << itf);
@@ -141,7 +141,7 @@ mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len) {
141141
return i;
142142
}
143143

144-
void tud_sof_cb(uint32_t frame_count) {
144+
void MICROPY_WRAP_TUD_SOF_CB(tud_sof_cb)(uint32_t frame_count) {
145145
if (--cdc_connected_flush_delay < 0) {
146146
// Finished on-connection delay, disable SOF interrupt again.
147147
tud_sof_cb_enable(false);
@@ -172,7 +172,7 @@ static struct {
172172
} prev_line_state = {0};
173173
#endif
174174

175-
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
175+
void MICROPY_WRAP_TUD_CDC_LINE_STATE_CB(tud_cdc_line_state_cb)(uint8_t itf, bool dtr, bool rts) {
176176
#if MICROPY_HW_USB_CDC && !MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDC
177177
if (dtr) {
178178
// A host application has started to open the cdc serial port.

shared/tinyusb/mp_usbd_cdc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_CDC_H
2828
#define MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_CDC_H
2929

30+
#include "mp_usbd.h"
31+
3032
#ifndef MICROPY_HW_USB_CDC_TX_TIMEOUT
3133
#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500)
3234
#endif
@@ -38,7 +40,7 @@
3840
#endif
3941

4042
uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags);
41-
void tud_cdc_rx_cb(uint8_t itf);
43+
void MICROPY_WRAP_TUD_CDC_RX_CB(tud_cdc_rx_cb)(uint8_t itf);
4244
mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len);
4345

4446
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_CDC_H

0 commit comments

Comments
 (0)