Skip to content
Open
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
1 change: 1 addition & 0 deletions src/portable/raspberrypi/rp2040/dcd_rp2040.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ static void hw_endpoint_init(hw_endpoint_t *ep, uint8_t ep_addr, uint16_t wMaxPa
hard_assert(hw_buffer_ptr < usb_dpram->epx_data + sizeof(usb_dpram->epx_data));
pico_info(" Allocated %d bytes (0x%p)\r\n", size, ep->hw_data_buf);
}
ep->configured = true;
}

static void hw_endpoint_enable(hw_endpoint_t *ep, uint8_t transfer_type) {
Expand Down
6 changes: 6 additions & 0 deletions src/portable/raspberrypi/rp2040/rp2040_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint* ep)
void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, tu_fifo_t *ff, uint16_t total_len) {
hw_endpoint_lock_update(ep, 1);

// We need to make sure the ep didn't get cleared from under us by an IRQ
if (!ep->configured) {
hw_endpoint_lock_update(ep, -1);
return;
}

if (ep->active) {
// TODO: Is this acceptable for interrupt packets?
TU_LOG(1, "WARN: starting new transfer on already active ep %02X\r\n", ep->ep_addr);
Expand Down
21 changes: 16 additions & 5 deletions src/portable/raspberrypi/rp2040/rp2040_usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RP2040_COMMON_H_

#include "pico.h"
#include "pico/critical_section.h"
#include "hardware/structs/usb.h"
#include "hardware/irq.h"
#include "hardware/resets.h"
Expand Down Expand Up @@ -72,8 +73,8 @@ typedef struct hw_endpoint {
uint8_t pending; // Transfer scheduled but not active
#endif

#if CFG_TUH_ENABLED
bool configured; // Is this a valid struct
#if CFG_TUH_ENABLED
uint8_t dev_addr;
uint8_t interrupt_num; // for host interrupt endpoints
#endif
Expand Down Expand Up @@ -107,10 +108,20 @@ bool hw_endpoint_xfer_continue(struct hw_endpoint *ep);
void hw_endpoint_reset_transfer(struct hw_endpoint *ep);
void hw_endpoint_start_next_buffer(struct hw_endpoint *ep);

TU_ATTR_ALWAYS_INLINE static inline void hw_endpoint_lock_update(__unused struct hw_endpoint * ep, __unused int delta) {
// todo add critsec as necessary to prevent issues between worker and IRQ...
// note that this is perhaps as simple as disabling IRQs because it would make
// sense to have worker and IRQ on same core, however I think using critsec is about equivalent.
TU_ATTR_ALWAYS_INLINE static inline void hw_endpoint_lock_update(__unused struct hw_endpoint * ep, int delta) {
static critical_section_t hw_endpoint_crit_sec;
static int hw_endpoint_crit_sec_ref = 0;
if ( !critical_section_is_initialized(&hw_endpoint_crit_sec) ) {
critical_section_init(&hw_endpoint_crit_sec);
}

if ( delta > 0 && !hw_endpoint_crit_sec_ref ) {
critical_section_enter_blocking(&hw_endpoint_crit_sec);
}
hw_endpoint_crit_sec_ref += delta;
if ( hw_endpoint_crit_sec_ref == 0 ) {
critical_section_exit(&hw_endpoint_crit_sec);
}
}

// #if CFG_TUD_ENABLED
Expand Down