加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flash memory E2Rom 等常用到的页写算法

发布时间:2020-12-15 20:05:29 所属栏目:百科 来源:网络整理
导读:long xxxPageWrite(long uAddr,const void *pSrcData,long iLen){ #define PAGE_SIZE 128 //页面大小 #define MAX_ADDRESS 0x10000UL //最大地址 long iDoneBytes=0; //写入的长度 long iOffset,iNewWrLen; unsigned char PageBuff[PAGE_SIZE]; //页面缓冲区
long xxxPageWrite(long uAddr,const void *pSrcData,long iLen)
{
  #define PAGE_SIZE 128 //页面大小
  #define MAX_ADDRESS  0x10000UL //最大地址

  long iDoneBytes=0; //写入的长度
  long iOffset,iNewWrLen;
  unsigned char PageBuff[PAGE_SIZE]; //页面缓冲区  
  const char *pData = (const char *)pSrcData; //源数据
  
  while(iDoneBytes < iLen)
  {
    iOffset = (uAddr % PAGE_SIZE); //页内偏移
    uAddr -= iOffset; //地址对齐到页起始
    if((uAddr + PAGE_SIZE) > MAX_ADDRESS)//超出最大地址
      break;

    //需要写入的字节数
    iNewWrLen = min(PAGE_SIZE,iLen - iDoneBytes) - iOffset; 
   
    if(iOffset //起始部分没有对齐
      || iNewWrLen != PAGE_SIZE //非整页数据
      || 0)//需要读出原始数据
    {
      //整页读出
      //读出从 uAddr 开始的 PAGE_SIZE 个字节数据到 PageBuff
      //比如 I2cRead(uAddr,PageBuff,PAGE_SIZE);      
    }

    //合并新写入数据
    memcpy(PageBuff + iOffset,pData,iNewWrLen);

    //页面写入
    {
      //循环写入PageBuff 的 PAGE_SIZE个字节的数据 到 uAddr
      //比如 I2cWrite(uAddr,PAGE_SIZE);  
    }

    //准备下帧
    uAddr += PAGE_SIZE; //页写地址
    pData += iNewWrLen; //源数据地址
    iDoneBytes += iNewWrLen;  //写入的长度
  }
  
  return iDoneBytes;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读