EEPROM读写(可自动跨页,解决“上卷”)

void EPROM_WriteMemory(SPI_WidthType * pWrBuff,uint16_t address,uint32_t length)
{
 
    uint32_t  i;
    uint32_t  FlowSize, FlowLen;
    //if((address + length) > (EEPROM_MAX_ROM_SIZE - 1 ))      //溢出
    //{
    //{return 0;}
      FlowSize = address % EEPROM_MAX_PAGE_SIZE;             //求余
        if(FlowSize)                                         //非页首地址
        {
          FlowLen = EEPROM_MAX_PAGE_SIZE - FlowSize;
          if(FlowLen > length)                               //不跨页
          {
            for(i = 0; i < length; i++)
              {
                  gu8SPI1_TxBuff[i+3] = * pWrBuff;
                  pWrBuff ++;      
              }
             EPROM_WriteSerialMemory(address,length);
          }
          else                                              //跨页
          {
              for(i = 0; i < FlowLen; i++)                  //不跨页部分写入
              {
                  gu8SPI1_TxBuff[i+3] = * pWrBuff;
                  pWrBuff ++;      
              }
              EPROM_WriteSerialMemory(address,FlowLen);
              
              address += FlowLen;                            //new addr
              length -= FlowLen;                            //new len
               for(i = 0; i < length; i++)                 //剩余跨页部分写入
              {
                  gu8SPI1_TxBuff[i+3] = * pWrBuff;
                  pWrBuff ++;
                  
              }
              EPROM_WriteSerialMemory(address,length);
          }
        }     
       else                                                //是页首地址
      {
        for(i = 0; i < length; i++)
              {
                  gu8SPI1_TxBuff[i+3] = * pWrBuff;
                  pWrBuff ++;      
              }
             EPROM_WriteSerialMemory(address,length);
      }
     
}

经验分享 程序员 微信小程序 职场和发展