本文共 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/