STM32使用FSMC控制NAND flash 例程
近幾天開發(fā)項(xiàng)目需要用到STM32驅(qū)動(dòng)NAND FLASH,但由于開發(fā)板例程以及固件庫是用于小頁(512B),我要用到的FLASH為1G bit的大頁(2K),多走了兩天彎路。以下筆記將說明如何將默認(rèn)固件庫修改為大頁模式以驅(qū)動(dòng)大容量NAND,并作驅(qū)動(dòng)。
本文硬件:控制器:STM32F103ZET6,存儲(chǔ)器:HY27UF081G2A
首先說一下NOR與NAND存儲(chǔ)器的區(qū)別,此類區(qū)別網(wǎng)上有很多,在此僅大致說明:
1、Nor讀取速度比NAND稍快
2、Nand寫入速度比Nor快很多
3、NAND擦除速度(4ms)遠(yuǎn)快于Nor(5s)
4、Nor 帶有SRAM接口,有足夠的地址引腳來尋址,可以很輕松的掛接到CPU地址和數(shù)據(jù)總線上,對(duì)CPU要求低
5、NAND用八個(gè)(或十六個(gè))引腳串行讀取數(shù)據(jù),數(shù)據(jù)總線地址總線復(fù)用,通常需要CPU支持驅(qū)動(dòng),且較為復(fù)雜
6、Nor主要占據(jù)1-16M容量市場(chǎng),并且可以片內(nèi)執(zhí)行,適合代碼存儲(chǔ)
7、NAND占據(jù)8-128M及以上市場(chǎng),通常用來作數(shù)據(jù)存儲(chǔ)
8、NAND便宜一些
9、NAND壽命比Nor長
10、NAND會(huì)產(chǎn)生壞塊,需要做壞塊處理和ECC
更詳細(xì)區(qū)別請(qǐng)繼續(xù)百度,以上內(nèi)容部分摘自神舟三號(hào)開發(fā)板手冊(cè)
下面是NAND的存儲(chǔ)結(jié)構(gòu):
正如硬盤的盤片被分為磁道,每個(gè)磁道又分為若干扇區(qū),一塊nand flash也分為若干block,每個(gè)block分為如干page。一般而言,block、page之間的關(guān)系隨著芯片的不同而不同。
需要注意的是,對(duì)于flash的讀寫都是以一個(gè)page開始的,但是在讀寫之前必須進(jìn)行flash的擦寫,而擦寫則是以一個(gè)block為單位的。
我們這次使用的HY27UF081G2A其PDF介紹:
Memory Cell Array
= (2K+64) Bytes x 64 Pages x 1,024 Blocks
由此可見,該NAND每頁2K,共64頁,1024塊。其中:每頁中的2K為主容量Data Field,64bit為額外容量Spare Field。Spare Field用于存貯檢驗(yàn)碼和其他信息用的,并不能存放實(shí)際的數(shù)據(jù)。由此可算出系統(tǒng)總?cè)萘繛?K*64*1024=134217728個(gè)byte,即1Gbit。
NAND閃存顆粒硬件接口:
軟件驅(qū)動(dòng):(此部分寫的是偽碼,僅用于解釋含義,可用代碼參見附件)
主程序:
#define BUFFER_SIZE 0x2000 //此部分定義緩沖區(qū)大小,即一次寫入的數(shù)據(jù)
#define NAND_HY_MakerID 0xAD //NAND廠商號(hào)
#define NAND_HY_DeviceID 0xF1 //NAND器件號(hào)
/*配置與SRAM連接的FSMC BANK2 NAND*/
NAND_Init();
/*讀取Nand Flash ID并打印*/
NAND_ReadID(&NAND_ID);
Tips:NAND器件的ID包含四部分:
1st Manufacturer Code
2nd Device Identifier
3rd Internal chip number, cell Type, Number of Simultaneously Programmed
pages.
4th Page size, spare size, Block size, Organization
if((NAND_ID.Maker_ID == NAND_HY_MakerID) && (NAND_ID.Device_ID == NAND_HY_DeviceID)) //判斷器件符合
{
/*設(shè)置NAND FLASH的寫地址*/
WriteReadAddr.Zone = 0x00;
WriteReadAddr.Block = 0x00;
WriteReadAddr.Page = 0x05;
/*擦除待寫入數(shù)據(jù)的塊*/
status = NAND_EraseBlock(WriteReadAddr);//寫入前必須擦出
/*將寫Nand Flash的數(shù)據(jù)BUFFER填充為從0x25開始的連續(xù)遞增的一串?dāng)?shù)據(jù) */
Fill_Buffer(TxBuffer, BUFFER_SIZE , 0x25);//填充數(shù)據(jù)以測(cè)試
/*將數(shù)據(jù)寫入到Nand Flash中。WriteReadAddr:讀寫的起始地址*/
status = NAND_WriteSmallPage(TxBuffer, WriteReadAddr, PageNumber); //主要寫入函數(shù),此部分默認(rèn)為小頁需要修改
/*從Nand Flash中讀回剛寫入的數(shù)據(jù)。?riteReadAddr:讀寫的起始地址*/
status = NAND_ReadSmallPage (RxBuffer, WriteReadAddr, PageNumber); //讀取主要函數(shù),也需要修改
/*判斷讀回的數(shù)據(jù)與寫入的數(shù)據(jù)是否一致*/
for(j = 0; j < BUFFER_SIZE; j++)
{
if(TxBuffer[j] != RxBuffer[j])
{
WriteReadStatus++;
}
}
if (WriteReadStatus == 0)
{
printf("nr Nand Flash讀寫訪問成功");
GPIO_ResetBits(GPIO_LED, DS2_PIN);
}
else
{
printf("nr Nand Flash讀寫訪問失敗");
printf("0x%x",WriteReadStatus);
GPIO_ResetBits(GPIO_LED, DS3_PIN);
}
}
else
{
printf("nr 沒有檢測(cè)到Nand Flash的ID");
GPIO_ResetBits(GPIO_LED, DS4_PIN);
}
fsmc_nand.c文件:
void NAND_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
FSMC_NAND_PCCARDTimingInitTypeDefp;
FSMC_NANDInitTypeDef FSMC_NANDInitStructure;
/*FSMC總線使用的GPIO組時(shí)鐘使能*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);
/*FSMC CLE, ALE, D0->D3, NOE, NWE and NCE2初始化,推挽復(fù)用輸出*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_14 | GPIO_Pin_15 |
GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*FSMC數(shù)據(jù)線FSMC_D[4:7]初始化,推挽復(fù)用輸出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/*FSMC NWAIT初始化,輸入上拉*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*FSMC INT2初始化,輸入上拉*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOG, &GPIO_InitStructure);
/*--------------FSMC 總線 存儲(chǔ)器參數(shù)配置------------------------------*/
p.FSMC_SetupTime = 0x1; //建立時(shí)間
p.FSMC_WaitSetupTime = 0x3; //等待時(shí)間
p.FSMC_HoldSetupTime = 0x2; //保持時(shí)間
p.FSMC_HiZSetupTime = 0x1; //高阻建立時(shí)間
FSMC_NANDInitStructure.FSMC_Bank = FSMC_Bank2_NAND; //使用FSMC BANK2
FSMC_NANDInitStructure.FSMC_Waitfeature = FSMC_Waitfeature_Enable; //使能FSMC的等待功能
FSMC_NANDInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b; //NAND Flash的數(shù)據(jù)寬度為8位
FSMC_NANDInitStructure.FSMC_ECC = FSMC_ECC_Enable; //使能ECC特性
FSMC_NANDInitStructure.FSMC_ECCPageSize = FSMC_ECCPageSize_2048Bytes; //ECC頁大小2048
FSMC_NANDInitStructure.FSMC_TCLRSetupTime = 0x00;
FSMC_NANDInitStructure.FSMC_TARSetupTime = 0x00;
FSMC_NANDInitStructure.FSMC_CommonSpaceTimingStruct = &p;
FSMC_NANDInitStructure.FSMC_AttributeSpaceTimingStruct = &p;
FSMC_NANDInit(&FSMC_NANDInitStructure);
/*!使能FSMC BANK2 */
FSMC_NANDCmd(FSMC_Bank2_NAND, ENABLE);
}
void NAND_ReadID(NAND_IDTypeDef* NAND_ID)
{
uint32_t data = 0;
/*!< Send Command to the command area */
*(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = 0x90;
*(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = 0x00;
/*!< Sequence to read ID from NAND flash */
data = *(__IO uint32_t *)(Bank_NAND_ADDR | DATA_AREA);
NAND_ID->Maker_ID = ADDR_1st_CYCLE (data);//四個(gè)周期讀取四個(gè)ID
NAND_ID->Device_ID= ADDR_2nd_CYCLE (data);
NAND_ID->Third_ID = ADDR_3rd_CYCLE (data);
NAND_ID->Fourth_ID= ADDR_4th_CYCLE (data);
}
uint32_t NAND_WriteSmallPage(uint8_t *pBuffer, NAND_ADDRESS Address, uint32_t NumPageToWrite)
{//傳入?yún)?shù):寫入數(shù)據(jù),寫入初始地址,要寫幾頁
uint32_t index = 0x00, numpagewritten = 0x00, addressstatus = NAND_VALID_ADDRESS;
uint32_t status = NAND_READY, size = 0x00;
while((NumPageToWrite != 0x00) && (addressstatus == NAND_VALID_ADDRESS) && (status == NAND_READY))
{
/*!< Page write command and address */
*(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = NAND_CMD_AREA_A;
*(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = NAND_CMD_WRITE0;
*(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = 0x00;
*(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = 0x00;//添加此句
*(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = ADDR_1st_CYCLE(ROW_ADDRESS);
*(__IO uint8_t *)(Bank_NAND_ADDR | ADD