博客
关于我
应用层读写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/

    你可能感兴趣的文章
    nodejs端口被占用原因及解决方案
    查看>>
    Nodejs简介以及Windows上安装Nodejs
    查看>>
    nodejs系列之express
    查看>>
    nodejs系列之Koa2
    查看>>
    Nodejs连接mysql
    查看>>
    nodejs连接mysql
    查看>>
    NodeJs连接Oracle数据库
    查看>>
    nodejs配置express服务器,运行自动打开浏览器
    查看>>
    NodeMCU教程 http请求获取Json中文乱码解决方案
    查看>>
    Nodemon 深入解析与使用
    查看>>
    NodeSession:高效且灵活的Node.js会话管理工具
    查看>>
    node~ http缓存
    查看>>
    node不是内部命令时配置node环境变量
    查看>>
    node中fs模块之文件操作
    查看>>
    Node中同步与异步的方式读取文件
    查看>>
    Node中的Http模块和Url模块的使用
    查看>>
    Node中自启动工具supervisor的使用
    查看>>
    Node入门之创建第一个HelloNode
    查看>>
    node全局对象 文件系统
    查看>>
    Node出错导致运行崩溃的解决方案
    查看>>