Skip to content

A fix for the SerialUSB buffer overflow bug. #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 25 additions & 5 deletions cores/arduino/USB/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,32 @@ uint32_t USBD_Send(uint32_t ep, const void* d, uint32_t len)
int r = len;
const uint8_t* data = (const uint8_t*)d;

if (!_usbConfiguration)
{
TRACE_CORE(printf("pb conf\n\r");)
if (!_usbConfiguration)
{
TRACE_CORE(printf("pb conf\n\r");)
return -1;
}
UDD_Send(ep, data, len);
}

size_t i = 0;
// Ensure the chunk size is aligned 4
size_t chunkSize = UDD_IN_CACHE_SIZE -(UDD_IN_CACHE_SIZE % 4);

// Send the full chunks
for (i = 0; i < (len / chunkSize); i++)
{
UDD_Send(ep, data + (i*chunkSize), chunkSize);

/* Clear the transfer complete flag */
udd_clear_IN_transf_cplt(ep);
/* Set the bank as ready */
udd_IN_transfer_allowed(ep);

/* Wait for transfer to complete */
while (!udd_is_IN_transf_cplt(ep)); // need fire exit.
}

// Send the remainder
UDD_Send(ep, data + (i * chunkSize), len % chunkSize);

/* Clear the transfer complete flag */
udd_clear_IN_transf_cplt(ep);
Expand Down
4 changes: 2 additions & 2 deletions cores/arduino/USB/samd21_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ extern "C"{
//#define TRACE_DEVICE(x) x
#define TRACE_DEVICE(x)

__attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/ uint8_t udd_ep_out_cache_buffer[4][64];
__attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/ uint8_t udd_ep_in_cache_buffer[4][128];
__attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/ uint8_t udd_ep_out_cache_buffer[4][UDD_OUT_CACHE_SIZE];
__attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/ uint8_t udd_ep_in_cache_buffer[4][UDD_IN_CACHE_SIZE];

/**
* USB SRAM data containing pipe descriptor table
Expand Down
3 changes: 3 additions & 0 deletions cores/arduino/USB/samd21_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ extern "C" {
#define EP0 0
#define EPX_SIZE 64// 64 for Full Speed, EPT size max is 1024

#define UDD_OUT_CACHE_SIZE 64
#define UDD_IN_CACHE_SIZE 128

// Force device low speed mode
#define udd_force_low_speed() USB->DEVICE.CTRLB.reg &= ~USB_DEVICE_CTRLB_SPDCONF_Msk; USB->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_SPDCONF_1_Val
// Force full speed mode
Expand Down