📄 dataflashdev.c
字号:
return (OK); }/********************************************************************************* dataflashIoctl - do device specific control function** This routine is called when the file system cannot handle an ioctl()* function.** RETURNS: OK or ERROR.*/LOCAL STATUS dataflashIoctl ( AT91PS_DataFlash_Device pdataflashDev, int function, int arg ) { FAST int status = ERROR; semTake (&dataflashMuteSem, WAIT_FOREVER); switch (function) { case FIODISKFORMAT: status = OK; break; default: (void) errnoSet (S_ioLib_UNKNOWN_REQUEST); }doneIoctl: semGive (&dataflashMuteSem); return (status); }/********************************************************************************* dataflashBlkRW - read or write sectors to a dataflash disk.** Read or write sectors to a dataflash disk.** RETURNS: OK, ERROR if the command didn't succeed.*/LOCAL STATUS dataflashBlkRW ( AT91PS_DataFlash_Device pDev, int startBlk, int nBlks, char *pBuf, int direction ) { BLK_DEV *pBlkDev = &pDev->blkDev; int src,ix ; int status = ERROR; int semStatus = 0; int Max_Read_DataBlock_Length = pDev->pDevice->pages_size; startBlk+=pDev->blkOffset; src = startBlk* Max_Read_DataBlock_Length; semTake (&dataflashMuteSem, WAIT_FOREVER); memset(databuf,0,1200);if(direction==O_RDONLY){ for (ix = 0; ix < nBlks; ix ++) { AT91F_DataFlashWaitReady(pDev->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT); AT91F_DataFlashPageRead(pDev,src,databuf,Max_Read_DataBlock_Length); semStatus = dataflashWaitReady(AT91C_DATAFLASH_TIMEOUT*1000); if ((dataflashStatus & STAT_ERR) || (semStatus == ERROR)) goto errorRW;/* AT91F_DataFlashRead(pDev,src,Max_Read_DataBlock_Length,databuf); semStatus = OK;*/ memcpy(pBuf,databuf, pBlkDev->bd_bytesPerBlk); #ifdef __TEST__ if(memcmp(pBuf,gpCache+src, pBlkDev->bd_bytesPerBlk)!=0) { fprintf(stderr,"not the same \n"); }#endif src += Max_Read_DataBlock_Length ; pBuf += pBlkDev->bd_bytesPerBlk;/*Max_Read_DataBlock_Length;*/ }}else{ for (ix = 0; ix < nBlks; ix ++) {#ifdef __TEST__ memcpy(gpCache+src,pBuf,pBlkDev->bd_bytesPerBlk); #endif memcpy(databuf,pBuf,pBlkDev->bd_bytesPerBlk); if(AT91F_DataFlashWaitReady(pDev->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT)==DATAFLASH_OK) { AT91F_DataFlashPagePgmBuf(pDev,(unsigned char*)databuf,(src),(Max_Read_DataBlock_Length) ); semStatus = dataflashWaitReady(AT91C_DATAFLASH_TIMEOUT*1000); if ((dataflashStatus & STAT_ERR) || (semStatus == ERROR)) goto errorRW; }else goto errorRW; /* AT91F_DataFlashWrite(pDev , (unsigned int*) databuf ,src,Max_Read_DataBlock_Length); semStatus =OK;*/ #ifdef __TEST__ AT91F_DataFlashWaitReady(pDev->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT); AT91F_DataFlashPageRead(pDev,src,databuf,Max_Read_DataBlock_Length); semStatus = dataflashWaitReady(AT91C_DATAFLASH_TIMEOUT*1000); if ((dataflashStatus & STAT_ERR) || (semStatus == ERROR)) goto errorRW; /*/AT91F_DataFlashWaitReady(pDev->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT); */ /*AT91F_DataFlashRead(pDev,src,Max_Read_DataBlock_Length,databuf);*/ if(0!=memcmp(databuf,pBuf,pBlkDev->bd_bytesPerBlk)) { fprintf(stderr,"write dataflash error \n"); } #endif src += Max_Read_DataBlock_Length ; pBuf += pBlkDev->bd_bytesPerBlk; /*Max_Read_DataBlock_Length;*/ }} status = OK;errorRW: if (status == ERROR) (void)errnoSet (S_ioLib_DEVICE_ERROR); semGive (&dataflashMuteSem); return (status); }/********************************************************************************* dataflashIntr - dataflash controller interrupt handler.** RETURNS: N/A*/LOCAL void dataflashIntr ( int ctrl ) {dataflashStatus = ( AT91C_BASE_SPI->SPI_SR & AT91C_BASE_SPI->SPI_IMR );AT91F_DataFlashHandler(pDataFlash_Device->pDataFlashDesc,dataflashStatus);/*/semGive (&dataflashSyncSem);*/ }/********************************************************************************* dataflashIntr - dataflash controller watchdog handler.** RETURNS: N/A*/LOCAL void dataflashWdog ( int ctrl ) { dataflashWaitForever = FALSE; }/********************************************************************************* dataflashWait - wait the drive ready** Wait the drive ready** RETURNS: OK, ERROR if the drive didn't become ready in certain period of time.*/LOCAL void dataflashWait ( int request ) { dataflashWaitReady(AT91C_DATAFLASH_TIMEOUT); if (dataflashDebug) printErr ("dataflashWait end: \n"); }/********************************************************************************* dataflashDiagnose - diagnose the drive** Diagnose the drive** RETURNS: OK, ERROR if the command didn't succeed.*/LOCAL STATUS dataflashDiagnose (void) { return (OK); }/********************************************************************************* dataflashPinit - Initialize drive parameters** Initialize drive parameters.** RETURNS: OK, ERROR if the command didn't succeed.*/void AT91F_CfgDataFlash (AT91PS_DataFlash_Device pdataflashDev){ /* Init AT91S_DataflashFeatures Structure for AT45DB642*/ DeviceAT45DB.pages_number = 8192; DeviceAT45DB.pages_size = 1056; DeviceAT45DB.page_offset = 11; DeviceAT45DB.byte_mask = 0x700; /* Init AT91S_DataflashFeatures Structure for AT45DCB004 <=> AT45DB321B*/ DeviceAT45DCB.pages_number = 8192; DeviceAT45DCB.pages_size = 528; DeviceAT45DCB.page_offset = 11; /*10*/ DeviceAT45DCB.byte_mask = 0x300; /* Init AT91S_DataflashFeatures Structure for AT45DCB002 <=> AT45DB161B*//* DeviceAT45DCB.pages_number = 4096; DeviceAT45DCB.pages_size = 528; DeviceAT45DCB.page_offset = 10; DeviceAT45DCB.byte_mask = 0x300; */ /* Init AT91S_DataflashDesc Structure*/ DataflashDesc.state = IDLE; DataflashDesc.DataFlash_state = IDLE; /* Init AT91S_DataFlash Global Structure, by default AT45DB choosen !!!*/ pdataflashDev->pDataFlashDesc = &DataflashDesc; pdataflashDev->pDevice = &DeviceAT45DB ;/*/&DeviceAT45DCB;*/}/*---------------------------------------------------------------------------- *//* \fn AT91F_DataFlashWaitReady *//* \brief wait for dataflash ready (bit7 of the status register == 1) */ /*---------------------------------------------------------------------------- */AT91S_DataFlashStatus AT91F_DataFlashWaitReady(AT91PS_DataflashDesc pDataFlashDesc, unsigned int timeout){ unsigned int i; pDataFlashDesc->DataFlash_state = IDLE; do { AT91F_DataFlashGetStatus(pDataFlashDesc); timeout--; /* dummy waiting time*/ for(i=0;i<10;i++); } while( ((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) && (timeout>0) ); if((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) return DATAFLASH_ERROR; return DATAFLASH_OK;}#define BUFFER_SIZE_DATAFLASH 1200void AT91F_Test(AT91PS_DataFlash_Device pDF){ int i; int sect=0; char Buffer_Dataflash[BUFFER_SIZE_DATAFLASH]; /* Wait DataFlash Ready*/ AT91F_DataFlashWaitReady(pDF->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT); /* Read Page 1*/ for(i=0;i<BUFFER_SIZE_DATAFLASH;i++) Buffer_Dataflash[i] = 0x00; AT91F_DataFlashPageRead(pDF,(sect*pDF->pDevice->pages_size),(unsigned char*)Buffer_Dataflash,(pDF->pDevice->pages_size) ); /* Wait end of Read*/ AT91F_DataFlashWaitReady(pDF->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT); /* Write Page 1*/ sprintf(Buffer_Dataflash,"\n\rI am Snow, I want to write to dataflash\n"); AT91F_DataFlashPagePgmBuf(pDF,(unsigned char*)Buffer_Dataflash,(sect*pDF->pDevice->pages_size),(pDF->pDevice->pages_size) ); /* Wait end of Write*/ AT91F_DataFlashWaitReady(pDF->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT); /* Read Page 1*/ for(i=0;i<BUFFER_SIZE_DATAFLASH;i++) Buffer_Dataflash[i] = 0x00; AT91F_DataFlashPageRead(pDF,(sect*pDF->pDevice->pages_size),(unsigned char*)Buffer_Dataflash,(pDF->pDevice->pages_size) ); /* Wait end of Read*/ AT91F_DataFlashWaitReady(pDF->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);}LOCAL STATUS dataflashPinit (AT91PS_DataFlash_Device pDF ) { /******************************/ /** For dataflash Init*/ /******************************/ volatile int status; int i; int timeout = AT91C_DATAFLASH_TIMEOUT ; unsigned char dfcode=0; AT91PS_DataflashDesc pDesc= pDF->pDataFlashDesc; for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) { AT91F_SPI_CfgPCS(AT91C_BASE_SPI,cs[i]); /*/ AT91F_DataFlashGetStatus(pDesc);*/ AT91F_DataFlashWaitReady(pDesc,AT91C_DATAFLASH_TIMEOUT); dfcode = ((pDesc->command[1] == 0xFF)? 0: pDesc->command[1] & 0x3C); switch (dfcode) { case AT45DB161: pDF->pDevice = &DeviceAT45DCB; break; case AT45DB321: pDF->pDevice = &DeviceAT45DCB; break; case AT45DB642: pDF->pDevice = &DeviceAT45DB; break; case AT45DB128: pDF->pDevice = &DeviceAT45DB; break; default: break; } } #if 0// AT91F_SPI_CfgPCS(AT91C_BASE_SPI,AT91C_SPI_PCS3_DATAFLASH_CARD);// pDF->pDevice = &DeviceAT45DCB; /* Wait DataFlash Ready*/// AT91F_DataFlashWaitReady(pDF->pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT); //AT91F_Test(pDF);#endif return (OK); }/********************************************************************************* dataflashPread - Read drive parameters** Read drive parameters.** RETURNS: OK, ERROR if the command didn't succeed.*/LOCAL STATUS dataflashPread ( int drive, void *buffer ) { return (OK); }/********************************************************************************* dataflashRecalib - recalibrate the drive** Recalibrate the drive** RETURNS: OK, ERROR if the command didn't succeed.*/LOCAL STATUS dataflashRecalib ( int drive ) { return (OK); }/********************************************************************************* dataflashSeek - seek the drive heads to the specified cylinder** Seek the drive heads to the specified cylinder** RETURNS: OK, ERROR if the command didn't succeed.*/LOCAL STATUS dataflashSeek(int drive, int cylinder, int head){ return (OK); }/********************************************************************************* dataflashRW - read/write a number of sectors on the current track** Read/write a number of sectors on the current track** RETURNS: OK, ERROR if the command didn't succeed.*//********************************************************************************* dataflashFormat - format the current track** format the current track; not supported.** RETURNS: ERROR always.*/LOCAL STATUS dataflashFormat ( int drive, int cylinder, int head, int interleave ) { return (ERROR); }/********************************************************************************* fdStatusChk - check a status of a floppy disk** This routine checks for a disk change on devices.** RETURNS: OK.*/LOCAL STATUS dataflashStatusChk ( AT91PS_DataFlash_Device pdataflashDev ) {/* pdataflashDev->blkDev.bd_readyChanged = TRUE;*/ pdataflashDev->blkDev.bd_mode = O_RDWR; #if 0 FD_TYPE *pType = &fdTypes[pFdDev->fdType]; wdCancel (fdWid); semTake (&fdMuteSem, WAIT_FOREVER); sysOutByte (FD_REG_OUTPUT, fdDORvalues[pFdDev->drive]); sysDelay (); /* * Bit 7 is set when a diskette is changed. * To clear the bit, we need to perform a seek. */ if (sysInByte (FD_REG_INPUT) & 0x80) { pFdDev->blkDev.bd_readyChanged = TRUE; /* do seek to clear DCHG bit in FD_REG_INPUT */ if (++fdCylinder >= pType->cylinders) fdCylinder = 1; fdDriveSelect (pFdDev->fdType, pFdDev->drive); (void) fdSeek(pFdDev->drive, fdCylinder, 0); /* set the bd_mode per the WP tab */ pFdDev->blkDev.bd_mode = (fdDriveIsWP(pFdDev->drive))? O_RDONLY : O_RDWR; } semGive (&fdMuteSem); wdStart (fdWid, (sysClkRateGet() * fdWdSec), (FUNCPTR)fdDriveRelease, 0);#endif return (OK); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -