Hi @KDJEM.1
I think I figured it out. I am checking the BUSY flag with autopolling after erase, and I am only proceeding when it becomes ready, it wasn't the problem.
I think this error was impossible for others to guess, because I didn't say I was uploading two programs after each other, because I didn't think it was important, but I will leave the solution here if someone runs into this niche error.
First I am using a program to load the pictures into the external flash for a graphical application. This code first erases the external flash and then loads the pictures into it. After this program is done, I am uploading a second program to the microcontroller, the main program that is responsible for the functionality of the device (this only reads the external flash, never writes anything into it).
I think during second program's upload the microcontroller gets a reset and the previously uploaded program's first few lines including the external flash's chip erase is able to run again before the second program is uploaded.
If I first use the external loader program, then I delete this program from the microcontroller with the STM32CubeProgrammer (chip erase on the internal flash) and only afterwards I upload the second program then it works fine and the pictures remain in the external flash.
So I thought the external flash write didn't work because the pictures were not in the external flash, but they just got erased again. And previously when I took the external load program apart to first only erase the external flash and then only write it, they remained in the external flash because even if the writing part rerun again during the main program's upload, it didn't matter.
Also correct me if I am wrong, but as far as I know memory-mapped NOR flash memory write is not possible with the HAL library. The reading works in memory-mapped mode, because you can randomly access any number of bytes anywhere with a single XSPI command.
But for writing you need to write the memory page by page (256 byte), send a new write enable command at every new page, and calculate the page's starting address every time. At the memory-mapped setup the HAL library only accepts a single XSPI command, so I don't think it can do the writing for NOR flash in memory-mapped mode.