• AVR Freaks

Need help,about PIC18F14K50 and MSD

Author
hitigon
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2011/02/20 02:17:48
  • Location: 0
  • Status: offline
2011/02/20 02:36:27 (permalink)
0

Need help,about PIC18F14K50 and MSD

Hey, I'm a new, know nothing about microchip, but here I have a project that needs to make this PIC18F14K50 device as MSD, so I googled  and found this http://www.microchip.com/forums/m379164.aspx.

so I edited the demo in USB Device - Mass Storage - Internal Flash\Firmware    -------C18 - PICDEM FSUSB.mcp like:

- Reduce the sector buffer size.
msd_buffer[256] (usb_device.c)

In MSDReadHandler() and MSDWriteHandler() (usb_function_msd.c)
- case MSD_READ10_WAIT: and case MSD_WRITE10_WAIT
// double the block address and transfer size after reading them from CBW
LBA.Val <<= 1;
TransferLength.Val <<= 1;

- Replace BLOCKLEN_512 to BLOCKLEN_256
#define BLOCKLEN_256 256
@chinzei

and my link script is:

// $Id: 18f14K50.lkr,v 1.1.4.1 2005/07/25 18:23:28 nairnj Exp $
// File: 18f14K50.lkr
// Sample linker script for the PIC18F14k50 processor

LIBPATH .

FILES c018i.o
FILES clib.lib
FILES p18f14k50.lib

// Bootloader
CODEPAGE   NAME=vectors    START=0x0            END=0x29           PROTECTED
CODEPAGE   NAME=bootloader   START=0x2A           END=0xFFF           PROTECTED
CODEPAGE   NAME=page       START=0x1000           END=0x3FFF

// Application
//CODEPAGE   NAME=boot       START=0x0            END=0x1F         PROTECTED
//CODEPAGE   NAME=vectors    START=0x1000         END=0x1029         PROTECTED
//CODEPAGE   NAME=page       START=0x102A         END=0x3FFF

CODEPAGE   NAME=idlocs     START=0x200000       END=0x200007       PROTECTED
CODEPAGE   NAME=config     START=0x300000       END=0x30000D       PROTECTED
CODEPAGE   NAME=devid      START=0x3FFFFE       END=0x3FFFFF       PROTECTED
CODEPAGE   NAME=eedata     START=0xF00000       END=0xF000FF       PROTECTED

ACCESSBANK NAME=accessram  START=0x0            END=0x5F
DATABANK   NAME=gpr0       START=0x60           END=0xFF
DATABANK   NAME=gpr1       START=0x100          END=0x1F3
DATABANK   NAME=usb2       START=0x200          END=0x2FF          PROTECTED
DATABANK   NAME=usb3       START=0x300          END=0x3FF          PROTECTED
DATABANK   NAME=usb4       START=0x400          END=0x4FF          PROTECTED
//DATABANK   NAME=USB_BDT      START=0x400            END=0x45B           PROTECTED
DATABANK   NAME=myMSD      START=0x500            END=0x6FF           PROTECTED
DATABANK   NAME=myUserRam  START=0x700            END=0x7FF           PROTECTED
DATABANK   NAME=sfr15      START=0xF40          END=0xF5F          PROTECTED
ACCESSBANK NAME=accesssfr  START=0xF60          END=0xFFF          PROTECTED

SECTION    NAME=CONFIG     ROM=config
SECTION    NAME=GP0        RAM=gpr0
SECTION    NAME=GP1        RAM=gpr1
//SECTION    NAME=usbram2     RAM=usb2

SECTION    NAME=USB_VARS   RAM=usb4

STACK SIZE=0x40 RAM=gpr1

but this device's page size is too small(0x1000~0x3FFF),  so when I build it,

Error - section '.code_usb_function_msd.o' can not fit the section. Section '.code_usb_function_msd.o' length=0x000008f8

Right now, what can I do to finish it? Really need some help, THX

hitigon


#1

