省流:FLASH_Program_DoubleWord函数使用时要求Vpp电压为7V~9V。
最近项目选择使用stm32f407来做,之前都是用的嵌入式linux板子。第一次用单片机做项目,买了现成的板子,然后基于自带的例程代码进行修改。
在做参数存储的时候,用的HAL库来进行单片机自身的Flash读写操作。因为要存储的参数是double数据,所以直接用联合体来做。
union
{
double data_f;
uint64_t data_d;
}
对data_f赋值后,直接用HAL_FLASH_Program
函数,写入FLASH_TYPEPROGRAM_DOUBLEWORD
类型的数据来存储data_d,结果写入一直失败。自带的例程写入FLASH_TYPEPROGRAM_WORD
类型的数据,每次都能成功。试了好多次,后来进入到HAL_FLASH_Program
内部查看源码,发现在写入FLASH_TYPEPROGRAM_DOUBLEWORD
时会调用函数FLASH_Program_DoubleWord
,而在这个函数上面有一段注释。里面说明了使用这个函数,设备的Vpp电压必须在7V~9V之间。而楼主使用的板子没有接Vpp供电。所以将联合体改成下面的格式,然后用byte写入,解决问题。
union
{
double data_f;
uint8_t buf[8];
}
/**
* @brief Program a double word (64-bit) at a specified address.
* @note This function must be used when the device voltage range is from
* 2.7V to 3.6V and Vpp in the range 7V to 9V.
*
* @note If an erase and a program operations are requested simultaneously,
* the erase operation is performed before the program one.
*
* @param Address specifies the address to be programmed.
* @param Data specifies the data to be programmed.
* @retval None
*/
static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)