Add external flash support and dual-bank flash swap functionality for PSoC6 devices#700
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds external flash support and dual-bank flash swap functionality for PSoC6 devices, along with enhanced flash programming capabilities using a read-modify-erase-write approach.
Changes:
- Removed NVM_FLASH_WRITEONCE restriction to enable flexible flash programming
- Added external QSPI flash support with SFDP-based configuration and temporary sector management
- Implemented dual-bank flash swap for hardware-assisted firmware updates
- Enhanced internal flash operations with optimized erase size selection (sector/subsector/row)
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| hal/psoc6.c | Core implementation of external flash, dual-bank swap, and enhanced flash HAL functions |
| docs/Targets.md | Documentation for new external flash and dual-bank features |
| config/examples/cypsoc6.config | Updated default configuration with NVM_FLASH_WRITEONCE=0 and fixed swap address |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Okay to test. Contributor agreement on file. Over to @danielinux to review. |
danielinux
left a comment
There was a problem hiding this comment.
Thanks for this contribution! A couple of minor things:
- check the return value of
ext_flash_read()as described indocs/HAL.md - it looks like the content of the temp sector is never restored at the end of
ext_flash_write()
hal/psoc6.c
Outdated
| } | ||
|
|
||
| offset = address - base_address; | ||
| return Cy_SMIF_MemRead(SMIF0, smif_blk_config->memConfig[0], offset, data, |
There was a problem hiding this comment.
looks like SMIF might be returning a status (0 for success) here? Double check that ext_flash_read eventually returns the number of bytes transferred to data, not 0, on success.
There was a problem hiding this comment.
Updated to return the number of bytes read from the external flash.
|
|
||
| /* Write the row data */ | ||
| status = Cy_SMIF_MemWrite(SMIF0, smif_blk_config->memConfig[0], | ||
| address - base_address, data, len, &smif_context); |
There was a problem hiding this comment.
Only the new data is written to the source sector, but the "remaining" backup data from the temp sector seems to never be copied back to the source sector after erase.
The rows before the written data, or after data + len, in the same sector, should be restored with their original values in tmp_sector
There was a problem hiding this comment.
ext_flash_write is implemented to ensure same sector is not erased each time the row is programmed. The algorithm is as follows:
- Read recovery marker — load
tmp_sector_infofrom emulated EEPROM; check if magic is valid andsector_addressmatches the current write's sector. - If no valid marker (first write into this sector):
- Erase EEPROM marker.
- Erase
SWAP_TMP_SECTOR_ADDR. - Copy entire source sector →
SWAP_TMP_SECTOR_ADDR, one row at a time. - Write magic + sector address to EEPROM (power-fail checkpoint).
- Erase the source sector.
- If valid marker (subsequent rows in same sector):
- Source sector is already erased from step 3 — skip backup and erase entirely.
- Read each target row; if any is not all
0xFF, erase the source sector and stop checking.
This handles the edge case where the source sector was partially re-written before a second write call to the same sector.
- Program the row — write
lenbytes toaddressviaCy_SMIF_MemWrite.
The sector backup + erase (step 2) is done once per sector. Subsequent writes to the same sector skip directly to step 4, avoiding redundant erase cycles. ext_flash_read transparently redirects reads for the active sector to SWAP_TMP_SECTOR_ADDR, preserving data integrity. ext_flash_lock clears the EEPROM marker when all writes to the sector are complete.
hal/psoc6.c
Outdated
| * @param data Pointer to destination buffer | ||
| * @param len Number of bytes to read (must be > 0) | ||
| * | ||
| * @return 0 on success, -1 on invalid parameters, or SMIF error code on failure |
There was a problem hiding this comment.
Please follow the API and return len on success
There was a problem hiding this comment.
Updated to return the number bytes instead of success or failure.
07505a1 to
53d6227
Compare
… PSoC6 devices. Removed NVM_FLASH_WRITEONCE restriction for psoc6. Update flash hal functions to support read-modify-erase-write way to perform the programming of flash.
53d6227 to
46ed900
Compare
Description
Add external flash support and dual-bank flash swap functionality for PSoC6 devices.
Removed NVM_FLASH_WRITEONCE restriction for PSoC6 devices.
Update flash hal functions of PSoC6 to support read-modify-erase-write way to perform the programming of flash
Testing
Tested using CY8CPROTO-062-4343W with the project: https://github.com/Pushyanth-Infineon/mtb-example-wolfboot-tpm
Usage:
Checklist