/****************************************************************************** * Function Name : FSMC_NAND_Test * Description : NAND test * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void FSMC_NAND_Test(void) { uint16_t index; NAND_ADDRESS WriteReadAddr; //FSMC_NAND_Init(); // already done in main printf("\r\n-------------------- NAND FLASH TEST -------------------------"); /* NAND memory address to write to */ WriteReadAddr.Zone = 0x00; WriteReadAddr.Block = 0x00; WriteReadAddr.Page = 0x00; /* Erase the NAND first Block */ FSMC_NAND_EraseBlock(WriteReadAddr); /* Fill the buffer to send */ for (index = 0; index < NAND_PAGE_SIZE; index++ ) { NAND_TxBuffer[index] = index; } /* Write data to FSMC NAND memory */ FSMC_NAND_WriteSmallPage(NAND_TxBuffer, WriteReadAddr, 1); /* Read back the written data */ FSMC_NAND_ReadSmallPage (NAND_RxBuffer, WriteReadAddr, 1); if( memcmp( (char*)NAND_TxBuffer, (char*)NAND_RxBuffer, NAND_PAGE_SIZE ) == 0 ) { printf("\r\n - Result : Nand Flash is OK"); } else { printf("\r\n - Result : Nand Flash is error"); } }
/******************************************************************************* * Function Name : NAND_Write_Cleanup * Description : None * Input : None * Output : None * Return : Status *******************************************************************************/ static uint16_t NAND_Write_Cleanup (void) { uint16_t tempSpareArea [32]; uint16_t Page_Back; if ( Block_State == OLD_BLOCK ) { /* precopy old first pages */ if (Initial_Page != 0) { Page_Back = wAddress.Page; fAddress.Page = wAddress.Page = 0; NAND_Copy (wAddress, fAddress, Initial_Page); wAddress.Page = Page_Back ; } /* postcopy remaining pages */ if ((NAND_BLOCK_SIZE - (wAddress.Page + 1)) != 0) { FSMC_NAND_AddressIncrement(&wAddress); fAddress.Page = wAddress.Page; NAND_Copy (wAddress, fAddress, NAND_BLOCK_SIZE - wAddress.Page); } /* assign logical address to new block */ tempSpareArea [0] = LogAddress | USED_BLOCK ; tempSpareArea [1] = 0xFFFF; tempSpareArea [2] = 0xFFFF; fAddress.Page = 0x00; FSMC_NAND_WriteSpareArea( (uint8_t *)tempSpareArea , fAddress , 1); /* erase old block */ FSMC_NAND_EraseBlock(wAddress); NAND_CleanLUT(wAddress.Zone); } else {/* unused block case */ /* assign logical address to the new used block */ tempSpareArea [0] = LogAddress | USED_BLOCK ; tempSpareArea [1] = 0xFFFF; tempSpareArea [2] = 0xFFFF; wAddress.Page = 0x00; FSMC_NAND_WriteSpareArea((uint8_t *)tempSpareArea , wAddress, 1); NAND_CleanLUT(wAddress.Zone); } return NAND_OK; }
/******************************************************************************* * Function Name : NAND_Format * Description : Format the entire NAND flash * Input : None * Output : None * Return : Status *******************************************************************************/ uint16_t NAND_Format (void) { NAND_ADDRESS phAddress; SPARE_AREA SpareArea; uint32_t BlockIndex; for (BlockIndex = 0 ; BlockIndex < NAND_ZONE_SIZE * NAND_MAX_ZONE; BlockIndex++) { phAddress = NAND_ConvertPhyAddress(BlockIndex * NAND_BLOCK_SIZE ); SpareArea = ReadSpareArea(BlockIndex * NAND_BLOCK_SIZE); if((SpareArea.DataStatus != 0)||(SpareArea.BlockStatus != 0)){ FSMC_NAND_EraseBlock (phAddress); } } NAND_BuildLUT(0); return NAND_OK; }
/** * @brief Main program. * @param None * @retval : None */ int main(void) { /* System Clocks Configuration */ RCC_Configuration(); /* PF.06, PF.07 and PF.08 config to drive LD1, LD2 and LD3 *****************/ /* Enable GPIOF clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); /* Configure PF.06, PF.07 and PF.08 as Output push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOF, &GPIO_InitStructure); /* 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)) { /* NAND memory address to write to */ WriteReadAddr.Zone = 0x00; WriteReadAddr.Block = 0x00; WriteReadAddr.Page = 0x00; /* Erase the NAND first Block */ status = FSMC_NAND_EraseBlock(WriteReadAddr); /* Write data to FSMC NAND memory */ /* Fill the buffer to send */ Fill_Buffer(TxBuffer, BUFFER_SIZE , 0x66); status = FSMC_NAND_WriteSmallPage(TxBuffer, WriteReadAddr, PageNumber); /* Read back the written data */ status = FSMC_NAND_ReadSmallPage (RxBuffer, WriteReadAddr, PageNumber); /* Verify the written data */ for(j = 0; j < BUFFER_SIZE; j++) { if(TxBuffer[j] != RxBuffer[j]) { WriteReadStatus++; } } if (WriteReadStatus == 0) { /* OK */ /* Turn on LD1 */ GPIO_SetBits(GPIOF, GPIO_Pin_6); } else { /* KO */ /* Turn on LD2 */ GPIO_SetBits(GPIOF, GPIO_Pin_7); } } else { /* Turn on LD3 */ GPIO_SetBits(GPIOF, GPIO_Pin_8); } while(1) { } }
void Task1 (void *pdata) { NAND_IDTypeDef NAND_ID; NAND_ADDRESS WriteReadAddr; char pStr[64]; u8 nFlag; __IO uint32_t PageNumber = 2; pdata = pdata; /* Prevent compiler warning */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); FSMC_NAND_Init(); /* NAND memory address to write to */ WriteReadAddr.Zone = 0x00; WriteReadAddr.Block = 0x00; WriteReadAddr.Page = 0x00; /* Erase the NAND first Block */ status = FSMC_NAND_EraseBlock(WriteReadAddr); Fill_Buffer(TxBuffer, BUFFER_SIZE , 0x66); status = FSMC_NAND_WriteSmallPage(TxBuffer, WriteReadAddr, PageNumber); status = FSMC_NAND_ReadSmallPage (RxBuffer, WriteReadAddr, PageNumber); for(;;) { FSMC_NAND_ReadID(&NAND_ID); sprintf(pStr, "Nand Flash ID = %02X,%02X,%02X,%02X ", NAND_ID.Maker_ID, NAND_ID.Device_ID, NAND_ID.Third_ID, NAND_ID.Fourth_ID); // UART_Print(pStr); nFlag = 1; if ((NAND_ID.Maker_ID == 0xEC) && (NAND_ID.Device_ID == 0xF1) && (NAND_ID.Third_ID == 0x80) && (NAND_ID.Fourth_ID == 0x15)) { // UART_Print("Type = K9F1G08U0A\n\r"); nFlag = 2; } else if ((NAND_ID.Maker_ID == 0xEC) && (NAND_ID.Device_ID == 0xF1) && (NAND_ID.Third_ID == 0x00) && (NAND_ID.Fourth_ID == 0x95)) { // UART_Print("Type = K9F1G08U0B\n\r"); nFlag = 3; } else if ((NAND_ID.Maker_ID == 0xEC) && (NAND_ID.Device_ID == 0xF1) && (NAND_ID.Fourth_ID == 0x15)) { // UART_Print("Type = K9F1G08U0M\n\r"); nFlag = 6; } else if ((NAND_ID.Maker_ID == 0xAD) && (NAND_ID.Device_ID == 0xF1) && (NAND_ID.Third_ID == 0x80) && (NAND_ID.Fourth_ID == 0x1D)) { // UART_Print("Type = HY27UF081G2A\n\r"); nFlag = 4; } else { // UART_Print("Type = Unknow\n\r"); nFlag = 5; } OSTimeDly(500); } }