你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F407.FLASH读写经验

[复制链接]
STMCU小助手 发布时间:2023-1-3 19:04
FLASH1.FLASH Introduce

** Main memory :** It is divided into 4 individual 16KB A sector 、1 individual 64KB Sectors and 7 individual 128KB A sector .boot0/boot1 All of them GND From the 0x08000000 Start running code .
** System memory :** The device starts from the system memory in bootstrap mode , It is mainly used to store chips bootloader Code , This code is solidified inside the chip when leaving the factory . Used to download code to main memory . When boot0 Pick up 3.3V、boot1 Pick up GND Start running from here , That is, enter the serial port download mode .


**OTP Area :**512 byte OTP( One time programmable ), For storing user data .OTP There are additional... In the area 16 Bytes , Used to lock the corresponding OTP Data blocks .
** Option bytes :** Used to configure read / write protection 、BOR Level 、 Software / Reset of hardware watchdog and device in standby or stop mode .

1.1 Programming and erasing of flash memory
STM32F4 The flash programming of is performed by 6 individual 32 Bit register control :
(1)FLASH Access control register (FLASH_ACR)
(2)FLASH Secret key register (FLASH_KEYR)
(3)FLASH Option key register (FLASH_OPTKEYR)
(4)FLASH Status register (FLASH_SR)
(5)FLASH Control register (FLASH_CR)
(6)FLASH Option control register (FLASH_OPTCR)
STM32F4 After reset ,FLASH Programming operations are protected , Cannot write FLASH_CR.
FALSH_CR The unlocking steps are :
(1) Write 0X45670123 To FLASH_KEYR
(2) Write 0XCDEF89AB To FLASH_KEYR
STM32F4 The number of programmed bits of flash memory passes through FLASH_CR Of PSIZE Field settings ,PSIZE The setting of must match the supply voltage .


STM32F4 Standard programming steps :
(1) Check FLASH_SR Medium BSY position , Ensure that no... Is currently being performed FLASH operation .
(2) take FLASH_CR In register PG Location 1, Activate FLASH Programming .
(3) Perform write operations
(4) wait for BSY A reset , Complete a program .

Sector erase steps :
(1) Check FLASH_CR Of LOCK Whether to unlock , If you don't unlock it first
(2) Check FLASH_SR In register BSY position , Ensure that no... Is currently being performed FLASH operation

(3) stay FLASH_CR In the register , take SER Location 1, And from the main memory block 12 Select the sector to erase from the sectors (SNB)

(4) take FLASH_CR In register STRT Location 1, Trigger erase operation
(5) wait for BSY A reset

2. Related registers

3. Configuration method
3.1 Inside flash Step by step
3.1.1 Unlock and lock


  1. #define FLASH_KEY1 ((uint32_t)0x45670123)
  2. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  3. #define FLASH_CR_LOCK ((uint32_t)0x80000000)
  4. // Unlock function
  5. void FLASH_Unlock(void)
  6. {

  7. if((FLASH->CR & FLASH_CR_LOCK) != RESET)
  8. {

  9. /* Authorize the FLASH Registers access */
  10. FLASH->KEYR = FLASH_KEY1;
  11. FLASH->KEYR = FLASH_KEY2;
  12. }
  13. }
  14. // Lock function
  15. void FLASH_Lock(void)
  16. {

  17. /* Set the LOCK Bit to lock the FLASH Registers access */
  18. FLASH->CR |= FLASH_CR_LOCK;
  19. }
复制代码



