Hello,
1- Most probably the corruption is due to NIOS II code is accidentally writing flash. Please check the pointers in your code.
a. The EPCS flash is memory-mapped via altera_avalon_epcs_flash_controller
b. There is risk when writing to memory-mapped region, the code interpreted as erase/write commands if done in specific sequence.
2- Using IORD_ALTERA_EPCQ_CONTROLLER_STATUS returns 0x20 does not corresponds to actual flash status register. Please try below:
a. The EPCQ controller in Quartus Standard does not directly exposed SPI-level operations like "read status register", instead, you need to use low-level flash driver that provides these operations. You need to include the EPCS Flash Driver. from this location <nios2_install>/components/altera_avalon_epcs_flash_controller.
b. use the "alt_epcs_flash_read_status_register()" function.
c. Use "alt_avalon_spi_command( )" with 0x05 to read Flash Status Register.
3- Protecting Flash from NIOS II writes
a. Avoid using "alt_lock_flash()" for permanent protection, please use WEL (Write Enable Latch) signal in your code:
i) every flash write or erase operation will require WEL to be set(via 0x06 WE command).
ii) if WEL is not set, write/erase will be ignored.
iii) WEL will be cleared automatically after each write/erase operation.
b. Avoid calling 0x06 (WE) unless in controlled condition in your code.
regards,
Farabi