linux mtd分区应用操作sample之某分区擦除

发布于:2024-06-06 ⋅ 阅读:(160) ⋅ 点赞:(0)

什么是擦除?
把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: 3
  flags: 3072
  Size: 9437184
  Erase Size: 65536
  Write Size: 1
  oobsize Size: 0
  padding Size: 0
millisecond: 3797
Erase operation successful. erase.length is1048576

实践验证了 擦除1M nor flash(64K 块大小)花了3.7S 真的好慢

在这里插入图片描述

AC Characteristics
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到