博客
关于我
应用层读写nandflash示例
阅读量:384 次
发布时间:2019-03-05

本文共 1236 字,大约阅读时间需要 4 分钟。

为了不影响其他文件,最好再多分出一个分区,专门用于Flash操作。以下是实现Flash存储器擦除和写入的C程序示例。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define N 32#define OFS (0) // 存储器区域起始位置#define block_size (128*1024) // 块大小#define page_size (2*1024) // 页大小int main(int argc, const char *argv[]) { int fd; int i, j; unsigned char oob_data[1024*2] = {0x53, 0x50, 0x4c, 0x20, 0, 0xff, 0, 0xff, 0x53, 0x50, 0x4c, 0x20, 0, 0xff, 0, 0xff, 0x53, 0x50, 0x4c, 0x20, 0, 0xff, 0, 0xff, 0x53, 0x50, 0x4c, 0x20, 0, 0xff, 0, 0xff}; struct mtd_oob_buf oob = {0, N, oobbuf}; struct mtd_oob_buf my_oob = {0, N, oob_data}; fd = open("/dev/mtd3", O_RDWR); if (fd < 0) { perror("fail to open\n"); exit(-1); } // 页对齐写入 pwrite(fd, oob_data, 1024*2, 1024*4); memset(oob_data, 0, 32); // 读取数据验证 pread(fd, oob_data, 32, 1024*4); for (i = 0; i < 32; i++) { if (i % 8 == 0) { printf("\n"); } printf("%2x ", oob_data[i]); } printf("\n"); return 0;}

代码解释

  • 包含头文件:程序中包含了必要的系统和嵌入式存储器操作相关头文件。
  • 定义常量:定义了存储器区域的起始位置、块大小和页大小。
  • 主函数实现
    • 打开目标存储器设备文件。
    • 定义并初始化需要擦除和写入的数据区域。
    • 使用pwrite函数执行一次性页对齐的写入操作。
    • 使用memset清除数据区域。
    • 使用pread函数读取数据区域,进行数据验证。
    • 遍历读取数据并输出结果。
  • 使用说明

    该程序用于擦除和写入嵌入式存储器中的某个区域,适用于需要低级别存储器操作的开发和调试场景。

    转载地址:http://ldjwz.baihongyu.com/

    你可能感兴趣的文章
    Node入门之创建第一个HelloNode
    查看>>
    node全局对象 文件系统
    查看>>
    Node出错导致运行崩溃的解决方案
    查看>>
    Node响应中文时解决乱码问题
    查看>>
    node基础(二)_模块以及处理乱码问题
    查看>>
    node安装及配置之windows版
    查看>>
    Node实现小爬虫
    查看>>
    Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
    查看>>
    Node提示:npm does not support Node.js v12.16.3
    查看>>
    Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
    查看>>
    Node服务在断开SSH后停止运行解决方案(创建守护进程)
    查看>>
    node模块化
    查看>>
    node环境下使用import引入外部文件出错
    查看>>
    node编译程序内存溢出
    查看>>
    Node读取并输出txt文件内容
    查看>>
    node防xss攻击插件
    查看>>
    noi 1996 登山
    查看>>
    noi 7827 质数的和与积
    查看>>
    NOI-1.3-11-计算浮点数相除的余数
    查看>>
    NOI2010 海拔(平面图最大流)
    查看>>