Skip to content

Add external flash support and dual-bank flash swap functionality for PSoC6 devices#700

Open
Pushyanth-Infineon wants to merge 1 commit intowolfSSL:masterfrom
Pushyanth-Infineon:psoc6_extflash_dualbank_support
Open

Add external flash support and dual-bank flash swap functionality for PSoC6 devices#700
Pushyanth-Infineon wants to merge 1 commit intowolfSSL:masterfrom
Pushyanth-Infineon:psoc6_extflash_dualbank_support

Conversation

@Pushyanth-Infineon
Copy link

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:

  • Import the project into ModusToolbox IDE using project-creator.
  • Switch to master branch in wolfssl submodule which have fix for includes in wolfcrypt psoc6 port:
    cd ../mtb_shared/wolfboot/wolfboot-psoc6/lib/wolfssl
    git switch master
    git pull origin
  • Program the bootloader and main application using ModusToolbox.

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@danielinux danielinux requested a review from Copilot February 27, 2026 16:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dgarske
Copy link
Contributor

dgarske commented Feb 27, 2026

Okay to test. Contributor agreement on file. Over to @danielinux to review.

Copy link
Member

@danielinux danielinux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution! A couple of minor things:

  • check the return value of ext_flash_read() as described in docs/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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ext_flash_write is implemented to ensure same sector is not erased each time the row is programmed. The algorithm is as follows:

  1. Read recovery marker — load tmp_sector_info from emulated EEPROM; check if magic is valid and sector_address matches the current write's sector.
  2. 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.
  3. 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.

  4. Program the row — write len bytes to address via Cy_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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the API and return len on success

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to return the number bytes instead of success or failure.

@Pushyanth-Infineon Pushyanth-Infineon force-pushed the psoc6_extflash_dualbank_support branch from 07505a1 to 53d6227 Compare March 3, 2026 17:47
… 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.
@Pushyanth-Infineon Pushyanth-Infineon force-pushed the psoc6_extflash_dualbank_support branch from 53d6227 to 46ed900 Compare March 3, 2026 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants