Skip to content

Commit 27ea4d1

Browse files
committed
add pre-init/post-deinit callback
Signed-off-by: HiFiPhile <admin@hifiphile.com>
1 parent 048eb5e commit 27ea4d1

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/device/usbd.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
8484
return false;
8585
}
8686

87+
TU_ATTR_WEAK void tud_pre_init_cb(uint8_t rhport) {
88+
(void) rhport;
89+
}
90+
91+
TU_ATTR_WEAK void tud_post_deinit_cb(uint8_t rhport) {
92+
(void) rhport;
93+
}
94+
8795
TU_ATTR_WEAK bool dcd_deinit(uint8_t rhport) {
8896
(void) rhport;
8997
return false;
@@ -561,6 +569,9 @@ bool tud_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
561569

562570
_usbd_rhport = rhport;
563571

572+
// MCU specific pre-init
573+
tud_pre_init_cb(rhport);
574+
564575
// Init device controller driver
565576
TU_ASSERT(dcd_init(rhport, rh_init));
566577
dcd_int_enable(rhport);
@@ -580,6 +591,9 @@ bool tud_deinit(uint8_t rhport) {
580591
dcd_disconnect(rhport);
581592
TU_ASSERT(dcd_deinit(rhport));
582593

594+
// MCU specific post-deinit
595+
tud_post_deinit_cb(rhport);
596+
583597
// Deinit class drivers
584598
for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) {
585599
usbd_class_driver_t const* driver = get_driver(i);

src/device/usbd.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ void tud_sof_cb(uint32_t frame_count);
175175
// Invoked when received control request with VENDOR TYPE
176176
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
177177

178+
// Invoked before tud_init() to allow MCU specific pre-initialization
179+
void tud_pre_init_cb(uint8_t rhport);
180+
181+
// Invoked after tud_deinit() to allow MCU specific post-de-initialization
182+
void tud_post_deinit_cb(uint8_t rhport);
183+
178184
//--------------------------------------------------------------------+
179185
// Binary Device Object Store (BOS) Descriptor Templates
180186
//--------------------------------------------------------------------+

src/host/usbh.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr) {
101101
(void) daddr;
102102
}
103103

104+
TU_ATTR_WEAK void tuh_pre_init_cb(uint8_t rhport) {
105+
(void) rhport;
106+
}
107+
108+
TU_ATTR_WEAK void tuh_post_deinit_cb(uint8_t rhport) {
109+
(void) rhport;
110+
}
104111
//--------------------------------------------------------------------+
105112
// Data Structure
106113
//--------------------------------------------------------------------+
@@ -522,6 +529,9 @@ bool tuh_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
522529
}
523530
}
524531

532+
// MCU specific pre-init
533+
tuh_pre_init_cb(rhport);
534+
525535
// Init host controller
526536
_usbh_data.controller_id = rhport;
527537
TU_ASSERT(hcd_init(rhport, rh_init));
@@ -540,6 +550,9 @@ bool tuh_deinit(uint8_t rhport) {
540550
TU_ASSERT(hcd_deinit(rhport));
541551
_usbh_data.controller_id = TUSB_INDEX_INVALID_8;
542552

553+
// MCU specific post-deinit
554+
tuh_post_deinit_cb(rhport);
555+
543556
// "unplug" all devices on this rhport (hub_addr = 0, hub_port = 0)
544557
process_removed_device(rhport, 0, 0);
545558

src/host/usbh.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ void tuh_umount_cb(uint8_t daddr);
134134
// Invoked when there is a new usb event, which need to be processed by tuh_task()/tuh_task_ext()
135135
void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr);
136136

137+
// Invoked before tuh_init() to allow MCU specific pre-initialization
138+
void tuh_pre_init_cb(uint8_t rhport);
139+
140+
// Invoked after tuh_deinit() to allow MCU specific post de-initialization
141+
void tuh_post_deinit_cb(uint8_t rhport);
142+
137143
//--------------------------------------------------------------------+
138144
// APPLICATION API
139145
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)