应用层读写nandflash示例

为了不影响其他文件,最好再多分出一个分区,专门用于flash操作
include sys/ioctl.h
include stdio.h
include mtd/mtd-user.h
include sys/types.h
include sys/stat.h
include fcntl.h
include unistd.h
include string.h
include stdlib.h

#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
      };

      unsigned char oobbuf[N];
     
      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);//写时必须页对齐,一次写一页(2k)的整数位
      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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值