background image

AN1003

DS01003A-page 14

©

 2005 Microchip Technology Inc.

The USB enumeration process is handled mainly in

usb9.c

. The 

SET_CONFIGURATION

 request is han-

dled by 

USBStdSetCfgHandler()

. This function

calls the function, 

MSDInitEP()

. The function,

MSDInitEP()

, configures and initializes a Bulk-In and

a Bulk-Out endpoint. 

The 

main()

 function in file 

main.c

 is an infinite loop

that services different tasks – USB or mass storage
application tasks. USB tasks are handled by

USBDriverService()

 which handles all USB

hardware interrupts. The mass storage application
tasks are handled by 

ProcessIO()

ProcessIO()

forms the core of the handling of mass storage
communications on Endpoint 1. Figure 9 shows the
flowchart of the 

ProcessIO()

.

When Endpoint 1 is initialized, the 

MSD_State

 is set to

MSD_WAIT

. The firmware basically waits for a CBW to

be received on Endpoint 1. Upon receiving a valid and
meaningful CBW (as defined in the USB Mass Storage
Class Bulk-Only Transport specification, see

“References”

), the CSW data is prepared. Basically,

the 

dCBWTag

  is  copied  to 

dCSWTag

 in order to

associate the CSW with the corresponding CBW and
the 

dCSWSignature

 field is set to “53425355h” (little-

endian). The 

Direction

 bit is read to find the direction

of data transfer (i.e., from host to device or vice versa)
and sets 

MSD_State

 to 

MSD_DATA_OUT

 or

MSD_DATA_IN

, respectively (see Figure 10). Further,

the first byte of 

CBWCB

 is the operation code of the

received command. This is used to decode the
command and take the appropriate action
(

MSDCommandHandler

). It may happen that the

command does not require any data transfer. The

Direction

 bit is ‘

0

’ in this case and the 

MSD_State

is set to 

MSD_DATA_OUT

. If there is no data transfer

required for a given command, the command is
executed and the status is sent using 

sendCSW()

. The

values of the 

dDataResidue

 and 

bCSWStatus

 fields

are set based on the result of the command execution.

Figure 11 shows the flowchart of the 

MSDDataIn()

function. This function is used to send the data
prepared while processing the command in

MSDCommandHandler()

, from the device to the host,

using 

MSD_BD_IN

. After command execution,

dCSWDataResidue

 reflects the number of bytes of

data obtained as a result of the command execution
that are to be sent to the host. In case of an error
(

bCSWStatus

! = 0x00), zero padded data of the size

expected by the host (

dCBWDataTransferLength

) is

sent. If there is no error, the size of data to be sent
(

dCSWDataResidue

), as a result of command execu-

tion, may not be the same as the size expected by the
host (

dCBWDataTransferLength

). In this case, the

dCSWDataResidue

 field in the CSW will reflect the dif-

ference. If the data to be sent is greater than

MSD_IN_EP_SIZE

 (64 bytes, size of the Endpoint 1 IN

buffer), then 

MSD_IN_EP_SIZE

 bytes of data are sent;

otherwise, the remaining 

dCSWDataResidue

 bytes of

data are sent using the 

MSD_BD_IN

 buffer.

Note that the only command where data needs to be
read from the host is the 

WRITE (10)

 command. In the

MSD_WAIT

 state, the 

MSD_BD_OUT

 points to the

msd_cbw

 structure in order to read the next command

block. But when a 

WRITE (10)

 command is received,

the device changes to 

MSD_DATA_OUT

. In this state,

the device must read more data from the host and write
it to the SD card. This is done using the 512-byte

msd_buffer

. So, in the 

MSD_DATA_OUT

 state, the

MSD_BD_OUT

 (Endpoint 1 OUT) buffer points to

msd_buffer

. In order to read the entire 512-byte data

block, after every read, the 

MSD_BD_OUT

 points to a

location in the 

msd_buffer

 incremented by

MSD_OUT_EP_SIZE

 (size of the Endpoint 1 OUT

buffer). Once the 

