博客
关于我
应用层读写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 libararies
    查看>>
    nodejs npm常用命令
    查看>>
    nodejs npm常用命令
    查看>>
    Nodejs process.nextTick() 使用详解
    查看>>
    nodejs 创建HTTP服务器详解
    查看>>
    nodejs 发起 GET 请求示例和 POST 请求示例
    查看>>
    NodeJS 导入导出模块的方法( 代码演示 )
    查看>>
    nodejs 开发websocket 笔记
    查看>>
    nodejs 的 Buffer 详解
    查看>>
    nodejs 读取xlsx文件内容
    查看>>
    nodejs 运行CMD命令
    查看>>
    Nodejs+Express+Mysql实现简单用户管理增删改查
    查看>>
    nodejs+nginx获取真实ip
    查看>>
    nodejs-mime类型
    查看>>
    NodeJs——(11)控制权转移next
    查看>>
    NodeJS、NPM安装配置步骤(windows版本)
    查看>>
    NodeJS、NPM安装配置步骤(windows版本)
    查看>>
    nodejs与javascript中的aes加密
    查看>>
    nodejs中Express 路由统一设置缓存的小技巧
    查看>>
    nodejs中express的使用
    查看>>