nand flash读写测试
引言
nand flash作为嵌入式系统中的常用设备,在嵌入式产品投入使用初期,需要进行一系列的读写测试,保证产品数据存储的可靠性。nand flash常用的测试方法可分为以下几内:
- uboot阶段,使用uboot中 nand 命令进行读写测试;
- nand flash根文件系统(ubifs格式)中,使用 dd 命令进行测试;
- 利用内核源码下的驱动文件进行测试。
本文主要讲解Linux内核源码下,nand 测试驱动的使用方法。
测试步骤
-
Linux内核源码下,除了系统运行所需的代码,还提供了一些列Linux应用层的测试代码,一般存储在对应驱动模块 tests 目录下,只有编译内核时进行配置,即可使用。
-
通过make menuconfig 使能该驱动模块。
-
将编译好的驱动拷贝到开发板,并查看nand flash的分区情况
-
insmod mtd_speedtest.ko dev=3 count=100
- dev=3 指的是当前的 mtdblock3 所挂在的MTD设备,例如当前使用的是nor flash的MTD分区3。
- count=100 指的是循环测试的次数。
测试结果
- 擦除块写速度: 1824 KiB/s
- 擦除块读速度: 3273 KiB/s
- 页写速度:1820 KiB/s
- 页读速度:3307 KiB/s
……
应用空间读取nand flash中存储的uboot镜像
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#define DEV "/dev/mtd1" // 该分区存放 uboot
#define MAX_BUF 10240000 // 10M
char *buf = NULL;
int main(int argc, char **argv)
{
int flag;
int fdr , fdw;
fdr = open(DEV, O_RDONLY);
if (fdr == -1) {
perror("open dev");
return -1;
}
if (access("uboot", F_OK) == 0) {
printf("uboot exists\n");
unlink("uboot");
}
fdw = open("uboot", O_WRONLY | O_CREAT);
if (fdw == -1) {
perror("open file");
return -2;
}
buf = (char *)malloc(MAX_BUF);
flag = read(fdr, buf, MAX_BUF);
if (flag < 0) {
perror("read");
return -3;
}
flag = write(fdw, buf, MAX_BUF);
if (flag < 0) {
perror("write");
return -4;
}
system("sync");
free(buf);
buf = NULL;
close(fdr);
close(fdw);
return 0;
}