msd_buffer

 is filled (8 reads of

64 bytes), the block of data is written to a specific loca-
tion in the SD card using the 

SECTORwrite(...)

function (defined in 

sdcard.c

). This process is

repeated if multiple blocks of data are to be written to
the SD card. The 

LBA

 field of the

 WRITE (10)

 

CBWCB

gives the information about the starting 

LBA

 and the

TRANSFER LENGTH

 field indicates the number of

contiguous 

LBA

s to be written. 

EXAMPLE  3:

STRUCTURE FOR COMMAND 
BLOCK WRAPPER

Out endpoint size is configured as 64 bytes. The

msd_buffer

 is a 512-byte buffer declared in the USB

dual port RAM area. The block size of the SD card is
512 bytes. The 

msd_buffer

 is used to read 512 bytes

from the host using multiple 64-byte reads from

MSD_BD_OUT

. Once 512 bytes of data are read from the

host (

msd_buffer

 is filled), the entire block of data is

written to the SD card using the function,

SECTORWrite()

. For 

WRITE (10)

 commands where

the 

TRANSFER LENGTH

 > 1, multiple blocks of 512 bytes

of data are written to consecutive sectors, starting with
the 

Logical Block Address

 field in the command

block. The translation between 

LBA

 and the physical

address is as follows: since each sector has
2

9

= 512 bytes, the physical address is obtained by left

shifting the 

LBA

 by 9 positions. Similarly, for 

READ (10)

,

a block of 512 bytes of data is read from the SD card
using the function, 

SECTORread()

, and then transmit-

ted to the host in 64-byte packets using the 

MSD_BD_IN

typedef struct _USB_MSD_CBW 

{

dword dCBWSignature;

dword dCBWTag;

dword dCBWDataTransferLength;

byte  bCBWFlags;

byte  bCBWLUN;

byte  bCBWCBLength;

byte  CBWCB[16];

} USB_MSD_CBW;

深圳市英锐恩科技有限公司

www.enroo-tech.com

Summary of Contents for AN1003

Page 1: ... TECH SHENZHEN CO LTD 中国 深圳市福田区福华路嘉汇新城汇商中心27楼2701 Enroo Tech Technologies CO Limited Light Tech International Limited 香港新界荃灣沙咀道 29 35 號科技中心 5 樓 5 室 联系电话 86 755 82543411 83167411 83283911 61357155 88845951 联系传真 86 755 82543511 联系邮件 enroo enroo com 公司网站 http www enroo com http www enroo tech com 英锐恩科技时刻为您提供技术支持 提供开发应用协助 提供成熟方案 提供免费样品 提供详细 资料 提供销售服务 单 片 机 集 成 方 案 全 方 位 解 决 服 务 商 优质智能电子产品 芯 方案解决商 ...

Page 2: ...d using FAT16 FAT32 or NTFS file format supported see References SD and MMC cards supported see References Uses the Windows operating system storage driver usbstor sys The Windows Server 2003 Windows XP Windows 2000 and Windows Me operating systems provide native support for USB Mass Storage Class devices Therefore MSD is compatible with these operating systems Version 1 0 of the MSD has the follo...

Page 3: ...s bulk endpoints To meet the needs of various applications using USB three speeds of operation have been designed in the USB V2 0 specification Low Speed LS 1 5 Mbps Full Speed FS 12 Mbps and High Speed HS 480 Mbps See References for detailed information on USB including references to the USB specification and USB related publications The PIC18F4550 used in this application is USB V2 0 compliant a...

Page 4: ...nnot communicate and the device is in the Attached state 18 If the device does not see any activity on the bus for 3 ms it goes into the Suspend state The device consumes minimal bus power in this state Control Transfer Control transfer enables the host and the device to exchange information about device configuration and other control messages Control transfers are ensured to have 10 percent of t...

Page 5: ...d a Status stage The Data stage may or may not be present for all command requests Figure 3 shows the flow of Command trans port Data In Data Out and Status transport for BOT The CBW is a short packet of exactly 31 bytes in length The CBW and all subsequent data and Command Sta tus Wrapper CSW start on a new packet boundary It is important to note that all CBW transfers are ordered little endian w...

