This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ARM7: LPC2148 & USB communication

I want to develop mass storage device using LPC2148.I am just referring code from Keil for USBMem. My problem is -
I get Get_descriptor command from PC to which I respond with device descriptor. On receiving 8th byte it generates Reset.
Then I get Set_address command. On response to it, I execute Set Address command in protocol engine.
Then again I get Get_descriptor command (It should actually have index field of 18 but I receive with 64).
Again after 8th byte it generates Reset and again gives Set_address command instead of Get_descriptor command for Configuration descriptor.
What can be the problem?

Parents
  • > What is sequence of next requests from host?

    For device firmware, you should not expect requests in sequence.
    Host puts any request as it likes.
    Device responds to it and does the requested job, if the device supports it. Otherwise, the device returns STALL.

    > As mine is mass storage device, can you please tell me what events are expected further.

    Ummm MSC (Mass Storage Class),
    It's really tough for starter, because you have to implement two layers of protocol.
    I recommend HID for the starting project, which is simpler.

    But if you dare to try MSC,
    Read out the MSC specs I refer in this post.
    Also, Jan Axelson's book, USB Mass Storage ( http://www.lvr.com/usbms.htm ) is your friend.
    Trace a successful implementation, like LPCUSB
    lpcusb.svn.sourceforge.net/.../msc_bot.c
    lpcusb.svn.sourceforge.net/.../msc_scsi.c
    And then, attack to the implementation.

    For MSC Bulk-Only Transport (BOT) implementation,
    Firstly, you have to implement the handlers for required class-specific request(s) at least, in addition to the standard device requests.
    Secondly, MSC-BOT carries SCSI command over bulk IN/OUT endpoints with command wrapper packets. You have to unwrap/wrap SCSI command and status, and transfer requested data or media sector (cluster) data
    Thirdly, SCSI command parser upon above MSC-BOT protocol, which handles the media actually.

    1. Class specific request

    these USB specs are the must.

    Mass Storage Class Specification Overview 1.3
    www.usb.org/.../usb-msc-overview-1.3b.pdf

    Mass Storage Bulk Only 1.0
    www.usb.org/.../usbmassbulk_10.pdf

    In "3 Functional Characteristics (usbmassbulk_10.pdf p7)" section, you'll find these class-specific requests.
    - 3.1 Bulk-Only Mass Storage Reset (class-specific request)
    - 3.2 Get Max LUN (class-specific request)

    "Bulk-Only Mass Storage Reset" is required, but "Get Max LUN" is optional for single-LUN device.
    If you don't implement "Get Max LUN", return STALL to this request.

    Other than these class-specific requests, these standard requests are used for error recovery.
    - Get_Status( ENDPOINT ) (usb_20.pdf 9.4.5 Get Status)
    - Clear_Feater( ENDPOINT_HALT ) (usb_20.pdf 9.4.1 Clear Feature)

    USB 2.0 spec
    www.usb.org/.../usb_20_122909-2.zip

    2. Command/Data/Status Protocol over bulk IN/OUT endpoints

    1) Command transport
    SCSI command comes to bulk OUT EP included in Command Block Wrapper (CBW) (usbmassbulk_10.pdf 5.1 Command Block Wrapper (CBW))
    Check the integrity of the CBW following to "6.2 Valid and Meaningful CBW" section.
    If it isn't valid, return STALL to bulk IN EP, following "6.6.1 CBW Not Valid"
    For valid CBW, pass the SCSI command to the parser.
    Depending on the SCSI command, MSC-BOT handler moves to one of Data In/ Data Out / Status transport stage.

    2) Data IN/Out transport
    Most of SCSI command exchange data on this stage.
    For example, Inquiry returns fixed inquiry response (a block of data) to bulk IN EP.
    Read(10)/Write(10) exchange block data (sector - 512 bytes or cluster - 4K bytes) over bulk IN/OUT EP.
    But some SCSI commands, like Test Unit Ready, don't have this stage.

    3) Status transport
    All of SCSI commands conclude the session in this stage.
    Firmware passes back Status report (the result of the SCSI command), included in Command Status Wrapper (CSW), to the bulk IN EP.

    3. SCSI command over above MSC protocol

    The required SCSI commands are listed up in "4.2 Command Set Information" (PDT=00h) section of this spec.

    MSC Compliance Test Specification
    www.usb.org/.../MSC-compliance-0_9a.pdf

    required SCSI commands
    - Inquiry - spc3r23.pdf(p142)
    - Request Sense - spc3r23.pdf(p221)
    - Test Unit Ready - spc3r23.pdf(p232)
    - Read(10) - sbc2r16.pdf(p47)
    - Read Capacity(10) - sbc2r16.pdf(p54)
    - Write(10) - sbc2r16.pdf(p78)

    Implement them all following the SCSI spec.
    As of these SCSI specs, the original t10.org has closed the links just for their members.
    Search "sbc2r16.pdf" on Goooogle, and you'll find them floating over the internet.



    This is just a brief outline of MSC-BOT and SCSI implementation.
    You'll learn a lot from a running example, the specs and the reference book.

    Good luck.

    Tsuneo

