STM32HAL内部EEPROM读写L151

简单生成代码后找到MDK的驱动文件
STM32HAL内部EEPROM读写L151_第1张图片
官方建议步骤

                  #####数据EEPROM编程功能#####

===============================================================================

 [..]擦除或编程的任何操作都应遵循以下步骤:
 (#)调用@ref HAL_FLASHEx_DATAEEPROM_Unlock()函数启用数据EEPROM访问
     和Flash程序擦除控制寄存器访问。
 (#)调用所需的功能来擦除或编程数据。
 (#)调用@ref HAL_FLASHEx_DATAEEPROM_Lock()来禁用数据EEPROM访问
     和Flash程序擦除控制寄存器访问(推荐
     保护DATA_EEPROM免受可能的意外操作)。

实验总结,只需要三步

  1. 解锁
  2. 写入数据(擦除也是写入数据只不过写的是0x00)
  3. 上锁(建议上锁,其实没啥用,不用问题不是很大)
void Program_EEPROM(void)
{
    uint32_t data = 0x12345678;
  
    HAL_FLASHEx_DATAEEPROM_Unlock();

    HAL_FLASHEx_DATAEEPROM_Program( FLASH_TYPEPROGRAMDATA_WORD,  0x08080000,  data);
    HAL_FLASHEx_DATAEEPROM_Program( FLASH_TYPEPROGRAMDATA_WORD,  0x08080000 + 4,  data);
    
    HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, 0x08080000);
    
    HAL_FLASHEx_DATAEEPROM_Lock();
}
  • FLASH_TYPEPROGRAMDATA_WORD 是数据类型,可以是Program word (32-bit) at a specified address OR Program word (16-bit) at a specified address OR Program word (8-bit) at a specified address
  • 0x08080000 是地址,在官方手册中,可以查到。
  • data 是写入的数据

实际运行效果
STM32HAL内部EEPROM读写L151_第2张图片
擦除也是写入

/**
  * @brief  Erase a word in data memory.
  * @param  Address specifies the address to be erased.
  * @param  TypeErase  Indicate the way to erase at a specified address.
  *         This parameter can be a value of @ref FLASH_Type_Program
  * @note   To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
  *         must be called before.
  *         Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
  *         and Flash program erase control register access(recommended to protect 
  *         the DATA_EEPROM against possible unwanted operation).
  * @retval HAL_StatusTypeDef HAL Status
  */
HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t TypeErase, uint32_t Address)
{
  HAL_StatusTypeDef status = HAL_OK;
  
  /* Check the parameters */
  assert_param(IS_TYPEPROGRAMDATA(TypeErase));
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
  
  /* Wait for last operation to be completed */
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  
  if(status == HAL_OK)
  {
    /* Clean the error context */
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;

    if(TypeErase == FLASH_TYPEERASEDATA_WORD)
    {
      /* Write 00000000h to valid address in the data memory */
      *(__IO uint32_t *) Address = 0x00000000U;
    }

    if(TypeErase == FLASH_TYPEERASEDATA_HALFWORD)
    {
      /* Write 0000h to valid address in the data memory */
      *(__IO uint16_t *) Address = (uint16_t)0x0000;
    }

    if(TypeErase == FLASH_TYPEERASEDATA_BYTE)
    {
      /* Write 00h to valid address in the data memory */
      *(__IO uint8_t *) Address = (uint8_t)0x00;
    }

    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  }
   
  /* Return the erase status */
  return status;
}  

不懂的可以搜索指针强制类型转换学习一下。

读取数据

读取数据参照上面擦写部分,只需要反过来就好了。

data_8bit = *(__IO uint8_t *) Address;
data_16bit = *(__IO uint16_t *) Address;
data_32bit = *(__IO uint32_t *) Address;

我有几个问题比较疑惑

  • 重新下载会擦除这部分区域吗?
    答:这部分除非是进行特意擦除这个部分才会失去数据,其他方式均不会影响这部分的数据,比如,烧录程序,清空程序。
  • flash也可以用一样的函数吗?
    个人觉得不行,flash是按页分的,EEPROM可以按字节编辑,所以肯定有区别。(未进行实验,也没什么意义)

你可能感兴趣的:(STM32)