The X-CUBE-EEPROM library not working with the STM32G474RB or dual Flash bank problem
I used the NUCLEO-G474RE (NUG474RE$AT1) board to test the X-CUBE-EEPROM library. I did not notice that the STM32G474RBT3 had been soldered instead of STM32G474RETx. It appears it has been soldered to the PCB originally since there are no signs of PCB rework / IC replacement and we have never used this chip version in our company. Anyhow, while testing using the aforementioned processor, I encountered the following problems.
I wrongly assumed first that the STM32G474RBT3 had 512 kB of Flash when it actually has only 128 kB. I set START_PAGE_ADDRESS to 0x0807C000, which is before the end of the 512 KB Flash. I encountered an issue with the PAGE(__ADDRESS__) macro. When the page address is too large, meaning it falls outside the Flash area, the current implementation truncates the value so that it falls within the area. However, it would make more sense to report an error.
After I realized that my "test device" has only 128 kB of Flash, I changed the emulated EEPROM START_PAGE_ADDRESS to 0x0801C000, which is close to the end of the second 64 kB Flash bank of the STM32G474RB. I discovered that the FI_PageErase() function does not erase the requested Flash page. I checked the code execution down to the FLASH unit register level, and in my opinion, the code is executing correctly. It sets PNB to 0x18, PER to 1, and BKER to 1 (page 2) before setting START to 1. According to "Table 2, Product Specific Features" in the STM32Gxx reference manual, the STM32G474RB also has dual-bank Flash. Note that if I manually set PNB to 0x38 and BKER to 0 (bank 1) before START is set to 1, the page at address 0x0801C000 is erased. Overall, it appears that the STM32G474RB does not have two Flash banks. If this is true, then this should be supported in the X-CUBE-EEPROM library (using just one bank for devices with smaller Flash). For STM32G474/473 processors with 512 KB of Flash memory, page erasure in the second bank functions properly, as does EEPROM emulation.
