Skip to content

Commit

Permalink
Some handover fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Nov 24, 2016
1 parent d54cff5 commit b747a81
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target extended-remote localhost:3333
1 change: 1 addition & 0 deletions inc/cdc_enumerate.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ typedef struct {

uint32_t USB_Read(void *pData, uint32_t length, uint32_t ep);
uint32_t USB_Write(const void *pData, uint32_t length, uint8_t ep_num);
uint32_t USB_WriteCore(const void *pData, uint32_t length, uint8_t ep_num, bool handoverMode);
void USB_ReadBlocking(void *dst, uint32_t length, uint32_t ep, PacketBuffer *cache);
uint32_t USB_ReadCore(void *pData, uint32_t length, uint32_t ep, PacketBuffer *cache);
bool USB_Ok(void);
Expand Down
11 changes: 9 additions & 2 deletions src/cdc_enumerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,20 +443,27 @@ void USB_ReadBlocking(void *dst, uint32_t length, uint32_t ep, PacketBuffer *cac
}

uint32_t USB_Write(const void *pData, uint32_t length, uint8_t ep_num) {
return USB_WriteCore(pData, length, ep_num, false);
}

uint32_t USB_WriteCore(const void *pData, uint32_t length, uint8_t ep_num, bool handoverMode) {
Usb *pUsb = pCdc.pUsb;
uint32_t data_address;

UsbDeviceDescriptor *epdesc = (UsbDeviceDescriptor *)USB->HOST.DESCADD.reg + ep_num;

/* Check for requirement for multi-packet or auto zlp */
if (length >= (1 << (epdesc->DeviceDescBank[1].PCKSIZE.bit.SIZE + 3))) {
assert(!handoverMode);
/* Update the EP data address */
data_address = (uint32_t)pData;
// data must be in RAM!
assert(data_address >= HMCRAMC0_ADDR);

// always disable AUTO_ZLP on MSC channel, otherwise enable
epdesc->DeviceDescBank[1].PCKSIZE.bit.AUTO_ZLP = ep_num == USB_EP_MSC_IN ? false : true;
} else if (handoverMode) {
data_address = (uint32_t)pData;
} else {
/* Copy to local buffer */
memcpy(endpointCache[ep_num].buf, pData, length);
Expand All @@ -478,8 +485,8 @@ uint32_t USB_Write(const void *pData, uint32_t length, uint8_t ep_num) {

/* Wait for transfer to complete */
while (!(pUsb->DEVICE.DeviceEndpoint[ep_num].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT1)) {
if (ep_num && !USB_Ok())
return -1;
// if (ep_num && !USB_Ok())
// return -1;
}

return length;
Expand Down
15 changes: 12 additions & 3 deletions src/msc.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,12 @@ static void udi_msc_sbc_trans(bool b_read) {
USB_Write(block_buffer, UDI_MSC_BLOCK_SIZE, USB_EP_MSC_IN);
} else {
USB_ReadBlocking(block_buffer, UDI_MSC_BLOCK_SIZE, USB_EP_MSC_OUT, 0);

#if 0
check_uf2_handover(block_buffer, udi_msc_nb_block - i - 1, USB_EP_MSC_IN,
USB_EP_MSC_OUT, udi_msc_cbw.dCBWTag);
#endif

write_block(udi_msc_addr + i, block_buffer, false);
}
udi_msc_csw.dCSWDataResidue -= UDI_MSC_BLOCK_SIZE;
Expand Down Expand Up @@ -724,7 +730,7 @@ static void process_handover_initial(UF2_HandoverArgs *handover, PacketBuffer *h
// read-write remaining blocks
handover_flash(handover, handoverCache);
// send USB response, as the user space isn't gonna do it
USB_Write((void *)&csw, sizeof(csw), handover->ep_in);
USB_WriteCore((void *)&csw, sizeof(csw), handover->ep_in, true);
}

static void process_handover(UF2_HandoverArgs *handover, PacketBuffer *handoverCache) {
Expand Down Expand Up @@ -761,15 +767,18 @@ static void process_handover(UF2_HandoverArgs *handover, PacketBuffer *handoverC
break;
}

USB_Write((void *)&csw, sizeof(csw), handover->ep_in);
USB_WriteCore((void *)&csw, sizeof(csw), handover->ep_in, true);
}

static void handover(UF2_HandoverArgs *args) {
// reset interrupt vectors, so that we're not disturbed by
// interrupt handlers from user space
SCB->VTOR = 0;

PacketBuffer cache;
USB->DEVICE.INTENCLR.reg = USB_DEVICE_INTENCLR_MASK;
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_MASK;

PacketBuffer cache = {0};

// They may have 0x80 bit set
args->ep_in &= 0xf;
Expand Down

0 comments on commit b747a81

Please sign in to comment.