stm32f4 hal 内部flash

uint16_t MEM_If_Init_FS(void) 
  •  
    {
  •  
     
  •  
    HAL_FLASH_Unlock();
  •  
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  •  
    FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  •  
     
  •  
    }
 
  1.  
    uint16_t MEM_If_DeInit_FS(void)
  2.  
    {
  3.  
    HAL_FLASH_Lock();
  4.  
    }
  5.  
     

flash 的檫除操作

  1.  
    uint16_t MEM_If_Erase_FS(uint32_t start_Add,uint32_t end_Add)
  2.  
    {
  3.  
    /* USER CODE BEGIN 3 */
  4.  
    uint32_t UserStartSector;
  5.  
    uint32_t SectorError;
  6.  
    FLASH_EraseInitTypeDef pEraseInit;
  7.  
     
  8.  
    /* Unlock the Flash to enable the flash control register access *************/
  9.  
    MEM_If_Init_FS();
  10.  
     
  11.  
    /* Get the sector where start the user flash area */
  12.  
    UserStartSector = GetSector(start_Add);
  13.  
     
  14.  
    pEraseInit.TypeErase = TYPEERASE_SECTORS;
  15.  
    pEraseInit.Sector = UserStartSector;
  16.  
    pEraseInit.NbSectors = GetSector(end_Add)-UserStartSector+1 ;
  17.  
    pEraseInit.VoltageRange = VOLTAGE_RANGE_3;
  18.  
     
  19.  
    if (HAL_FLASHEx_Erase(&pEraseInit, &SectorError) != HAL_OK)
  20.  
    {
  21.  
    /* Error occurred while page erase */
  22.  
    return (1);
  23.  
    }
  24.  
     
  25.  
    return (USBD_OK);
  26.  
    /* USER CODE END 3 */
  27.  
    }

擦除操作,先解锁,然后清空所需flash所在sector。注意这里的sector使用GetSector()函数获取的,是一个整型数字(对于F4就是0~11)。
其中的NbSectors是需要清除的sector的个数。
 


下面是获取sector的编号
我们先按照用户手册上写的在文件usbd_dfu_if.h中define一些需要用到的sector起始地址

  1.  
    #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbyte */
  2.  
    #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbyte */
  3.  
    #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbyte */
  4.  
    #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbyte */
  5.  
    #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbyte */
  6.  
    #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbyte */
  7.  
    #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbyte */
  8.  
    #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbyte */
  9.  
    #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbyte */
  10.  
    #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbyte */
  11.  
    #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbyte */
  12.  
    #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbyte */


再在usbd_dfu_if.c文件末尾添加函数:

  1.  
    /**
  2.  
    * @brief Gets the sector of a given address
  3.  
    * @param Address: Flash address
  4.  
    * @retval The sector of a given address
  5.  
    */
  6.  
    static uint32_t GetSector(uint32_t Address)
  7.  
    {
  8.  
    uint32_t sector = 0;
  9.  
     
  10.  
    if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
  11.  
    {
  12.  
    sector = FLASH_SECTOR_0;
  13.  
    }
  14.  
    else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
  15.  
    {
  16.  
    sector = FLASH_SECTOR_1;
  17.  
    }
  18.  
    else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
  19.  
    {
  20.  
    sector = FLASH_SECTOR_2;
  21.  
    }
  22.  
    else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
  23.  
    {
  24.  
    sector = FLASH_SECTOR_3;
  25.  
    }
  26.  
    else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
  27.  
    {
  28.  
    sector = FLASH_SECTOR_4;
  29.  
    }
  30.  
    else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
  31.  
    {
  32.  
    sector = FLASH_SECTOR_5;
  33.  
    }
  34.  
    else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
  35.  
    {
  36.  
    sector = FLASH_SECTOR_6;
  37.  
    }
  38.  
    else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
  39.  
    {
  40.  
    sector = FLASH_SECTOR_7;
  41.  
    }
  42.  
    else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
  43.  
    {
  44.  
    sector = FLASH_SECTOR_8;
  45.  
    }
  46.  
    else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
  47.  
    {
  48.  
    sector = FLASH_SECTOR_9;
  49.  
    }
  50.  
    else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
  51.  
    {
  52.  
    sector = FLASH_SECTOR_10;
  53.  
    }
  54.  
    else
  55.  
    {
  56.  
    sector = FLASH_SECTOR_11;
  57.  
    }
  58.  
     
  59.  
    return sector;
  60.  
    }

 

flash写入操作

  1.  
    uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len)
  2.  
    {
  3.  
    /* USER CODE BEGIN 3 */
  4.  
    uint32_t i = 0;
  5.  
     
  6.  
    for(i = 0; i < Len; i+=4)
  7.  
    {
  8.  
    /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  9.  
    be done by byte */
  10.  
    if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)(dest+i), *(uint32_t*)(src+i)) == HAL_OK)
  11.  
    {
  12.  
    /* Check the written value */
  13.  
    if(*(uint32_t *)(src + i) != *(uint32_t*)(dest+i))
  14.  
    {
  15.  
    /* Flash content doesn't match SRAM content */
  16.  
    return 2;
  17.  
    }
  18.  
    }
  19.  
    else
  20.  
    {
  21.  
    /* Error occurred while writing data in Flash memory */
  22.  
    return 1;
  23.  
    }
  24.  
    }
  25.  
    return (HAL_OK);
  26.  
    /* USER CODE END 3 */
  27.  
    }

flash读出操作

    1.  
      uint8_t *MEM_If_Read_FS (uint8_t *src, uint8_t *dest, uint32_t Len)
    2.  
      {
    3.  
      /* Return a valid address to avoid HardFault */
    4.  
      /* USER CODE BEGIN 4 */
    5.  
      uint32_t i = 0;
    6.  
      uint8_t *psrc = src;
    7.  
       
    8.  
      for(i = 0; i < Len; i++)
    9.  
      {
    10.  
      dest[i] = *psrc++;
    11.  
      }
    12.  
      return HAL_OK;
    13.  
       
    14.  
      /* USER CODE END 4 */
    15.  
      }
posted @ 2020-11-25 09:09  dreamrj  阅读(528)  评论(0编辑  收藏  举报