Skip to content

Commit

Permalink
Merge pull request #77 from c1728p9/minor_fixes_and_updates
Browse files Browse the repository at this point in the history
Minor fixes and updates
  • Loading branch information
c1728p9 committed Mar 22, 2016
2 parents 8e14abb + 5be81c5 commit 82eef63
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 32 deletions.
55 changes: 41 additions & 14 deletions source/daplink/drag-n-drop/virtual_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static void file_change_cb_stub(const vfs_filename_t filename, vfs_file_change_t
static uint32_t cluster_to_sector(uint32_t cluster_idx);
static bool filename_valid(const vfs_filename_t filename);
static bool filename_character_valid(char character);
static void set_init_done(void);

// If sector size changes update comment below
COMPILER_ASSERT(0x0200 == VFS_SECTOR_SIZE);
Expand Down Expand Up @@ -233,13 +234,15 @@ static const FatDirectoryEntry_t dir_entry_tmpl = {
mbr_t mbr;
file_allocation_table_t fat;
virtual_media_t virtual_media[16];
root_dir_t dir;
root_dir_t dir_current;
FatDirectoryEntry_t dir_initial[VFS_MAX_FILES];
uint8_t file_count;
vfs_file_change_cb_t file_change_cb;
uint32_t virtual_media_idx;
uint32_t fat_idx;
uint32_t dir_idx;
uint32_t data_start;
bool init_complete;

// Virtual media must be larger than the template
COMPILER_ASSERT(sizeof(virtual_media) > sizeof(virtual_media_tmpl));
Expand Down Expand Up @@ -282,12 +285,14 @@ void vfs_init(const vfs_filename_t drive_name, uint32_t disk_size)
memset(&fat, 0, sizeof(fat));
fat_idx = 0;
memset(&virtual_media, 0, sizeof(virtual_media));
memset(&dir, 0, sizeof(dir));
memset(&dir_current, 0, sizeof(dir_current));
memset(&dir_initial, 0, sizeof(dir_initial));
dir_idx = 0;
file_count = 0;
file_change_cb = file_change_cb_stub;
virtual_media_idx = 0;
data_start = 0;
init_complete = false;
// Initialize MBR
memcpy(&mbr, &mbr_tmpl, sizeof(mbr_t));
mbr.total_logical_sectors = ((disk_size + KB(64)) / mbr.bytes_per_sector);
Expand All @@ -312,8 +317,8 @@ void vfs_init(const vfs_filename_t drive_name, uint32_t disk_size)
fat_idx++;
// Initialize root dir
dir_idx = 0;
dir.f[dir_idx] = root_dir_entry;
memcpy(dir.f[dir_idx].filename, drive_name, sizeof(dir.f[0].filename));
dir_current.f[dir_idx] = root_dir_entry;
memcpy(dir_current.f[dir_idx].filename, drive_name, sizeof(dir_current.f[0].filename));
dir_idx++;
}

Expand Down Expand Up @@ -349,12 +354,12 @@ vfs_file_t vfs_create_file(const vfs_filename_t filename, vfs_read_cb_t read_cb,
}

// Update directory entry
if (dir_idx >= ELEMENTS_IN_ARRAY(dir.f)) {
if (dir_idx >= ELEMENTS_IN_ARRAY(dir_current.f)) {
util_assert(0);
return 0;
return VFS_FILE_INVALID;
}

de = &dir.f[dir_idx];
de = &dir_current.f[dir_idx];
dir_idx++;
memcpy(de, &dir_entry_tmpl, sizeof(dir_entry_tmpl));
memcpy(de->filename, filename, 11);
Expand All @@ -365,7 +370,7 @@ vfs_file_t vfs_create_file(const vfs_filename_t filename, vfs_read_cb_t read_cb,
// Update virtual media
if (virtual_media_idx >= ELEMENTS_IN_ARRAY(virtual_media)) {
util_assert(0);
return 0;
return VFS_FILE_INVALID;
}

virtual_media[virtual_media_idx].read_cb = read_zero;
Expand Down Expand Up @@ -427,6 +432,8 @@ void vfs_read(uint32_t requested_sector, uint8_t *buf, uint32_t num_sectors)
memset(buf, 0, num_sectors * VFS_SECTOR_SIZE);
current_sector = 0;

set_init_done();

for (i = 0; i < ELEMENTS_IN_ARRAY(virtual_media); i++) {
uint32_t vm_sectors = virtual_media[i].length / VFS_SECTOR_SIZE;
uint32_t vm_start = current_sector;
Expand Down Expand Up @@ -460,6 +467,8 @@ void vfs_write(uint32_t requested_sector, const uint8_t *buf, uint32_t num_secto
uint32_t current_sector;
current_sector = 0;

set_init_done();

for (i = 0; i < virtual_media_idx; i++) {
uint32_t vm_sectors = virtual_media[i].length / VFS_SECTOR_SIZE;
uint32_t vm_start = current_sector;
Expand Down Expand Up @@ -534,15 +543,25 @@ static uint32_t read_fat(uint32_t sector_offset, uint8_t *data, uint32_t num_sec
static uint32_t read_dir(uint32_t sector_offset, uint8_t *data, uint32_t num_sectors)
{
uint32_t start_index;
uint32_t copy_size;

if ((sector_offset + num_sectors) * VFS_SECTOR_SIZE > sizeof(dir)) {
if ((sector_offset + num_sectors) * VFS_SECTOR_SIZE > sizeof(dir_current)) {
// Trying to read too much of the root directory
util_assert(0);
return 0;
}

// Zero buffer
memset(data, 0, num_sectors * VFS_SECTOR_SIZE);
start_index = sector_offset * VFS_SECTOR_SIZE / sizeof(FatDirectoryEntry_t);
memcpy(data, &dir.f[start_index], num_sectors * VFS_SECTOR_SIZE);

// Copy data if anything can be copied
if (start_index < ELEMENTS_IN_ARRAY(dir_initial)) {
util_assert(sizeof(dir_initial) > sector_offset * VFS_SECTOR_SIZE);
copy_size = sizeof(dir_initial) - sector_offset * VFS_SECTOR_SIZE;
memcpy(data, &dir_initial[start_index], copy_size);
}

return num_sectors * VFS_SECTOR_SIZE;
}

Expand All @@ -554,15 +573,15 @@ static void write_dir(uint32_t sector_offset, const uint8_t *data, uint32_t num_
uint32_t num_entries;
uint32_t i;

if ((sector_offset + num_sectors) * VFS_SECTOR_SIZE > sizeof(dir)) {
if ((sector_offset + num_sectors) * VFS_SECTOR_SIZE > sizeof(dir_current)) {
// Trying to write too much of the root directory
util_assert(0);
return;
}

start_index = sector_offset * VFS_SECTOR_SIZE / sizeof(FatDirectoryEntry_t);
num_entries = num_sectors * VFS_SECTOR_SIZE / sizeof(FatDirectoryEntry_t);
old_entry = &dir.f[start_index];
old_entry = &dir_current.f[start_index];
new_entry = (FatDirectoryEntry_t *)data;
// If this is the first sector start at index 1 to get past drive name
i = 0 == sector_offset ? 1 : 0;
Expand Down Expand Up @@ -592,7 +611,7 @@ static void write_dir(uint32_t sector_offset, const uint8_t *data, uint32_t num_
}
}

memcpy(&dir.f[start_index], data, num_sectors * VFS_SECTOR_SIZE);
memcpy(&dir_current.f[start_index], data, num_sectors * VFS_SECTOR_SIZE);
}

static void file_change_cb_stub(const vfs_filename_t filename, vfs_file_change_t change, vfs_file_t file, vfs_file_t new_file_data)
Expand Down Expand Up @@ -629,7 +648,7 @@ static bool filename_valid(const vfs_filename_t filename)
}

// Make sure all the characters are valid
for (i = 0; i < sizeof(filename); i++) {
for (i = 0; i < sizeof(vfs_filename_t); i++) {
if (!filename_character_valid(filename[i])) {
return false;
}
Expand Down Expand Up @@ -664,3 +683,11 @@ static bool filename_character_valid(char character)
// All of the checks have passed so this is a valid file name character
return true;
}

static void set_init_done(void)
{
if (!init_complete) {
memcpy(&dir_initial, &dir_current, MIN(sizeof(dir_initial), sizeof(dir_current)));
init_complete = true;
}
}
1 change: 1 addition & 0 deletions source/daplink/drag-n-drop/virtual_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {
#define VFS_SECTOR_SIZE 512
#define VFS_INVALID_SECTOR 0xFFFFFFFF
#define VFS_FILE_INVALID 0
#define VFS_MAX_FILES 16

typedef char vfs_filename_t[11];

Expand Down
13 changes: 2 additions & 11 deletions source/hic_hal/atmel/sam3u2c/usbd_ATSAM3U2C.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ void UDPHS_IRQHandler(void)

void USBD_Handler(void)
{
uint32_t intsta, eptsta, n, i;
uint32_t intsta, eptsta, n;
intsta = UDPHS->UDPHS_INTSTA & UDPHS->UDPHS_IEN;

/* End of Bus Reset Interrupt */
Expand Down Expand Up @@ -680,16 +680,7 @@ void USBD_Handler(void)
}

/* Endpoint Interrupts */
for (i = 0; i <= USBD_EP_NUM; i++) {
// Process endpoint 0 last if multiple events are pending at the same time.
// This allows other endpoints to process their data before potentially
// executing a command sent on endpoint 0.
// This fixes a bug that occurs when an MSD SCSI command is aborted
// by sending MSC_REQUEST_RESET on the control endpoint.
// If there is unprocessed BULK OUT data at the same time this command is sent
// then the USB state will be reset to MSC_BS_CBW causing an error when the
// previous BULK OUT data is processed.
n = i == USBD_EP_NUM ? 0 : i + 1;
for (n = 0; n <= USBD_EP_NUM; n++) {
if (intsta & (1 << (n + 8))) {
eptsta = UDPHS->UDPHS_EPT[n].UDPHS_EPTSTA; /* Read EP status */

Expand Down
4 changes: 2 additions & 2 deletions source/hic_hal/nxp/lpc11u35/usb_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@
#define USBD_CDC_ACM_HS_BINTERVAL1 0
#define USBD_CDC_ACM_CIF_STRDESC L"mbed Serial Port"
#define USBD_CDC_ACM_DIF_STRDESC L"mbed Serial Port"
#define USBD_CDC_ACM_SENDBUF_SIZE 256
#define USBD_CDC_ACM_RECEIVEBUF_SIZE 256
#define USBD_CDC_ACM_SENDBUF_SIZE 64
#define USBD_CDC_ACM_RECEIVEBUF_SIZE 64
#if (((USBD_CDC_ACM_HS_ENABLE1) && (USBD_CDC_ACM_SENDBUF_SIZE < USBD_CDC_ACM_HS_WMAXPACKETSIZE1)) || (USBD_CDC_ACM_SENDBUF_SIZE < USBD_CDC_ACM_WMAXPACKETSIZE1))
#error "Send Buffer size must be larger or equal to Bulk In maximum packet size!"
#endif
Expand Down
21 changes: 16 additions & 5 deletions source/usb/msc/usbd_msc.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ BOOL USBD_MSC_Reset(void)
{
USBD_EndPointStall = 0x00000000; /* EP must stay stalled */
USBD_MSC_CSW.dSignature = 0; /* invalid signature */
BulkStage = MSC_BS_CBW;
BulkStage = MSC_BS_RESET;
return (__TRUE);
}

Expand Down Expand Up @@ -221,9 +221,6 @@ void USBD_MSC_MemoryRead(void)

if (Length == 0) {
BulkStage = MSC_BS_DATA_IN_LAST;
}

if (BulkStage != MSC_BS_DATA_IN) {
USBD_MSC_CSW.bStatus = CSW_CMD_PASSED;
}
}
Expand Down Expand Up @@ -1045,6 +1042,9 @@ void USBD_MSC_BulkIn(void)
case MSC_BS_CSW:
BulkStage = MSC_BS_CBW;
break;

default:
break;
}
}

Expand Down Expand Up @@ -1073,7 +1073,6 @@ void USBD_MSC_BulkOut(void)
USBD_MSC_MemoryVerify();
break;
}

break;

case MSC_BS_CSW:
Expand All @@ -1082,6 +1081,18 @@ void USBD_MSC_BulkOut(void)
util_assert(0);
break;

case MSC_BS_RESET:
// If Bulk-Only Mass Storage Reset command was received on
// Control Endpoint ignore next Bulk OUT transfer if it was not
// a CBW (as it means it was a unprocessed leftover from
// transfer before reset happened)
BulkStage = MSC_BS_CBW;
if (BulkLen == sizeof(USBD_MSC_CBW)) {
// If it is a CBW size packet go process it as CBW
USBD_MSC_GetCBW();
}
break;

default:
USBD_MSC_SetStallEP(usbd_msc_ep_bulkout);
USBD_MSC_CSW.bStatus = CSW_PHASE_ERROR;
Expand Down
1 change: 1 addition & 0 deletions source/usb/usb_msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#define MSC_BS_DATA_IN_LAST_STALL 4 /* Data In Last Phase with Stall */
#define MSC_BS_CSW 5 /* Command Status Wrapper */
#define MSC_BS_ERROR 6 /* Error */
#define MSC_BS_RESET 7 /* Bulk-Only Mass Storage Reset */


/* Bulk-only Command Block Wrapper */
Expand Down

0 comments on commit 82eef63

Please sign in to comment.