Page 6: ... a data retrieval problem the card responds with an error response instead of a time out as in the SD Bus mode See References for information on the SD card specification COMMUNICATION OVERVIEW This section provides a general overview of the com munication between the SD card and the Personal Computer PC application and system hardware Figure 4 shows the functional block diagram of the entire syst...

Page 7: ...ntroller and a digital temperature sensor While not being used in the USB SD card mass storage application these features may be useful in developing other USB applications More details of the PICDEM FS USB Demonstration Board can be found in the PICDEM FS USB Demonstration Board User s Guide see References FIGURE 4 PC MSD COMMUNICATION BLOCK DIAGRAM PC Application e g file explorer Win32 Subsyste...

Page 8: ...ovides card edge signals connect JP2 JP3 and JP5 on the card edge side 3 If the demonstration board does not provide standard PICtail signals open J3 jump signals from J5 and J11 to appropriate signals on J2 SCSI Commands After the successful enumeration of the target USB device the host initiates commands according to the bInterfaceSubClass specified in the interface descriptor during the enumera...

Page 9: ...plements the fixed format current error code sense data response defined in system usb class msd msd h MODE SENSE 6 Opcode 1Ah The MODE SENSE 6 command provides a means for a device server to report parameters to an appli cation client It is a complementary command to the MODE SELECT 6 command The mode parameter header that is used by the MODE SENSE 6 and the MODE SELECT 6 command is shown in Appe...

Page 10: ...Data CSD register read from the SD card This information is conveyed to the PC host in response to the READ CAPACITY command The exact size of the disk can be seen in the disk properties on the PC Further details on firmware and SCSI command implementation can be found in SCSI Commands The project framework is organized under a single root directory with each subdirectory containing files for each...

Page 11: ...uld always be maintained The basic directory structure is similar to the PICDEM FS USB demonstration code This backward compatibility has been maintained to ensure that users already familiar with PICDEM FS USB demonstration code can easily integrate this mass storage application Figure 6 shows the directory structure for the MSD application Function Description Table 1 and Table 2 provide brief d...

Page 12: ...mmand MSDStopStartHandler Executes the START STOP command IsMeaningfulCBW Checks if the received CBW is meaningful IsValidCBW Checks if the received CBW is valid PrepareCSWData Prepares CSW data Tag and Signature SendData byte Y byte X Sends X bytes of data starting at location Y SendCSW Sends the CSW and sets MSD_State to MSD_WAIT ResetSenseData Initializes the sense response data MSDDataIn Sends...

Page 13: ...FERS EXAMPLE 2 MODIFIED LINKER SCRIPT if defined USB_USE_MSD volatile far USB_MSD_CBW msd_cbw volatile far USB_MSD_CSW msd_csw pragma udata myMSD 0x600 volatile far char msd_buffer 512 endif 60h 5Fh 1FFh 100h FFh 00h Access RAM GPR0 GPR1 2FFh 200h 3FFh 300h 400h 5FFh 7FFh 600h GPR2 GPR3 ep0Bo Endpoint 0 ep0Bi Endpoint 0 ep15Bo Endpoint 15 ep15Bi Endpoint 15 msd_buffer The ep n Bo and ep n Bi actua...

Page 14: ...Fh Bulk Only Mass Storage Reset or FEh Get Max LUN If a Bulk Only Mass Storage Reset request is received the firmware disables Endpoint 1 clears the STALL and reinitializes Endpoint 1 The response to the Get Max LUN request is one byte that consists of the maximum LUN supported by the device For example if the device supports three LUNs then the LUNs would be numbered from 0 to 2 and the return va...

Page 15: ...s the size expected by the host dCBWDataTransferLength In this case the dCSWDataResidue field in the CSW will reflect the dif ference If the data to be sent is greater than MSD_IN_EP_SIZE 64 bytes size of the Endpoint 1 IN buffer then MSD_IN_EP_SIZE bytes of data are sent otherwise the remaining dCSWDataResidue bytes of data are sent using the MSD_BD_IN buffer Note that the only command where data...

