⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msd-b.h

📁 PIC18F4550 SD CARD USB
💻 H
字号:
#ifndef MSD_H
#define MSD_H

/** I N C L U D E S **********************************************************/
#include "system\typedefs.h"
/** D E F I N I T I O N S ****************************************************/

/* MSD Interface Class Code */
#define MSD_INTF                    0x08

/* MSD Interface Class SubClass Codes */
#define MSD_INTF_SUBCLASS          0x06

/* MSD Interface Class Protocol Codes */
#define MSD_PROTOCOL           0x50

/* Class Commands */
#define MSD_RESET 0xff
#define GET_MAX_LUN 0xfe

/* SCSI Transparent Command Set Sub-class code */
#define INQUIRY 						0x12							// standard response format 36 Bytes, max can be 96 SPC pp143
#define READ_FORMAT_CAPACITY 			0x23			 
#define READ_CAPACITY 					0x25
#define READ 							0x28
#define WRITE 							0x2a
#define REQUEST_SENSE 					0x03
#define MODE_SENSE 						0x1a
#define PREVENT_ALLOW_MEDIUM_REMOVAL 	0x1e
#define TEST_UNIT_READY 				0x00
#define VERIFY 							0x2f
#define STOP_START 						0x1b

// Various States of Mass Storage Firmware
#define MSD_WAIT 0			// Waiting for a vaid Command Block Wrapper (CBW)
#define MSD_CBW 1			// Received and processing a valid CBW
#define MSD_DATA_IN 2		// IN Data State ( Device-> Host)
#define MSD_DATA_OUT 3		// OUT Data State (Host -> Device)
#define MSD_CSW 4			// Sending a Command Status Wrapper (CSW) 

#define INVALID_CBW 1
#define VALID_CBW !INVALID_CBW
#define MAX_LUN 0

/** S T R U C T U R E S ******************************************************/
 
typedef struct _USB_MSD_CBW 		//31 bytes total Command Block Wrapper
{
    dword dCBWSignature;			// 55 53 42 43h
    dword dCBWTag;					// sent by host, device echos this value in CSW (associated a CSW with a CBW)
    dword dCBWDataTransferLength; 	// number of bytes of data host expects to transfer
    byte bCBWFlags; 				// CBW flags, only bit 7 is used 0-data out from host to device, 1 device to host, rest bits 0
    byte bCBWLUN;					// Most Significant 4bits are always zero, 0 in our case as only one logical unit
    byte bCBWCBLength;				// Here most significant 3bits are zero
    union {
	    struct {
	     	byte CBWCB[16];			// Command block to be executed by the device
	    };
	    struct {					// Inquiry command format
			byte Opcode;
			byte EVPD;				// only b0 is enable vital product data
			byte PageCode;
			word AllocationLength;
			byte Control;
		};
		struct {					// Read Capacity 10
			byte Opcode;
			byte Reserved1;
			dword LBA;				// Logical Block Address
			word Reserved2;
			byte PMI;				// Partial medium Indicator b0 only
			byte Control; 
		};
		struct {					// Command Block for Read 10 (0x28)& Write 10 (0x2a)commands
			byte Opcode;
			byte Flags;				// b7-b5 RDProtect, b4 DPO, b3 FUA, b2 Reserved, b1 FUA_NV, b0 Obsolete
			dword LBA;				
			byte GroupNumber;		// b4-b0 is Group Number rest are reserved
			word TransferLength;
			byte Control;		
		};
		struct {					// Request Sense 0x03		
			byte Opcode;
			byte Desc;
			word Reserved;
			byte AllocationLength;
			byte Control;
		};	
		struct {					// Mode Sense 0x1a
			byte Opcode;
			byte DBD;				// actually only b3 is used as disable block descriptor
			byte PageCode;			// b7,b6 PC=Page Control, b5-b0 PageCode
									// Page Control bits 00=> CurrentValue, 01=>Changeable Values,10=>Default Value, 11=>Saved Values
			byte SubPageCode;
			byte AllocationLength;
			byte Control;		
		};
		struct {					// PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
			byte Opcode;
			byte Reserved[3];
			byte Prevent;			// only b1-b0 is prevent, rest reserved
			byte Control;
		};
		struct {					// TEST_UNIT_READY 0x00
			byte Opcode;
			dword Reserved;
			byte Control;			
		};
		struct {					// VERIFY 10 Command 0x2f
			byte Opcode;
			byte VRProtect;			// b7-b5 VRProtect, b4 DPO, b3-b2,Reserved, b1 BYTCHK, b0 Obsolete
			dword LBA;
			byte GroupNumber;		// b4-b0 Group Number, rest reserved
			word VerificationLength;	
			byte Control;
		};	
		struct {					// STOP_START 0x1b
			byte Opcode;
			byte Immed;
			word Reserved;
			byte Start;				// b7-b4 PowerCondition, b3-b2reserved, b1 LOEJ, b0 Start
			byte Control; 	
		};	
	}CB;
} USB_MSD_CBW;

