I am working with an STM32 L476RG evaluation board on a project. The goal currently is to utilize STM's mass storage library to write data to flash over USB. I've got the USB library working properly (simply reading and writing data from/to RAM as a start), but I've encountered a strange problem when using flash.
If I write to the flash in the STORAGE_Init_FS function (Using HAL_FLASH_Program(....)), I can write to the flash just fine. I clear the page, write arbitrary data to it, and it appears in the memory view of the debugger.
However, if I try to do the exact same thing in STORAGE_Write_FS (tried with line-for-line, the same code), then the flash operation returns an error and doesn't ever write any data.
Here are the relevant code portions:
{
/* USER CODE BEGIN 2 */
USB_Flash_Init();
return (USBD_OK);
/* USER CODE END 2 */
}
HAL_StatusTypeDef USB_Flash_Init() {
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_FLASH_CLK_ENABLE();
/* Clear flash flags */
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
HAL_FLASH_Lock();
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, 0x8080000, 0x0001);
HAL_FLASH_Lock();
return HAL_OK;
}
In the above code, the data 0x0001 is properly written to memory address 0x8080000. (The erase call is not there, but the page has been erased).
However, in the below function,
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 7 */
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_FLASH_CLK_ENABLE();
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
HAL_FLASH_Lock();
HAL_FLASH_Unlock();
FLASH_PageErase(0, FLASH_BANK_2);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, 0x8080000, 0x0001);
FLASH_PageErase(0, FLASH_BANK_2);
HAL_FLASH_Lock();
return (USBD_OK);
/* USER CODE END 7 */
}
The data gets properly wiped (viewing the memory address through the debugger), but then never written, it remains 0xFFFFFFFF. I know some of the init stuff had already been done, but I wanted to be certain it wasn't the cause so I threw it in there as well. The only thing I can really think of would be the stack potentially not having enough space (unlikely with the massive amount of memory this thing has). In order to verify this wasn't the cause, I tried both taking my 2k buffer (not shown here) out of the function to no longer be a local variable, and increasing the stack and heap sizes to 0x4096 (excessive) in the RAM.ld.
Neither changed the behavior. Anyone I've spoken to has been at a loss as to the cause of this. What could I be doing wrong?
I do get two errors when I check HAL_FLASH_CheckError() (might not be the exact function name), 0xA0, which corresponds to HAL_FLASH_ERROR_PGS and HAL_FLASH_ERROR_PGA, programming sequence and programming alignment errors respectively. I'm not certain these errors are relevant, considering the actual function calls and setup are identical, only the scope is different.
If anyone has any advice, I'd be glad to hear it! Hopefully I'm just missing some control register or bit somewhere.