调用STM32F4 库函数FLASH_ProgramWord() 出错问题

来源:互联网 发布:无锡一米网络怎么样 编辑:程序博客网 时间:2024/05/21 17:22

最近操作STM32F4的内部flash,吓了一跳。以前的STM32F107的时候还是4k一页,现在只分了12个Sector,后面几个128K这么大。

从官网下了Flash programming manual,


擦除之前得清除flash flag

/* Clear All pending flags */
FLASH_ClearFlag( FLASH_FLAG_EOP |  FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);


同时查看擦除函数,看着函数说明,正确的传参数就OK了

/**
  * @brief  Erases a specified FLASH Sector.
  *   
  * @param  FLASH_Sector: The Sector number to be erased.
  *          This parameter can be a value between FLASH_Sector_0 and FLASH_Sector_11
  *    
  * @param  VoltageRange: The device voltage range which defines the erase parallelism.  
  *          This parameter can be one of the following values:
  *            @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V, 
  *                                  the operation will be done by byte (8-bit) 
  *            @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
  *                                  the operation will be done by half word (16-bit)
  *            @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
  *                                  the operation will be done by word (32-bit)
  *            @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp, 
  *                                  the operation will be done by double word (64-bit)
  *       
  * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  *                       FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  */
FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)

{

}


然后是写函数:

/**
  * @brief  Programs a word (32-bit) at a specified address.
  * @param  Address: specifies the address to be programmed.
  *         This parameter can be any address in Program memory zone or in OTP zone.  
  * @note   This function must be used when the device voltage range is from 2.7V to 3.6V. 
  * @param  Data: specifies the data to be programmed.
  * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
  *                       FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
  */
FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)

{

}


在这里要注意FLASH_ProgramWord函数的参数 : Address 必须为4的整倍数,否则写入报错: FLASH_ERROR_PROGRAM(写入错误?查了很久也没找到关于这个错误的描述,花费了不少时间)




原创粉丝点击