Skip to content

Commit

Permalink
Merge pull request #1835 from MasterQ32/otg_bringup
Browse files Browse the repository at this point in the history
Implements deinit functions for host/device mode switch
  • Loading branch information
hathach authored Apr 8, 2024
2 parents d33fe38 + be25a3f commit 42decf2
Show file tree
Hide file tree
Showing 33 changed files with 312 additions and 102 deletions.
4 changes: 4 additions & 0 deletions src/class/audio/audio_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,10 @@ void audiod_init(void)
}
}

bool audiod_deinit(void) {
return false; // TODO not implemented yet
}

void audiod_reset(uint8_t rhport)
{
(void) rhport;
Expand Down
1 change: 1 addition & 0 deletions src/class/audio/audio_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ static inline bool tud_audio_fb_set(uint32_t feedback)
// Internal Class Driver API
//--------------------------------------------------------------------+
void audiod_init (void);
bool audiod_deinit (void);
void audiod_reset (uint8_t rhport);
uint16_t audiod_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool audiod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
Expand Down
7 changes: 5 additions & 2 deletions src/class/bth/bth_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ bool tud_bt_acl_data_send(void *event, uint16_t event_len)
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void btd_init(void)
{
void btd_init(void) {
tu_memclr(&_btd_itf, sizeof(_btd_itf));
}

bool btd_deinit(void) {
return true;
}

void btd_reset(uint8_t rhport)
{
(void)rhport;
Expand Down
1 change: 1 addition & 0 deletions src/class/bth/bth_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ bool tud_bt_acl_data_send(void *acl_data, uint16_t data_len);
// Internal Class Driver API
//--------------------------------------------------------------------+
void btd_init (void);
bool btd_deinit (void);
void btd_reset (uint8_t rhport);
uint16_t btd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool btd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const *request);
Expand Down
32 changes: 30 additions & 2 deletions src/class/cdc/cdc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,39 @@ void cdcd_init(void)
// In this way, the most current data is prioritized.
tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true);

tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, osal_mutex_create(&p_cdc->rx_ff_mutex));
tu_fifo_config_mutex(&p_cdc->tx_ff, osal_mutex_create(&p_cdc->tx_ff_mutex), NULL);
#if OSAL_MUTEX_REQUIRED
osal_mutex_t mutex_rd = osal_mutex_create(&p_cdc->rx_ff_mutex);
osal_mutex_t mutex_wr = osal_mutex_create(&p_cdc->tx_ff_mutex);
TU_ASSERT(mutex_rd != NULL && mutex_wr != NULL, );

tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, mutex_rd);
tu_fifo_config_mutex(&p_cdc->tx_ff, mutex_wr, NULL);
#endif
}
}

bool cdcd_deinit(void) {
#if OSAL_MUTEX_REQUIRED
for(uint8_t i=0; i<CFG_TUD_CDC; i++) {
cdcd_interface_t* p_cdc = &_cdcd_itf[i];
osal_mutex_t mutex_rd = p_cdc->rx_ff.mutex_rd;
osal_mutex_t mutex_wr = p_cdc->tx_ff.mutex_wr;

if (mutex_rd) {
osal_mutex_delete(mutex_rd);
tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, NULL);
}

if (mutex_wr) {
osal_mutex_delete(mutex_wr);
tu_fifo_config_mutex(&p_cdc->tx_ff, NULL, NULL);
}
}
#endif

return true;
}

void cdcd_reset(uint8_t rhport)
{
(void) rhport;
Expand Down
1 change: 1 addition & 0 deletions src/class/cdc/cdc_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ static inline bool tud_cdc_write_clear(void)
// INTERNAL USBD-CLASS DRIVER API
//--------------------------------------------------------------------+
void cdcd_init (void);
bool cdcd_deinit (void);
void cdcd_reset (uint8_t rhport);
uint16_t cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool cdcd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
Expand Down
7 changes: 5 additions & 2 deletions src/class/dfu/dfu_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,14 @@ void dfu_moded_reset(uint8_t rhport)
reset_state();
}

