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

【经验分享】STM32F0使用FLASH

[复制链接]
STMCU小助手 发布时间:2021-11-22 22:03
STM32F0使用FLASH


  1. /* Private typedef -----------------------------------------------------------*/
  2. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  3. /* Private define ------------------------------------------------------------*/
  4. #define FLASH_PAGE_SIZE         ((uint32_t)0x00000400)   /* FLASH Page Size */
  5. #define FLASH_USER_START_ADDR   ((uint32_t)0x08003000)   /* Start @ of user Flash area */
  6. #define FLASH_USER_END_ADDR     ((uint32_t)0x08003000)   /* End @ of user Flash area */
  7. #define DATA_32                 ((uint32_t)0x12345678)

  8. /* Private macro -------------------------------------------------------------*/
  9. /* Private variables ---------------------------------------------------------*/
  10. uint32_t EraseCounter = 0x00, Address = 0x00;
  11. uint32_t Data = 0x3210ABCD;
  12. uint32_t NbrOfPage = 0x00;
  13. __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;
  14. __IO TestStatus MemoryProgramStatus = PASSED;

  15. /* Private function prototypes -----------------------------------------------*/
  16. /* Private functions ---------------------------------------------------------*/


  17. //void delayus(uint16_t u16us)   
  18. //{   
  19. //   uint16_t i,j;   
  20. //   
  21. //    for(i=0;i<u16us;i++)   
  22. //    {   
  23. //    //   for(j=0;j<30;j++)   
  24. //     {         
  25. //            __NOP();   
  26. //     }              
  27. //     }     
  28. //}   
  29. void FlashRead()
  30. {

  31. /* Lock the Flash to disable the flash control register access (recommended
  32.      to protect the FLASH memory against possible unwanted operation) *********/
  33.   FLASH_Lock();


  34.   /* Check if the programmed data is OK
  35.       MemoryProgramStatus = 0: data programmed correctly
  36.       MemoryProgramStatus != 0: number of words not programmed correctly ******/
  37.   Address = FLASH_USER_START_ADDR;
  38. // MemoryProgramStatus = PASSED;

  39.     CONFIG_A = *(__IO uint16_t *)Address;    Address = Address + 2;
  40.     CONFIG_B = *(__IO uint16_t *)Address;    Address = Address + 2;
  41.     CONFIG_C = *(__IO uint16_t *)Address;    Address = Address + 2;

  42. }

  43. void FlashSave()
  44. {
  45.    /*!< At this stage the microcontroller clock setting is already configured,
  46.        this is done through SystemInit() function which is called from startup
  47.        file (startup_stm32f0xx.s) before to branch to application main.
  48.        To reconfigure the default setting of SystemInit() function, refer to
  49.        system_stm32f0xx.c file
  50.      */

  51.   /* Unlock the Flash to enable the flash control register access *************/
  52.   FLASH_Unlock();

  53.   /* Erase the user Flash area
  54.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  55.   /* Clear pending flags (if any) */  
  56.   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);

  57.   /* Define the number of page to be erased */
  58. // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;


  59.   /* Erase the FLASH pages */
  60. // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
  61.   {
  62.     if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)
  63.     {
  64.      /* Error occurred while sector erase.
  65.          User can add here some code to deal with this error  */
  66.       while (1)
  67.       {
  68.         TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误
  69.       }
  70.     }
  71.   }
  72.   /* Program the user Flash area word by word
  73.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  74.   Address = FLASH_USER_START_ADDR;

  75. // while (Address < FLASH_USER_END_ADDR)
  76. //  {
  77.     if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
  78.     {

  79.     }
  80.           else
  81.     {
  82.       /* Error occurred while writing data in Flash memory.
  83.          User can add here some code to deal with this error */
  84.      delayus(100);



  85.     }
  86.       Address = Address + 2;
  87.     if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)
  88.     {

  89.     }
  90.                 else
  91.     {
  92.       /* Error occurred while writing data in Flash memory.
  93.          User can add here some code to deal with this error */
  94.           delayus(100);
  95.     }
  96.     Address = Address + 2;

  97.     if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE)
  98.      {

  99.     }
  100.       else
  101.     {
  102.       /* Error occurred while writing data in Flash memory.
  103.          User can add here some code to deal with this error */
  104.           delayus(100);
  105.     }




  106. }
复制代码



收藏 评论0 发布时间:2021-11-22 22:03

举报

0个回答

所属标签

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