Page 16: ...k length in bytes is expected for the READ CAPACITY command To obtain this information we read the Card Specific Data CSD from the SD card by calling the CSDread function defined in sdcard c The CSDread function issues the SPI command CSD_READ to the SD card and reads the response in the global variable gblCSDReg The card capacity not including the security protected area can be computed from the ...

Page 17: ...Prepare CSW dCSWTag dCSWSignature Decode and Process the MSDCommandHandler USBBufferReady MSD_BD_OUT USBDriverService Command Is CBW valid Is CBW Is Direction bit 1 Set MSD_State MSD_DATA_IN Return Yes Yes No No No Yes Set MSD_State MSD_DATA_OUT gblCBW meaningful Received CBWCB 深圳市英锐恩科技有限公司 www enroo tech com ...

Page 18: ... 0x0 Zero Padded Data Output dataLen min bCBWDataTransferLength MSD_IN_EP_SIZE Bytes of Data Written to Transmit Register and Sent dCSWDataResidue dataLen dCBWDataTransferLength dataLen Remaining Data dCSWDataResidue Written to Transmit dCSWDataResidue dCBWDataTransferLength 0 Return Yes No Yes No Register and Sent sendData to Transmit Register dCBWDataTransferLength dCSWDataResidue and remaining ...

Page 19: ...BLE 3 MSD SD CARD TEST RESULTS The firmware can be modified to interface with other portable media cards with an SPI interface For exam ple compact Flash mini SD card XD picture card memory stick pro and so on This enhanced application can be developed as a multi card reader device Many digital devices such as Digital Cameras MP3 Players and PDAs have an SD card interface for bulk storage This app...

Page 20: ...IDE available by license free of charge from the Microchip web site http www microchip com mplabide SCSI Primary Commands 2 SPC 2 Revision i23 18 July 2003 http www t10 org ftp t10 drafts spc2 spc2i23 pdf SCSI Primary Commands 3 SPC 3 Revision 21d 14 February 2005 http www t10 org ftp t10 drafts spc3 spc3r23 pdf SCSI Block Commands 2 SBC 2 Revision 16 13 November 2004 http www t10 org ftp t10 draf...

Page 21: ... returns a success So if the D2 LED is not toggling there was an error Check whether the SD card was properly inserted in the card reader interface before connecting the USB cable Q What is the communication protocol between the SD card and the PICDEM FS USB Demonstration Board A The SD card is operated in Single Bit mode using the SPI bus protocol Q What is the data transfer speed A The maximum d...

Page 22: ...oftware is owned by the Company and or its supplier and is protected under applicable copyright laws All rights are reserved Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws as well as to civil liability for the breach of the terms and conditions of this license THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION NO WARRANTIES WHETHER ...

Page 23: ...terfaces supported by this configuration bConfigurationValue 01h Index value of this configuration bmAttributes C0h Configuration Characteristics Bit Description 7 Reserved set to 1 6 Self powered 5 Remote wake up 4 0 Reserved MaxPower 32h Maximum power consumption of the USB device from the bus expressed in 2 mA units i e 50 100 mA Field Value Description bDescriptorType 04h Interface descriptor ...

Page 24: ...mCfg USB_DEV_DSC CONFIGURATION DESCRIPTOR typedef struct _USB_CFG_DSC byte bLength byte bDscType word wTotalLength byte bNumIntf byte bCfgValue byte iCfg byte bmAttributes byte bMaxPower USB_CFG_DSC INTERFACE DESCRIPTOR typedef struct _USB_INTF_DSC byte bLength byte bDscType byte bIntfNum byte bAltSetting byte bNumEPs byte bIntfCls byte bIntfSubCls byte bIntfProtocol byte iIntf USB_INTF_DSC ENDPOI...

Page 25: ...10000001b 10000010b GET_STATUS Index value of the configuration 00000000b SET_ADDRESS The host specifies an address to use in future communications with the device 00000000b SET_CONFIGURATION 00000000b SET_DESCRIPTOR The host adds a descriptor or updates an existing descriptor 00000000b 00000001b 00000010b SET_FEATURE The host requests to enable a feature on a device interface or endpoint 00000001...

