Board logo

标题: STM32 对内部FLASH读写接口函数(4) [打印本页]

作者: yuyang911220    时间: 2014-9-25 23:22     标题: STM32 对内部FLASH读写接口函数(4)

/*******************起始指针在Flash页的开始端****************************
else
{ printf("startptr in Page head \r\n");
  if((NumByteToWrite % PAGE_SIZE)!= 0)
  {
    //读取1 PAGE数据到内存,修改,然后写进去
   I2C_EE_BufferRead(0,0,(u16)(tempaddress+NumByteToWrite-(NumByteToWrite% PAGE_SIZE)),PAGE_SIZE,buf1);
   printf("alreadycopy to bug1 \r\n");
   memcpy(buf1,pRomData+NumByteToWrite-(NumByteToWrite% PAGE_SIZE),(NumByteToWrite % PAGE_SIZE));
//end of debug
  }
  //擦除Flash
  if((NumByteToWrite%PAGE_SIZE) == 0 )
  {
      pagenumber = NumByteToWrite/PAGE_SIZE;
  }
  else
  {
      pagenumber = NumByteToWrite/PAGE_SIZE + 1;
  }
   for(EraseCounter = 0; (EraseCounter < pagenumber)&& (FLASHStatus == FLASH_COMPLETE);EraseCounter++)
  {
       FLASHStatus = FLASH_ErasePage(startaddress +(PAGE_SIZE * EraseCounter));
  }

  //写Flash
  if(pagenumber == 1)   /*只有一页
  {   

   i=PAGE_SIZE/4;
   datasource =(uint32_t)buf1;
   FlashAddress= startaddress;
   while(i-->0)
   {
       FLASH_ProgramWord(FlashAddress,*(uint32_t *)datasource);
       if (*(uint32_t *)FlashAddress != *(uint32_t *)datasource)
       {
           printf("I2C_EE_BufferWrite error!\r\n");
           return ;
       }
       datasource +=4;
       FlashAddress +=4;
   }  
  }
  else /*很多页时,先写前面页,最后写buf1
  {
   while(pagenumber-- > 1 )
   {
    datasource= (u32)pRomData;
    FlashAddress= startaddress;
    i=PAGE_SIZE/4;
    while(i-- >0)
    {
           FLASH_ProgramWord( FlashAddress, *(uint32_t *)datasource );
           if (*(uint32_t *)FlashAddress != *(uint32_t *)datasource)
           {
                 printf("I2C_EE_BufferWrite error!\r\n");
                 return ;
           }
           datasource += 4;
           FlashAddress += 4;
    }
   }
   //写后面的页
   datasource= (uint32_t)buf1;
   FlashAddress= startaddress+(pagenumber-1)*PAGE_SIZE;
   i=PAGE_SIZE/4;
   while(i -->0)
   {
      FLASH_ProgramWord( FlashAddress, *(uint32_t*)datasource );
      if (*(uint32_t *)FlashAddress != *(uint32_t *)datasource)
       {
            printf("I2C_EE_BufferWrite error!\r\n");
             return;
       }
      datasource += 4;
      FlashAddress += 4;
   }
  }   
}
}





欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0