u8 nand_flash_init(void)
{
FSMC_NANDInitTypeDef FSMC_NandInitTypeDef;
FSMC_NAND_PCCARDTimingInitTypeDef FSMC_NnadPccardTimingInitTypeDef;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_11|GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_11|GPIO_Pin_12);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_14|GPIO_Pin_15);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOG, &GPIO_InitStructure);
FSMC_NnadPccardTimingInitTypeDef.FSMC_SetupTime = 0x1;
FSMC_NnadPccardTimingInitTypeDef.FSMC_WaitSetupTime = 0x3;
FSMC_NnadPccardTimingInitTypeDef.FSMC_HoldSetupTime = 0x2;
FSMC_NnadPccardTimingInitTypeDef.FSMC_HiZSetupTime = 0x1;
FSMC_NandInitTypeDef.FSMC_Bank = FSMC_Bank3_NAND;
FSMC_NandInitTypeDef.FSMC_Waitfeature = FSMC_Waitfeature_Enable;
FSMC_NandInitTypeDef.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
FSMC_NandInitTypeDef.FSMC_ECC = FSMC_ECC_Enable;
FSMC_NandInitTypeDef.FSMC_ECCPageSize = FSMC_ECCPageSize_2048Bytes;
FSMC_NandInitTypeDef.FSMC_TCLRSetupTime = 0x00;
FSMC_NandInitTypeDef.FSMC_TARSetupTime = 0x00;
FSMC_NandInitTypeDef.FSMC_CommonSpaceTimingStruct = &FSMC_NnadPccardTimingInitTypeDef;
FSMC_NandInitTypeDef.FSMC_AttributeSpaceTimingStruct = &FSMC_NnadPccardTimingInitTypeDef;
FSMC_NANDInit(&FSMC_NandInitTypeDef);
/* FSMC NAND Bank Cmd Test */
FSMC_NANDCmd(FSMC_Bank3_NAND, ENABLE);
NAND_Reset();
delay_ms(100);
nand_dev.id=NAND_ReadID();
NAND_ModeSet(4);
if(nand_dev.id==MT29F128G08CFABA)
{
nand_dev.page_totalsize=8936;
nand_dev.page_mainsize=8192;
nand_dev.page_sparesize=744;
nand_dev.block_pagenum=256;
nand_dev.plane_blocknum=2048;
nand_dev.block_totalnum=8192;
}
else return 1;
return 0;
}
u8 NAND_ModeSet(u8 mode)
{
*(__IO uint8_t *)(NAND_ADDRESS|NAND_CMD)=NAND_FEATURE;
*(__IO uint8_t *)(NAND_ADDRESS|NAND_ADDR)=0X01;
*(__IO uint8_t *)NAND_ADDRESS=mode;
*(__IO uint8_t *)NAND_ADDRESS=0;
*(__IO uint8_t *)NAND_ADDRESS=0;
*(__IO uint8_t *)NAND_ADDRESS=0;
if(NAND_WaitForReady()==NSTA_READY)return 0;
else return 1;
}
u32 NAND_ReadID(void)
{
u8 deviceid[8];
u32 id;
*(__IO uint8_t *)(NAND_ADDRESS|NAND_CMD)=NAND_READID;
*(__IO uint8_t *)(NAND_ADDRESS|NAND_ADDR)=0X00;
//IDò»12óD5¸ö×Ö½ú
deviceid[0]=*(vu8*)NAND_ADDRESS;
deviceid[1]=*(vu8*)NAND_ADDRESS;
deviceid[2]=*(vu8*)NAND_ADDRESS;
deviceid[3]=*(vu8*)NAND_ADDRESS;
deviceid[4]=*(vu8*)NAND_ADDRESS;
deviceid[5]=*(vu8*)NAND_ADDRESS;
deviceid[6]=*(vu8*)NAND_ADDRESS;
id=((u32)deviceid[1])<<24|((u32)deviceid[2])<<16|((u32)deviceid[3])<<8|deviceid[4];
return id;
}
u8 NAND_ReadStatus(void)
{
vu8 data=0;
*(__IO uint8_t *)(NAND_ADDRESS|NAND_CMD)=NAND_READSTA;
NAND_Delay(NAND_TWHR_DELAY);
data=*(__IO uint8_t *)NAND_ADDRESS;
return data;
}
u8 NAND_WaitForReady(void)
{
u8 status=0;
vu32 time=0;
while(1)
{
status=NAND_ReadStatus();
if(status&NSTA_READY)break;
time++;
if(time>=0X1FFFFFFF)return NSTA_TIMEOUT;
}
return NSTA_READY;
}
u8 NAND_Reset(void)
{
*(__IO uint8_t *)(NAND_ADDRESS|NAND_CMD)=NAND_RESET;
if(NAND_WaitForReady()==NSTA_READY)return 0;
else return 1;
}
程序是根据原子stm32f207修改的,目前读id失败,找不到错误了,谢谢大神帮忙看!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
好的,谢谢
一周热门 更多>