typedef struct _USB_MSD_CSW			// Command Status Wrapper
{
	dword dCSWSignature;			// 55 53 42 53h Signature of a CSW packet
	dword dCSWTag;					// echo the dCBWTag of the CBW packet
	dword dCSWDataResidue;			// difference in data expected (dCBWDataTransferLength) and actual amount processed/sent
	byte bCSWStatus;				// 00h Command Passed, 01h Command Failed, 02h Phase Error, rest obsolete/reserved
} USB_MSD_CSW;

typedef struct 
{
 	byte Peripheral; 					// Peripheral_Qualifier:3; Peripheral_DevType:5;
	byte Removble;						// removable medium bit7 = 0 means non removable, rest reserved
	byte Version;						// version
	byte Response_Data_Format:4;		// b7,b6 Obsolete, b5 Access control co-ordinator, b4 hierarchical addressing support 
										// b3:0 response data format 2 indicates response is in format defined by spec
	byte AdditionalLength;				// length in bytes of remaining in standard inquiry data
	byte Sccstp; 						// b7 SCCS, b6 ACC, b5-b4 TGPS, b3 3PC, b2-b1 Reserved, b0 Protected 
	byte bqueetc;						// b7 bque, b6- EncServ, b5-VS, b4-MultiP, b3-MChngr, b2-b1 Obsolete, b0-Addr16	
    byte CmdQue;                        // b7-b6 Obsolete, b5-WBUS, b4-Sync, b3-Linked, b2 Obsolete,b1 Cmdque, b0-VS
	char vendorID[8];	
	char productID[16];
	char productRev[4];
} InquiryResponse;

/******************************************************************************
 * Macro:           (bit) mMSDRxIsBusy(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This macro is used to check if HID OUT endpoint is
 *                  busy (owned by SIE) or not.
 *                  Typical Usage: if(mMSDRxIsBusy())
 *
 * Note:            None
 *****************************************************************************/
#define mMSDRxIsBusy()              MSD_BD_OUT.Stat.UOWN

/******************************************************************************
 * Macro:           (bit) mMSDTxIsBusy(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This macro is used to check if HID IN endpoint is
 *                  busy (owned by SIE) or not.
 *                  Typical Usage: if(mMSDTxIsBusy())
 *
 * Note:            None
 *****************************************************************************/
#define mMSDTxIsBusy()              MSD_BD_IN.Stat.UOWN

void USBCheckMSDRequest(void);
void MSDInitEP(void);
void MSDTxPkt(char *buffer, byte len);
void MSDOutHandler(void);
void MSDInHandler(void);
void ProcessIO(void);
void MSDErrorCSW(void);
void MSDSuccessCSW(dword);
void MSDInquiryHandler(void);
void MSDReadCapacityHandler(void);
void MSDReadFormattedCapacityHandler(void);
void MSDReadHandler(void);
void MSInit(void); 
void RequestSenseHandler(void);
byte isMeaningfulCBW(void);
byte isValidCBW(void);
void prepareCSWData(void);

extern void SocketInitialize(void);
extern byte MediaDetect(void);
extern SDC_Error MediaInitialize(void);
extern void DetectSDCard(void); 
extern SDC_Error sdcardinitialize(void);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -