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

    你可能感兴趣的文章
    PyQt5教程7:布局Layout管理
    查看>>
    pyqt5教程8:对话框
    查看>>
    pyqt5教程9:Widgets组件
    查看>>
    PyQt5教程——组件(7)
    查看>>
    pyqt5计时器的使用
    查看>>
    PyQt5设计桌面程序步骤
    查看>>
    PyQt5:检查鼠标是否在Enter-Event中按住
    查看>>
    pyqt出现“Ui_Dialog has no attribute “show””
    查看>>
    PyQt:如何使用QAxWidget引用发送参数
    查看>>
    PyQt:我可以制作一个带有图标和文本的工具栏按钮吗?
    查看>>
    pyscopg2:是否可以在循环中动态添加 %s
    查看>>
    Pyserial 缓冲区的填充速度比我读的快
    查看>>
    PySpark - 在数据框中求和一列并将结果返回为 int
    查看>>
    pyspark On Yarn 的模块依赖问题
    查看>>
    PySpeech(Python)-转录MP3文件?
    查看>>
    Pyspider WebUI 未授权访问致远程代码执行漏洞复现
    查看>>
    pyspider爬虫学习-文档翻译-Architecture.md
    查看>>
    pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
    查看>>
    Pytesseract集字符白名单
    查看>>
    PyTorch DataLoader 报错 Segmentation fault (core dumped) 的原因及解决方案
    查看>>