STM32 内部flash的读写程序

来源:互联网 发布:suse网络不通 编辑:程序博客网 时间:2024/06/05 11:42
/* Base address of the Flash sectors */#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes *///stm32f205rbt6 max 128Kbyte#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */static uint32_t GetSector(uint32_t Address){  uint32_t sector = 0;    if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))  {    sector = FLASH_SECTOR_0;    }  else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))  {    sector = FLASH_SECTOR_1;    }  else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))  {    sector = FLASH_SECTOR_2;    }  else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))  {    sector = FLASH_SECTOR_3;    }  else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))  {    sector = FLASH_SECTOR_4;    }  else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))  {    sector = FLASH_SECTOR_5;    }  else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))  {    sector = FLASH_SECTOR_6;    }  else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))  {    sector = FLASH_SECTOR_7;    }  else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))  {    sector = FLASH_SECTOR_8;    }  else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))  {    sector = FLASH_SECTOR_9;    }  else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))  {    sector = FLASH_SECTOR_10;    }  else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11) */  {    sector = FLASH_SECTOR_11;    }  return sector;}


写函数!

/* Get the 1st sector to erase */intwriteFlash(uint32_t startAddr,uint32_t endAddr,uint32_t* data,uint32_t len){/* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*/uint32_t FirstSector = 0, NbOfSectors = 0, Address = 0;uint32_t SectorError;/*Variable used for Erase procedure*/FLASH_EraseInitTypeDef EraseInitStruct;/* Unlock the Flash to enable the flash control register access *************/ HAL_FLASH_Unlock();FirstSector = GetSector(startAddr);/* Get the number of sector to erase from 1st sector*/NbOfSectors = GetSector(endAddr) - FirstSector + 1;/* Fill EraseInit structure*/EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;EraseInitStruct.Sector = FirstSector;EraseInitStruct.NbSectors = NbOfSectors;if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){ /*   Error occurred while sector erase.   User can add here some code to deal with this error.   SectorError will contain the faulty sector and then to know the code error on this sector,  user can call function 'HAL_FLASH_GetError()'*/HAL_FLASH_Lock();return -1;}/* Program the user Flash area word by word    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/Address = startAddr;while(len -- ){if(data == NULL) break;if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, *data) == HAL_OK){      Address = Address + 4;}else{       HAL_FLASH_Lock();      return -1;}data++;}/* Lock the Flash to disable the flash control register access (recommendedto protect the FLASH memory against possible unwanted operation) *********/HAL_FLASH_Lock();return 0;}


读取函数!

int readFlash(uint32_t startAddr,uint32_t *pdata,uint32_t len){if(pdata == NULL) return -1;while(len --){if(pdata == NULL) break;*pdata = *(__IO uint32_t*)startAddr;startAddr += 4;pdata++;}return 0;}

STM的内部flash的读取对应做升级非常方便!IAP做U盘读取文件,写入flash,检验写入都很好!



0 0
原创粉丝点击