因为使用FreeRTOS SDK无法基于NAND FLASH创建文件系统,所以需要NOR FLASH。
一、NOR FLASH说明
FLASH型号W25Q128JVEIQ,容量:16MB。其内部是由数据存储单元和各种控制器组成:
存储单元的最小单位为一个寄存器,每个寄存器可存储1个字节的数据。
每256个寄存器组成一页(Page),也就是一页能存储256Byte数据,
每16页组成一个扇区(Sector),一个扇区能储存16x256=4096Byte数据(近似4KB)。比如扇区0的数据地址范围为000000 h-000FFF h。
每16个扇区又组成一个块(Block),一个块能储存4096x16=65536Byte数据(近似64K)。例如块0的数据地址范围为000000 h-00FFFF h 。
整个存储单元共256个块,所以其总存储容量为256x65536=16777216Byte数据,近似为16MByte。数据地址范围为000000 h-FFFFFF h。
二、固件编译及烧录
1.编译:
make x2100_nor_defconfig
make
选择nor的配置文件即可。
2.烧录:
注意:nor存储器烧录过程会比较慢。
三、用户数据存储
1.FLASH空间划分
预留4MB空间作为固件存储,数据存储起始地址为0x40 0000
2.使能SFC控制器
存储介质为NOR FLASH,时钟频率100MHz。
3.调用驱动操作Flash
包含驱动头文件,头文件位于\freertos\include\driver下
#include <driver/sfc_nor.h>
驱动函数
#ifndef _SFC_NOR_H_
#define _SFC_NOR_H_
#include <stdio.h>
struct flash_info{
const char *name;
uint32_t id;
uint32_t pagesize;
uint32_t chipsize;
uint32_t erasesize;
uint32_t addrsize;
};
void sfc_nor_flash_init(void);
int sfc_nor_flash_read(uint32_t from, uint32_t len, uint8_t *buf);
int sfc_nor_flash_write(uint32_t to, uint32_t len, const uint8_t *buf);
int sfc_nor_flash_erase(uint32_t addr, uint32_t len);
const struct flash_info *sfc_nor_flash_info(void);
#endif /* _SFC_NOR_H_ */
使用之前不需要调用sfc_nor_flash_init()函数,系统已初始化过。
读取Flash信息
struct flash_info *flashInfo;
flashInfo = sfc_nor_flash_info();
printf("flash name:%s\n", flashInfo->name);
printf("flash id:%04X\n", flashInfo->id);
printf("flash pagesize:%d\n", flashInfo->pagesize);
printf("flash chipsize:%d\n", flashInfo->chipsize);
printf("flash erasesize:%d\n", flashInfo->erasesize);
printf("flash addrsize:%d\n", flashInfo->addrsize);
执行结果:
flash name:WIN25Q128JVSQ
flash id:EF4018
flash pagesize:256
flash chipsize:16777216
flash erasesize:4096
flash addrsize:3
擦除、写、读 测试
int ret = 0;
int buffer_test_len = 5;
uint8_t dataWriteBuf[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
uint8_t dataReadBuf[5];
ret = sfc_nor_flash_erase( 0x400000, 0x1000 ); /* offset: 4MByte, length: 4096 */
if (ret < 0)
{
printf("sfc nor flash test: erase failed. ret=%d\n", ret);
return;
}
ret = sfc_nor_flash_write( 0x400000, buffer_test_len, dataWriteBuf );
if (ret != buffer_test_len)
printf("sfc nor flash test: write lenght(%d) not equal actual(%d).\n", buffer_test_len, ret);
ret = sfc_nor_flash_read( 0x400000, buffer_test_len ,dataReadBuf );
if (ret != buffer_test_len)
printf("sfc nor flash test: read lenght(%d) not equal actual(%d).\n", buffer_test_len, ret);
for( int i = 0; i < buffer_test_len; i++ )
{
printf("data[%d]:%02X ", i , dataReadBuf[i]);
}
printf("\n");
执行结果:
data[0]:01 data[1]:02 data[2]:03 data[3]:04 data[4]:05
未完,待续......