FLASH页写入

一、简介

       由于FLASH特性,页写入,扇区擦除。因此在写入时,需要考虑写入地址和写入长度并对其做相应处理,以下是代码实现,使用的FLASH模块是MX25L12835F。

二、代码实现

#define PAGE_SIZE  256

void FLASH_PAGE_WRITE(uint8_t* buf, uint32_t wICAddr, uint16_t numByteToWrite)
{
  uint8_t numOfPage, numOfSingle, offsetAddr, count;
  //写入整页数
  numOfPage = numByteToWrite / PAGE_SIZE;
  //写入整页后剩余字节数
  numOfSingle = numByteToWrite % PAGE_SIZE;
  //写入地址在页中的偏移量
  offsetAddr = wICAddr % PAGE_SIZE;
  //页中剩余可写入字节
  count = PAGE_SIZE - offsetAddr;

  //写入地址为整
  if (offsetAddr == 0)
  {
    //不足一页
    if (numOfPage == 0)
    {
      flash_write(wICAddr, buf, numOfSingle);
    }
    //超过一页
    else
    {
      //整页处理
      while (numOfPage --)
      {
        flash_write(wICAddr, buf, PAGE_SIZE);
        wICAddr += PAGE_SIZE;
        buf += PAGE_SIZE;
      }

      //不足一页处理
      if (numOfSingle != 0)
      {
        flash_write(wICAddr, buf, numOfSingle);
      }
    }
  }
  //写入地址不为整
  else
  {
    //不足一页
    if (numOfPage == 0)
    {
      //写入字节数不超过页中剩余可写入字节
      if (numOfSingle <= count)
      {
        flash_write(wICAddr, buf, numOfSingle);
      }
      //写入字节数超过页中剩余可写入字节
      else
      {
        flash_write(wICAddr, buf, count);
        wICAddr += count;
        buf += count;
        flash_write(wICAddr, buf, numOfSingle - count);
      }
    }
    //超过一页
    else
    {
      flash_write(wICAddr, buf, count);
      wICAddr += count;
      buf += count;
      numOfPage = (numByteToWrite - count) / PAGE_SIZE;
      numOfSingle = (numByteToWrite - count) % PAGE_SIZE;
      
      //整页处理
      while (numOfPage --)
      {
        flash_write(wICAddr, buf, PAGE_SIZE);
        wICAddr += PAGE_SIZE;
        buf += PAGE_SIZE; 
      }

      //不足一页的处理
      if (numOfSingle != 0)
      {
        flash_write(wICAddr, buf, numOfSingle );
      }
    }
  }
}

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值