- 1 #include "stm32f10x_flash.h"
- 2
- 3 #define StartServerManageFlashAddress ((u32)0x08036000)//读写起始地址(内部flash的主存储块地址从0x08036000开始)
- 4
- 5 //从指定地址开始写入多个数据
- 6 void FLASH_WriteMoreData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite)
- 7 {
- 8 uint32_t offsetAddress=startAddress - FLASH_BASE; //计算去掉0X08000000后的实际偏移地址
- 9 uint32_t sectorPosition=offsetAddress/SECTOR_SIZE; //计算扇区地址,对于STM32F103VET6为0~255
- 10 uint32_t sectorStartAddress=sectorPosition*SECTOR_SIZE+FLASH_BASE; //对应扇区的首地址
- 11 uint16_t dataIndex;
- 12
- 13 if(startAddress<FLASH_BASE||((startAddress+countToWrite*2)>=(FLASH_BASE + SECTOR_SIZE * FLASH_SIZE)))
- 14 {
- 15 return;//非法地址
- 16 }
- 17 FLASH_Unlock(); //解锁写保护
- 18
- 19 FLASH_ErasePage(sectorStartAddress);//擦除这个扇区
- 20
- 21 for(dataIndex=0;dataIndex<countToWrite;dataIndex++)
- 22 {
- 23 FLASH_ProgramHalfWord(startAddress+dataIndex*2,writeData[dataIndex]);
- 24 }
- 25
- 26 FLASH_Lock();//上锁写保护
- 27 }
- 28
- 29 //读取指定地址的半字(16位数据)
- 30 uint16_t FLASH_ReadHalfWord(uint32_t address)
- 31 {
- 32 return *(__IO uint16_t*)address;
- 33 }
- 34
- 35 //从指定地址开始读取多个数据
- 36 void FLASH_ReadMoreData(uint32_t startAddress,uint16_t *readData,uint16_t countToRead)
- 37 {
- 38 uint16_t dataIndex;
- 39 for(dataIndex=0;dataIndex<countToRead;dataIndex++)
- 40 {
- 41 readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2);
- 42 }
- 43 }
- 44
- 45 void write_to_flash(void)
- 46 {
- 47 u16 buff[1200];
- 48 u16 count_len = 2272 / 2;
- 50 FLASH_WriteMoreData(StartServerManageFlashAddress,buff,count_len);
- 55 }
- 56
- 57 void read_from_flash(void)
- 58 {
- 59 u16 buff[1200];
- 60 u16 count_len = 2272 / 2;
- 61 FLASH_WriteMoreData(StartServerManageFlashAddress,buff,count_len);
- 66
- 67 }
|