什么是擦除?
把flash相关的区域数据bit置为1的过程
#include <mtd/mtd-user.h>
#include <mtd/mtd-abi.h>
struct erase_info_user {__u32 start; // 起点 __u32 length; //长度 块大小对齐 不然报参数失败
};struct erase_info_user64 {__u64 start;__u64 length;
};
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <mtd/mtd-user.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>int main()
{int mtd_fd; // 文件描述符struct erase_info_user erase; // 擦除信息结构体unsigned int erase_start, erase_length;struct mtd_info_user mtd_info;struct timeval tv1,tv2;// 打开MTD设备mtd_fd = open("/dev/mtd1", O_RDWR);if (mtd_fd < 0) {perror("Error opening MTD device");return EXIT_FAILURE;}// 获取MTD设备信息if (ioctl(mtd_fd, MEMGETINFO, &mtd_info)) {perror("Error getting MTD info");close(mtd_fd);return EXIT_FAILURE;}printf("MTD Device Info:\n");printf(" type: %llu\n", mtd_info.type); //识别是nor flash 还是 nand flash printf(" flags: %llu\n", mtd_info.flags);printf(" Size: %llu\n", mtd_info.size); // 分区的大小 printf(" Erase Size: %u\n", mtd_info.erasesize); // 擦除块大小printf(" Write Size: %u\n", mtd_info.writesize);printf(" oobsize Size: %u\n", mtd_info.oobsize);printf(" padding Size: %u\n", mtd_info.padding);// 设置擦除的起始地址和长度// 这些值应该基于设备的擦除块大小和你要擦除的区域erase_start = 0; // 擦除起始地址erase_length = 16*mtd_info.erasesize; // 擦除块数 // 1M空间// 填充擦除信息结构体erase.start = erase_start;erase.length = erase_length;gettimeofday(&tv1, NULL);// 执行擦除操作if (ioctl(mtd_fd, MEMERASE, &erase) < 0) {perror("Error erasing MTD device");close(mtd_fd);return EXIT_FAILURE;}gettimeofday(&tv2, NULL);printf("millisecond: %ld\n",(tv2.tv_sec * 1000 + tv2.tv_usec / 1000) - ( tv1.tv_sec * 1000 + tv1.tv_usec / 1000));printf("Erase operation successful. erase.length is%d\n",erase.length);// 关闭MTD设备close(mtd_fd);return EXIT_SUCCESS;
}
./mtd_test
MTD Device Info:type: 3flags: 3072Size: 9437184Erase Size: 65536Write Size: 1oobsize Size: 0padding Size: 0
millisecond: 3797
Erase operation successful. erase.length is1048576
实践验证了 擦除1M nor flash(64K 块大小)花了3.7S 真的好慢