Page 26: ...utes 02h This is a Bulk endpoint bMaxPacketSize 40h Maximum packet size Shall be 8 16 32 or 64 bytes 64 bytes in our case bInterval 00h Does not apply to Bulk endpoints Bulk Out Endpoint Descriptor Field Value Description bDescriptorType 05h Endpoint descriptor type bEndpointAddress 01h The address of this endpoint on the USB device The address is encoded as follows Bit Description 3 0 The endpoin...

Page 27: ...per CBW Byte Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0 3 dCBWSignature 4 7 dCBWTag 8 11 dCBWDataTransferLength 12 bmCBWFlags 13 Reserved 0 bCBWLUN 14 Reserved 0 BCBMCBLength 15 30 CBWCB Command Status Wrapper CSW Byte Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0 3 dCBWSignature 4 7 dCSWTag 8 11 dCSWDataResidue 12 bCSWStatus 深圳市英锐恩科技有限公司 www enroo tech com ...

Page 28: ...10 28h Transfers binary data from the media to the host WRITE 10 2Ah Transfers binary data from the host to the media MODE SENSE 6 1Ah Requests device to report parameters REQUEST SENSE 6 03h Transfers status sense data to the host PREVENT ALLOW MEDIUM REMOVAL 1Eh Prevents or allows the removal of media from a removable media device TEST UNIT READY 00h Requests to check if logical unit is ready VE...

Page 29: ... 7 ADDITIONAL SENSE LENGTH n 7 8 11 COMMAND SPECIFIC INFORMATION 12 ADDITIONAL SENSE CODE 13 ADDITIONAL SENSE CODE QUALIFIER 14 FIELD REPLACEMENT UNIT CODE 15 17 SENSE KEY SPECIFIC 18 n Additional Sense Bytes Standard Inquiry Data Format Byte Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0 PERIPHERAL QUALIFIER PERIPHERAL DEVICE TYPE 1 RMB Reserved 2 Version 3 Obsolete NORMACA HISUP RESPONSE DATA...

Page 30: ...4 READ 10 COMMAND Byte Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0 Operation Code 28h 1 RDPROTECT DPO FUA Reserved FUA_NV Obsolete 2 MSB Logical Block Address 5 LSB 6 Reserved Group Number 7 MSB TRANSFER LENGTH 8 LSB 9 CONTROL 深圳市英锐恩科技有限公司 www enroo tech com ...

Page 31: ...IGURE J 1 PICtail BOARD FOR SD AND MMC CARDS SCHEMATIC 11 12 13 10 8 9 1 2 3 4 5 6 10 8 9 11 12 13 4 5 6 1 2 3 180Ω 01 μF FPS009 2203 10 DM1AA SF PEJ 21 Card dect dect Write 180Ω 10 μF 1 μF 1 μF V IN GND SHDN V OUT NC TC1186 PICtail CONN PICtail 深圳市英锐恩科技有限公司 www enroo tech com ...

Page 32: ...rks mentioned herein are property of their respective companies 2005 Microchip Technology Incorporated Printed in the U S A All Rights Reserved Printed on recycled paper Note the following details of the code protection feature on Microchip devices Microchip products meet the specification contained in their particular Microchip Data Sheet Microchip believes that its family of products is one of t...

Page 33: ...英锐恩科技有限公司 ENROO TECH SHENZHEN CO LTD 中国 深圳市福田区福华路嘉汇新城汇商中心27楼2701 Enroo Tech Technologies CO Limited Light Tech International Limited 香港新界荃灣沙咀道 29 35 號科技中心 5 樓 5 室 联系电话 86 755 82543411 83167411 83283911 61357155 88845951 联系传真 86 755 82543511 联系邮件 enroo enroo com 公司网站 http www enroo com http www enroo tech com 单 片 机 集 成 方 案 全 方 位 解 决 服 务 商 优质智能电子产品 芯 方案解决商 ...

Reviews: