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

    你可能感兴趣的文章
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 下载依赖慢的解决方案(亲测有效)
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>
    npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
    查看>>
    npm—小记
    查看>>
    npm介绍以及常用命令
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>
    npm切换到淘宝源
    查看>>
    npm切换源淘宝源的两种方法
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm包管理深度探索:从基础到进阶全面教程!
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布包--所遇到的问题
    查看>>