cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U575 - programming water marks..

Nicholas Parker
Associate III

Hi,

I'm a bit stuck.   I'm trying to program watermarks for BANK2 (to make the entire bank secure so I can read the whole area using secure alias) for a custom boot-loader.
 
The code below is executing in secure context from BANK1 (and no bank swap).  It runs without error, but after a reset, the option bytes are still unchanged.
 
Im calling it with (FLASH_BANK_2, 0, 63, 0) as arguments. (63 being the last flash page of a 512KB bank - device is 1MB)
 
I am pretty sure I misunderstand something.
 
Kind Regards, Nick


/**
 * @brief Configures or Disables Secure Watermark for a specific bank.
 * @PArambank: FLASH_BANK_1 or FLASH_BANK_2
 * @PAramstart_page: Start page (Set > end_page to disable)
 * @PAramend_page: End page
 * @PAramlaunch: If SET, triggers a System Reset immediately to apply changes.
 * @return true if the operation was successful, false otherwise
 * @note to disable WM, set start_page > end_page (eg start_page=1, end_page=0)
 */
bool FLASH_OB_configure_watermarks(uint32_t bank, uint8_t start_page, uint8_t end_page, uint8_t launch)
{
  HAL_FLASH_Unlock();
  HAL_FLASH_OB_Unlock();

  FLASH_OBProgramInitTypeDef obInit = {0};
  obInit.OptionType = OPTIONBYTE_WMSEC;

  // Set Area Config
  if (bank == FLASH_BANK_1)
    obInit.WMSecConfig = OB_WMSEC_SECURE_AREA_CONFIG | OB_WMSEC_AREA1;
  else if (bank == FLASH_BANK_2)
    obInit.WMSecConfig = OB_WMSEC_SECURE_AREA_CONFIG | OB_WMSEC_AREA2;
  else {
    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();
    return false;
  }

  obInit.WMSecStartPage = start_page;
  obInit.WMSecEndPage = end_page;

  // HAL_FLASHEx_OBProgram handles unlock, set, program, and wait (BSY flag)
  if (HAL_FLASHEx_OBProgram(&obInit) != HAL_OK)
  {
    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();
    return false;
  }

  // 5. Finalize and Reset if requested
  if (launch)
  {
    HAL_FLASH_OB_Launch();
  }

  HAL_FLASH_OB_Lock();
  HAL_FLASH_Lock();
  return true;
}
1 ACCEPTED SOLUTION

Accepted Solutions
Nicholas Parker
Associate III

Oops.  It looks like I forgot to call

 HAL_FLASH_OB_Launch();
 
(I was mistakenly calling NVIC_reset() )
 
 

View solution in original post

1 REPLY 1
Nicholas Parker
Associate III

Oops.  It looks like I forgot to call

 HAL_FLASH_OB_Launch();
 
(I was mistakenly calling NVIC_reset() )