3 Replies Latest reply on Apr 24, 2019 5:03 AM by GeonaP_26

    How to write data to flash in PSoC 4 BLE


      Hi, I am writing an application with the CYBLE-212019-00 BLE module, which has the PSoC 4 256KB chip. I also has a BLE Pioneer Kit (CY8CKIT-042-BLE) for helping me to develop the application. And I am using the PSoC Creator 4.1 IDE.


      I would like to store a few run-time application data to flash. The data is not received from the BLE. I have searched for a few hours and I found there can be 3 ways to do it:

      1. Using the CyBle_StoreAppData() function generated by the BLE stack.

           - I found a discussion in: "how to store a byte array to flash memory",

      2. Using the CySysFlashWriteRow() function generated by the system (CyFlash)

           - This method is I found from the PSoC Creator example project

      3. Storing in the "User SFlash"

           - that I found in: Project #029: Supervisory Flash on PSoC 4 BLE


      First question is, which method I should use?


      I have tried the 3rd method in the Pioneer Kit successfully. However when I implement it into my BLE application, when I read the memory from the flash address, I found that they are not empty indeed (I saw the memory is not 0xFF). Then for the first trial I write data in the 1st row of the SFlash, my application lost the BLE connection function (The BLE function has been tested working well in my application before). Then for the second trial, I write the data in the 3rd row of the SFlash, the application even hang.


      The macro defined in the example code is like this:

      *                  Enums and macros
      #define SWITCH_PRESSED                  (0u)   /* Active low user switch on BLE Pioneer kit */ 
      #define USER_SFLASH_ROW_SIZE            (128u) /* SFlash row size for 128KB flash BLE device. For other PSoC 4 BLE devices 
                                                      * with higher flash size, this example project might need some modification.
                                                      * Please check the device datasheet and TRM before using this code on non 128KB
                                                      * flash devices */
      #define SFLASH_STARTING_VALUE           (0x00) /* Starting value to be stored in user SFlash to demonstrate SFlash write API */
      #define USER_SFLASH_ROWS                (4u)   /* Total number of user SFlash rows supported by the device */
      #define USER_SFLASH_BASE_ADDRESS        (0x0FFFF200u) /* Starting address of user SFlash row for 128KB PSoC 4 BLE device */    
      #define LOAD_FLASH          0x80000004
      #define WRITE_USER_SFLASH_ROW          0x80000018
      #define USER_SFLASH_WRITE_SUCCESSFUL     0xA0000000 


      I changed the "USER_SFLASH_ROW_SIZE" to 256u as I found somewhere that somebody said it is 256B for my device.


      Second question: Are these Marcos correct for my device(256KB )?


      Third question:

           For using CyBle_StoreAppData(), as discussed in "how to store a byte array to flash memory", the simple example there will initialize a const array variable, how can I initialize the array but not have it remember the initialization values?


      Thank you very much.

        • 1. Re: How to write data to flash in PSoC 4 BLE
          1. In addition to the flash address space, PSoC 4200 BLE contains four supervisory rows which can be used to store application-specific information called - User Supervisory Flash (SFlash). The data in user Sflash will be retained over normal programming cycle. The third method mentioned updates user SFlash. We have separate API for updating Flash and user SFlash. Please use CySysFlashWriteRow to update Flash row. Whereas, use CySysSFlashWriteUserRow to write data to SFlash user configurable area. It has been added from cyboot version 5.30.
          2. Project #029: Supervisory Flash on PSoC 4 BLE is configured for 128 kB device. You can straight away use CySysSFlashWriteUserRow API to update User SFlash (using cyboot v5.30 or above).
          3. I did not understand the query completely. Please let me know if you are facing any issue with the following lines of code.
          int main()
              volatile uint8_t write_array[2] = {0xAB,0xCB};
              volatile uint8 *unvolatile_data;
              volatile uint8 read_data[2];
              static const uint8 DATABASE_ROM[10]={0};
              unvolatile_data=(volatile uint8 *) &DATABASE_ROM[0];
              CyBle_StoreAppData(&write_array[0],DATABASE_ROM,1,0);  //Write one byte of write_array to the DATABASE_ROM[0] which is stored in ROM.    
              CyBle_StoreAppData(&write_array[1],DATABASE_ROM,1,0);  //Write one byte of write_array to the DATABASE_ROM[0] which is stored in ROM.    
          } /* End of main */
          • 2. Re: How to write data to flash in PSoC 4 BLE

            Hi GeonaM,


            I have tried CySysSFlashWriteUserRow on my application, but the returned value for calling the function is 0, which is failure.


            I have also tried to replace the WriteUserSFlashRow in the Project #029 (User_SFlash_Write) example by the CySysSFlashWriteUserRow and tested it on my BLE Pioneer Kit board, the result is also failure. (If using the WriteUserSFlashRow the result is successful).



            On the other hand, I have also tried your code example in my application, it is successful.

            • 3. Re: How to write data to flash in PSoC 4 BLE

              A return value of 0x00 means CY_SYS_FLASH_SUCCESS. You can refer to CyFlash.h.


              /** Completed successfully. */
              #define CY_SYS_FLASH_SUCCESS            (0x00u)