STM32CubeMX系列教程20:Nand Flash
上图是常见的Nand Flash所拥有的引脚(Pin)所对应的功能,简单翻译如下:
I/O0 ~ I/O7:用于输入地址/数据/命令,输出数据
CLE:Command Latch Enable,命令锁存使能,在输入命令之前,要先在模式寄存器中,设置CLE使能
ALE:Address Latch Enable,地址锁存使能,在输入地址之前,要先在模式寄存器中,设置ALE使能
CE#:Chip Enable,芯片使能,在操作Nand Flash之前,要先选中此芯片,才能操作
RE#:Read Enable,读使能,在读取数据之前,要先使CE#有效。
WE#:Write Enable,写使能,在写取数据之前,要先使WE#有效。
WP#:Write Protect,写保护
R/B#:Ready/Busy Output,就绪/忙,主要用于在发送完编程/擦除命令后,检测这些操作是否完成,忙,表示编程/擦除操作仍在进行中,就绪表示操作完成.
Vcc:Power,电源
Vss:Ground,接地
N.C:Non-Connection,未定义,未连接
FMC 功能块可连接:同步/异步静态存储器、 SDRAM 存储器和 NAND Flash。其主要用途有:
将 AHB 数据通信事务转换为适当的外部器件协议
满足外部存储器器件的访问时间要求
声明变量: RxBuffer,TxBnffer为读写缓存,大小可以Nand flash一页的长度;NAND_ID储存ID信息,WriteReadAddr为存储Nand Flash操作地址。
/* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ /* for K9F1G08 */ #define NAND_PAGE_SIZE ((uint16_t)0x0800) /* 2 * 1024 bytes per page w/o Spare Area */ NAND_IDTypeDef NAND_ID; NAND_AddressTypeDef WriteReadAddr; static uint8_t TxBuffer [NAND_PAGE_SIZE]; static uint8_t RxBuffer [NAND_PAGE_SIZE]; uint16_t i; /* USER CODE END PV */
添加应用程序,程序中先读取Nand Flash ID,然后配置操作地址,对页进行擦除,写,读等操作
/* USER CODE BEGIN 2 */ printf("\r\nNandFlash Example \r\n\r\n"); /* Read the NAND memory ID */ HAL_NAND_Read_ID(&hnand1, &NAND_ID); printf("Nand Flash ID = 0x%02X,0x%02X,0x%02X,0x%02X ",NAND_ID.Maker_Id, NAND_ID.Device_Id, NAND_ID.Third_Id, NAND_ID.Fourth_Id ); if ((NAND_ID.Maker_Id == 0xEC) && (NAND_ID.Device_Id == 0xF1) && (NAND_ID.Third_Id == 0x80) && (NAND_ID.Fourth_Id == 0x15)) { printf("Type = K9F1G08U0A\r\n"); } else if ((NAND_ID.Maker_Id == 0xEC) && (NAND_ID.Device_Id == 0xF1) && (NAND_ID.Third_Id == 0x00) && (NAND_ID.Fourth_Id == 0x95)) { printf("Type = K9F1G08U0B\r\n"); } else if ((NAND_ID.Maker_Id == 0xAD) && (NAND_ID.Device_Id == 0xF1) && (NAND_ID.Third_Id == 0x80) && (NAND_ID.Fourth_Id == 0x1D)) { printf("Type = HY27UF081G2A\r\n"); } else { printf("Type = Unknow\r\n"); } /* NAND memory address to write to */ WriteReadAddr.Zone = 0x00; WriteReadAddr.Block = 0x00; WriteReadAddr.Page = 0x00; /* Erase the NAND first Block */ HAL_NAND_Erase_Block(&hnand1,&WriteReadAddr); /* Fill the buffer to send */ for (i = 0; i < NAND_PAGE_SIZE; i++ ) { TxBuffer[i] = i; } /* Write data to FMC NAND memory */ HAL_NAND_Write_Page(&hnand1, &WriteReadAddr, TxBuffer, 1); printf("\r\nWritten to the number of£º \r\n"); for(i = 0; i < 2048; i++) printf("0x%02X \r",TxBuffer[i]); HAL_Delay(100); /* Read data from FMC NAND memory */ HAL_NAND_Read_Page(&hnand1, &WriteReadAddr, RxBuffer, 1); printf("\r\nRead receive: \r\n"); for(i = 0; i < 2048; i++) printf("0x%02X \r",RxBuffer[i]);