作者:陈贞_Rock
转自:http://blog.chinaunix.net/uid-26729065-id-3419459.html
点击(此处)折叠或打开
- #ifndef __FLASH_H__
- #define __FLASH_H__
- #include "stm32f10x.h"
- #include "stm32f10x_flash.h"
- #if defined (STM32F10X_HD)|| defined(STM32F10X_HD_VL)|| defined (STM32F10X_CL)|| defined(STM32F10X_XL)
- #define FLASH_PAGE_SIZE ((uint16_t)0x800)
- #else
- #define FLASH_PAGE_SIZE ((uint16_t)0x400)
- #endif
-
int Flash_Read(uint32_t iAddress, uint8_t*buf, int32_t iNbrToRead);
-
int Flash_Write(uint32_t iAddress, uint8_t*buf, uint32_t iNbrToWrite);
- #endif
源文件:
文件名:FLASH.c
点击(此处)折叠或打开
- #include "FLASH.h"
- uint16_t Flash_Write_Without_check(uint32_t iAddress, uint8_t*buf, uint16_t iNumByteToWrite)
-
{
- uint16_t i;
- volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
- i = 0;
- // FLASH_UnlockBank1();
- while((i< iNumByteToWrite)&&(FLASHStatus== FLASH_COMPLETE))
- {
- FLASHStatus = FLASH_ProgramHalfWord(iAddress,*(uint16_t*)buf);
- i = i+2;
- iAddress = iAddress + 2;
- buf = buf + 2;
- }
- return iNumByteToWrite;
-
}
-
/**
-
* @brief Programs a half word at a specifiedOption Byte Data address.
-
* @note This function can be usedfor all STM32F10x devices.
-
* @param Address: specifies the addressto be programmed.
-
* @param buf: specifies the datato be programmed.
-
* @param iNbrToWrite: the numberto write into flash
-
* @retval if success return the numberto write,-1iferror
-
*
-
*/
-
int Flash_Write(uint32_t iAddress, uint8_t*buf, uint32_t iNbrToWrite)
-
{
- /* Unlock the Flash Bank1 ProgramErase controller*/
- uint32_t secpos;
- uint32_t iNumByteToWrite = iNbrToWrite;
- uint16_t secoff;
- uint16_t secremain;
- uint16_t i = 0;
- uint8_t tmp[FLASH_PAGE_SIZE];
- FLASH_UnlockBank1();
- secpos=iAddress & (~(FLASH_PAGE_SIZE-1));//扇区地址
- secoff=iAddress & (FLASH_PAGE_SIZE -1);//在扇区内的偏移
- secremain=FLASH_PAGE_SIZE-secoff;//扇区剩余空间大小
- volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
- if(iNumByteToWrite<=secremain) secremain= iNumByteToWrite;//不大于4096个字节
- while( 1)
- {
- Flash_Read(secpos, tmp, FLASH_PAGE_SIZE);//读出整个扇区
- for(i=0;i<secremain;i++)
- { //校验数据
- if(tmp[secoff+i]!=0XFF)break;//需要擦除
- }
- if(i<secremain)
- { //需要擦除
- FLASHStatus = FLASH_ErasePage(secpos);//擦除这个扇区
- if(FLASHStatus!= FLASH_COMPLETE)
- return -1;
- for(i=0;i<secremain;i++)//复制
- {
- tmp[i+secoff]=buf[i];
- }
- Flash_Write_Without_check(secpos,tmp,FLASH_PAGE_SIZE);//写入整个扇区
- }
- else
- {
- Flash_Write_Without_check(iAddress,buf,secremain);//写已经擦除了的,直接写入扇区剩余区间.
- }
- if(iNumByteToWrite==secremain)//写入结束了
- {
- break;
- }
- else
- {
- secpos += FLASH_PAGE_SIZE;
- secoff = 0;//偏移位置为0
- buf += secremain;//指针偏移
- iAddress += secremain;//写地址偏移
- iNumByteToWrite -= secremain;//字节数递减
- if(iNumByteToWrite>FLASH_PAGE_SIZE) secremain=FLASH_PAGE_SIZE;//下一个扇区还是写不完
- else secremain = iNumByteToWrite; //下一个扇区可以写完了
- }
- }
- FLASH_LockBank1();
- return iNbrToWrite;
-
}
-
/**
-
* @brief Programs a half word at a specifiedOption Byte Data address.
-
* @note This function can be usedfor all STM32F10x devices.
-
* @param Address: specifies the addressto be programmed.
-
* @param buf: specifies the datato be programmed.
-
* @param iNbrToWrite: the numberto read from flash
-
* @retval if success return the numberto write, withouterror
-
*
-
*/
-
int Flash_Read(uint32_t iAddress, uint8_t*buf, int32_t iNbrToRead)
-
{
- int i = 0;
- while(i< iNbrToRead)
- {
- *(buf+ i)=*(__IO uint8_t*) iAddress++;
- i++;
- }
- return i;
- }