2021-06-17 04:00 AM
I have made a DFU application on the STM32L462RET6 CPU.
From the CUBEIDE I set the:
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:
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?
2021-06-17 05:12 AM
How large is your loader at this point?
2021-06-17 05:27 AM
The loader is 17Kb for release and 36 for debug so maybe that is why debug crashes :)
2021-06-17 05:53 AM
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.