What causes HAL_FLASH_OB_Unlock() to call HardFaultHandler()? It calls WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1); and jumps directly to HardFaultHandler().
The HAL_FLASH documentation is especially poor.
There in no overview of what you have to do.
You would think (because of lack of anything saying different) you have to
HAL_FLASH_OB_Unlock()
HAL_FLASH_Program(...)
HAL_FLASH_OB_Lock()
The first command HAL_FLASH_OB_Unlock() crashes.
HAL_FLASH_Program(...) says the format is
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
Nowhere in the HAL Documentation's (UM1940) 1441 pages does it say what "TypeProgram" is, what it is used for, or what it should be.
It says TypeProgram indicates the way to program at a specific address. The value can be a value of FLASH Type Program. What is FLASH Type Program?
On page 234 it has
FLASH Type Program
FLASH_TYPEPROGRAM_BYTE
FLASH_TYPEPROGRAM_HALFWORD
FLASH_TYPEPROGRAM_WORD
FLASH_TYPEPROGRAM_DOUBLEWORD
I cannot program that with a byte or a halfword, or a word. it can only be a word.
Looking into the code for
HAL_FLASH_Program() in stm32g0xx_hal_flash.c it says
/**
* @brief Program double word or fast program of a row at a specified address.
* @param TypeProgram Indicate the way to program at a specified address.
* This parameter can be a value of @ref FLASH_Type_Program
* @param Address Specifies the address to be programmed.
* @param Data Specifies the data to be programmed
* This parameter is the data for the double word program and the address where
* are stored the data for the row fast program depending on the TypeProgram:
* TypeProgram = FLASH_TYPEPROGRAM_DOUBLEWORD (64-bit)
* TypeProgram = FLASH_TYPEPROGRAM_FAST (32-bit).
*
* @retval HAL_StatusTypeDef HAL Status
*/
So one of the options is FLASH_TYPEPROGRAM_FAST. But that is not on the list on page 234! Why?
The macro
#define IS_FLASH_TYPEPROGRAM(__VALUE__) (((__VALUE__) == FLASH_TYPEPROGRAM_DOUBLEWORD) || \
((__VALUE__) == FLASH_TYPEPROGRAM_FAST))
It does not say any of the other values are valid from Page 234.
I still do not know why HAL_FLASH_OB_Unlock() crashes. It is the first step.
Kip
