Eli Hughes
/
COMPACT_FLASH
Compact Flash I/O test
Revision 1:dc171f34db9b, committed 2011-12-30
- Comitter:
- emh203
- Date:
- Fri Dec 30 21:02:16 2011 +0000
- Parent:
- 0:6b1e6c9e48ba
- Commit message:
Changed in this revision
diff -r 6b1e6c9e48ba -r dc171f34db9b DataTypes.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DataTypes.c Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,194 @@ +#include "mbed.h" +#include "DataTypes.h" + +CHAR StringBuffer[256]; + +SIGNED_WORD ByteStackPush(ByteStack * Stack,BYTE Val) { + if (Stack->Ptr == Stack->Size-1) { + return STACK_FULL; + } else { + Stack->Ptr++; + Stack->StackSpace[Stack->Ptr] = Val; + return STACK_PUSH_OK; + } +} + +SIGNED_WORD ByteStackPOP(ByteStack * Stack) { + if (Stack->Ptr == 0) { + return STACK_EMPTY; + } else { + Stack->Ptr--; + return Stack->StackSpace[Stack->Ptr]; + } +} + +SIGNED_BYTE BitStackPush(BitStack * Stack,BOOL Val) { + WORD Offset; + BYTE Mask; + + if (Stack->Ptr == Stack->Size-1) { + return STACK_FULL; + } else { + + Stack->Ptr++; + Offset = (Stack->Ptr)>>3; + Mask = 0x01<<(Stack->Ptr&0x07); + + if (Val) { + Stack->StackSpace[Offset] |= Mask; + } else { + Stack->StackSpace[Offset] &= ~Mask; + } + + return STACK_PUSH_OK; + } +} + +SIGNED_BYTE BitStackPop(BitStack * Stack) { + WORD Offset; + BYTE Mask; + + if (Stack->Ptr == 0) { + return STACK_EMPTY; + } else { + + Stack->Ptr++; + Offset = (Stack->Ptr)>>3; + Mask = 0x01<<(Stack->Ptr&0x07); + + if (Stack->StackSpace[Offset] | Mask) { + return TRUE; + } else { + return FALSE; + } + } +} + +#ifndef INLINE_BITPLANE_PUT +void BitPlane_Put(BitPlane * BP, WORD X,WORD Y, BOOL Value) +{ + WORD Offset; + BYTE Mask; + + Offset = (Y * ((BP->SizeX)>>3)) + (X>>3); + Mask = 0x01 << (X & 0x07); + + if(Value) + { + BP->BitPlaneSpace[Offset] |= Mask; + } + else + { + BP->BitPlaneSpace[Offset] &= ~Mask; + } +} +#endif + +#ifndef INLINE_BITPLANE_GET +BOOL BitPlane_Get(BitPlane * BP, WORD X,WORD Y) +{ + WORD Offset; + BYTE Mask; + + Offset = (Y * ((BP->SizeX)>>3)) + (X>>3); + Mask = 0x01 << (X & 0x07); + + if((BP->BitPlaneSpace[Offset])&Mask) + { + return TRUE; + } + else + { + return FALSE; + } +} +#endif +void BitPlane_Clear(BitPlane * BP) { + WORD PlaneSpaceSize; + WORD i; + + PlaneSpaceSize = ((BP->SizeX)>>3) * BP->SizeY; + + for (i=0;i<PlaneSpaceSize;i++) { + BP->BitPlaneSpace[i] = 0; + } +} + + + +void InitByteQueue(ByteQueue *BQ,WORD Size,BYTE * Storage) { + WORD i; + + BQ->QueueSize = Size; + BQ->ReadPtr=0; + BQ->WritePtr=0; + BQ->QueueStorage = Storage; + + for (i=0;i<BQ->QueueSize;i++) { + BQ->QueueStorage[i] = 0; + } +} + +WORD BytesInQueue(ByteQueue *BQ) { + if (BQ->ReadPtr > BQ->WritePtr) { + return (BQ->QueueSize - BQ->ReadPtr + BQ->WritePtr); + } else if (BQ->WritePtr > BQ->ReadPtr) { + return (BQ->WritePtr - BQ->ReadPtr); + } else { + return 0; + } +} + +SIGNED_WORD ByteEnqueue(ByteQueue *BQ,BYTE Val) { + if (BytesInQueue(BQ) == BQ->QueueSize) { + return QUEUE_FULL; + } else { + BQ->QueueStorage[BQ->WritePtr] = Val; + BQ->WritePtr++; + + if (BQ->WritePtr >= BQ->QueueSize) { + BQ->WritePtr = 0; + } + return QUEUE_OK; + } +} + +SIGNED_WORD ByteArrayEnqueue(ByteQueue *BQ,BYTE *Buf,WORD Len) { + WORD i; + for (i=0;i<Len;i++) { + ByteEnqueue(BQ,Buf[i]); + } + return QUEUE_OK; +} + +SIGNED_WORD PrintfEnqueue(ByteQueue *BQ, const char *FormatString,...) +{ + + va_list argptr; + va_start(argptr,FormatString); + vsprintf((CHAR *)StringBuffer,FormatString,argptr); + va_end(argptr); + + ByteArrayEnqueue(BQ,(BYTE *)StringBuffer,strlen(StringBuffer)); + + return QUEUE_OK; +} + + + + +SIGNED_WORD ByteDequeue(ByteQueue *BQ,BYTE *Val) { + + if (BytesInQueue(BQ) == 0) { + return QUEUE_EMPTY; + } else { + *Val = BQ->QueueStorage[BQ->ReadPtr]; + + BQ->ReadPtr++; + + if (BQ->ReadPtr >= BQ->QueueSize) { + BQ->ReadPtr = 0; + } + return QUEUE_OK; + } +} \ No newline at end of file
diff -r 6b1e6c9e48ba -r dc171f34db9b DataTypes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DataTypes.h Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,139 @@ +#include <stdio.h> +#include <stdarg.h> +#include "ff.h" + +#define INLINE_BITPLANE_PUT +#define INLINE_BITPLANE_GET + +#ifndef DATATYPES_H_ +#define DATATYPES_H_ + +//#define BYTE unsigned char +#define WORD unsigned short +#define DWORD unsigned long + +#define SIGNED_BYTE signed char +#define SIGNED_WORD signed short +#define SIGNED_DWORD signed int + +//#define CHAR char + +#define BOOL BYTE + +#ifndef TRUE +#define TRUE 1 +#endif + + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef YEP +#define YEP TRUE +#endif + + +#ifndef NOPE +#define NOPE FALSE +#endif + + +#define STACK_FULL -1 +#define STACK_EMPTY -2 +#define STACK_PUSH_OK 0 + +#define QUEUE_FULL -1 +#define QUEUE_EMPTY -2 +#define QUEUE_OK 0 + + +typedef struct{ + + BYTE *StackSpace; + WORD Ptr; + WORD Size; +} ByteStack; + +typedef struct{ + BYTE *StackSpace; + BYTE Ptr; + BYTE Size; +} BitStack; + +typedef struct { + + BYTE *BitPlaneSpace; + + WORD SizeX; // must be a BYTE aligned + WORD SizeY; + +} BitPlane; + + +typedef struct { + + WORD ReadPtr; + WORD WritePtr; + WORD QueueSize; + BYTE *QueueStorage; + +} ByteQueue; + + + +#ifdef INLINE_BITPLANE_PUT + inline void BitPlane_Put(BitPlane * BP, WORD X,WORD Y, BOOL Value) + { + WORD Offset; + BYTE Mask; + + Offset = (Y * ((BP->SizeX)>>3)) + (X>>3); + Mask = 0x01 << (X & 0x07); + + if(Value) + { + BP->BitPlaneSpace[Offset] |= Mask; + } + else + { + BP->BitPlaneSpace[Offset] &= ~Mask; + } + } +#else + void BitPlane_Put(BitPlane * BP, WORD X,WORD Y, BOOL Value); +#endif + +#ifdef INLINE_BITPLANE_GET + inline BOOL BitPlane_Get(BitPlane * BP, WORD X,WORD Y) + { + WORD Offset; + BYTE Mask; + + Offset = (Y * ((BP->SizeX)>>3)) + (X>>3); + Mask = 0x01 << (X & 0x07); + + if((BP->BitPlaneSpace[Offset])&Mask) + { + return TRUE; + } + else + { + return FALSE; + } + } +#else + BOOL BitPlane_Get(BitPlane * BP, WORD X,WORD Y); +#endif + + +void BitPlane_Clear(BitPlane * BP); + +void InitByteQueue(ByteQueue *BQ,WORD Size,BYTE * Storage); +WORD BytesInQueue(ByteQueue *BQ); +SIGNED_WORD ByteDequeue(ByteQueue *BQ,BYTE *Val); +SIGNED_WORD ByteEnqueue(ByteQueue *BQ,BYTE Val); +SIGNED_WORD ByteArrayEnqueue(ByteQueue *BQ,BYTE *Buf,WORD Len); +SIGNED_WORD PrintfEnqueue(ByteQueue *BQ, const char *FormatString,...); + +#endif /*DATATYPES_H_*/ \ No newline at end of file
diff -r 6b1e6c9e48ba -r dc171f34db9b FAT_FS/diskio.c --- a/FAT_FS/diskio.c Tue Dec 27 02:03:56 2011 +0000 +++ b/FAT_FS/diskio.c Fri Dec 30 21:02:16 2011 +0000 @@ -3,8 +3,29 @@ #include "integer.h" #include "diskio.h" #include "stdarg.h" +#include "usbhost_inc.h" -#define _CF_DEBUG_MESSAGES + +/*-------------------------------------------------------------------------- + + Module Private Functions + +---------------------------------------------------------------------------*/ + +static volatile DSTATUS Stat[3] = {STA_NOINIT | STA_NODISK}; /* Disk status */ +static volatile BYTE DiskProcTimer[3]={0}; /* 100Hz decrement timer */ + + +BYTE RAM_DISK[512]; + + +//Usb Stuff +uint32_t _numBlks; +uint32_t _blkSize; +USB_INT08U inquiryResult[INQUIRY_LENGTH]; + + +//#define _CF_DEBUG_MESSAGES //#define _CF_DEBUG_READ_ATA #ifdef _CF_DEBUG_MESSAGES @@ -12,11 +33,9 @@ #else #define CF_DEBUG #endif - void disk_timerproc (void); - #define LED1_MASK (uint32_t )(1<<18) #define LED2_MASK (uint32_t )(1<<20) #define LED3_MASK (uint32_t )(1<<21) @@ -106,57 +125,39 @@ #define SET_DATA_BUS_AS_OUTPUTS LPC_GPIO0->FIODIR|=DATA_BUS_MASK #define SET_DATA_BUS_AS_INPUTS LPC_GPIO0->FIODIR&=~DATA_BUS_MASK - - /* ATA command */ #define CMD_RESET 0x08 /* DEVICE RESET */ -#define CMD_READ 0x20 /* READ SECTOR(S) */ +#define CMD_READ 0x20 /* READ SECTOR(S) */ #define CMD_WRITE 0x30 /* WRITE SECTOR(S) */ -#define CMD_IDENTIFY 0xEC /* DEVICE IDENTIFY */ -#define CMD_SETFEATURES 0xEF /* SET FEATURES */ +#define CMD_IDENTIFY 0xEC /* DEVICE IDENTIFY */ +#define CMD_SETFEATURES 0xEF /* SET FEATURES */ /* ATA register bit definitions */ #define LBA 0xE0 #define BUSY 0x80 #define DRDY 0x40 -#define DF 0x20 -#define DRQ 0x08 -#define ERR 0x01 +#define DF 0x20 +#define DRQ 0x08 +#define ERR 0x01 #define SRST 0x40 #define nIEN 0x20 /* Bit definitions for Control Port */ #define CTL_READ 0x20 -#define CTL_WRITE 0x40 -#define CTL_RESET 0x80 +#define CTL_WRITE 0x40 +#define CTL_RESET 0x80 #define REG_DATA 0x0 -#define REG_ERROR 0x1 +#define REG_ERROR 0x1 #define REG_FEATURES 0x1 -#define REG_COUNT 0x2 -#define REG_SECTOR 0x3 +#define REG_COUNT 0x2 +#define REG_SECTOR 0x3 #define REG_CYLL 0x4 #define REG_CYLH 0x5 -#define REG_DEV 0x6 -#define REG_COMMAND 0x7 -#define REG_STATUS 0x7 -#define REG_DEVCTRL 0xE -#define REG_ALTSTAT 0xE - - - -/*-------------------------------------------------------------------------- - - Module Private Functions - ----------------------------------------------------------------------------*/ - -static volatile -DSTATUS Stat = STA_NOINIT; /* Disk status */ - -static volatile -BYTE DiskProcTimer; /* 100Hz decrement timer */ - - +#define REG_DEV 0x6 +#define REG_COMMAND 0x7 +#define REG_STATUS 0x7 +#define REG_DEVCTRL 0xE +#define REG_ALTSTAT 0xE void InitCompactFlashInterface() { @@ -176,38 +177,6 @@ NVIC_SetVector(SysTick_IRQn, (uint32_t)(&disk_timerproc)); } - -void CompactFlashIO_Test() -{ - - SET_DATA_BUS_AS_OUTPUTS; - while(1) - { - - wait(0.1); - COMPACT_FLASH_RESET_ACTIVE; - COMPACT_FLASH_POWER_ENABLE; - IOWR_ACTIVE; - IORD_ACTIVE; - CS0_ACTIVE; - CS1_ACTIVE; - SET_CF_ADDRESS(0x04); - SET_CF_DATA(0x80); - - wait(0.1); - COMPACT_FLASH_RESET_INACTIVE; - COMPACT_FLASH_POWER_DISABLE; - IOWR_INACTIVE; - IORD_INACTIVE; - CS0_INACTIVE; - CS1_ACTIVE; - SET_CF_ADDRESS(0x00); - SET_CF_DATA(0x00); - } -} - - - /*-----------------------------------------------------------------------*/ /* Read an ATA register */ /*-----------------------------------------------------------------------*/ @@ -218,8 +187,7 @@ ) { BYTE rd; - DWORD i; - + CS0_ACTIVE; SET_DATA_BUS_AS_INPUTS; SET_CF_ADDRESS(reg); @@ -254,7 +222,7 @@ BYTE dat /* Data to be written */ ) { - DWORD i; + __nop(); CS0_ACTIVE; SET_DATA_BUS_AS_OUTPUTS; @@ -288,7 +256,6 @@ ) { BYTE c = 0, dl, dh; - DWORD i; SET_CF_ADDRESS(REG_DATA); /* Select Data register */ __nop(); @@ -343,9 +310,9 @@ { BYTE s; - DiskProcTimer = 100; /* Time out = 1 sec */ + DiskProcTimer[CF] = 100; /* Time out = 1 sec */ do { - if (!DiskProcTimer) return 0; /* Abort when timeout occured */ + if (!DiskProcTimer[CF]) return 0; /* Abort when timeout occured */ s = read_ata(REG_STATUS); /* Get status */ } while ((s & (BUSY|DRQ)) != DRQ); /* Wait for BUSY goes low and DRQ goes high */ @@ -356,75 +323,115 @@ - /*-----------------------------------------------------------------------*/ /* Initialize Disk Drive */ /*-----------------------------------------------------------------------*/ DSTATUS disk_initialize ( - BYTE drv /* Physical drive nmuber (0) */ + BYTE drv /* Physical drive number (0) */ ) { - if (drv) return STA_NOINIT; /* Supports only single drive */ - - Stat |= STA_NOINIT; - - for (DiskProcTimer = 10; DiskProcTimer; ); /* 100ms */ - - if (Stat & STA_NODISK) return Stat; /* Exit when socket is empty */ - - /* Initialize CFC control port */ - COMPACT_FLASH_POWER_ENABLE; - - for (DiskProcTimer = 1;DiskProcTimer; ); /* 10ms */ - - SET_DATA_BUS_AS_INPUTS; - for (DiskProcTimer = 5; DiskProcTimer; ); /* 50ms */ - COMPACT_FLASH_RESET_INACTIVE; - for (DiskProcTimer = 5; DiskProcTimer; ); /* 50ms */ - write_ata(REG_DEV, LBA); /* Select Device 0 */ - DiskProcTimer = 200; - do { /* Wait for card goes ready */ - if (!DiskProcTimer) - { - CF_DEBUG("Timeout waiting for card BUSY to go inactive\r\n"); - return Stat; - } - } while (read_ata(REG_STATUS) & BUSY); + DSTATUS RetVal; + USB_INT32S rc; + + switch(drv) + { + case COMPACT_FLASH: + + Stat[CF] |= STA_NOINIT; + for (DiskProcTimer[CF] = 10; DiskProcTimer[CF]; ); /* 100ms */ + if (Stat[CF] & STA_NODISK) return Stat[CF]; /* Exit when socket is empty */ + COMPACT_FLASH_POWER_ENABLE; /* Initialize CFC control port */ + for (DiskProcTimer[CF] = 1;DiskProcTimer[CF]; ); /* 10ms */ + + SET_DATA_BUS_AS_INPUTS; + for (DiskProcTimer[CF] = 5; DiskProcTimer[CF]; ); /* 50ms */ + COMPACT_FLASH_RESET_INACTIVE; + for (DiskProcTimer[CF] = 5; DiskProcTimer[CF]; ); /* 50ms */ + write_ata(REG_DEV, LBA); /* Select Device 0 */ + DiskProcTimer[CF] = 200; + + do { /* Wait for card goes ready */ + if (!DiskProcTimer[CF]) + { + CF_DEBUG("Timeout waiting for card BUSY to go inactive\r\n"); + return Stat[CF]; + } + } while (read_ata(REG_STATUS) & BUSY); + + + write_ata(REG_DEVCTRL, SRST | nIEN); /* Software reset */ + for (DiskProcTimer[CF] = 2; DiskProcTimer[CF]; ); /* 20ms */ + + write_ata(REG_DEVCTRL, nIEN); /* Release software reset */ + + for (DiskProcTimer[CF] = 2; DiskProcTimer[CF]; ); /* 20ms */ + + DiskProcTimer[CF] = 200; + do { /* Wait for card goes ready */ + if (!DiskProcTimer[CF]) + { + CF_DEBUG("Timeout waiting for card DRDY\r\n"); + return Stat[CF]; + } + } while ((read_ata(REG_STATUS) & (DRDY|BUSY)) != DRDY); + + CF_DEBUG("Setting to 8-bit PIO MOD\r\n"); + write_ata(REG_FEATURES, 0x01); /* Select 8-bit PIO transfer mode */ + write_ata(REG_COMMAND, CMD_SETFEATURES); + DiskProcTimer[CF] = 200; + do { + if (!DiskProcTimer[CF]) + { + CF_DEBUG("Timeout waiting after trying to call the SETFEATURES command\r\n"); + return Stat[CF]; + } + + } while (read_ata(REG_STATUS) & (BUSY | ERR)); + + Stat[CF] &= ~STA_NOINIT; /* When device goes ready, clear STA_NOINIT */ + + RetVal = Stat[CF]; + + break; + + case USB: + + Host_Init(); + if(!Host_EnumDev()) + { + + rc = MS_Init( &_blkSize, &_numBlks, inquiryResult ); + if (rc != OK) + { + TERMINAL_PRINTF("Could not initialize mass storage interface: %d\r\n", rc); + Stat[USB] |= STA_NOINIT; + } + else + { + Stat[USB] &= ~STA_NOINIT; + } + } + else + { + Stat[USB] |= STA_NOINIT; + } + + RetVal = Stat[USB]; + break; + + + case RAM: + Stat[RAM] &= ~STA_NOINIT; + RetVal = Stat[RAM]; + break; + + default: + RetVal = STA_NOINIT; + break; + } - write_ata(REG_DEVCTRL, SRST | nIEN); /* Software reset */ - for (DiskProcTimer = 2; DiskProcTimer; ); /* 20ms */ - write_ata(REG_DEVCTRL, nIEN); /* Release software reset */ - for (DiskProcTimer = 2; DiskProcTimer; ); /* 20ms */ - DiskProcTimer = 200; - do { /* Wait for card goes ready */ - if (!DiskProcTimer) - { - CF_DEBUG("Timeout waiting for card DRDY\r\n"); - return Stat; - } - } while ((read_ata(REG_STATUS) & (DRDY|BUSY)) != DRDY); - - - - - CF_DEBUG("Setting to 8-bit PIO MOD\r\n"); - write_ata(REG_FEATURES, 0x01); /* Select 8-bit PIO transfer mode */ - write_ata(REG_COMMAND, CMD_SETFEATURES); - DiskProcTimer = 100; - do { - wait(.25); - if (!DiskProcTimer) - { - CF_DEBUG("Timeout waiting after trying to call the SETFEATURES command\r\n"); - return Stat; - } - - } while (read_ata(REG_STATUS) & (BUSY | ERR)); - - Stat &= ~STA_NOINIT; /* When device goes ready, clear STA_NOINIT */ - - return Stat; + return RetVal; } @@ -434,11 +441,32 @@ /*-----------------------------------------------------------------------*/ DSTATUS disk_status ( - BYTE drv /* Physical drive nmuber (0) */ + BYTE drv ) { - if (drv) return STA_NOINIT; /* Supports only single drive */ - return Stat; + DSTATUS RetVal; + + switch(drv) + { + case CF: + RetVal = Stat[CF]; + break; + + + case USB: + RetVal = Stat[USB]; + break; + + case RAM: + RetVal = Stat[RAM]; + break; + + default: + RetVal = STA_NOINIT; + break; + } + + return RetVal; } @@ -455,83 +483,104 @@ ) { BYTE c; - DWORD i; - - - if (drv || !count) return RES_PARERR; - if (Stat & STA_NOINIT) return RES_NOTRDY; - - /* Issue Read Setor(s) command */ - write_ata(REG_COUNT, count); - write_ata(REG_SECTOR, (BYTE)sector); - write_ata(REG_CYLL, (BYTE)(sector >> 8)); - write_ata(REG_CYLH, (BYTE)(sector >> 16)); - write_ata(REG_DEV, ((BYTE)(sector >> 24) & 0x0F) | LBA); - write_ata(REG_COMMAND, CMD_READ); - - - do { - if (!wait_data()) return RES_ERROR; /* Wait data ready */ - - - SET_CF_ADDRESS(REG_DATA); - __nop(); - __nop(); - __nop(); - __nop(); - CS0_ACTIVE; - c = 0; - - __nop(); - __nop(); - __nop(); + DWORD i,j; - SET_DATA_BUS_AS_INPUTS; - do { - IORD_ACTIVE; /* IORD = L */ - __nop(); - __nop(); - __nop(); - GET_CF_DATA(*buff++); /* Get even data */ - __nop(); - __nop(); - __nop(); - - __nop(); - __nop(); - __nop(); + switch(drv) + { + case CF: + if (Stat[CF] & STA_NOINIT) return RES_NOTRDY; - IORD_INACTIVE; /* IORD = H */ - __nop(); - __nop(); - __nop(); - - IORD_ACTIVE; /* IORD = L */ - __nop(); - __nop(); - __nop(); - - GET_CF_DATA(*buff++); /* Get Odd data */ - __nop(); - __nop(); - __nop(); - - __nop(); - IORD_INACTIVE; /* IORD = H */ - __nop(); - __nop(); - __nop(); - - - } while (--c); - } while (--count); - - CS0_INACTIVE; - read_ata(REG_ALTSTAT); - read_ata(REG_STATUS); - - - return RES_OK; + /* Issue Read Setor(s) command */ + write_ata(REG_COUNT, count); + write_ata(REG_SECTOR, (BYTE)sector); + write_ata(REG_CYLL, (BYTE)(sector >> 8)); + write_ata(REG_CYLH, (BYTE)(sector >> 16)); + write_ata(REG_DEV, ((BYTE)(sector >> 24) & 0x0F) | LBA); + write_ata(REG_COMMAND, CMD_READ); + + do { + if (!wait_data()) return RES_ERROR; /* Wait data ready */ + + SET_CF_ADDRESS(REG_DATA); + __nop(); + __nop(); + __nop(); + __nop(); + CS0_ACTIVE; + c = 0; + + __nop(); + __nop(); + __nop(); + + SET_DATA_BUS_AS_INPUTS; + do { + IORD_ACTIVE; /* IORD = L */ + __nop(); + __nop(); + __nop(); + GET_CF_DATA(*buff++); /* Get even data */ + __nop(); + __nop(); + __nop(); + + __nop(); + __nop(); + __nop(); + + IORD_INACTIVE; /* IORD = H */ + __nop(); + __nop(); + __nop(); + + IORD_ACTIVE; /* IORD = L */ + __nop(); + __nop(); + __nop(); + + GET_CF_DATA(*buff++); /* Get Odd data */ + __nop(); + __nop(); + __nop(); + + __nop(); + IORD_INACTIVE; /* IORD = H */ + __nop(); + __nop(); + __nop(); + + + } while (--c); + } while (--count); + + CS0_INACTIVE; + read_ata(REG_ALTSTAT); + read_ata(REG_STATUS); + + return RES_OK; + break; + + + case USB: + + if ( OK == MS_BulkRecv(sector, 1, (USB_INT08U *)buff) ) + return RES_OK; + else + return RES_NOTRDY; + break; + + case RAM: + for(i=0;i<512;i++) + { + buff[i] = RAM_DISK[i]; + } + return RES_OK; + break; + + default: + return RES_PARERR; + break; + } } @@ -548,84 +597,112 @@ { BYTE s, c; DWORD i; - - if (drv || !count) return RES_PARERR; - if (Stat & STA_NOINIT) return RES_NOTRDY; - - /* Issue Write Setor(s) command */ - write_ata(REG_COUNT, count); - write_ata(REG_SECTOR, (BYTE)sector); - write_ata(REG_CYLL, (BYTE)(sector >> 8)); - write_ata(REG_CYLH, (BYTE)(sector >> 16)); - write_ata(REG_DEV, ((BYTE)(sector >> 24) & 0x0F) | LBA); - write_ata(REG_COMMAND, CMD_WRITE); - - - - do { - if (!wait_data()) return RES_ERROR; + DRESULT RetVal; - SET_CF_ADDRESS(REG_DATA); - __nop(); - __nop(); - __nop(); - CS0_ACTIVE; - __nop(); - __nop(); - __nop(); - - SET_DATA_BUS_AS_OUTPUTS; - c = 0; - do { - SET_CF_DATA(*buff++); /* Set even data */ - __nop(); - __nop(); - __nop(); - - IOWR_ACTIVE; /* IOWR = L */ - __nop(); - __nop(); - __nop(); + switch(drv) + { + case CF: + /* Issue Write Setor(s) command */ + write_ata(REG_COUNT, count); + write_ata(REG_SECTOR, (BYTE)sector); + write_ata(REG_CYLL, (BYTE)(sector >> 8)); + write_ata(REG_CYLH, (BYTE)(sector >> 16)); + write_ata(REG_DEV, ((BYTE)(sector >> 24) & 0x0F) | LBA); + write_ata(REG_COMMAND, CMD_WRITE); + + do { + if (!wait_data()) return RES_ERROR; + + SET_CF_ADDRESS(REG_DATA); + __nop(); + __nop(); + __nop(); + CS0_ACTIVE; + __nop(); + __nop(); + __nop(); + + SET_DATA_BUS_AS_OUTPUTS; + c = 0; + do { + SET_CF_DATA(*buff++); /* Set even data */ + __nop(); + __nop(); + __nop(); + + IOWR_ACTIVE; /* IOWR = L */ + __nop(); + __nop(); + __nop(); + + IOWR_INACTIVE; /* IOWR = H */ + __nop(); + __nop(); + __nop(); + + SET_CF_DATA(*buff++); /* Set odd data */ + __nop(); + __nop(); + __nop(); + + IOWR_ACTIVE; /* IOWR = L */ + __nop(); + __nop(); + __nop(); + + IOWR_INACTIVE; /* IOWR = H */ + __nop(); + __nop(); + __nop(); + } while (--c); + + } while (--count); + + SET_DATA_BUS_AS_INPUTS; + CS0_INACTIVE; + + DiskProcTimer[CF] = 100; + do { + if (!DiskProcTimer[CF]) return RES_ERROR; + s = read_ata(REG_STATUS); + } while (s & BUSY); + if (s & ERR) return RES_ERROR; + + read_ata(REG_ALTSTAT); + read_ata(REG_STATUS); - IOWR_INACTIVE; /* IOWR = H */ - __nop(); - __nop(); - __nop(); - - SET_CF_DATA(*buff++); /* Set odd data */ - __nop(); - __nop(); - __nop(); - - IOWR_ACTIVE; /* IOWR = L */ - __nop(); - __nop(); - __nop(); - - IOWR_INACTIVE; /* IOWR = H */ - __nop(); - __nop(); - __nop(); + RetVal = RES_OK; + break; + + + case USB: + if ( OK == MS_BulkSend(sector, 1, (USB_INT08U *)buff) ) + RetVal = RES_OK; + else; + RetVal = RES_NOTRDY; + break; + + case RAM: + + for(i=0;i<512;i++) + { + RAM_DISK[i] = buff[i]; + } + RetVal = RES_OK; + + break; + + default: + RetVal = RES_PARERR; + break; + + return RetVal; + } - } while (--c); + } - } while (--count); - SET_DATA_BUS_AS_INPUTS; - CS0_INACTIVE; - - DiskProcTimer = 100; - do { - if (!DiskProcTimer) return RES_ERROR; - s = read_ata(REG_STATUS); - } while (s & BUSY); - if (s & ERR) return RES_ERROR; - - read_ata(REG_ALTSTAT); - read_ata(REG_STATUS); - - return RES_OK; -} - + + /*-----------------------------------------------------------------------*/ /* Miscellaneous Functions */ @@ -638,45 +715,136 @@ ) { BYTE n, w, ofs, dl, dh, *ptr = (BYTE *)buff; - - - if (drv) return RES_PARERR; - if (Stat & STA_NOINIT) return RES_NOTRDY; - - switch (ctrl) { - case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */ - ofs = 60; w = 2; n = 0; - break; - - case GET_BLOCK_SIZE : /* Get erase block size in sectors (DWORD) */ - *(DWORD*)buff = 32; - return RES_OK; - - case CTRL_SYNC : /* Nothing to do */ - return RES_OK; - - case ATA_GET_REV : /* Get firmware revision (8 chars) */ - ofs = 23; w = 4; n = 4; - break; - - case ATA_GET_MODEL : /* Get model name (40 chars) */ - ofs = 27; w = 20; n = 20; - break; - - case ATA_GET_SN : /* Get serial number (20 chars) */ - ofs = 10; w = 10; n = 10; - break; - + USB_INT32S rc; + + switch(drv) + { + case CF: + switch (ctrl) + { + case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */ + ofs = 60; w = 2; n = 0; + break; + + case GET_BLOCK_SIZE : /* Get erase block size in sectors (DWORD) */ + *(DWORD*)buff = 32; + return RES_OK; + break; + case CTRL_SYNC : /* Nothing to do */ + return RES_OK; + break; + case ATA_GET_REV : /* Get firmware revision (8 chars) */ + ofs = 23; w = 4; n = 4; + break; + + case ATA_GET_MODEL : /* Get model name (40 chars) */ + ofs = 27; w = 20; n = 20; + break; + + case ATA_GET_SN : /* Get serial number (20 chars) */ + ofs = 10; w = 10; n = 10; + break; + + default: + return RES_PARERR; + } + + write_ata(REG_COMMAND, CMD_IDENTIFY); + + if (!wait_data()) return RES_ERROR; + + read_part(ptr, ofs, w); + + while (n--) + { + dl = *ptr; dh = *(ptr+1); + *ptr++ = dh; *ptr++ = dl; + } + + return RES_OK; + break; + + + case USB: + rc = MS_Init( &_blkSize, &_numBlks, inquiryResult ); + + if (rc != OK) + { + TERMINAL_PRINTF("Could not get data from USB Inquiry: %d\r\n", rc); + return RES_ERROR; + } + else + { + Stat[USB] &= ~STA_NOINIT; + switch (ctrl) + { + case GET_SECTOR_COUNT : + *(DWORD *)(buff) =_numBlks; + break; + + case GET_BLOCK_SIZE : + *(DWORD *)(buff) = _blkSize; + return RES_OK; + break; + case CTRL_SYNC : + return RES_OK; + break; + case ATA_GET_REV : + memcpy ((CHAR *)buff,(const char *)&inquiryResult[32],8); + return RES_OK; + break; + + case ATA_GET_MODEL : + memcpy ((CHAR *)buff, (const char *)&inquiryResult[8],8); + return RES_OK; + break; + + case ATA_GET_SN : + memcpy ((CHAR *)buff,(const char *)&inquiryResult[16],8); + return RES_OK; + break; + + default: + return RES_PARERR; + } + } + + break; + + case RAM: + switch (ctrl) + { + case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */ + *(DWORD *)(buff) = 1; + break; + + case GET_BLOCK_SIZE : /* Get erase block size in sectors (DWORD) */ + *(DWORD *)(buff) = 1; + return RES_OK; + break; + case CTRL_SYNC : /* Nothing to do */ + return RES_OK; + break; + case ATA_GET_REV : /* Get firmware revision (8 chars) */ + strcpy ((CHAR *)buff,"Rev 0.01"); + break; + + case ATA_GET_MODEL : /* Get model name (40 chars) */ + strcpy ((CHAR *)buff,"Wavenumber RAM Drive"); + break; + + case ATA_GET_SN : /* Get serial number (20 chars) */ + strcpy ((CHAR *)buff,"12345"); + break; + default: + return RES_PARERR; + } + return RES_OK; + break; + default: - return RES_PARERR; - } - - write_ata(REG_COMMAND, CMD_IDENTIFY); - if (!wait_data()) return RES_ERROR; - read_part(ptr, ofs, w); - while (n--) { - dl = *ptr; dh = *(ptr+1); - *ptr++ = dh; *ptr++ = dl; + return RES_PARERR; + break; } return RES_OK; @@ -692,29 +860,43 @@ { static BYTE pv; BYTE n; - - LED2_TOGGLE; + + n = DiskProcTimer[CF]; /* 100Hz decrement timer */ + if (n) DiskProcTimer[CF] = --n; - n = DiskProcTimer; /* 100Hz decrement timer */ - if (n) DiskProcTimer = --n; + n = DiskProcTimer[USB]; /* 100Hz decrement timer */ + if (n) DiskProcTimer[USB] = --n; + + n = DiskProcTimer[RAM]; /* 100Hz decrement timer */ + if (n) DiskProcTimer[RAM] = --n; n = pv; pv = COMPACT_FLASH_CARD_DETECTED ; /* Sapmle socket switch */ + + //Check Compact Flash Card Detect if (n == pv) { /* Have contacts stabled? */ if (!COMPACT_FLASH_CARD_DETECTED ) { /* CD1 or CD2 is high (Socket empty) */ - Stat |= (STA_NODISK | STA_NOINIT); + Stat[CF] |= (STA_NODISK | STA_NOINIT); SET_DATA_BUS_TO_INPUTS; /* Float D0-D7 */ COMPACT_FLASH_RESET_ACTIVE; /* Assert RESET# */ COMPACT_FLASH_POWER_DISABLE; /* Power OFF */ LED1_OFF; - - } else { /* CD1 and CD2 are low (Card inserted) */ - Stat &= ~STA_NODISK; + } + else + { /* CD1 and CD2 are low (Card inserted) */ + Stat[CF] &= ~STA_NODISK; LED1_ON; } } + + //Check to see if a USB drive is connected + if(HOST_RhscIntr>0) + LED2_ON; + else + LED2_OFF; + }
diff -r 6b1e6c9e48ba -r dc171f34db9b FAT_FS/diskio.h --- a/FAT_FS/diskio.h Tue Dec 27 02:03:56 2011 +0000 +++ b/FAT_FS/diskio.h Fri Dec 30 21:02:16 2011 +0000 @@ -77,5 +77,11 @@ #define NAND_FORMAT 30 /* Create physical format */ +//Available Drives +#define CF 0 +#define COMPACT_FLASH CF +#define USB 1 +#define RAM 2 + #define _DISKIO #endif
diff -r 6b1e6c9e48ba -r dc171f34db9b MODSERIAL.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MODSERIAL.lib Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/MODSERIAL/#af2af4c61c2f
diff -r 6b1e6c9e48ba -r dc171f34db9b Terminal.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.c Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,393 @@ +#include "Terminal.h" +#include "DataTypes.h" +#include "mbed.h" +#include "MODSERIAL.h" +#include "diskio.h" + +//***************************************************************** +//Terminal Configuration +//***************************************************************** + +#define MAX_TERMINAL_LINE_CHARS 64 +#define MAX_TERMINAL_CMD_CHARS 32 +#define TERMINAL_QUEUE_SIZE 512 +#define NUM_TERMINAL_CMDS 3 + + +MODSERIAL pc(USBTX, USBRX,TERMINAL_QUEUE_SIZE,TERMINAL_QUEUE_SIZE); // tx, rx + +Timer DataSpeedTimer; + +typedef void (*TerminalCallback)(char *); + +//These are the terminal command names that map to the callbacks +char *TerminalCommands[NUM_TERMINAL_CMDS] = {"help","reboot","dprobe"}; + +void TerminalCmd_Help(char *arg); +void TerminalCmd_Reboot(char *arg); +void TerminalCmd_dprobe(char *arg); + + +//Probes a disk +BOOL dprobe(BYTE Disk); +void SpeedTest(BYTE Disk); + +//Populate this array with the callback functions +TerminalCallback TerminalCallbacks[NUM_TERMINAL_CMDS] ={ + TerminalCmd_Help, + TerminalCmd_Reboot, + TerminalCmd_dprobe + }; + +//***************************************************************** +//Plumbing..... +//***************************************************************** + + +BYTE TerminalInQueueStore[TERMINAL_QUEUE_SIZE]; +BYTE TerminalOutQueueStore[TERMINAL_QUEUE_SIZE]; +CHAR TerminalLineBuf[MAX_TERMINAL_LINE_CHARS]; +BYTE TerminalPos; +CHAR TerminalCmdBuf[MAX_TERMINAL_CMD_CHARS+1]; +CHAR TerminalArgs[MAX_TERMINAL_LINE_CHARS-MAX_TERMINAL_CMD_CHARS]; +BYTE NextCharIn; +BOOL CmdFound; + +void TerminalBootMsg() +{ + +TERMINAL_PRINTF("\r\n\r\n"); +TERMINAL_PRINTF("***********************************\r\n"); +TERMINAL_PRINTF(" /|\r\n"); +TERMINAL_PRINTF(" ____ __ -- |\r\n"); +TERMINAL_PRINTF(" (___ \\ \\ \\ _|_\r\n"); +TERMINAL_PRINTF(" __) )______\\ \\ \r\n"); +TERMINAL_PRINTF(" / __/( __ )> \\ \r\n"); +TERMINAL_PRINTF(" | |___ | || |/ ^ \\ \r\n"); +TERMINAL_PRINTF(" |_____)|_||_/_/ \\_\\ \r\n"); +TERMINAL_PRINTF(" \r\n"); +TERMINAL_PRINTF("Active Pickguard \r\n"); +TERMINAL_PRINTF("Copyright (C) <2011> Eli Hughes\r\n"); +TERMINAL_PRINTF("Wavenumber LLC\r\n"); +TERMINAL_PRINTF("***********************************\r\n\r\n>"); + +} + +void InitTerminal() +{ + TerminalBootMsg(); +} + +void TerminalCmd_Help(char *arg) +{ + BYTE i; + + TERMINAL_PRINTF("\r\n\r\bCommandList:\r\n"); + TERMINAL_PRINTF("----------------------\r\n"); + + for(i=0;i<NUM_TERMINAL_CMDS;i++) + { + TERMINAL_PRINTF("%s\r\n",TerminalCommands[i]); + } + +} + +void TerminalCmd_Reboot(char *arg) +{ + TerminalBootMsg(); +} + +void TerminalCmd_dprobe(char *arg) +{ + if(strcmp(arg,"CF") == 0) + { + if(dprobe(COMPACT_FLASH)==TRUE) + SpeedTest(COMPACT_FLASH); + } + else if (strcmp(arg,"USB") == 0) + { + if(dprobe(USB)==TRUE) + SpeedTest(USB); + } + else if (strcmp(arg,"RAM") == 0) + { + if(dprobe(RAM)==TRUE) + SpeedTest(RAM); + } + else + { + TERMINAL_PRINTF("\r\nUsage: dprobe DISK\r\n\r\nDISK can be CF, USB or RAM\r\n"); + } +} + + +void ProcessTerminal() +{ + BYTE i,j; + BOOL ArgsFound; + + if(TERMINAL_READABLE) + { + NextCharIn = TERMINAL_GETC; + + switch(NextCharIn) + { + case '\r': + + TerminalLineBuf[TerminalPos++] = 0x0; + TERMINAL_PUTC(NextCharIn); + + if(TerminalPos > 1) + { + //find the command + i=0; + while(TerminalLineBuf[i]>0x20 && TerminalLineBuf[i]<0x7f) + { + TerminalCmdBuf[i] = TerminalLineBuf[i]; + i++; + + if(i==MAX_TERMINAL_CMD_CHARS) + { + break; + } + } + + TerminalCmdBuf[i] = 0; + TerminalCmdBuf[i+1] = 0; + + + ArgsFound = TRUE; + memset(TerminalArgs,0x00,sizeof(TerminalArgs)); + //scan for num terminator or next non whitespace + while(TerminalLineBuf[i]<=0x20 && (i<MAX_TERMINAL_LINE_CHARS)) + { + if(TerminalLineBuf[i] == 0x00) + { + + //if we find a NULL terminator before a non whitespace character they flag for no arguments + ArgsFound = FALSE; + break; + } + i++; + } + + if(ArgsFound == TRUE) + { + strcpy(TerminalArgs,&TerminalLineBuf[i]); + + //trim trailing whitespace + i = sizeof(TerminalArgs)-1; + + while((TerminalArgs[i]<0x21) && (i>0)) + { + TerminalArgs[i]= 0x00; + i--; + } + } + + CmdFound = FALSE; + for(j=0;j<NUM_TERMINAL_CMDS;j++) + { + if(strcmp(TerminalCmdBuf,TerminalCommands[j]) == 0) + { + TERMINAL_PRINTF("\r\n"); + if(TerminalCallbacks[j] != NULL) + TerminalCallbacks[j](TerminalArgs); + + CmdFound = TRUE; + break; + } + } + if(CmdFound == FALSE) + { + TERMINAL_PRINTF("\r\n%s command not recognized.\r\n",TerminalCmdBuf); + } + } + TERMINAL_PRINTF("\r\n>"); + TerminalPos = 0; + + break; + + case '\b': + if(TerminalPos > 0) + { + TerminalPos--; + TERMINAL_PUTC(NextCharIn); + } + break; + + default: + + if(TerminalPos == 0 && NextCharIn == 0x020) + { + //Do nothing if space bar is pressed at beginning of line + } + else if(NextCharIn >= 0x20 && NextCharIn<0x7F) + { + + if(TerminalPos < MAX_TERMINAL_LINE_CHARS-1) + { + TerminalLineBuf[TerminalPos++] = NextCharIn; + TERMINAL_PUTC(NextCharIn); + } + } + + break; + + } + } + +} + + +#define SECTORS_TO_READ_WRITE 4096 +BYTE DataBuf[512]; +BYTE DataBufCheck[512]; + +void SpeedTest(BYTE Disk) +{ + +float DataWriteSpeed = 0; +float DataReadSpeed = 0; +float DataReadWriteSpeed = 0; +int i,j,k; + + +TERMINAL_PRINTF("\r\nData write test started...\r\n",DataReadSpeed); +DataSpeedTimer.reset(); +DataSpeedTimer.start(); + +for(i=0;i<SECTORS_TO_READ_WRITE;i++) +{ + if(disk_write (Disk,&DataBuf[0],i,1) == RES_ERROR) + { + TERMINAL_PRINTF("Error writing test block %d, stopping.\r\n", i); + return; + } +} +DataSpeedTimer.stop(); + +DataWriteSpeed = (((SECTORS_TO_READ_WRITE * 512.0)/1024.0))/(DataSpeedTimer.read()); + +TERMINAL_PRINTF("Data write speed %.1f kb/s\r\n\r\n",DataWriteSpeed); +TERMINAL_PRINTF("Data read test started...\r\n",DataReadSpeed); + +DataSpeedTimer.reset(); +DataSpeedTimer.start(); +for(i=0;i<SECTORS_TO_READ_WRITE;i++) +{ + if(disk_read ( Disk,&DataBuf[0],i,1) == RES_ERROR) + { + TERMINAL_PRINTF("Error reading test block %d, stopping.\r\n", i); + return; + } +} +DataSpeedTimer.stop(); + +DataReadSpeed = (((SECTORS_TO_READ_WRITE * 512.0)/1024.0))/(DataSpeedTimer.read()); +TERMINAL_PRINTF("Data read speed %.1f kb/s\r\r\n\n",DataReadSpeed); +TERMINAL_PRINTF("Data read/write test started...\r\n",DataReadSpeed); + +DataSpeedTimer.reset(); +DataSpeedTimer.start(); + +for(j=0;j<SECTORS_TO_READ_WRITE;j++) +{ + + for(i=0;i<512;i++) + { + DataBuf[i] = rand(); + DataBufCheck[i] = 0; + } + + if(disk_write (Disk,&DataBuf[0],j,1) == RES_ERROR) + { + TERMINAL_PRINTF("Error writing test block %d, stopping.\r\n", i); + return; + } + + if(disk_read (Disk,&DataBufCheck[0],j,1) == RES_ERROR) + { + TERMINAL_PRINTF("Error reading test block %d, stopping.\r\n", i); + return; + } + + + for(i=0;i<512;i++) + { + if(DataBuf[i]!=DataBufCheck[i]) + { + TERMINAL_PRINTF("Readback failure on test block %d byte %d\r\n",j,i); + + for(k=0;k<512;k++) + { + TERMINAL_PRINTF("k:%d Wrote: %d Read: %d\r\n",k,DataBuf[k],DataBufCheck[k]); + break; + } + return; + } + } +} +DataSpeedTimer.stop(); + +DataReadWriteSpeed = (((SECTORS_TO_READ_WRITE * 512.0)/1024.0))/(DataSpeedTimer.read()); +TERMINAL_PRINTF("Data read/write speed %.1f kb/s\r\n",DataReadWriteSpeed); +} + +BOOL dprobe(BYTE Disk) +{ + DWORD NumSectors; + CHAR ModelName[41] = {0}; + CHAR SerialNumber[21] = {0}; + CHAR FirmwareVerion[9] = {0}; + BOOL RetVal; + + if(disk_status(Disk)&STA_NODISK) + { + TERMINAL_PRINTF("\r\nDisk not attached"); + RetVal = FALSE; + } + else + { + TERMINAL_PRINTF("\r\n\r\nDisk Detected\r\n\r\n\n"); + if(disk_initialize(Disk)&STA_NOINIT) + { + TERMINAL_PRINTF("Disk Failed Initialization\r\n"); + RetVal = FALSE; + } + else + { + TERMINAL_PRINTF("Disk Initialization Success!\r\n"); + + + TERMINAL_PRINTF("Probing attached Disk\r\n"); + TERMINAL_PRINTF("-----------------------\r\n"); + + disk_ioctl (Disk, + GET_SECTOR_COUNT, + &NumSectors + ); + disk_ioctl (Disk, + ATA_GET_MODEL, + ModelName + ); + disk_ioctl (Disk, + ATA_GET_REV, + FirmwareVerion + ); + disk_ioctl (Disk, + ATA_GET_SN, + SerialNumber + ); + + + TERMINAL_PRINTF("Model Name: %s\r\n", ModelName); + TERMINAL_PRINTF("Firmware Version: %s\r\n",FirmwareVerion); + TERMINAL_PRINTF("Serial Number: %s\r\n",SerialNumber); + TERMINAL_PRINTF("Number of Sectors: %d\r\n",NumSectors); + RetVal = TRUE; + } + } + + return RetVal; +} \ No newline at end of file
diff -r 6b1e6c9e48ba -r dc171f34db9b Terminal.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.h Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,17 @@ +#include "MODSERIAL.h" + +#ifndef TERMINAL_H_ +#define TERMINAL_H_ + +void ProcessTerminal(); +void InitTerminal(); + +extern MODSERIAL pc; + + +#define TERMINAL_PRINTF(...) pc.printf(__VA_ARGS__) +#define TERMINAL_PUTC(c) pc.putc(c) +#define TERMINAL_READABLE pc.readable() +#define TERMINAL_GETC pc.getc() + +#endif /* TERMINAL_H_ */
diff -r 6b1e6c9e48ba -r dc171f34db9b USBHostLite/usbhost_cpu.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBHostLite/usbhost_cpu.h Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,35 @@ +/* +************************************************************************************************************** +* NXP USB Host Stack +* +* (c) Copyright 2008, NXP SemiConductors +* (c) Copyright 2008, OnChip Technologies LLC +* All Rights Reserved +* +* www.nxp.com +* www.onchiptech.com +* +* File : usbhost_cpu.h +* Programmer(s) : Ravikanth.P +* Version : +* +************************************************************************************************************** +*/ + +#ifndef USBHOST_CPU_H +#define USBHOST_CPU_H + +/* +************************************************************************************************************** +* TYPE DEFINITIONS OF DATA TYPES +************************************************************************************************************** +*/ + +typedef unsigned int USB_INT32U; +typedef signed int USB_INT32S; +typedef unsigned short USB_INT16U; +typedef signed short USB_INT16S; +typedef unsigned char USB_INT08U; +typedef signed char USB_INT08S; + +#endif
diff -r 6b1e6c9e48ba -r dc171f34db9b USBHostLite/usbhost_err.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBHostLite/usbhost_err.h Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,63 @@ +/* +************************************************************************************************************** +* NXP USB Host Stack +* +* (c) Copyright 2008, NXP SemiConductors +* (c) Copyright 2008, OnChip Technologies LLC +* All Rights Reserved +* +* www.nxp.com +* www.onchiptech.com +* +* File : usbhost_err.h +* Programmer(s) : Ravikanth.P +* Version : +* +************************************************************************************************************** +*/ + +#ifndef USBHOST_ERR_H +#define USBHOST_ERR_H + + +/* +************************************************************************************************************** +* GENERAL DEFINITIONS +************************************************************************************************************** +*/ + +#define OK 0 +#define MATCH_FOUND 0 + +/* +************************************************************************************************************** +* HOST CONTROLLER SPECIFIC ERROR CODES +************************************************************************************************************** +*/ + +#define ERR_TD_FAIL -1 + +/* +************************************************************************************************************** +* MASS STORAGE SPECIFIC ERROR CODES +************************************************************************************************************** +*/ + +#define ERR_MS_CMD_FAILED -10 +#define ERR_BAD_CONFIGURATION -11 +#define ERR_NO_MS_INTERFACE -12 + +/* +************************************************************************************************************** +* FAT SPECIFIC ERROR CODES +************************************************************************************************************** +*/ + +#define MATCH_NOT_FOUND -20 +#define ERR_FAT_NOT_SUPPORTED -21 +#define ERR_OPEN_LIMIT_REACHED -22 +#define ERR_INVALID_BOOT_SIG -23 +#define ERR_INVALID_BOOT_SEC -24 +#define ERR_ROOT_DIR_FULL -25 + +#endif
diff -r 6b1e6c9e48ba -r dc171f34db9b USBHostLite/usbhost_inc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBHostLite/usbhost_inc.h Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,39 @@ +/* +************************************************************************************************************** +* NXP USB Host Stack +* +* (c) Copyright 2008, NXP SemiConductors +* (c) Copyright 2008, OnChip Technologies LLC +* All Rights Reserved +* +* www.nxp.com +* www.onchiptech.com +* +* File : usbhost_inc.h +* Programmer(s) : Ravikanth.P +* Version : +* +************************************************************************************************************** +*/ + +#ifndef USBHOST_INC_H +#define USBHOST_INC_H + +/* +************************************************************************************************************** +* INCLUDE HEADER FILES +************************************************************************************************************** +*/ + +#include "usbhost_cpu.h" +#include "usbhost_err.h" +#include "usbhost_lpc17xx.h" +#include "usbhost_ms.h" +#include "mbed.h" + + +#ifdef TARGET_LPC2368 +#error "There is no USB host on the LPC2368!" +#endif + +#endif
diff -r 6b1e6c9e48ba -r dc171f34db9b USBHostLite/usbhost_lpc17xx.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBHostLite/usbhost_lpc17xx.c Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,821 @@ +/* +************************************************************************************************************** +* NXP USB Host Stack +* +* (c) Copyright 2008, NXP SemiConductors +* (c) Copyright 2008, OnChip Technologies LLC +* All Rights Reserved +* +* www.nxp.com +* www.onchiptech.com +* +* File : usbhost_lpc17xx.c +* Programmer(s) : Ravikanth.P +* Version : +* +************************************************************************************************************** +*/ + +/* +************************************************************************************************************** +* INCLUDE HEADER FILES +************************************************************************************************************** +*/ + +#include "usbhost_lpc17xx.h" + +/* +************************************************************************************************************** +* GLOBAL VARIABLES +************************************************************************************************************** +*/ +volatile int gUSBConnected; + +volatile USB_INT32U HOST_RhscIntr = 0; /* Root Hub Status Change interrupt */ +volatile USB_INT32U HOST_WdhIntr = 0; /* Semaphore to wait until the TD is submitted */ +volatile USB_INT08U HOST_TDControlStatus = 0; +volatile HCED *EDCtrl; /* Control endpoint descriptor structure */ +volatile HCED *EDBulkIn; /* BulkIn endpoint descriptor structure */ +volatile HCED *EDBulkOut; /* BulkOut endpoint descriptor structure */ +volatile HCTD *TDHead; /* Head transfer descriptor structure */ +volatile HCTD *TDTail; /* Tail transfer descriptor structure */ +volatile HCCA *Hcca; /* Host Controller Communications Area structure */ + USB_INT16U *TDBufNonVol; /* Identical to TDBuffer just to reduce compiler warnings */ +volatile USB_INT08U *TDBuffer; /* Current Buffer Pointer of transfer descriptor */ + +// USB host structures +// AHB SRAM block 1 +#define HOSTBASEADDR 0x2007C000 +// reserve memory for the linker +static USB_INT08U HostBuf[0x200] __attribute__((at(HOSTBASEADDR))); +/* +************************************************************************************************************** +* DELAY IN MILLI SECONDS +* +* Description: This function provides a delay in milli seconds +* +* Arguments : delay The delay required +* +* Returns : None +* +************************************************************************************************************** +*/ + +void Host_DelayMS (USB_INT32U delay) +{ + volatile USB_INT32U i; + + + for (i = 0; i < delay; i++) { + Host_DelayUS(1000); + } +} + +/* +************************************************************************************************************** +* DELAY IN MICRO SECONDS +* +* Description: This function provides a delay in micro seconds +* +* Arguments : delay The delay required +* +* Returns : None +* +************************************************************************************************************** +*/ + +void Host_DelayUS (USB_INT32U delay) +{ + volatile USB_INT32U i; + + + for (i = 0; i < (4 * delay); i++) { /* This logic was tested. It gives app. 1 micro sec delay */ + ; + } +} + +// bits of the USB/OTG clock control register +#define HOST_CLK_EN (1<<0) +#define DEV_CLK_EN (1<<1) +#define PORTSEL_CLK_EN (1<<3) +#define AHB_CLK_EN (1<<4) + +// bits of the USB/OTG clock status register +#define HOST_CLK_ON (1<<0) +#define DEV_CLK_ON (1<<1) +#define PORTSEL_CLK_ON (1<<3) +#define AHB_CLK_ON (1<<4) + +// we need host clock, OTG/portsel clock and AHB clock +#define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN) + +/* +************************************************************************************************************** +* INITIALIZE THE HOST CONTROLLER +* +* Description: This function initializes lpc17xx host controller +* +* Arguments : None +* +* Returns : +* +************************************************************************************************************** +*/ +void Host_Init(void) +{ + // PRINT_Log("In Host_Init\n"); + NVIC_DisableIRQ(USB_IRQn); /* Disable the USB interrupt source */ + + // turn on power for USB + LPC_SC->PCONP |= (1UL<<31); + // Enable USB host clock, port selection and AHB clock + LPC_USB->USBClkCtrl |= CLOCK_MASK; + // Wait for clocks to become available + while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK) + ; + + // it seems the bits[0:1] mean the following + // 0: U1=device, U2=host + // 1: U1=host, U2=host + // 2: reserved + // 3: U1=host, U2=device + // NB: this register is only available if OTG clock (aka "port select") is enabled!! + // since we don't care about port 2, set just bit 0 to 1 (U1=host) + LPC_USB->OTGStCtrl |= 1; + + // now that we've configured the ports, we can turn off the portsel clock + LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; + + // power pins are not connected on mbed, so we can skip them + /* P1[18] = USB_UP_LED, 01 */ + /* P1[19] = /USB_PPWR, 10 */ + /* P1[22] = USB_PWRD, 10 */ + /* P1[27] = /USB_OVRCR, 10 */ + /*LPC_PINCON->PINSEL3 &= ~((3<<4) | (3<<6) | (3<<12) | (3<<22)); + LPC_PINCON->PINSEL3 |= ((1<<4)|(2<<6) | (2<<12) | (2<<22)); // 0x00802080 + */ + + // configure USB D+/D- pins + /* P0[29] = USB_D+, 01 */ + /* P0[30] = USB_D-, 01 */ + LPC_PINCON->PINSEL1 &= ~((3<<26) | (3<<28)); + LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // 0x14000000 + + //PRINT_Log("Initializing Host Stack\n"); + + Hcca = (volatile HCCA *)(HostBuf+0x000); + TDHead = (volatile HCTD *)(HostBuf+0x100); + TDTail = (volatile HCTD *)(HostBuf+0x110); + EDCtrl = (volatile HCED *)(HostBuf+0x120); + EDBulkIn = (volatile HCED *)(HostBuf+0x130); + EDBulkOut = (volatile HCED *)(HostBuf+0x140); + TDBuffer = (volatile USB_INT08U *)(HostBuf+0x150); + + /* Initialize all the TDs, EDs and HCCA to 0 */ + Host_EDInit(EDCtrl); + Host_EDInit(EDBulkIn); + Host_EDInit(EDBulkOut); + Host_TDInit(TDHead); + Host_TDInit(TDTail); + Host_HCCAInit(Hcca); + + Host_DelayMS(50); /* Wait 50 ms before apply reset */ + LPC_USB->HcControl = 0; /* HARDWARE RESET */ + LPC_USB->HcControlHeadED = 0; /* Initialize Control list head to Zero */ + LPC_USB->HcBulkHeadED = 0; /* Initialize Bulk list head to Zero */ + + /* SOFTWARE RESET */ + LPC_USB->HcCommandStatus = OR_CMD_STATUS_HCR; + LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL; /* Write Fm Interval and Largest Data Packet Counter */ + + /* Put HC in operational state */ + LPC_USB->HcControl = (LPC_USB->HcControl & (~OR_CONTROL_HCFS)) | OR_CONTROL_HC_OPER; + LPC_USB->HcRhStatus = OR_RH_STATUS_LPSC; /* Set Global Power */ + + LPC_USB->HcHCCA = (USB_INT32U)Hcca; + LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus; /* Clear Interrrupt Status */ + + + LPC_USB->HcInterruptEnable = OR_INTR_ENABLE_MIE | + OR_INTR_ENABLE_WDH | + OR_INTR_ENABLE_RHSC; + + NVIC_SetPriority(USB_IRQn, 0); /* highest priority */ + /* Enable the USB Interrupt */ + NVIC_EnableIRQ(USB_IRQn); + // PRINT_Log("Host Initialized\n"); +} + +/* +************************************************************************************************************** +* INTERRUPT SERVICE ROUTINE +* +* Description: This function services the interrupt caused by host controller +* +* Arguments : None +* +* Returns : None +* +************************************************************************************************************** +*/ + +void USB_IRQHandler (void) __irq +{ + USB_INT32U int_status; + USB_INT32U ie_status; + + int_status = LPC_USB->HcInterruptStatus; /* Read Interrupt Status */ + ie_status = LPC_USB->HcInterruptEnable; /* Read Interrupt enable status */ + + if (!(int_status & ie_status)) { + return; + } else { + + int_status = int_status & ie_status; + if (int_status & OR_INTR_STATUS_RHSC) { /* Root hub status change interrupt */ + if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_CSC) { + if (LPC_USB->HcRhStatus & OR_RH_STATUS_DRWE) { + /* + * When DRWE is on, Connect Status Change + * means a remote wakeup event. + */ + HOST_RhscIntr = 1;// JUST SOMETHING FOR A BREAKPOINT + } + else { + /* + * When DRWE is off, Connect Status Change + * is NOT a remote wakeup event + */ + if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_CCS) { + if (!gUSBConnected) { + HOST_TDControlStatus = 0; + HOST_WdhIntr = 0; + HOST_RhscIntr = 1; + gUSBConnected = 1; + } + else + PRINT_Log("Spurious status change (connected)?\n"); + } else { + if (gUSBConnected) { + LPC_USB->HcInterruptEnable = 0; // why do we get multiple disc. rupts??? + HOST_RhscIntr = 0; + gUSBConnected = 0; + } + else + PRINT_Log("Spurious status change (disconnected)?\n"); + } + } + LPC_USB->HcRhPortStatus1 = OR_RH_PORT_CSC; + } + if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRSC) { + LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; + } + } + if (int_status & OR_INTR_STATUS_WDH) { /* Writeback Done Head interrupt */ + HOST_WdhIntr = 1; + HOST_TDControlStatus = (TDHead->Control >> 28) & 0xf; + } + LPC_USB->HcInterruptStatus = int_status; /* Clear interrupt status register */ + } + return; +} + + +/* +************************************************************************************************************** +* PROCESS TRANSFER DESCRIPTOR +* +* Description: This function processes the transfer descriptor +* +* Arguments : ed Endpoint descriptor that contains this transfer descriptor +* token SETUP, IN, OUT +* buffer Current Buffer Pointer of the transfer descriptor +* buffer_len Length of the buffer +* +* Returns : OK if TD submission is successful +* ERROR if TD submission fails +* +************************************************************************************************************** +*/ + +USB_INT32S Host_ProcessTD (volatile HCED *ed, + volatile USB_INT32U token, + volatile USB_INT08U *buffer, + USB_INT32U buffer_len) +{ + volatile USB_INT32U td_toggle; + + + if (ed == EDCtrl) { + if (token == TD_SETUP) { + td_toggle = TD_TOGGLE_0; + } else { + td_toggle = TD_TOGGLE_1; + } + } else { + td_toggle = 0; + } + TDHead->Control = (TD_ROUNDING | + token | + TD_DELAY_INT(0) | + td_toggle | + TD_CC); + TDTail->Control = 0; + TDHead->CurrBufPtr = (USB_INT32U) buffer; + TDTail->CurrBufPtr = 0; + TDHead->Next = (USB_INT32U) TDTail; + TDTail->Next = 0; + TDHead->BufEnd = (USB_INT32U)(buffer + (buffer_len - 1)); + TDTail->BufEnd = 0; + + ed->HeadTd = (USB_INT32U)TDHead | ((ed->HeadTd) & 0x00000002); + ed->TailTd = (USB_INT32U)TDTail; + ed->Next = 0; + + if (ed == EDCtrl) { + LPC_USB->HcControlHeadED = (USB_INT32U)ed; + LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_CLF; + LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_CLE; + } else { + LPC_USB->HcBulkHeadED = (USB_INT32U)ed; + LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_BLF; + LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_BLE; + } + + Host_WDHWait(); + +// if (!(TDHead->Control & 0xF0000000)) { + if (!HOST_TDControlStatus) { + return (OK); + } else { + return (ERR_TD_FAIL); + } +} + +/* +************************************************************************************************************** +* ENUMERATE THE DEVICE +* +* Description: This function is used to enumerate the device connected +* +* Arguments : None +* +* Returns : None +* +************************************************************************************************************** +*/ + +USB_INT32S Host_EnumDev (void) +{ + USB_INT32S rc; + + // PRINT_Log("Connect a Mass Storage device\r\n"); + while (!HOST_RhscIntr) + __WFI(); + Host_DelayMS(100); /* USB 2.0 spec says atleast 50ms delay beore port reset */ + LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRS; // Initiate port reset + while (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRS) + __WFI(); // Wait for port reset to complete... + LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; // ...and clear port reset signal + Host_DelayMS(200); /* Wait for 100 MS after port reset */ + + EDCtrl->Control = 8 << 16; /* Put max pkt size = 8 */ + /* Read first 8 bytes of device desc */ + rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_DEVICE, 0, TDBuffer, 8); + if (rc != OK) { + PRINT_Err(rc); + return (rc); + } + EDCtrl->Control = TDBuffer[7] << 16; /* Get max pkt size of endpoint 0 */ + rc = HOST_SET_ADDRESS(1); /* Set the device address to 1 */ + if (rc != OK) { + PRINT_Err(rc); + return (rc); + } + Host_DelayMS(2); + EDCtrl->Control = (EDCtrl->Control) | 1; /* Modify control pipe with address 1 */ + /* Get the configuration descriptor */ + rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, 9); + if (rc != OK) { + PRINT_Err(rc); + return (rc); + } + /* Get the first configuration data */ + rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, ReadLE16U(&TDBuffer[2])); + if (rc != OK) { + PRINT_Err(rc); + return (rc); + } + rc = MS_ParseConfiguration(); /* Parse the configuration */ + if (rc != OK) { + PRINT_Err(rc); + return (rc); + } + rc = USBH_SET_CONFIGURATION(1); /* Select device configuration 1 */ + if (rc != OK) { + PRINT_Err(rc); + } + Host_DelayMS(100); /* Some devices may require this delay */ + return (rc); +} + +/* +************************************************************************************************************** +* RECEIVE THE CONTROL INFORMATION +* +* Description: This function is used to receive the control information +* +* Arguments : bm_request_type +* b_request +* w_value +* w_index +* w_length +* buffer +* +* Returns : OK if Success +* ERROR if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S Host_CtrlRecv ( USB_INT08U bm_request_type, + USB_INT08U b_request, + USB_INT16U w_value, + USB_INT16U w_index, + USB_INT16U w_length, + volatile USB_INT08U *buffer) +{ + USB_INT32S rc; + + + Host_FillSetup(bm_request_type, b_request, w_value, w_index, w_length); + rc = Host_ProcessTD(EDCtrl, TD_SETUP, TDBuffer, 8); + if (rc == OK) { + if (w_length) { + rc = Host_ProcessTD(EDCtrl, TD_IN, TDBuffer, w_length); + } + if (rc == OK) { + rc = Host_ProcessTD(EDCtrl, TD_OUT, NULL, 0); + } + } + return (rc); +} + +/* +************************************************************************************************************** +* SEND THE CONTROL INFORMATION +* +* Description: This function is used to send the control information +* +* Arguments : None +* +* Returns : OK if Success +* ERR_INVALID_BOOTSIG if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S Host_CtrlSend ( USB_INT08U bm_request_type, + USB_INT08U b_request, + USB_INT16U w_value, + USB_INT16U w_index, + USB_INT16U w_length, + volatile USB_INT08U *buffer) +{ + USB_INT32S rc; + + + Host_FillSetup(bm_request_type, b_request, w_value, w_index, w_length); + + rc = Host_ProcessTD(EDCtrl, TD_SETUP, TDBuffer, 8); + if (rc == OK) { + if (w_length) { + rc = Host_ProcessTD(EDCtrl, TD_OUT, TDBuffer, w_length); + } + if (rc == OK) { + rc = Host_ProcessTD(EDCtrl, TD_IN, NULL, 0); + } + } + return (rc); +} + +/* +************************************************************************************************************** +* FILL SETUP PACKET +* +* Description: This function is used to fill the setup packet +* +* Arguments : None +* +* Returns : OK if Success +* ERR_INVALID_BOOTSIG if Failed +* +************************************************************************************************************** +*/ + +void Host_FillSetup (USB_INT08U bm_request_type, + USB_INT08U b_request, + USB_INT16U w_value, + USB_INT16U w_index, + USB_INT16U w_length) +{ + int i; + for (i=0;i<w_length;i++) + TDBuffer[i] = 0; + + TDBuffer[0] = bm_request_type; + TDBuffer[1] = b_request; + WriteLE16U(&TDBuffer[2], w_value); + WriteLE16U(&TDBuffer[4], w_index); + WriteLE16U(&TDBuffer[6], w_length); +} + + + +/* +************************************************************************************************************** +* INITIALIZE THE TRANSFER DESCRIPTOR +* +* Description: This function initializes transfer descriptor +* +* Arguments : Pointer to TD structure +* +* Returns : None +* +************************************************************************************************************** +*/ + +void Host_TDInit (volatile HCTD *td) +{ + + td->Control = 0; + td->CurrBufPtr = 0; + td->Next = 0; + td->BufEnd = 0; +} + +/* +************************************************************************************************************** +* INITIALIZE THE ENDPOINT DESCRIPTOR +* +* Description: This function initializes endpoint descriptor +* +* Arguments : Pointer to ED strcuture +* +* Returns : None +* +************************************************************************************************************** +*/ + +void Host_EDInit (volatile HCED *ed) +{ + + ed->Control = 0; + ed->TailTd = 0; + ed->HeadTd = 0; + ed->Next = 0; +} + +/* +************************************************************************************************************** +* INITIALIZE HOST CONTROLLER COMMUNICATIONS AREA +* +* Description: This function initializes host controller communications area +* +* Arguments : Pointer to HCCA +* +* Returns : +* +************************************************************************************************************** +*/ + +void Host_HCCAInit (volatile HCCA *hcca) +{ + USB_INT32U i; + + + for (i = 0; i < 32; i++) { + + hcca->IntTable[i] = 0; + hcca->FrameNumber = 0; + hcca->DoneHead = 0; + } + +} + +/* +************************************************************************************************************** +* WAIT FOR WDH INTERRUPT +* +* Description: This function is infinite loop which breaks when ever a WDH interrupt rises +* +* Arguments : None +* +* Returns : None +* +************************************************************************************************************** +*/ + +void Host_WDHWait (void) +{ + while (!HOST_WdhIntr) + __WFI(); + + HOST_WdhIntr = 0; +} + +/* +************************************************************************************************************** +* READ LE 32U +* +* Description: This function is used to read an unsigned integer from a character buffer in the platform +* containing little endian processor +* +* Arguments : pmem Pointer to the character buffer +* +* Returns : val Unsigned integer +* +************************************************************************************************************** +*/ + +USB_INT32U ReadLE32U (volatile USB_INT08U *pmem) +{ + USB_INT32U val = *(USB_INT32U*)pmem; +#ifdef __BIG_ENDIAN + return __REV(val); +#else + return val; +#endif +} + +/* +************************************************************************************************************** +* WRITE LE 32U +* +* Description: This function is used to write an unsigned integer into a charecter buffer in the platform +* containing little endian processor. +* +* Arguments : pmem Pointer to the charecter buffer +* val Integer value to be placed in the charecter buffer +* +* Returns : None +* +************************************************************************************************************** +*/ + +void WriteLE32U (volatile USB_INT08U *pmem, + USB_INT32U val) +{ +#ifdef __BIG_ENDIAN + *(USB_INT32U*)pmem = __REV(val); +#else + *(USB_INT32U*)pmem = val; +#endif +} + +/* +************************************************************************************************************** +* READ LE 16U +* +* Description: This function is used to read an unsigned short integer from a charecter buffer in the platform +* containing little endian processor +* +* Arguments : pmem Pointer to the charecter buffer +* +* Returns : val Unsigned short integer +* +************************************************************************************************************** +*/ + +USB_INT16U ReadLE16U (volatile USB_INT08U *pmem) +{ + USB_INT16U val = *(USB_INT16U*)pmem; +#ifdef __BIG_ENDIAN + return __REV16(val); +#else + return val; +#endif +} + +/* +************************************************************************************************************** +* WRITE LE 16U +* +* Description: This function is used to write an unsigned short integer into a charecter buffer in the +* platform containing little endian processor +* +* Arguments : pmem Pointer to the charecter buffer +* val Value to be placed in the charecter buffer +* +* Returns : None +* +************************************************************************************************************** +*/ + +void WriteLE16U (volatile USB_INT08U *pmem, + USB_INT16U val) +{ +#ifdef __BIG_ENDIAN + *(USB_INT16U*)pmem = (__REV16(val) & 0xFFFF); +#else + *(USB_INT16U*)pmem = val; +#endif +} + +/* +************************************************************************************************************** +* READ BE 32U +* +* Description: This function is used to read an unsigned integer from a charecter buffer in the platform +* containing big endian processor +* +* Arguments : pmem Pointer to the charecter buffer +* +* Returns : val Unsigned integer +* +************************************************************************************************************** +*/ + +USB_INT32U ReadBE32U (volatile USB_INT08U *pmem) +{ + USB_INT32U val = *(USB_INT32U*)pmem; +#ifdef __BIG_ENDIAN + return val; +#else + return __REV(val); +#endif +} + +/* +************************************************************************************************************** +* WRITE BE 32U +* +* Description: This function is used to write an unsigned integer into a charecter buffer in the platform +* containing big endian processor +* +* Arguments : pmem Pointer to the charecter buffer +* val Value to be placed in the charecter buffer +* +* Returns : None +* +************************************************************************************************************** +*/ + +void WriteBE32U (volatile USB_INT08U *pmem, + USB_INT32U val) +{ +#ifdef __BIG_ENDIAN + *(USB_INT32U*)pmem = val; +#else + *(USB_INT32U*)pmem = __REV(val); +#endif +} + +/* +************************************************************************************************************** +* READ BE 16U +* +* Description: This function is used to read an unsigned short integer from a charecter buffer in the platform +* containing big endian processor +* +* Arguments : pmem Pointer to the charecter buffer +* +* Returns : val Unsigned short integer +* +************************************************************************************************************** +*/ + +USB_INT16U ReadBE16U (volatile USB_INT08U *pmem) +{ + USB_INT16U val = *(USB_INT16U*)pmem; +#ifdef __BIG_ENDIAN + return val; +#else + return __REV16(val); +#endif +} + +/* +************************************************************************************************************** +* WRITE BE 16U +* +* Description: This function is used to write an unsigned short integer into the charecter buffer in the +* platform containing big endian processor +* +* Arguments : pmem Pointer to the charecter buffer +* val Value to be placed in the charecter buffer +* +* Returns : None +* +************************************************************************************************************** +*/ + +void WriteBE16U (volatile USB_INT08U *pmem, + USB_INT16U val) +{ +#ifdef __BIG_ENDIAN + *(USB_INT16U*)pmem = val; +#else + *(USB_INT16U*)pmem = (__REV16(val) & 0xFFFF); +#endif +}
diff -r 6b1e6c9e48ba -r dc171f34db9b USBHostLite/usbhost_lpc17xx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBHostLite/usbhost_lpc17xx.h Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,259 @@ +/* +************************************************************************************************************** +* NXP USB Host Stack +* +* (c) Copyright 2008, NXP SemiConductors +* (c) Copyright 2008, OnChip Technologies LLC +* All Rights Reserved +* +* www.nxp.com +* www.onchiptech.com +* +* File : usbhost_lpc17xx.h +* Programmer(s) : Ravikanth.P +* Version : +* +************************************************************************************************************** +*/ + +#include "Terminal.h" + +#ifndef USBHOST_LPC17xx_H +#define USBHOST_LPC17xx_H + +/* +************************************************************************************************************** +* INCLUDE HEADER FILES +************************************************************************************************************** +*/ + +#include "usbhost_inc.h" + +/* +************************************************************************************************************** +* PRINT CONFIGURATION +************************************************************************************************************** +*/ + +#define PRINT_ENABLE 1 + +#if PRINT_ENABLE +#define PRINT_Log(...) pc.printf(__VA_ARGS__);pc.printf("\r\n") +#define PRINT_Err(rc) pc.printf("ERROR: In %s at Line %u - rc = %d\r\n", __FUNCTION__, __LINE__, rc) + +#else +#define PRINT_Log(...) do {} while(0) +#define PRINT_Err(rc) do {} while(0) + +#endif + +/* +************************************************************************************************************** +* GENERAL DEFINITIONS +************************************************************************************************************** +*/ + +#define DESC_LENGTH(x) x[0] +#define DESC_TYPE(x) x[1] + + +#define HOST_GET_DESCRIPTOR(descType, descIndex, data, length) \ + Host_CtrlRecv(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, \ + (descType << 8)|(descIndex), 0, length, data) + +#define HOST_SET_ADDRESS(new_addr) \ + Host_CtrlSend(USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, SET_ADDRESS, \ + new_addr, 0, 0, NULL) + +#define USBH_SET_CONFIGURATION(configNum) \ + Host_CtrlSend(USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, SET_CONFIGURATION, \ + configNum, 0, 0, NULL) + +#define USBH_SET_INTERFACE(ifNum, altNum) \ + Host_CtrlSend(USB_HOST_TO_DEVICE | USB_RECIPIENT_INTERFACE, SET_INTERFACE, \ + altNum, ifNum, 0, NULL) + +/* +************************************************************************************************************** +* OHCI OPERATIONAL REGISTER FIELD DEFINITIONS +************************************************************************************************************** +*/ + + /* ------------------ HcControl Register --------------------- */ +#define OR_CONTROL_CLE 0x00000010 +#define OR_CONTROL_BLE 0x00000020 +#define OR_CONTROL_HCFS 0x000000C0 +#define OR_CONTROL_HC_OPER 0x00000080 + /* ----------------- HcCommandStatus Register ----------------- */ +#define OR_CMD_STATUS_HCR 0x00000001 +#define OR_CMD_STATUS_CLF 0x00000002 +#define OR_CMD_STATUS_BLF 0x00000004 + /* --------------- HcInterruptStatus Register ----------------- */ +#define OR_INTR_STATUS_WDH 0x00000002 +#define OR_INTR_STATUS_RHSC 0x00000040 + /* --------------- HcInterruptEnable Register ----------------- */ +#define OR_INTR_ENABLE_WDH 0x00000002 +#define OR_INTR_ENABLE_RHSC 0x00000040 +#define OR_INTR_ENABLE_MIE 0x80000000 + /* ---------------- HcRhDescriptorA Register ------------------ */ +#define OR_RH_STATUS_LPSC 0x00010000 +#define OR_RH_STATUS_DRWE 0x00008000 + /* -------------- HcRhPortStatus[1:NDP] Register -------------- */ +#define OR_RH_PORT_CCS 0x00000001 +#define OR_RH_PORT_PRS 0x00000010 +#define OR_RH_PORT_CSC 0x00010000 +#define OR_RH_PORT_PRSC 0x00100000 + + +/* +************************************************************************************************************** +* FRAME INTERVAL +************************************************************************************************************** +*/ + +#define FI 0x2EDF /* 12000 bits per frame (-1) */ +#define DEFAULT_FMINTERVAL ((((6 * (FI - 210)) / 7) << 16) | FI) + +/* +************************************************************************************************************** +* TRANSFER DESCRIPTOR CONTROL FIELDS +************************************************************************************************************** +*/ + +#define TD_ROUNDING (USB_INT32U) (0x00040000) /* Buffer Rounding */ +#define TD_SETUP (USB_INT32U)(0) /* Direction of Setup Packet */ +#define TD_IN (USB_INT32U)(0x00100000) /* Direction In */ +#define TD_OUT (USB_INT32U)(0x00080000) /* Direction Out */ +#define TD_DELAY_INT(x) (USB_INT32U)((x) << 21) /* Delay Interrupt */ +#define TD_TOGGLE_0 (USB_INT32U)(0x02000000) /* Toggle 0 */ +#define TD_TOGGLE_1 (USB_INT32U)(0x03000000) /* Toggle 1 */ +#define TD_CC (USB_INT32U)(0xF0000000) /* Completion Code */ + +/* +************************************************************************************************************** +* USB STANDARD REQUEST DEFINITIONS +************************************************************************************************************** +*/ + +#define USB_DESCRIPTOR_TYPE_DEVICE 1 +#define USB_DESCRIPTOR_TYPE_CONFIGURATION 2 +#define USB_DESCRIPTOR_TYPE_INTERFACE 4 +#define USB_DESCRIPTOR_TYPE_ENDPOINT 5 + /* ----------- Control RequestType Fields ----------- */ +#define USB_DEVICE_TO_HOST 0x80 +#define USB_HOST_TO_DEVICE 0x00 +#define USB_REQUEST_TYPE_CLASS 0x20 +#define USB_RECIPIENT_DEVICE 0x00 +#define USB_RECIPIENT_INTERFACE 0x01 + /* -------------- USB Standard Requests -------------- */ +#define SET_ADDRESS 5 +#define GET_DESCRIPTOR 6 +#define SET_CONFIGURATION 9 +#define SET_INTERFACE 11 + +/* +************************************************************************************************************** +* TYPE DEFINITIONS +************************************************************************************************************** +*/ + +typedef struct hcEd { /* ----------- HostController EndPoint Descriptor ------------- */ + volatile USB_INT32U Control; /* Endpoint descriptor control */ + volatile USB_INT32U TailTd; /* Physical address of tail in Transfer descriptor list */ + volatile USB_INT32U HeadTd; /* Physcial address of head in Transfer descriptor list */ + volatile USB_INT32U Next; /* Physical address of next Endpoint descriptor */ +} HCED; + +typedef struct hcTd { /* ------------ HostController Transfer Descriptor ------------ */ + volatile USB_INT32U Control; /* Transfer descriptor control */ + volatile USB_INT32U CurrBufPtr; /* Physical address of current buffer pointer */ + volatile USB_INT32U Next; /* Physical pointer to next Transfer Descriptor */ + volatile USB_INT32U BufEnd; /* Physical address of end of buffer */ +} HCTD; + +typedef struct hcca { /* ----------- Host Controller Communication Area ------------ */ + volatile USB_INT32U IntTable[32]; /* Interrupt Table */ + volatile USB_INT32U FrameNumber; /* Frame Number */ + volatile USB_INT32U DoneHead; /* Done Head */ + volatile USB_INT08U Reserved[116]; /* Reserved for future use */ + volatile USB_INT08U Unknown[4]; /* Unused */ +} HCCA; + +/* +************************************************************************************************************** +* EXTERN DECLARATIONS +************************************************************************************************************** +*/ + +extern volatile HCED *EDBulkIn; /* BulkIn endpoint descriptor structure */ +extern volatile HCED *EDBulkOut; /* BulkOut endpoint descriptor structure */ +extern volatile HCTD *TDHead; /* Head transfer descriptor structure */ +extern volatile HCTD *TDTail; /* Tail transfer descriptor structure */ +extern volatile USB_INT08U *TDBuffer; /* Current Buffer Pointer of transfer descriptor */ +extern volatile USB_INT32U HOST_RhscIntr; + +extern volatile int gUSBConnected; + +/* +************************************************************************************************************** +* FUNCTION PROTOTYPES +************************************************************************************************************** +*/ + +void Host_Init (void); + +extern "C" void USB_IRQHandler(void) __irq; + +USB_INT32S Host_EnumDev (void); + +USB_INT32S Host_ProcessTD(volatile HCED *ed, + volatile USB_INT32U token, + volatile USB_INT08U *buffer, + USB_INT32U buffer_len); + +void Host_DelayUS ( USB_INT32U delay); +void Host_DelayMS ( USB_INT32U delay); + + +void Host_TDInit (volatile HCTD *td); +void Host_EDInit (volatile HCED *ed); +void Host_HCCAInit (volatile HCCA *hcca); + +USB_INT32S Host_CtrlRecv ( USB_INT08U bm_request_type, + USB_INT08U b_request, + USB_INT16U w_value, + USB_INT16U w_index, + USB_INT16U w_length, + volatile USB_INT08U *buffer); + +USB_INT32S Host_CtrlSend ( USB_INT08U bm_request_type, + USB_INT08U b_request, + USB_INT16U w_value, + USB_INT16U w_index, + USB_INT16U w_length, + volatile USB_INT08U *buffer); + +void Host_FillSetup( USB_INT08U bm_request_type, + USB_INT08U b_request, + USB_INT16U w_value, + USB_INT16U w_index, + USB_INT16U w_length); + + +void Host_WDHWait (void); + + +USB_INT32U ReadLE32U (volatile USB_INT08U *pmem); +void WriteLE32U (volatile USB_INT08U *pmem, + USB_INT32U val); +USB_INT16U ReadLE16U (volatile USB_INT08U *pmem); +void WriteLE16U (volatile USB_INT08U *pmem, + USB_INT16U val); +USB_INT32U ReadBE32U (volatile USB_INT08U *pmem); +void WriteBE32U (volatile USB_INT08U *pmem, + USB_INT32U val); +USB_INT16U ReadBE16U (volatile USB_INT08U *pmem); +void WriteBE16U (volatile USB_INT08U *pmem, + USB_INT16U val); + +#endif
diff -r 6b1e6c9e48ba -r dc171f34db9b USBHostLite/usbhost_ms.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBHostLite/usbhost_ms.c Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,455 @@ +/* +************************************************************************************************************** +* NXP USB Host Stack +* +* (c) Copyright 2008, NXP SemiConductors +* (c) Copyright 2008, OnChip Technologies LLC +* All Rights Reserved +* +* www.nxp.com +* www.onchiptech.com +* +* File : usbhost_ms.c +* Programmer(s) : Ravikanth.P +* Version : +* +************************************************************************************************************** +*/ + +/* +************************************************************************************************************** +* INCLUDE HEADER FILES +************************************************************************************************************** +*/ + +#include "usbhost_ms.h" + +/* +************************************************************************************************************** +* GLOBAL VARIABLES +************************************************************************************************************** +*/ + +USB_INT32U MS_BlkSize; + +/* +************************************************************************************************************** +* INITIALIZE MASS STORAGE INTERFACE +* +* Description: This function initializes the mass storage interface +* +* Arguments : None +* +* Returns : OK if Success +* ERR_INVALID_BOOTSIG if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S MS_Init (USB_INT32U *blkSize, USB_INT32U *numBlks, USB_INT08U *inquiryResult) +{ + USB_INT08U retry; + USB_INT32S rc; + + MS_GetMaxLUN(); /* Get maximum logical unit number */ + retry = 80; + while(retry) { + rc = MS_TestUnitReady(); /* Test whether the unit is ready */ + if (rc == OK) { + break; + } + MS_GetSenseInfo(); /* Get sense information */ + retry--; + } + if (rc != OK) { + PRINT_Err(rc); + return (rc); + } + rc = MS_ReadCapacity(numBlks, blkSize); /* Read capacity of the disk */ + MS_BlkSize = *blkSize; // Set global + rc = MS_Inquire (inquiryResult); + return (rc); +} +/* +************************************************************************************************************** +* PARSE THE CONFIGURATION +* +* Description: This function is used to parse the configuration +* +* Arguments : None +* +* Returns : OK if Success +* ERR_INVALID_BOOTSIG if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S MS_ParseConfiguration (void) +{ + volatile USB_INT08U *desc_ptr; + USB_INT08U ms_int_found; + + + desc_ptr = TDBuffer; + ms_int_found = 0; + + if (desc_ptr[1] != USB_DESCRIPTOR_TYPE_CONFIGURATION) { + return (ERR_BAD_CONFIGURATION); + } + desc_ptr += desc_ptr[0]; + + while (desc_ptr != TDBuffer + ReadLE16U(&TDBuffer[2])) { + + switch (desc_ptr[1]) { + + case USB_DESCRIPTOR_TYPE_INTERFACE: /* If it is an interface descriptor */ + if (desc_ptr[5] == MASS_STORAGE_CLASS && /* check if the class is mass storage */ + desc_ptr[6] == MASS_STORAGE_SUBCLASS_SCSI && /* check if the subclass is SCSI */ + desc_ptr[7] == MASS_STORAGE_PROTOCOL_BO) { /* check if the protocol is Bulk only */ + ms_int_found = 1; + desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ + } + break; + + case USB_DESCRIPTOR_TYPE_ENDPOINT: /* If it is an endpoint descriptor */ + if ((desc_ptr[3] & 0x03) == 0x02) { /* If it is Bulk endpoint */ + if (desc_ptr[2] & 0x80) { /* If it is In endpoint */ + EDBulkIn->Control = 1 | /* USB address */ + ((desc_ptr[2] & 0x7F) << 7) | /* Endpoint address */ + (2 << 11) | /* direction */ + (ReadLE16U(&desc_ptr[4]) << 16); /* MaxPkt Size */ + desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ + } else { /* If it is Out endpoint */ + EDBulkOut->Control = 1 | /* USB address */ + ((desc_ptr[2] & 0x7F) << 7) | /* Endpoint address */ + (1 << 11) | /* direction */ + (ReadLE16U(&desc_ptr[4]) << 16); /* MaxPkt Size */ + desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ + } + } else { /* If it is not bulk end point */ + desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ + } + break; + + default: /* If the descriptor is neither interface nor endpoint */ + desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ + break; + } + } + if (ms_int_found) { + PRINT_Log("Mass Storage device connected\n"); + return (OK); + } else { + PRINT_Log("Not a Mass Storage device\n"); + return (ERR_NO_MS_INTERFACE); + } +} + +/* +************************************************************************************************************** +* GET MAXIMUM LOGICAL UNIT +* +* Description: This function returns the maximum logical unit from the device +* +* Arguments : None +* +* Returns : OK if Success +* ERR_INVALID_BOOTSIG if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S MS_GetMaxLUN (void) +{ + USB_INT32S rc; + + + rc = Host_CtrlRecv(USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE, + MS_GET_MAX_LUN_REQ, + 0, + 0, + 1, + TDBuffer); + return (rc); +} + +/* +************************************************************************************************************** +* GET SENSE INFORMATION +* +* Description: This function is used to get sense information from the device +* +* Arguments : None +* +* Returns : OK if Success +* ERROR if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S MS_GetSenseInfo (void) +{ + USB_INT32S rc; + + + Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_REQUEST_SENSE, 6); + rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, 18); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); + if (rc == OK) { + if (TDBuffer[12] != 0) { + rc = ERR_MS_CMD_FAILED; + } + } + } + } + return (rc); +} + +/* +************************************************************************************************************** +* TEST UNIT READY +* +* Description: This function is used to test whether the unit is ready or not +* +* Arguments : None +* +* Returns : OK if Success +* ERROR if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S MS_TestUnitReady (void) +{ + USB_INT32S rc; + + + Fill_MSCommand(0, 0, 0, MS_DATA_DIR_NONE, SCSI_CMD_TEST_UNIT_READY, 6); + rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); + if (rc == OK) { + if (TDBuffer[12] != 0) { + rc = ERR_MS_CMD_FAILED; + } + } + } + return (rc); +} + +/* +************************************************************************************************************** +* READ CAPACITY +* +* Description: This function is used to read the capacity of the mass storage device +* +* Arguments : None +* +* Returns : OK if Success +* ERROR if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S MS_ReadCapacity (USB_INT32U *numBlks, USB_INT32U *blkSize) +{ + USB_INT32S rc; + + + Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_READ_CAPACITY, 10); + rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, 8); + if (rc == OK) { + if (numBlks) + *numBlks = ReadBE32U(&TDBuffer[0]); + if (blkSize) + *blkSize = ReadBE32U(&TDBuffer[4]); + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); + if (rc == OK) { + if (TDBuffer[12] != 0) { + rc = ERR_MS_CMD_FAILED; + } + } + } + } + return (rc); +} + + + +USB_INT32S MS_Inquire (USB_INT08U *response) +{ + USB_INT32S rc; + USB_INT32U i; + + Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_INQUIRY, 6); + rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, INQUIRY_LENGTH); + if (rc == OK) { + if (response) { + for ( i = 0; i < INQUIRY_LENGTH; i++ ) + *response++ = *TDBuffer++; +#if 0 + MemCpy (response, TDBuffer, INQUIRY_LENGTH); + StrNullTrailingSpace (response->vendorID, SCSI_INQUIRY_VENDORCHARS); + StrNullTrailingSpace (response->productID, SCSI_INQUIRY_PRODUCTCHARS); + StrNullTrailingSpace (response->productRev, SCSI_INQUIRY_REVCHARS); +#endif + } + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); + if (rc == OK) { + if (TDBuffer[12] != 0) { // bCSWStatus byte + rc = ERR_MS_CMD_FAILED; + } + } + } + } + return (rc); +} + +/* +************************************************************************************************************** +* RECEIVE THE BULK DATA +* +* Description: This function is used to receive the bulk data +* +* Arguments : None +* +* Returns : OK if Success +* ERR_INVALID_BOOTSIG if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S MS_BulkRecv ( USB_INT32U block_number, + USB_INT16U num_blocks, + volatile USB_INT08U *user_buffer) +{ + USB_INT32S rc; + int i; + volatile USB_INT08U *c = user_buffer; + for (i=0;i<MS_BlkSize*num_blocks;i++) + *c++ = 0; + + + Fill_MSCommand(block_number, MS_BlkSize, num_blocks, MS_DATA_DIR_IN, SCSI_CMD_READ_10, 10); + + rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkIn, TD_IN, user_buffer, MS_BlkSize * num_blocks); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); + if (rc == OK) { + if (TDBuffer[12] != 0) { + rc = ERR_MS_CMD_FAILED; + } + } + } + } + return (rc); +} + +/* +************************************************************************************************************** +* SEND BULK DATA +* +* Description: This function is used to send the bulk data +* +* Arguments : None +* +* Returns : OK if Success +* ERR_INVALID_BOOTSIG if Failed +* +************************************************************************************************************** +*/ + +USB_INT32S MS_BulkSend ( USB_INT32U block_number, + USB_INT16U num_blocks, + volatile USB_INT08U *user_buffer) +{ + USB_INT32S rc; + + + Fill_MSCommand(block_number, MS_BlkSize, num_blocks, MS_DATA_DIR_OUT, SCSI_CMD_WRITE_10, 10); + + rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkOut, TD_OUT, user_buffer, MS_BlkSize * num_blocks); + if (rc == OK) { + rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); + if (rc == OK) { + if (TDBuffer[12] != 0) { + rc = ERR_MS_CMD_FAILED; + } + } + } + } + return (rc); +} + +/* +************************************************************************************************************** +* FILL MASS STORAGE COMMAND +* +* Description: This function is used to fill the mass storage command +* +* Arguments : None +* +* Returns : OK if Success +* ERR_INVALID_BOOTSIG if Failed +* +************************************************************************************************************** +*/ + +void Fill_MSCommand (USB_INT32U block_number, + USB_INT32U block_size, + USB_INT16U num_blocks, + MS_DATA_DIR direction, + USB_INT08U scsi_cmd, + USB_INT08U scsi_cmd_len) +{ + USB_INT32U data_len; + static USB_INT32U tag_cnt = 0; + USB_INT32U cnt; + + + for (cnt = 0; cnt < CBW_SIZE; cnt++) { + TDBuffer[cnt] = 0; + } + switch(scsi_cmd) { + + case SCSI_CMD_TEST_UNIT_READY: + data_len = 0; + break; + case SCSI_CMD_READ_CAPACITY: + data_len = 8; + break; + case SCSI_CMD_REQUEST_SENSE: + data_len = 18; + break; + case SCSI_CMD_INQUIRY: + data_len = 36; + break; + default: + data_len = block_size * num_blocks; + break; + } + WriteLE32U(TDBuffer, CBW_SIGNATURE); + WriteLE32U(&TDBuffer[4], tag_cnt); + WriteLE32U(&TDBuffer[8], data_len); + TDBuffer[12] = (direction == MS_DATA_DIR_NONE) ? 0 : direction; + TDBuffer[14] = scsi_cmd_len; /* Length of the CBW */ + TDBuffer[15] = scsi_cmd; + if ((scsi_cmd == SCSI_CMD_REQUEST_SENSE) + || (scsi_cmd == SCSI_CMD_INQUIRY)) { + TDBuffer[19] = (USB_INT08U)data_len; + } else { + WriteBE32U(&TDBuffer[17], block_number); + } + WriteBE16U(&TDBuffer[22], num_blocks); +}
diff -r 6b1e6c9e48ba -r dc171f34db9b USBHostLite/usbhost_ms.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBHostLite/usbhost_ms.h Fri Dec 30 21:02:16 2011 +0000 @@ -0,0 +1,101 @@ +/* +************************************************************************************************************** +* NXP USB Host Stack +* +* (c) Copyright 2008, NXP SemiConductors +* (c) Copyright 2008, OnChip Technologies LLC +* All Rights Reserved +* +* www.nxp.com +* www.onchiptech.com +* +* File : usbhost_ms.h +* Programmer(s) : Ravikanth.P +* Version : +* +************************************************************************************************************** +*/ + +#ifndef USBHOST_MS_H +#define USBHOST_MS_H + +/* +************************************************************************************************************** +* INCLUDE HEADER FILES +************************************************************************************************************** +*/ + +#include "usbhost_inc.h" + +/* +************************************************************************************************************** +* MASS STORAGE SPECIFIC DEFINITIONS +************************************************************************************************************** +*/ + +#define MS_GET_MAX_LUN_REQ 0xFE +#define MASS_STORAGE_CLASS 0x08 +#define MASS_STORAGE_SUBCLASS_SCSI 0x06 +#define MASS_STORAGE_PROTOCOL_BO 0x50 + +#define INQUIRY_LENGTH 36 +/* +************************************************************************************************************** +* SCSI SPECIFIC DEFINITIONS +************************************************************************************************************** +*/ + +#define CBW_SIGNATURE 0x43425355 +#define CSW_SIGNATURE 0x53425355 +#define CBW_SIZE 31 +#define CSW_SIZE 13 +#define CSW_CMD_PASSED 0x00 +#define SCSI_CMD_REQUEST_SENSE 0x03 +#define SCSI_CMD_TEST_UNIT_READY 0x00 +#define SCSI_CMD_INQUIRY 0x12 +#define SCSI_CMD_READ_10 0x28 +#define SCSI_CMD_READ_CAPACITY 0x25 +#define SCSI_CMD_WRITE_10 0x2A + +/* +************************************************************************************************************** +* TYPE DEFINITIONS +************************************************************************************************************** +*/ + +typedef enum ms_data_dir { + + MS_DATA_DIR_IN = 0x80, + MS_DATA_DIR_OUT = 0x00, + MS_DATA_DIR_NONE = 0x01 + +} MS_DATA_DIR; + +/* +************************************************************************************************************** +* FUNCTION PROTOTYPES +************************************************************************************************************** +*/ + +USB_INT32S MS_BulkRecv ( USB_INT32U block_number, + USB_INT16U num_blocks, + volatile USB_INT08U *user_buffer); + +USB_INT32S MS_BulkSend ( USB_INT32U block_number, + USB_INT16U num_blocks, + volatile USB_INT08U *user_buffer); +USB_INT32S MS_ParseConfiguration(void); +USB_INT32S MS_TestUnitReady (void); +USB_INT32S MS_ReadCapacity (USB_INT32U *numBlks, USB_INT32U *blkSize); +USB_INT32S MS_GetMaxLUN (void); +USB_INT32S MS_GetSenseInfo (void); +USB_INT32S MS_Init (USB_INT32U *blkSize, USB_INT32U *numBlks, USB_INT08U *inquiryResult); +USB_INT32S MS_Inquire (USB_INT08U *response); + +void Fill_MSCommand ( USB_INT32U block_number, + USB_INT32U block_size, + USB_INT16U num_blocks, + MS_DATA_DIR direction, + USB_INT08U scsi_cmd, + USB_INT08U scsi_cmd_len); +#endif
diff -r 6b1e6c9e48ba -r dc171f34db9b main.cpp --- a/main.cpp Tue Dec 27 02:03:56 2011 +0000 +++ b/main.cpp Fri Dec 30 21:02:16 2011 +0000 @@ -1,166 +1,18 @@ #include "mbed.h" #include "ff.h" #include "diskio.h" - -Serial pc(USBTX, USBRX); // tx, rx - -CHAR ModelName[81] = {0}; -CHAR SerialNumber[9] = {0}; -CHAR FirmwareVerion[41] = {0}; -BYTE DataBuf[512]; -BYTE DataBufCheck[512]; - - -Timer DataSpeedTimer; - -#define SECTORS_TO_READ_WRITE 4096 - +#include "MODSERIAL.h" +#include "Terminal.h" +#include "usbhost_inc.h" int main() { - DWORD i,j,k; - DWORD NumSectors; - DWORD Sector; InitCompactFlashInterface(); - float DataWriteSpeed = 0; - float DataReadSpeed = 0; - float DataReadWriteSpeed = 0; - - - for(i=0;i<512;i++) - DataBuf[i] = rand(); - + InitTerminal(); + while(1) { - wait(2.0); - if(disk_status(0)&STA_NODISK) - { - pc.printf("\r\n\r\nNo Disk\r\n"); - } - else - { - pc.printf("\r\n\r\nDisk Detected\r\n\r\n\n"); - if(disk_initialize(0)&STA_NOINIT) - { - pc.printf("Disk Failed Initialization\r\n"); - } - else - { - pc.printf("Disk Initialization Success!\r\n"); - - - pc.printf("Probing attached Disk\r\n"); - pc.printf("-----------------------\r\n"); - - disk_ioctl (0, /* Physical drive nmuber (0) */ - GET_SECTOR_COUNT, /* Control code */ - &NumSectors /* Buffer to send/receive data block */ - ); - disk_ioctl (0, /* Physical drive nmuber (0) */ - ATA_GET_MODEL, /* Control code */ - ModelName /* Buffer to send/receive data block */ - ); - disk_ioctl (0, /* Physical drive nmuber (0) */ - ATA_GET_REV, /* Control code */ - FirmwareVerion /* Buffer to send/receive data block */ - ); - disk_ioctl (0, /* Physical drive nmuber (0) */ - ATA_GET_SN, /* Control code */ - SerialNumber /* Buffer to send/receive data block */ - ); - - - pc.printf("Model Name: %d\r\n", ModelName); - pc.printf("Firmware Version: %s\r\n",FirmwareVerion); - pc.printf("Serial Number: %s\r\n",SerialNumber); - pc.printf("Number of Sectors: %d\r\n",NumSectors); - - DataSpeedTimer.reset(); - DataSpeedTimer.start(); - - pc.printf("Data write test started...\r\n\r\n",DataReadSpeed); - for(i=0;i<SECTORS_TO_READ_WRITE;i++) - { - if(disk_write (0,&DataBuf[0],i,1) == RES_ERROR) - { - pc.printf("Error writing test block %d, stopping.\r\n", i); - break; - } - } - DataSpeedTimer.stop(); - - DataWriteSpeed = (((SECTORS_TO_READ_WRITE * 512.0)/1024.0))/(DataSpeedTimer.read()); - pc.printf("Data write speed %.1f kb/s\r\n",DataWriteSpeed); - - - pc.printf("Data read test started...\r\n\r\n",DataReadSpeed); - DataSpeedTimer.reset(); - DataSpeedTimer.start(); - for(i=0;i<SECTORS_TO_READ_WRITE;i++) - { - if(disk_read (0,&DataBuf[0],i,1) == RES_ERROR) - { - pc.printf("Error reading test block %d, stopping.\r\n", i); - break; - } - } - DataSpeedTimer.stop(); - - DataReadSpeed = (((SECTORS_TO_READ_WRITE * 512.0)/1024.0))/(DataSpeedTimer.read()); - pc.printf("Data read speed %.1f kb/s\r\n",DataReadSpeed); - - - pc.printf("Data read/write test started...\r\n\r\n",DataReadSpeed); - DataSpeedTimer.reset(); - DataSpeedTimer.start(); - for(j=0;j<SECTORS_TO_READ_WRITE;j++) - { - - for(i=0;i<512;i++) - { - DataBuf[i] = rand(); - DataBufCheck[i] = 0; - } - - - - if(disk_write (0,&DataBuf[0],j,1) == RES_ERROR) - { - pc.printf("Error writing test block %d, stopping.\r\n", i); - break; - } - - if(disk_read (0,&DataBufCheck[0],j,1) == RES_ERROR) - { - pc.printf("Error reading test block %d, stopping.\r\n", i); - - break; - } - - - for(i=0;i<512;i++) - { - if(DataBuf[i]!=DataBufCheck[i]) - { - pc.printf("Readback failure on test block %d byte %d\r\n",j,i); - - for(k=0;k<512;k++) - { - pc.printf("k:%d Wrote: %d Read: %d\r\n",k,DataBuf[k],DataBufCheck[k]); - } - break; - } - } - } - DataSpeedTimer.stop(); - - DataReadWriteSpeed = (((SECTORS_TO_READ_WRITE * 512.0)/1024.0))/(DataSpeedTimer.read()); - pc.printf("Data read/write speed %.1f kb/s\r\n",DataReadWriteSpeed); - - } - } - - + ProcessTerminal(); } }