18F14K50 has USB RAM of just 256 bytes (200h - 2FFh). But in the original device stack, msd_buffer occupies 512 bytes on the USB RAM. Its size has to be shrunk into this tiny USB RAM.
Actually, this mods aren't so much. You don't need to change the sector size.
1) Reduce the size of msd_buffer into 64 bytes, ie. just to bulk packet size.
C:\Microchip Solutions v2010-10-19\Microchip\Include\USB\usb_function_msd.h
extern volatile char msd_buffer; // <---- msd_buffer;
C:\Microchip Solutions v2010-10-19\Microchip\USB\usb_device.c
volatile char msd_buffer; // <---- msd_buffer;
C:\Microchip Solutions v2010-10-19\USB Device - Mass Storage - Internal Flash\Firmware\usb_config.h
#define MSD_BUFFER_ADDRESS 0x600 // <---- 0x280 (or less? - tune it looking in the map file, so that it follows just after BDT)
2) Mods for MSDReadHandler()
This function is modified to fit to shrunk msd_buffer.
The original reads out one sector (512 bytes) at a time from program ROM at case MSD_READ10_SECTOR.
Instead, we read it split into 64 bytes at case MSD_READ10_TX_PACKET, just before the 64 bytes is armed to the bulk IN endpoint.
Comment the "if" clause including LUNSectorRead(). Instead, calculate the target ROM address here.
C:\Microchip Solutions v2010-10-19\Microchip\USB\MSD Device Driver\usb_function_msd.c
static ROM char * ROM_disk_ptr; // <---- add this lines
//if the old data isn't completely sent yet
if(USBHandleBusy(USBMSDInHandle) != 0)
/* // <---------- comment this "if" clause
//Try to read a sector worth of data from the media, but check for
if(LUNSectorRead(LBA.Val, (BYTE*)&msd_buffer) != TRUE)
if(MSDRetryAttempt < MSD_FAILED_READ_MAX_ATTEMPTS)
//Too many consecutive failed reads have occurred. Need to
//give up and abandon the sector read attempt; something must
//be wrong and we don't want to get stuck in an infinite loop.
//Need to indicate to the host that a device error occurred.
//However, we can't send the CSW immediately, since the host
//still expects to receive sector read data on the IN endpoint
//first. Therefore, we still send dummy bytes, before
//we send the CSW with the failed status in it.
msd_csw.bCSWStatus=0x01; // Error 0x01 Refer page#18
// of BOT specifications
//Set error status sense keys, so the host can check them later
//to determine how to proceed.
}//else we successfully read a sector worth of data from our media
// calculate ROM address from LBA // <---- add these lines
ROM_disk_ptr = (ROM char *)(MASTER_BOOT_RECORD_ADDRESS + (LBA.Val * MEDIA_SECTOR_SIZE));
/* Write next chunk of data to EP Buffer and send */
//Make sure the endpoint is available before using it.
// <---- insert these lines - from here
// Read 64 bytes from program ROM into the packet buffer
ROM_disk_ptr += MSD_IN_EP_SIZE; // move to next address
// <---- to here
// <---- modify these lines -- additional change
//Prepare the USB module to send an IN transaction worth of data to the host.
USBMSDInHandle = USBTxOnePacket(MSD_DATA_IN_EP,ptrNextData,MSD_IN_EP_SIZE);
USBMSDInHandle = USBTxOnePacket(MSD_DATA_IN_EP,(BYTE *)msd_buffer,MSD_IN_EP_SIZE);
3) MSDWriteHandler() is also modified in similar way, but the order is a little different.
The mods are left for your homework
case MSD_WRITE10_BLOCK: calculate ROM address from LBA into ROM_disk_ptr
case MSD_WRITE10_RX_PACKET: write 64 bytes to the program ROM at ROM_disk_ptr, and move ROM_disk_ptr by 64 bytes
case MSD_WRITE10_SECTOR: comment the "if" clause including LUNSectorWrite()
Error - section '.code_usb_function_msd.o' can not fit the section. Section '.code_usb_function_msd.o'
Tune the values of MDD_INTERNAL_FLASH_DRIVE_CAPACITY and FILES_ADDRESS on FSconfig.h, to reduce the program ROM requirement
Feb 22, 2011, the code snippet on "2) Mods for MSDReadHandler() " was revised at the last line.
post edited by chinzei - 2011/02/21 13:58:51