cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L462 With USB DFU goes into Hardware fault when in the methods from usb_dfu_if.c I use the HAL_FLASHEx_Erase.

MRosc.1
Associate II

I have made a DFU application on the STM32L462RET6 CPU.

From the CUBEIDE I set the:

  • HSE
  • Enable USB
  • Select the DFU as a Class For fs Ip.
  • Set the app start address to 0x8008000
  • Set the DFU media interface to @Internal Flash  /0x08000000/01*032Ka,01*480Kg
  • Set the priority for the USB to 15 (to avoid HAL_Delay issues)
  • Automatically config the clocks
  • Generate the project

Now I have a working DFU application.

In main.c add some code to check some pin in case I want to run the bootloader even when I have an apllication at the app address and set some LEdDto blink in the while(1) using HAL_Delay()...

At this point I can run the code in DEBUG and RELEASE and I can connect via USB with the DFUSe Demo tool and "download an application to the device".

Since the methods from the usb_dfu_if.c are empty nothing happens. (All good so far!)

Now I add code in the usb_dfu_if.c file I edit 3 methods:

  • MEM_If_Init_FS() - add HAL_FLASH_Unlock();
  • MEM_If_DeInit_FS() - add HAL_FLASH_Lock()
  • MEM_If_Erase_FS() - erase a page (0x8008000 - first app code page)

Here is the actual method

<pre>

/**

 * @brief Erase sector.

 * @param Add: Address of sector to be erased.

 * @retval 0 if operation is successful, MAL_FAIL else.

 */

uint16_t MEM_If_Erase_FS(uint32_t Add)

{

 /* USER CODE BEGIN 2 */

uint32_t PageError = 0;

 /* Variable contains Flash operation status */

 HAL_StatusTypeDef status;

__HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_OPTVERR);

FLASH_EraseInitTypeDef eraseStr;

uint32_t Page = GetPage(Add);

eraseStr.Banks = 0;

eraseStr.NbPages = 1;

eraseStr.TypeErase = FLASH_TYPEERASE_PAGES;

status = HAL_FLASHEx_Erase(&eraseStr, &PageError);

 if (status != HAL_OK)

 {

  return USBD_FAIL;

 }

 return (USBD_OK);

 /* USER CODE END 2 */

}

</pre>

Now when I run the code I get the following behaviour:

I connect to it and start an update using a DFU file generated from an hex.

Debug - As soon as HAL erase flash method from MEM_If_Erase_FS() ends the void HardFault_Handler(void) is called and the LED stops blinking.

Release - LED continues to blink, I can still access the device, so when I disconnect and reconnect the USB I can "write something to it again" but it allways hangs at erase or write indefenetly.

From what I can understand so far there is a issue with teh USB DFU class and the HAL_FLASHEx methods. They are somehow incompatible.

Any idea what i missed or what else I could try to get this running?

3 REPLIES 3

How large is your loader at this point?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
MRosc.1
Associate II

The loader is 17Kb for release and 36 for debug so maybe that is why debug crashes :)

MRosc.1
Associate II

Update:

Debug fix (hardware_fault handler:( not enough space so move firmware to a higher address.

Release fix (DFUSe hanging in erase/write:( Fixed the Release version by erasing the entire flash not just one page and then write the firmware to it. (now it no longer hangs at write! ) Most likely Write returned an error due to trying to write a flash page that was not previously erased.