3.1.2 Write operations
  1. // Write doubleword
  2. FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data)
  3. {

  4. FLASH_Status status = FLASH_COMPLETE;
  5. /* Check the parameters */
  6. assert_param(IS_FLASH_ADDRESS(Address));
  7. /* Wait for last operation to be completed */
  8. status = FLASH_WaitForLastOperation();
  9. if(status == FLASH_COMPLETE)
  10. {

  11. /* if the previous operation is completed, proceed to program the new data */
  12. FLASH->CR &= CR_PSIZE_MASK;
  13. FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
  14. FLASH->CR |= FLASH_CR_PG;
  15. *(__IO uint64_t*)Address = Data;
  16. /* Wait for last operation to be completed */
  17. status = FLASH_WaitForLastOperation();
  18. /* if the program operation is completed, disable the PG Bit */
  19. FLASH->CR &= (~FLASH_CR_PG);
  20. }
  21. /* Return the Program Status */
  22. return status;
  23. }
  24. // Write word
  25. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  26. {

  27. FLASH_Status status = FLASH_COMPLETE;
  28. /* Check the parameters */
  29. assert_param(IS_FLASH_ADDRESS(Address));
  30. /* Wait for last operation to be completed */
  31. status = FLASH_WaitForLastOperation();
  32. if(status == FLASH_COMPLETE)
  33. {

  34. /* if the previous operation is completed, proceed to program the new data */
  35. FLASH->CR &= CR_PSIZE_MASK;
  36. FLASH->CR |= FLASH_PSIZE_WORD;
  37. FLASH->CR |= FLASH_CR_PG;
  38. *(__IO uint32_t*)Address = Data;
  39. /* Wait for last operation to be completed */
  40. status = FLASH_WaitForLastOperation();
  41. /* if the program operation is completed, disable the PG Bit */
  42. FLASH->CR &= (~FLASH_CR_PG);
  43. }
  44. /* Return the Program Status */
  45. return status;
  46. }
  47. // Write half word
  48. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  49. {

  50. FLASH_Status status = FLASH_COMPLETE;
  51. /* Check the parameters */
  52. assert_param(IS_FLASH_ADDRESS(Address));
  53. /* Wait for last operation to be completed */
  54. status = FLASH_WaitForLastOperation();
  55. if(status == FLASH_COMPLETE)
  56. {

  57. /* if the previous operation is completed, proceed to program the new data */
  58. FLASH->CR &= CR_PSIZE_MASK;
  59. FLASH->CR |= FLASH_PSIZE_HALF_WORD;
  60. FLASH->CR |= FLASH_CR_PG;
  61. *(__IO uint16_t*)Address = Data;
  62. /* Wait for last operation to be completed */
  63. status = FLASH_WaitForLastOperation();
  64. /* if the program operation is completed, disable the PG Bit */
  65. FLASH->CR &= (~FLASH_CR_PG);
  66. }
  67. /* Return the Program Status */
  68. return status;
  69. }
  70. // Write Bytes
  71. FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
  72. {

  73. FLASH_Status status = FLASH_COMPLETE;
  74. /* Check the parameters */
  75. assert_param(IS_FLASH_ADDRESS(Address));
  76. /* Wait for last operation to be completed */
  77. status = FLASH_WaitForLastOperation();
  78. if(status == FLASH_COMPLETE)
  79. {

  80. /* if the previous operation is completed, proceed to program the new data */
  81. FLASH->CR &= CR_PSIZE_MASK;
  82. FLASH->CR |= FLASH_PSIZE_BYTE;
  83. FLASH->CR |= FLASH_CR_PG;
  84. *(__IO uint8_t*)Address = Data;
  85. /* Wait for last operation to be completed */
  86. status = FLASH_WaitForLastOperation();
  87. /* if the program operation is completed, disable the PG Bit */
  88. FLASH->CR &= (~FLASH_CR_PG);
  89. }
  90. /* Return the Program Status */
  91. return status;
  92. }
复制代码


3.1.3 Erase operation
  1. //FLASH_Sector: Select erase sector
  2. //VoltageRange: Select the power supply voltage range
  3. FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange);
  4. FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange);
  5. FLASH_Status FLASH_EraseAllBank1Sectors(uint8_t VoltageRange);
  6. FLASH_Status FLASH_EraseAllBank2Sectors(uint8_t VoltageRange);
复制代码


