STM32-移植FATFS的NANDFLASH驱动(2)
- UID
- 1029342
- 性别
- 男
|
STM32-移植FATFS的NANDFLASH驱动(2)
5,媒介控制接口:
[cpp] view plaincopy
- DRESULT disk_ioctl (BYTE drv,BYTE ctrl, void *buff)
- {
- DRESULT res = RES_OK;
- uint32_t result;
- if (drv){ return RES_PARERR;}
- switch(ctrl)
- {
- case CTRL_SYNC:
- break;
- case GET_BLOCK_SIZE:
- <span style="color:#000099;">*(DWORD*)buff = NAND_BLOCK_SIZE</span>;
- break;
- case GET_SECTOR_COUNT:
- <span style="color:#000099;">*(DWORD*)buff = (((NAND_MAX_ZONE/2) * NAND_ZONE_SIZE) * NAND_BLOCK_SIZE);
- </span> break;
- case GET_SECTOR_SIZE:
- <span style="color:#000099;">*(WORD*)buff = NAND_PAGE_SIZE;
- </span> break;
- default:
- <span style="color:#000099;"> res = RES_PARERR;
- </span> break;
- }
- return res;
- }
6,媒介多扇区读接口:
[cpp] view plaincopy
- DRESULT disk_read (BYTE drv,BYTE *buff,DWORD sector,BYTE count)
- {
- uint32_t result;
- if (drv || !count){ return RES_PARERR;}
- result = FSMC_NAND_ReadSmallPage(buff, sector, count);
- if(result & NAND_READY){ return RES_OK; }
- else { return RES_ERROR; }
- }
7,媒介多扇区写接口:
[cpp] view plaincopy
- #if _READONLY == 0
- DRESULT disk_write (BYTE drv,const
BYTE *buff,DWORD sector,BYTE count) - {
- uint32_t result;
- uint32_t BackupBlockAddr;
- uint32_t WriteBlockAddr;
- uint16_t IndexTmp = 0;
- uint16_t OffsetPage;
- /* NAND memory write page at block address*/
- WriteBlockAddr = (sector/NAND_BLOCK_SIZE);
- /* NAND memory backup block address*/
- BackupBlockAddr = (WriteBlockAddr + (NAND_MAX_ZONE/2)*NAND_ZONE_SIZE);
- OffsetPage = sector%NAND_BLOCK_SIZE;
- if (drv || !count){ return RES_PARERR;}
- /* Erase the NAND backup Block */
- result = FSMC_NAND_EraseBlock(BackupBlockAddr*NAND_BLOCK_SIZE);
- /* Backup the NAND Write Block to High zone*/
- for (IndexTmp = 0; IndexTmp < NAND_BLOCK_SIZE; IndexTmp++ )
- {
- FSMC_NAND_MoveSmallPage (WriteBlockAddr*NAND_BLOCK_SIZE+IndexTmp,BackupBlockAddr*NAND_BLOCK_SIZE+IndexTmp);
- }
- /* Erase the NAND Write Block */
- result = FSMC_NAND_EraseBlock(WriteBlockAddr*NAND_BLOCK_SIZE);
- /*return write the block with modify*/
- for (IndexTmp = 0; IndexTmp < NAND_BLOCK_SIZE; IndexTmp++ )
- {
- if((IndexTmp>=OffsetPage)&&(IndexTmp < (OffsetPage+count)))
- {
- FSMC_NAND_WriteSmallPage((uint8_t *)buff, WriteBlockAddr*NAND_BLOCK_SIZE+IndexTmp, 1);
- buff = (uint8_t *)buff + NAND_PAGE_SIZE;
- }
- else
- {
- FSMC_NAND_MoveSmallPage (BackupBlockAddr*NAND_BLOCK_SIZE+IndexTmp,WriteBlockAddr*NAND_BLOCK_SIZE+IndexTmp);
- }
- }
- if(result == NAND_READY){ return RES_OK;}
- else { return RES_ERROR;}
- }
- #endif /* _READONLY */
五,调用接口及测试代码
1,调用接口,先初始化FSMC和NANDFLASH:
[cpp] view plaincopy
- //NANDFLASH HY27UF081G2A-TPCB
- #define NAND_HY_MakerID 0xAD
- #define NAND_HY_DeviceID 0xF1
- /* Configure the NAND FLASH */
- void NAND_Configuration(void)
- {
- NAND_IDTypeDef NAND_ID;
- /* Enable the FSMC Clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
- /* FSMC Initialization */
- FSMC_NAND_Init();
- /* NAND read ID command */
- FSMC_NAND_ReadID(&NAND_ID);
- /* Verify the NAND ID */
- if((NAND_ID.Maker_ID == NAND_ST_MakerID) && (NAND_ID.Device_ID == NAND_ST_DeviceID))
- {
- printf("ST NANDFLASH");
- }
- else
- if((NAND_ID.Maker_ID == NAND_HY_MakerID) && (NAND_ID.Device_ID == NAND_HY_DeviceID))
- {
- printf("HY27UF081G2A-TPCB");
- }
- printf(" ID = 0x%x%x%x%x \n\r",NAND_ID.Maker_ID,NAND_ID.Device_ID,NAND_ID.Third_ID,NAND_ID.Fourth_ID);
- }
|
|
|
|
|
|