Reply
  • > What is sequence of next requests from host?

    For device firmware, you should not expect requests in sequence.
    Host puts any request as it likes.
    Device responds to it and does the requested job, if the device supports it. Otherwise, the device returns STALL.

    > As mine is mass storage device, can you please tell me what events are expected further.

    Ummm MSC (Mass Storage Class),
    It's really tough for starter, because you have to implement two layers of protocol.
    I recommend HID for the starting project, which is simpler.

    But if you dare to try MSC,
    Read out the MSC specs I refer in this post.
    Also, Jan Axelson's book, USB Mass Storage ( http://www.lvr.com/usbms.htm ) is your friend.
    Trace a successful implementation, like LPCUSB
    lpcusb.svn.sourceforge.net/.../msc_bot.c
    lpcusb.svn.sourceforge.net/.../msc_scsi.c
    And then, attack to the implementation.

    For MSC Bulk-Only Transport (BOT) implementation,
    Firstly, you have to implement the handlers for required class-specific request(s) at least, in addition to the standard device requests.
    Secondly, MSC-BOT carries SCSI command over bulk IN/OUT endpoints with command wrapper packets. You have to unwrap/wrap SCSI command and status, and transfer requested data or media sector (cluster) data
    Thirdly, SCSI command parser upon above MSC-BOT protocol, which handles the media actually.

    1. Class specific request

    these USB specs are the must.

    Mass Storage Class Specification Overview 1.3
    www.usb.org/.../usb-msc-overview-1.3b.pdf

    Mass Storage Bulk Only 1.0
    www.usb.org/.../usbmassbulk_10.pdf

    In "3 Functional Characteristics (usbmassbulk_10.pdf p7)" section, you'll find these class-specific requests.
    - 3.1 Bulk-Only Mass Storage Reset (class-specific request)
    - 3.2 Get Max LUN (class-specific request)

    "Bulk-Only Mass Storage Reset" is required, but "Get Max LUN" is optional for single-LUN device.
    If you don't implement "Get Max LUN", return STALL to this request.

    Other than these class-specific requests, these standard requests are used for error recovery.
    - Get_Status( ENDPOINT ) (usb_20.pdf 9.4.5 Get Status)
    - Clear_Feater( ENDPOINT_HALT ) (usb_20.pdf 9.4.1 Clear Feature)

    USB 2.0 spec
    www.usb.org/.../usb_20_122909-2.zip

    2. Command/Data/Status Protocol over bulk IN/OUT endpoints

    1) Command transport
    SCSI command comes to bulk OUT EP included in Command Block Wrapper (CBW) (usbmassbulk_10.pdf 5.1 Command Block Wrapper (CBW))
    Check the integrity of the CBW following to "6.2 Valid and Meaningful CBW" section.
    If it isn't valid, return STALL to bulk IN EP, following "6.6.1 CBW Not Valid"
    For valid CBW, pass the SCSI command to the parser.
    Depending on the SCSI command, MSC-BOT handler moves to one of Data In/ Data Out / Status transport stage.

    2) Data IN/Out transport
    Most of SCSI command exchange data on this stage.
    For example, Inquiry returns fixed inquiry response (a block of data) to bulk IN EP.
    Read(10)/Write(10) exchange block data (sector - 512 bytes or cluster - 4K bytes) over bulk IN/OUT EP.
    But some SCSI commands, like Test Unit Ready, don't have this stage.

    3) Status transport
    All of SCSI commands conclude the session in this stage.
    Firmware passes back Status report (the result of the SCSI command), included in Command Status Wrapper (CSW), to the bulk IN EP.

    3. SCSI command over above MSC protocol

    The required SCSI commands are listed up in "4.2 Command Set Information" (PDT=00h) section of this spec.

    MSC Compliance Test Specification
    www.usb.org/.../MSC-compliance-0_9a.pdf

    required SCSI commands
    - Inquiry - spc3r23.pdf(p142)
    - Request Sense - spc3r23.pdf(p221)
    - Test Unit Ready - spc3r23.pdf(p232)
    - Read(10) - sbc2r16.pdf(p47)
    - Read Capacity(10) - sbc2r16.pdf(p54)
    - Write(10) - sbc2r16.pdf(p78)

    Implement them all following the SCSI spec.
    As of these SCSI specs, the original t10.org has closed the links just for their members.
    Search "sbc2r16.pdf" on Goooogle, and you'll find them floating over the internet.



    This is just a brief outline of MSC-BOT and SCSI implementation.
    You'll learn a lot from a running example, the specs and the reference book.

    Good luck.

    Tsuneo

Children
  • Implementing class requests for mass storage class.
    Got GET MAX LUN request.
    Replied it with value zero.
    using bus hound software. so my device replies with 1 byte that is 0. but host does not respond.
    why is this so?

  • Uho, you are actually attacking to MSC implementation.
    Impressed with your guts.



    > Got GET MAX LUN request.
    Replied it with value zero.
    using bus hound software. so my device replies with 1 byte that is 0. but host does not respond.

    Ah, you are trapped with Get_Max_LUN pitfall.
    Maybe you weren't aware of this sentence on the MSC-BOT spec.
    www.usb.org/.../usbmassbulk_10.pdf

    3.2 Get Max LUN (class-specific request)
    The device shall return one byte of data that contains the maximum LUN supported by the device. For example, if the device supports four LUNs then the LUNs would be numbered from 0 to 3 and the return value would be 3. If no LUN is associated with the device, the value returned shall be 0.
    ...
    Devices that do not support multiple LUNs may STALL this command.

    That is, for single LUN, you have to return STALL
    Surely, it's confusing. ;-) I've seen many ones also have been trapped.

    To return STALL to IN EP0, put this protocol engine command.
    - Set_Endpoint_Status (IN EP0: 0x00410500, ST bit: 0x00010100)

    When next SETUP transaction comes, STALL is cleared automatically by the protocol engine.

    Tsuneo

  • 1. I stalled control IN endpoint.
    I got clear_feature command then.
    I replied to it just by executing
    Set_Endpoint_Status (IN EP0: 0x00410500, ST bit: 0x00000100) and sent zero length packet.
    When i analyzed Bus hound, host does not respond after clear_feature.
    It again asks
    1. Get max lun
    2. Clear_feature
    and does it for 3 times to Reset the device.
    What should i do?

    2. I am getting string descriptor request for language id and serial no. but not for manufacturer string. I checked for device descriptor index fields. it seems to be fine. What can be the problem?

  • > 1. I stalled control IN endpoint.
    I got clear_feature command then.

    Ummm...
    Clear_Feature is unexpected response.
    For protocol STALL on default endpoint, Clear_Feature is not issued.
    Clear_Feature is used for recovery of bulk/interrupt endpoint.

    a) Which endpoint does the Clear_Feature points,
    Post the 8 byte SETUP data of the Clear_Feature

    b) Do you see any other bus activity between Get_Max_LUN and Clear_Feature on bus hound?
    Does the host happen to send CBW to bulk OUT endpoint?

    Tsuneo

  • Which endpoint does the Clear_Feature points,
    Post the 8 byte SETUP data of the Clear_Feature

    important fields of setup packet are ->
    recipient - endpoint
    endpoint no - 0
    feature - endpoint_halt

    on bus hound we found in following sequence
    GetMaxLun(3 times)
    Stall pid
    Clear feature(3)
    cancelled
    Reset(3)
    no response

    We have not yet written structure for CBW. There is no interrupt on bulk endpoints.

  • > endpoint no - 0

    Do you mean OUT EP0 ?
    wIndex = 0x0000 : OUT EP0, 0x0080 : IN EP0

    It's better to show the 8-bytes SETUP data of the request directly, than giving the interpreted value.
    On Bus Hound log, You'll see the original SETUP data. It's more reliable evidence.



    Anyway, host should be satisfied by both of these device responses.
    - Return 1 byte of 0 on data stage to Get_Max_LUN request
    - STALL to Get_Max_LUN request

    I investigated more on this issue, and found this sentence on the compliance test spec.

    MSC Compliance Test Specification
    www.usb.org/.../MSC-compliance-0_9a.pdf
    - Class-Specific Request Assertions
    5.5.2 If the device supports only one LUN, it may report so via the Get Max LUN request or may fail that request.

    Also, I observed enumeration of six USB memories on a hardware analyzer. All of them returns "0" to Get_Max_LUN.



    Did you "realize" the bulk IN/OUT endpoints on the handling of Set_Configuration request ?
    These bulk EPs should be "realized" like the default EP.
    Sound like the error is caused by disabled bulk EPs

    Tsuneo

  • I was realizing bulk endpoints one after the other.
    i.e. USBReEp=0x00000010 USBReEp=0x00000020
    That i changed to USBReEp=0x00000030.
    And i got bulk OUT interrupt.

    Could not save the bus hound file but details are -
    GetMaxLun(2)
    stall pid
    CLEARFEATURE(2) -> data - 02 01 00 00 00 00 00 00
    canceled -> data - c0010000
    no response -> data - c0000005
    RESET(2)
    ok
    USBCBW

  • This is bus hound file

    Bus Hound 6.01 capture on Windows XP Service Pack 2 (x86). Complements of http://www.perisoft.net
    
    bulkoutint
    
      Device - Device ID (followed by the endpoint for USB devices)
                (28) USB Mass Storage Device
      Phase  - Phase Type
                CTL   USB control transfer       RESET bus reset
                IN    Data in transfer           USTS  USB status
                NTSTS NTSTATUS value             ok    command complete
                OUT   Data out transfer
      Data   - Hex dump of the data transferred
      Descr  - Description of the phase
      Delta  - Elapsed time from the previous phase to the current phase
      Cmd... - Position in the captured data
    
    
    Device  Phase  Data                                                Description       Delta  Cmd.Phase.Ofs(rep)
    ------  -----  --------------------------------------------------  ----------------  -----  ------------------
      28.0  CTL    80 06 00 01  00 00 12 00                            GET DESCRIPTOR     26sc         1.1.0
      28.0  IN     12 01 00 02  00 00 00 40                            .......@          3.6ms         1.2.0
      28.0  CTL    80 06 00 02  00 00 09 00                            GET DESCRIPTOR     36us         2.1.0
      28.0  IN     09 02 20 00  01 01 01 60                            .. ....'          3.9ms         2.2.0
      28.0  CTL    80 06 00 02  00 00 20 00                            GET DESCRIPTOR     32us         3.1.0
      28.0  IN     09 02 20 00  01 01 01 60                            .. ....'          3.9ms         3.2.0
      28.0  CTL    80 06 00 03  00 00 02 00                            GET DESCRIPTOR     28us         4.1.0
      28.0  IN     04 03                                               ..                3.9ms         4.2.0
      28.0  CTL    80 06 00 03  00 00 04 00                            GET DESCRIPTOR     28us         5.1.0
      28.0  IN     04 03 09 04                                         ....              3.9ms         5.2.0
      28.0  CTL    80 06 0c 03  09 04 02 00                            GET DESCRIPTOR     25us         6.1.0
      28.0  IN     1a 03                                               ..                3.9ms         6.2.0
      28.0  CTL    80 06 0c 03  09 04 1a 00                            GET DESCRIPTOR     27us         7.1.0
      28.0  IN     1a 03 41 00  42 00 43 00                            ..A.B.C.          3.9ms         7.2.0
      28.0  CTL    00 09 01 00  00 00 00 00                            SET CONFIG         34us         8.1.0
      28.0  CTL    01 0b 00 00  00 00 00 00                            SET INTERFACE      38ms         9.1.0
      28.0  CTL    a1 fe 00 00  00 00 01 00                            GET MAX LUN        39ms        10.1.0(3)
      28.0  USTS   c0000004                                            stall pid         4.3ms        10.2.0
      28.0  CTL    02 01 00 00  00 00 00 00                            CLEAR FEATURE      27us        11.1.0(3)
      28.0  USTS   c0010000                                            canceled          5.0sc        11.2.0
      28.2  USTS   c0000005                                            no response        10sc        16.1.0
      28    RESET                                                                         68us        17.1.0
      28    ok                                                                           188ms        17.2.0
      28.2  OUT    55 53 42 43  08 e0 6f 89                            USBC..o.          1.9ms        18.1.0
      28    RESET                                                                         19sc        19.1.0(3)
      28    NTSTS  c000009c                                            device data err   5.1sc        19.2.0
    
    

    We are studying SCSI commands. Now our device appears as removable disk in my computer.
    Thank you!!!
    Having problem in answering Read capacity command.

  • What can be the cause of delay betweem Get_Max_LUN and next command? It is 5sec and then 10 sec.

  • We were successful in removing that delay. But still not able to respond READ CAPACITY. It gives error 'Time out'. What is meaning of this error? We have written handler for it.

  • Current status ->
    I am stuck at same position. This is bus hound file -

    
    Device  Phase  Data                                                Description       Delta  Cmd.Phase.Ofs(rep)
    ------  -----  --------------------------------------------------  ----------------  -----  ------------------
      28.0  CTL    80 06 00 01  00 00 12 00                            GET DESCRIPTOR    6.7mn         1.1.0
      28.0  IN     12 01 00 02  00 00 00 40                            .......@          3.2ms         1.2.0
      28.0  CTL    80 06 00 02  00 00 09 00                            GET DESCRIPTOR     30us         2.1.0
      28.0  IN     09 02 20 00  01 01 01 60                            .. ....'          3.9ms         2.2.0
      28.0  CTL    80 06 00 02  00 00 20 00                            GET DESCRIPTOR     27us         3.1.0
      28.0  IN     09 02 20 00  01 01 01 60                            .. ....'          3.9ms         3.2.0
      28.0  CTL    00 09 01 00  00 00 00 00                            SET CONFIG         34us         4.1.0
      28.0  CTL    01 0b 00 00  00 00 00 00                            SET INTERFACE      40ms         5.1.0
      28.0  CTL    a1 fe 00 00  00 00 01 00                            GET MAX LUN        39ms         6.1.0
      28.0  IN     00                                                  .                 3.8ms         6.2.0
      28.2  OUT    55 53 42 43  08 50 67 89      /* CBW without RESET *               USBC.Pg.        1.9ms         7.1.0(2)
      28.2  IN     00 80 00 02  1f 80 00 00                            ........          1.9ms         8.1.0(2)
      28.2  IN     55 53 42 53  08 50 67 89                            USBS.Pg.          2.0ms         9.1.0(2)
      29    CMD    12 00 00 00  24 00                                  INQUIRY            68ms        13.1.0
      29    IN     00 80 00 02  1f 80 00 00                            ........            5us        13.2.0
      29    CMD    25 00 00 00  00 00 00 00                            READ CAPACITY     6.0ms        14.1.0
      29    SSTS   09                                                  timeout           9.0sc        14.2.0
      28    RESET                                                                         68us        15.1.0
      28    ok                                                                           186ms        15.2.0
      29    CMD    25 00 00 00  00 00 00 00                            READ CAPACITY     810ms        16.1.0
      28.2  OUT    55 53 42 43  c0 92 3b 89                            USBC..;.          1.8ms        17.1.0
      28.2  IN     00 00 00 1f  00 00 02 00                            ........          1.9ms        18.1.0
      28.2  IN     55 53 42 53  c0 92 3b 89                            USBS..;.          1.9ms        19.1.0
      29    IN     00 00 00 1f  00 00 02 00                            ........            7us        16.2.0
      29    CMD    28 00 00 00  00 00 00 00                            READ               20us        20.1.0
      28.2  OUT    55 53 42 43  c0 92 3b 89                            USBC..;.          1.9ms        21.1.0
      28.2  IN     00 00 00 1f  00 00 02 00                            ........          2.0ms        22.1.0
      28.2  IN     55 53 42 53  c0 92 3b 89                            USBS..;.          999us        23.1.0
      29    IN     00 00 00 1f  00 00 02 00                            ........            6us        20.2.0
      29    SSTS   52                                                  data overrun        3us        20.3.0
      29    CMD    28 00 00 00  00 00 00 00                            READ              496ms        24.1.0
      29    SSTS   49                                                  timeout           9.4sc        24.2.0
    
    


    I am not getting mass storage class reset at all.
    Even before 1st CBW, there is no reset.
    SCSI commands continue to come. So i don't get whether i have answered command properly or not.

  • Now, when i connect my LPC2148 board to PC, i get 'Disk in drive is not formatted. Do you want to format it now?'. And if i say, yes. Then it says, 'Disk in drive can't be formatted'. What is its meaning?

  • > Now, when i connect my LPC2148 board to PC, i get 'Disk in drive is not formatted. Do you want to format it now?'. And if i say, yes. Then it says, 'Disk in drive can't be formatted'. What is its meaning?

    It means no partition structure (Master Boot Record, boot sector etc.) is assigned on your "disk".
    For "RAM" (SD card) based disk, make partition(s) on the disk before formatting. DOS command, FDISK, or Windows disk management does the job.
    www.vistarewired.com/.../how-to-resize-a-partition-in-windows-vista

    For "ROM" based disk, you have to give the contents of the disk with your program code.

    You'll find such a file in many USB examples

    Keil "LPC2148 USB Mass Storage Device Example" - DiskImg.c
    http://www.keil.com/download/files/lpc2148_usbmem.zip

    Microchip USB examples - Microchip Application Libraries v2010-02-09
    ww1.microchip.com/.../MCHP_App_ Lib v2010_02_09_Installer.zip
    C:\MicrochipSolutions\USB Device - Mass Storage - Internal Flash\Firmware\Files.c

    Microchip one is better than Keil's, because it has many comments.

    To realize the FAT format, this MS FAT spec is required.
    "Microsoft Extensible Firmware Initiative FAT32 File System Specification"
    www.microsoft.com/.../fatgen.mspx

    Tsuneo