我是靠谱客的博主 瘦瘦猫咪,最近开发中收集的这篇文章主要介绍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 );
      }
    }
  }
}

最后

以上就是瘦瘦猫咪为你收集整理的FLASH页写入的全部内容,希望文章能够帮你解决FLASH页写入所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部