Skip to content

Commit ac16594

Browse files
committed
Disabled SRAM shortcut when in USBCore::send
The shortcut has some issues: - sometimes it fails when sending an odd number of bytes (may be due to memory alignment?) - the data pointer should point to "stable" data (and this is not guaranteed by caller, it may be some sort of temporary buffer) - the SRAM is not guaranteed to start at 0x20000000 All the above problems must be properly fixed before reenabling this part
1 parent 45d787d commit ac16594

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

Diff for: cores/arduino/USB/USBCore.cpp

+33-24
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,17 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
565565
if (len > 16384)
566566
return -1;
567567

568+
#if 0
569+
// This shortcut has some issues:
570+
// - sometimes it fails when sending an odd number of bytes (may be
571+
// due to memory alignment?)
572+
// - the data pointer should point to "stable" data (and this is not
573+
// guaranteed by caller, it may be some sort of temporary buffer)
574+
// - the SRAM is not guaranteed to start at 0x20000000
575+
576+
// All the above problems must be properly fixed before reenabling
577+
// this part
578+
568579
if ((unsigned int)data > 0x20000000)
569580
{
570581
// Buffer in RAM
@@ -583,39 +594,37 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
583594
while (!usbd.epBank1IsTransferComplete(ep)) {
584595
; // need fire exit.
585596
}
586-
len = 0;
597+
return 0;
587598
}
588-
else
599+
#endif
600+
601+
// Flash area
602+
while (len != 0)
589603
{
590-
// Flash area
591-
while (len != 0)
592-
{
593-
if (len >= 64) {
594-
length = 64;
595-
} else {
596-
length = len;
597-
}
604+
if (len >= 64) {
605+
length = 64;
606+
} else {
607+
length = len;
608+
}
598609

599-
/* memcopy could be safer in multi threaded environment */
600-
memcpy(&udd_ep_in_cache_buffer[ep], data, length);
610+
/* memcopy could be safer in multi threaded environment */
611+
memcpy(&udd_ep_in_cache_buffer[ep], data, length);
601612

602-
usbd.epBank1SetAddress(ep, &udd_ep_in_cache_buffer[ep]);
603-
usbd.epBank1SetByteCount(ep, length);
613+
usbd.epBank1SetAddress(ep, &udd_ep_in_cache_buffer[ep]);
614+
usbd.epBank1SetByteCount(ep, length);
604615

605-
// Clear the transfer complete flag
606-
usbd.epBank1AckTransferComplete(ep);
616+
// Clear the transfer complete flag
617+
usbd.epBank1AckTransferComplete(ep);
607618

608-
// RAM buffer is full, we can send data (IN)
609-
usbd.epBank1SetReady(ep);
619+
// RAM buffer is full, we can send data (IN)
620+
usbd.epBank1SetReady(ep);
610621

611-
// Wait for transfer to complete
612-
while (!usbd.epBank1IsTransferComplete(ep)) {
613-
; // need fire exit.
614-
}
615-
len -= length;
622+
// Wait for transfer to complete
623+
while (!usbd.epBank1IsTransferComplete(ep)) {
624+
; // need fire exit.
616625
}
626+
len -= length;
617627
}
618-
619628
return len;
620629
}
621630

0 commit comments

Comments
 (0)