13 Replies Related Threads

    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/20 14:53:06 (permalink)
    0
    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[512];   // <---- msd_buffer[];

    C:\Microchip Solutions v2010-10-19\Microchip\USB\usb_device.c
    volatile char msd_buffer[512];          // <---- msd_buffer[64];

    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

    BYTE MSDReadHandler(void)
    {
        ...
            case MSD_READ10_SECTOR:
                //if the old data isn't completely sent yet
                if(USBHandleBusy(USBMSDInHandle) != 0)
                {
                    break;
                }
    /*                    // <---------- comment this "if" clause
                //Try to read a sector worth of data from the media, but check for
                //possible errors.
                if(LUNSectorRead(LBA.Val, (BYTE*)&msd_buffer[0]) != TRUE)
                {
                    if(MSDRetryAttempt < MSD_FAILED_READ_MAX_ATTEMPTS)
                    {
                        MSDRetryAttempt++;
                        break;
                    }
                    else
                    { 
                        //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.
                        gblSenseData[LUN_INDEX].SenseKey=S_MEDIUM_ERROR;
                        gblSenseData[LUN_INDEX].ASC=ASC_NO_ADDITIONAL_SENSE_INFORMATION;
                        gblSenseData[LUN_INDEX].ASCQ=ASCQ_NO_ADDITIONAL_SENSE_INFORMATION;
                    }
                }//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));


            ...
            ...
            case MSD_READ10_TX_PACKET:
                /* Write next chunk of data to EP Buffer and send */
               
                //Make sure the endpoint is available before using it.
                if(USBHandleBusy(USBMSDInHandle))
                {
                    break;
                }

                                                             // <---- insert these lines - from here
                // Read 64 bytes from program ROM into the packet buffer
                memcpypgm2ram
                (
                    (void*) msd_buffer,
                    (ROM void*)(ROM_disk_ptr),
                    MSD_IN_EP_SIZE
                );
                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 wink

    [Hint]
    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

    Tsuneo


    [EDiT]
    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
    #2
    hitigon
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2011/02/20 02:17:48
    • Location: 0
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 00:47:54 (permalink)
    0
    Really thank u for this :D

    still have  questions

    chinzei

    Tune the values of MDD_INTERNAL_FLASH_DRIVE_CAPACITY and FILES_ADDRESS on FSconfig.h, to reduce the program ROM requirement



    I don't know how to make it right, and how to change the link script file.

    hitigon

    #3
    hitigon
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2011/02/20 02:17:48
    • Location: 0
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 02:01:55 (permalink)
    0
    I changed the value of MDD_INTERNAL_FLASH_DRIVE_CAPACITY to 2 and FILES_ADDRESS to 0x11EA, it doesn't work, it needs more spaces to store the files...is there something with my codes?
    #4
    newfound
    Super Member
    • Total Posts : 1822
    • Reward points : 0
    • Joined: 2003/11/07 12:35:49
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 02:22:53 (permalink)
    0
    I believe the microchip MSD class is rather large.

    If you are not using a bootloader then remove the bootloader page from the linker script. It is marked as protected so the linker will not use this page unless the source file specifically names the section. 

    //CODEPAGE   NAME=bootloader   START=0x2A           END=0xFFF           PROTECTED (Removed)
    CODEPAGE   NAME=page       START=0x2A           END=0x3FFF // Change

    Try this out. There may be something I missed or that someone else of greater knowledge can point out but I think what I have said should help never the less.
    #5
    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 02:55:01 (permalink)
    0
    Copy this hardware profile header file (for PIC18F14K50) to your project folder.
    C:\Microchip Solutions v2010-10-19\USB Device - CDC - Basic Demo\CDC - Basic Demo - Firmware\HardwareProfile - Low Pin Count USB Development Kit.h

    Modify this profile header as follows
    - comment this line
    //    #define PROGRAMMABLE_WITH_USB_HID_BOOTLOADER   

    - Add these lines

        /*******************************************************************/
        /******** MDD File System selection options ************************/
        /*******************************************************************/
        #define USE_PIC18

        #define ERASE_BLOCK_SIZE 64     // for PIC18F14K50
        #define WRITE_BLOCK_SIZE 16


    how to change the link script file.

    Without bootloader by above setting, you don't need to modify the default linker script at all. Delete the file under "Linker Script", and keep it blank on the project files pane of Microchip IDE

    I don't know how to make it right

    Bootloader should occupy the program ROM.
    With quick test without bootloader, these values give successful compile.
    I don't yet cut off unused code so much. If you would tune the code more intensively, you'll get one or more capacity

    #define MDD_INTERNAL_FLASH_DRIVE_CAPACITY 12
    #define FILES_ADDRESS 0x1C00

    Tsuneo
    #6
    hitigon
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2011/02/20 02:17:48
    • Location: 0
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 03:48:18 (permalink)
    0
    I set the value of MDD_INTERNAL_FLASH_DRIVE_CAPACITY to 2, not 12, and delete the linker script, then build succussfully.

    I got the hex file and wrote into the device, my OS can read it as a disk, so it works?!

    but the device needs to be formatted, I'm not sure if it should be or not.

    Thanks guys , @chinzei and @newfound
    post edited by hitigon - 2011/02/21 04:04:23
    #7
    jeff_m_lee
    Super Member
    • Total Posts : 469
    • Reward points : 0
    • Joined: 2005/06/08 09:04:36
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 07:12:29 (permalink)
    0

    the device needs to be formatted, I'm not sure if it should be or not.

     
    Some years ago I experimented with MSD on a 18F2455. A "Real" format didn't work so what I did was copy the first 512 bytes from a formatted memory stick to the PIC, with dd, then the PC was happy to write files to it. 
    #8
    hitigon
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2011/02/20 02:17:48
    • Location: 0
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 08:20:53 (permalink)
    0
    you mean that this disk can't be used?sad I tried LibUSB-win to write and read data from the device, it doesn't work at all.
    #9
    jeff_m_lee
    Super Member
    • Total Posts : 469
    • Reward points : 0
    • Joined: 2005/06/08 09:04:36
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 09:55:44 (permalink)
    0
    In my case the PIC could be used as a disk, but it was a different PIC and I wasn't using the Microchip stack. All in all it was pretty pointless because the Disk OS causes too large a overhead on available space and the Flash can only (reliably) be written too < 1000 times.  
    #10
    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/21 14:00:37 (permalink)
    0
    Auuu, sorry, my bad.
    I missed to modify one line of above code.

    At "2) Mods for MSDReadHandler()" section, this additional modification was required, at the bottom of the code snippet.
     
    C:\Microchip Solutions v2010-10-19\Microchip\USB\MSD Device Driver\usb_function_msd.c

                                                             // <---- modify these lines
    /*
                //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);


    Above snippet was revised, too.

    For your convenience, I attached a project for Low-Pin-Count Demo Board (PIC18F14K50) to this post.
    The code was confirmed on this board on my side.
    But, I didn't modify "3) MSDWriteHandler()" yet on this project, for your pleasrue wink

    Tsuneo
    post edited by chinzei - 2011/02/21 14:01:47
    #11
    hitigon
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2011/02/20 02:17:48
    • Location: 0
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/23 04:25:07 (permalink)
    0
    I still can't format the disk by Windows, sad I also tried your hex file.
    #12
    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/23 14:44:42 (permalink)
    0
    I still can't format the disk by Windows, sad I also tried your hex file.

    You have to implement "3) MSDWriteHandler()" mods.
    It's your homework wink

    Hints are already given on above post.
    Another hint is,

    Extract erase and write code sequence from MDD_IntFlash_SectorWrite() (Internal Flash.c)

    ERASE_BLOCK_SIZE (64bytes) of PIC18F14K50 matches to the packet size.
    It makes the process much simpler.

    Tsuneo
    #13
    jeff_m_lee
    Super Member
    • Total Posts : 469
    • Reward points : 0
    • Joined: 2005/06/08 09:04:36
    • Status: offline
    Re:Need help,about PIC18F14K50 and MSD 2011/02/24 06:45:35 (permalink)
    0
    It's your homework

    but the device needs to be formatted,

     
    If he does his homework - are you saying Windows will successfully format the device? If so what size will it come back with ?
    #14
    Jump to:
    © 2019 APG vNext Commercial Version 4.5