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


    什么是擦除?
    把flash相关的区域数据bit置为1的过程
    在这里插入图片描述

    #include 
    #include 
    struct erase_info_user {
    	__u32 start;   // 起点  
    	__u32 length;  //长度    块大小对齐   不然报参数失败    
    };
    
    struct erase_info_user64 {
    	__u64 start;
    	__u64 length;
    };
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include  
    #include 
    #include 
    
    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
    在这里插入图片描述

  • 相关阅读:
    k8s部署手册-v06
    Opencv cuda版本在ubuntu22.04中安装办法,解决Could NOT find CUDNN的办法
    北邮22级信通院数电:Verilog-FPGA(10)第十周实验 实现移位寄存器74LS595(仿真方法验证)
    IO系列第一章——BIO
    使用 Burpsuite 与 xray 进行联动
    完美解决 Error: Cannot find module ‘@vue/cli-plugin-eslint‘ 报错
    初识 Python
    如何在 C 语言中处理不同进制的数字表示?
    springboot事务的失效场景
    容器运行时分析
  • 原文地址:https://blog.csdn.net/M_1308347688/article/details/139395676