Storing Information in Flash and Recovering After Reset in PSoC 4 Devices – KBA228412

Version 2

    Author: BragadeeshV_41           Version: **


    Translation - Japanese: PSoC 4デバイスでのフラッシュへの情報保存とリセット後の復旧 - KBA228412- Community Translated (JA)


    There may be situations in your application where you need to recover some data even after reset such a hard reset or soft reset such as counting the number of times a WDT reset occured. This can be done by storing the information in flash. When the required user data is stored in flash, the contents are retained even after reset.

    There are three ways of storing information in flash:

    1. Defining custom locations in the linker script and using flash write APIs to write to that region of the flash
    2. Using User Sflash region
    3. Using the EEPROM Component and using EEPROM APIs to write to that location


    Note: All the three options may not be available for the device you are using. Refer to the device-specific datasheet.

    The following example shows how to count the number of times a WDT reset occurred and store the count value in flash region.


    Option 1: Defining custom locations in linker script:


    See the application note AN89610 - PSoC® 4 and PSoC 5LP Arm® Cortex® Code Optimization to learn how to modify linker scripts to define a custom location in the flash region to store a variable.


    Use the following code snippet to detect a WDT reset event and increment the count variable:


    /* To store the reset count */

    volatile uint8 resetCounter CY_SECTION(".MY_Section1");

    uint8 WDT_Count[128];

    uint8 resetReason;





    if(resetReason == CY_SYS_RESET_WDT)


    WDT_Count[0] = resetCounter + 1;

    returnVal = CySysFlashWriteRow(160u,WDT_Count); //Flash write can be done as only row operations.



    Option 2: Using a user Sflash region:

    Some PSoC 4 devices support a dedicated Sflash region that can be used to store user data.

    Use the following code to use Sflash region to store user data:

        uint8_t buff[CY_SFLASH_SIZEOF_USERROW];

        uint8_t *mySFlash = (uint8_t*) CY_SFLASH_USERBASE;


        if(resetReason == CY_SYS_RESET_WDT)


            buff[0] = 1 + mySFlash[0] ;





    See the system reference guide for more information on flash write APIs.


    Option 3: Using the EEPROM Component and using EEPROM APIs to write to that location

    This approach is the same as Option 2, but uses different APIs. See the code snippet in Option 2 and the code example CE195313 - PSoC Emulated EEPROM to use EEPROM for this application.