void dfu_moded_init(void)
{
void dfu_moded_init(void) {
dfu_moded_reset(0);
}

bool dfu_moded_deinit(void) {
return true;
}

uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
{
(void) rhport;
Expand Down
1 change: 1 addition & 0 deletions src/class/dfu/dfu_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ TU_ATTR_WEAK void tud_dfu_abort_cb(uint8_t alt);
// Internal Class Driver API
//--------------------------------------------------------------------+
void dfu_moded_init(void);
bool dfu_moded_deinit(void);
void dfu_moded_reset(uint8_t rhport);
uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
Expand Down
7 changes: 5 additions & 2 deletions src/class/dfu/dfu_rt_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void dfu_rtd_init(void)
{
void dfu_rtd_init(void) {
}

bool dfu_rtd_deinit(void) {
return true;
}

void dfu_rtd_reset(uint8_t rhport)
Expand Down
1 change: 1 addition & 0 deletions src/class/dfu/dfu_rt_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void tud_dfu_runtime_reboot_to_dfu_cb(void);
// Internal Class Driver API
//--------------------------------------------------------------------+
void dfu_rtd_init(void);
bool dfu_rtd_deinit(void);
void dfu_rtd_reset(uint8_t rhport);
uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
Expand Down
7 changes: 5 additions & 2 deletions src/class/hid/hid_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,14 @@ bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id,
//--------------------------------------------------------------------+
// USBD-CLASS API
//--------------------------------------------------------------------+
void hidd_init(void)
{
void hidd_init(void) {
hidd_reset(0);
}

bool hidd_deinit(void) {
return true;
}

void hidd_reset(uint8_t rhport)
{
(void) rhport;
Expand Down
1 change: 1 addition & 0 deletions src/class/hid/hid_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y
// Internal Class Driver API
//--------------------------------------------------------------------+
void hidd_init (void);
bool hidd_deinit (void);
void hidd_reset (uint8_t rhport);
uint16_t hidd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
Expand Down
36 changes: 30 additions & 6 deletions src/class/midi/midi_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,25 +373,49 @@ bool tud_midi_n_packet_write (uint8_t itf, uint8_t const packet[4])
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void midid_init(void)
{
void midid_init(void) {
tu_memclr(_midid_itf, sizeof(_midid_itf));

for(uint8_t i=0; i<CFG_TUD_MIDI; i++)
{
for (uint8_t i = 0; i < CFG_TUD_MIDI; i++) {
midid_interface_t* midi = &_midid_itf[i];

// config fifo
tu_fifo_config(&midi->rx_ff, midi->rx_ff_buf, CFG_TUD_MIDI_RX_BUFSIZE, 1, false); // true, true
tu_fifo_config(&midi->tx_ff, midi->tx_ff_buf, CFG_TUD_MIDI_TX_BUFSIZE, 1, false); // OBVS.

#if CFG_FIFO_MUTEX
tu_fifo_config_mutex(&midi->rx_ff, NULL, osal_mutex_create(&midi->rx_ff_mutex));
tu_fifo_config_mutex(&midi->tx_ff, osal_mutex_create(&midi->tx_ff_mutex), NULL);
osal_mutex_t mutex_rd = osal_mutex_create(&midi->rx_ff_mutex);
osal_mutex_t mutex_wr = osal_mutex_create(&midi->tx_ff_mutex);
TU_ASSERT(mutex_wr != NULL && mutex_wr != NULL, );

tu_fifo_config_mutex(&midi->rx_ff, NULL, mutex_rd);
tu_fifo_config_mutex(&midi->tx_ff, mutex_wr, NULL);
#endif
}
}

bool midid_deinit(void) {
#if CFG_FIFO_MUTEX
for(uint8_t i=0; i<CFG_TUD_MIDI; i++) {
midid_interface_t* midi = &_midid_itf[i];
osal_mutex_t mutex_rd = midi->rx_ff.mutex_rd;
osal_mutex_t mutex_wr = midi->tx_ff.mutex_wr;

if (mutex_rd) {
osal_mutex_delete(mutex_rd);
tu_fifo_config_mutex(&midi->rx_ff, NULL, NULL);
}

if (mutex_wr) {
osal_mutex_delete(mutex_wr);
tu_fifo_config_mutex(&midi->tx_ff, NULL, NULL);
}
}
#endif

return true;
}

void midid_reset(uint8_t rhport)
{
(void) rhport;
Expand Down
1 change: 1 addition & 0 deletions src/class/midi/midi_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ static inline bool tud_midi_packet_write (uint8_t const packet[4])
// Internal Class Driver API
//--------------------------------------------------------------------+
void midid_init (void);
bool midid_deinit (void);
void midid_reset (uint8_t rhport);
uint16_t midid_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool midid_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
Expand Down
8 changes: 6 additions & 2 deletions src/class/msc/msc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,15 @@ static inline void set_sense_medium_not_present(uint8_t lun)
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void mscd_init(void)
{
void mscd_init(void) {
tu_memclr(&_mscd_itf, sizeof(mscd_interface_t));
}

bool mscd_deinit(void) {
// nothing to do
return true;
}

void mscd_reset(uint8_t rhport)
{
(void) rhport;
Expand Down
1 change: 1 addition & 0 deletions src/class/msc/msc_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun);
// Internal Class Driver API
//--------------------------------------------------------------------+
void mscd_init (void);
bool mscd_deinit (void);
void mscd_reset (uint8_t rhport);
uint16_t mscd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool mscd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * p_request);
Expand Down
7 changes: 5 additions & 2 deletions src/class/net/ecm_rndis_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,14 @@ void netd_report(uint8_t *buf, uint16_t len)
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void netd_init(void)
{
void netd_init(void) {
tu_memclr(&_netd_itf, sizeof(_netd_itf));
}

bool netd_deinit(void) {
return true;
}

void netd_reset(uint8_t rhport)
{
(void) rhport;
Expand Down
4 changes: 4 additions & 0 deletions src/class/net/ncm_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ void netd_init(void)
ncm_prepare_for_tx();
}

bool netd_deinit(void) {
return true;
}

void netd_reset(uint8_t rhport)
{
(void) rhport;
Expand Down
1 change: 1 addition & 0 deletions src/class/net/net_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void tud_network_link_state_cb(bool state);
// INTERNAL USBD-CLASS DRIVER API
//--------------------------------------------------------------------+
void netd_init (void);
bool netd_deinit (void);
void netd_reset (uint8_t rhport);
uint16_t netd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
Expand Down
7 changes: 7 additions & 0 deletions src/class/usbtmc/usbtmc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ void usbtmcd_init_cb(void)
usbtmcLock = osal_mutex_create(&usbtmcLockBuffer);
}

bool usbtmcd_deinit(void) {
#if OSAL_MUTEX_REQUIRED
osal_mutex_delete(usbtmcLock);
#endif
return true;
}

uint16_t usbtmcd_open_cb(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
{
(void)rhport;
Expand Down
3 changes: 2 additions & 1 deletion src/class/usbtmc/usbtmc_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ bool tud_usbtmc_start_bus_read(void);

/* "callbacks" from USB device core */

void usbtmcd_init_cb(void);
bool usbtmcd_deinit(void);
uint16_t usbtmcd_open_cb(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
void usbtmcd_reset_cb(uint8_t rhport);
bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
bool usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
void usbtmcd_init_cb(void);

/************************************************************
* USBTMC Descriptor Templates
Expand Down
38 changes: 32 additions & 6 deletions src/class/vendor/vendor_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,47 @@ uint32_t tud_vendor_n_write_available (uint8_t itf)
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void vendord_init(void)
{
void vendord_init(void) {
tu_memclr(_vendord_itf, sizeof(_vendord_itf));

for(uint8_t i=0; i<CFG_TUD_VENDOR; i++)
{
for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) {
vendord_interface_t* p_itf = &_vendord_itf[i];

// config fifo
tu_fifo_config(&p_itf->rx_ff, p_itf->rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE, 1, false);
tu_fifo_config(&p_itf->tx_ff, p_itf->tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE, 1, false);

tu_fifo_config_mutex(&p_itf->rx_ff, NULL, osal_mutex_create(&p_itf->rx_ff_mutex));
tu_fifo_config_mutex(&p_itf->tx_ff, osal_mutex_create(&p_itf->tx_ff_mutex), NULL);
#if OSAL_MUTEX_REQUIRED
osal_mutex_t mutex_rd = osal_mutex_create(&p_itf->rx_ff_mutex);
osal_mutex_t mutex_wr = osal_mutex_create(&p_itf->tx_ff_mutex);
TU_ASSERT(mutex_rd && mutex_wr,);

tu_fifo_config_mutex(&p_itf->rx_ff, NULL, mutex_rd);
tu_fifo_config_mutex(&p_itf->tx_ff, mutex_wr, NULL);
#endif
}
}

bool vendord_deinit(void) {
#if OSAL_MUTEX_REQUIRED
for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) {
vendord_interface_t* p_itf = &_vendord_itf[i];
osal_mutex_t mutex_rd = p_itf->rx_ff.mutex_rd;
osal_mutex_t mutex_wr = p_itf->tx_ff.mutex_wr;

if (mutex_rd) {
osal_mutex_delete(mutex_rd);
tu_fifo_config_mutex(&p_itf->rx_ff, NULL, NULL);
}

if (mutex_wr) {
osal_mutex_delete(mutex_wr);
tu_fifo_config_mutex(&p_itf->tx_ff, NULL, NULL);
}
}
#endif

return true;
}

void vendord_reset(uint8_t rhport)
Expand Down
1 change: 1 addition & 0 deletions src/class/vendor/vendor_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ static inline uint32_t tud_vendor_write_available (void)
// Internal Class Driver API
//--------------------------------------------------------------------+
void vendord_init(void);
bool vendord_deinit(void);
void vendord_reset(uint8_t rhport);
uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
Expand Down
4 changes: 4 additions & 0 deletions src/class/video/video_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,10 @@ void videod_init(void) {
}
}

bool videod_deinit(void) {
return true;
}

void videod_reset(uint8_t rhport) {
(void) rhport;
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO; ++i) {
Expand Down
Loading

0 comments on commit 42decf2

Please sign in to comment.