3.1.4 obtain FLASH state
  1. FLASH_Status FLASH_GetStatus(void)
  2. {

  3. FLASH_Status flashstatus = FLASH_COMPLETE;
  4. if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
  5. {

  6. flashstatus = FLASH_BUSY;
  7. }
  8. else
  9. {

  10. if((FLASH->SR & FLASH_FLAG_WRPERR) != (uint32_t)0x00)
  11. {

  12. flashstatus = FLASH_ERROR_WRP;
  13. }
  14. else
  15. {

  16. if((FLASH->SR & FLASH_FLAG_RDERR) != (uint32_t)0x00)
  17. {

  18. flashstatus = FLASH_ERROR_RD;
  19. }
  20. else
  21. {

  22. if((FLASH->SR & (uint32_t)0xE0) != (uint32_t)0x00)
  23. {

  24. flashstatus = FLASH_ERROR_PROGRAM;
  25. }
  26. else
  27. {

  28. if((FLASH->SR & FLASH_FLAG_OPERR) != (uint32_t)0x00)
  29. {

  30. flashstatus = FLASH_ERROR_OPERATION;
  31. }
  32. else
  33. {

  34. flashstatus = FLASH_COMPLETE;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. /* Return the FLASH Status */
  41. return flashstatus;
  42. }
复制代码


The return value is defined by the enumeration type
  1. typedef enum
  2. {

  3. FLASH_BUSY = 1,// Operation busy
  4. FLASH_ERROR_RD,// Read protection error
  5. FLASH_ERROR_PGS,// Programming sequence error
  6. FLASH_ERROR_PGP,// Programming parallel bit error
  7. FLASH_ERROR_PGA,// Programming alignment error
  8. FLASH_ERROR_WRP,// Write protect error
  9. FLASH_ERROR_PROGRAM,// Programming error
  10. FLASH_ERROR_OPERATION,// Wrong operation
  11. FLASH_COMPLETE// End of operation
  12. }FLASH_Status;
复制代码

3.1.5 Wait for the operation to complete
When performing flash write operation , Any read operation to flash memory will lock the bus , After the write operation is completed Read operation can be carried out correctly ; When writing or erasing , Cannot read code or data . So before each operation , We all have to wait for the last operation to complete before this operation can start .
  1. FLASH_Status FLASH_WaitForLastOperation(void)
  2. {

  3. __IO FLASH_Status status = FLASH_COMPLETE;
  4. /* Check for the FLASH Status */
  5. status = FLASH_GetStatus();
  6. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset. Even if the FLASH operation fails, the BUSY flag will be reset and an error flag will be set */
  7. while(status == FLASH_BUSY)
  8. {

  9. status = FLASH_GetStatus();
  10. }
  11. /* Return the operation status */
  12. return status;
  13. }
复制代码

3.1.6 Read FLASH Specify address data
  1. // Read the word at the specified address (32 Bit data )
  2. //faddr: Read the address
  3. // Return value : Corresponding data .
  4. u32 STMFLASH_ReadWord(u32 faddr)
  5. {
  6. return *(u32*)faddr; }
复制代码



3.2FLASH Read data functions
  1. // Read the data of the specified length from the specified address
  2. //ReadAddr: Initial address
  3. //pBuffer: Data pointer
  4. //NumToRead: word (4 position ) Count
  5. void STM32_FLASH_Read(u32 ReadAddr,u32 *pBuffer,u32 NumToRead)
  6. {

  7. u32 i;
  8. for(i=0;i<NumToRead;i++)
  9. {

  10. pBuffer[i]=STM32_FLASH_ReadWord(ReadAddr);// Read 4 Bytes .
  11. ReadAddr+=4;// The offset 4 Bytes .
  12. }
  13. }
复制代码



3.3FLASH Write data functions
  1. // Write data of the specified length from the specified address
  2. // Particular attention : because STM32F4 The sector of is too big , There is no way to save sector data locally , So this function
  3. // Write the address if not 0XFF, Then the whole sector will be erased without saving the sector data . therefore
  4. // Write non 0XFF The address of , This will result in data loss of the entire sector . It is recommended to make sure that the sector is
  5. // No important data , It's best to wipe out the whole sector first , Then slowly write back .
  6. // The function pair OTP The area is also effective ! It can be used to write OTP District !
  7. //OTP Area address range :0X1FFF7800~0X1FFF7A0F
  8. //WriteAddr: Initial address ( This address must be 4 Multiple !!)
  9. //pBuffer: Data pointer
  10. //NumToWrite: word (32 position ) Count ( Is to write 32 Number of bit data .)
  11. void STM32_FLASH_Write(u32 WriteAddr,u32 *pBuffer,u32 NumToWrite)
  12. {

  13. FLASH_Status status = FLASH_COMPLETE;
  14. u32 addrx=0;
  15. u32 endaddr=0;
  16. if(WriteAddr<STM32_FLASH_BASE||WriteAddr%4)return; // Illegal address
  17. FLASH_Unlock(); // Unlock
  18. FLASH_DataCacheCmd(DISABLE);//FLASH During erasure , Data caching must be disabled
  19. addrx=WriteAddr; // The starting address for writing
  20. endaddr=WriteAddr+NumToWrite*4; // Write end address
  21. if(addrx<0X1FFF0000) // Only the main storage area , To perform the erase operation !!
  22. {

  23. while(addrx<endaddr) // Clear all obstacles .( Right FFFFFFFF The place of , Erase first )
  24. {

  25. if(STM32_FLASH_ReadWord(addrx)!=0XFFFFFFFF)// There are not 0XFFFFFFFF The place of , To erase this sector
  26. {

  27. status=FLASH_EraseSector(STM32_FLASH_GetFlashSector(addrx),VoltageRange_3);//VCC=2.7~3.6V Between !!
  28. if(status!=FLASH_COMPLETE)break; // Something went wrong
  29. }else addrx+=4;
  30. }
  31. }
  32. if(status==FLASH_COMPLETE)
  33. {

  34. while(WriteAddr<endaddr)// Writing data
  35. {

  36. if(FLASH_ProgramWord(WriteAddr,*pBuffer)!=FLASH_COMPLETE)// Write data
  37. {

  38. break; // Write exception
  39. }
  40. WriteAddr+=4;
  41. pBuffer++;
  42. }
  43. }
  44. FLASH_DataCacheCmd(ENABLE); //FLASH Erase end , Turn on the data cache
  45. FLASH_Lock();// locked
  46. }
复制代码
  1. // Save the usage of this macro definition , Very good
  2. const u8 text_buf[]="www.prechin.net";
  3. #define TEXTLEN sizeof(text_buf)
  4. #define TEXTSIZE (TEXTLEN/4+((TEXTLEN%4)?1:0))
复制代码


If the explanation is not in place, I hope you can point out , There is something I need to explain , I hope you will put forward , I'll issue a document to explain .

转载自:Autumn grass fire


收藏 评论0 发布时间:2023-1-3 19:04

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版