// Handler for file data arriving over USB. This function is responsible // for detecting the start of a BIN/HEX file and performing programming static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_of_sectors) { error_t status; stream_type_t stream; uint32_t size; vfs_user_printf("virtual_fs_user file_data_handler(sector=%i, num_of_sectors=%i)\r\n", sector, num_of_sectors); // this is the key for starting a file write - we dont care what file types are sent // just look for something unique (NVIC table, hex, srec, etc) until root dir is updated if (!file_transfer_state.stream_open) { // look for file types we can program stream = stream_start_identify((uint8_t*)buf, VFS_SECTOR_SIZE * num_of_sectors); if (STREAM_TYPE_NONE != stream) { status = stream_open(stream); vfs_user_printf(" stream_open stream=%i ret %i\r\n", stream, status); transfer_update_stream_open(stream, sector, status); } } // Transfer must have been started for processing beyond this point if (!file_transfer_state.stream_open) { return; } // Transfer must not be finished if (file_transfer_state.transfer_finished) { return; } // Ignore sectors coming before this file if (sector < file_transfer_state.start_sector) { return; } // sectors must be in order if (sector != file_transfer_state.file_next_sector) { vfs_user_printf(" SECTOR OUT OF ORDER\r\n"); return; } size = VFS_SECTOR_SIZE * num_of_sectors; status = stream_write((uint8_t*)buf, size); vfs_user_printf(" stream_write ret %i\r\n", status); transfer_update_stream_data(sector, size, status); }
// Handler for file data arriving over USB. This function is responsible // for detecting the start of a BIN/HEX file and performing programming static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_of_sectors) { stream_type_t stream; uint32_t size; // this is the key for starting a file write - we dont care what file types are sent // just look for something unique (NVIC table, hex, srec, etc) until root dir is updated if (!file_transfer_state.stream_started) { // look for file types we can program stream = stream_start_identify((uint8_t *)buf, VFS_SECTOR_SIZE * num_of_sectors); if (STREAM_TYPE_NONE != stream) { transfer_stream_open(stream, sector); } } if (file_transfer_state.stream_started) { // Ignore sectors coming before this file if (sector < file_transfer_state.start_sector) { return; } // sectors must be in order if (sector != file_transfer_state.file_next_sector) { vfs_mngr_printf("vfs_manager file_data_handler sector=%i\r\n", sector); if (sector < file_transfer_state.file_next_sector) { vfs_mngr_printf(" sector out of order! lowest ooo = %i\r\n", file_transfer_state.last_ooo_sector); if (VFS_INVALID_SECTOR == file_transfer_state.last_ooo_sector) { file_transfer_state.last_ooo_sector = sector; } file_transfer_state.last_ooo_sector = MIN(file_transfer_state.last_ooo_sector, sector); } else { vfs_mngr_printf(" sector not part of file transfer\r\n"); } vfs_mngr_printf(" discarding data - size transferred=0x%x, data=%x,%x,%x,%x,...\r\n", file_transfer_state.size_transferred, buf[0], buf[1], buf[2], buf[3]); return; } // This sector could be part of the file so record it size = VFS_SECTOR_SIZE * num_of_sectors; file_transfer_state.size_transferred += size; file_transfer_state.file_next_sector = sector + num_of_sectors; // If stream processing is done then discard the data if (file_transfer_state.stream_finished) { vfs_mngr_printf("vfs_manager file_data_handler\r\n sector=%i, size=%i\r\n", sector, size); vfs_mngr_printf(" discarding data - size transferred=0x%x, data=%x,%x,%x,%x,...\r\n", file_transfer_state.size_transferred, buf[0], buf[1], buf[2], buf[3]); transfer_update_state(ERROR_SUCCESS); return; } transfer_stream_data(sector, buf, size); } }