1 2 Previous Next 27 Replies Latest reply on Jun 28, 2017 10:52 AM by niti.gupta_1697551

    How to store a byte array to flash memory?

    rabiul.chowdhury.1

      My PSoC board connects to an Android app though Bluetooth. The Android app sends a 4 byte array and I want to save this array to flash storage and later retrieve it and assign it to a variable. How can I go about doing this? 

        • 1. Re: How to store a byte array to flash memory?
          jrow

          Hi Rabiul,

             

          The function you want to look into is CyBle_StoreAppData(). There aren't many examples that show this, but the BLE Component's generated source uses the relevant API call to store client characteristic configuration descriptor data for bonded devices.

             

          Basically, you need to declare some global constant data in your code so the compiler/linker assigns it to a predictable location in flash, and then use that API call to write new values (from RAM) whenever you need to update it. You can read the value of the const byte array at any time, but you have to use CyBle_StoreAppData() to change it. Data stored this way will persist across resets and power cycles.

             

          Here's a really simple example:

             
          /* constant variable container, will be stored in flash */ const uint8 flash_storage[16] = {0};  /* simple function to write data void StoreInFlash(uint8 *newData, uint8 length) {     /* make sure the BLE stack is not in low-power mode */     CyBle_ExitLPM();      /* repeat flash write attempt until success */      do     {         /* write segment of configuration structure from RAM into flash */         CYBLE_API_RESULT_T result = CyBle_StoreAppData(             newData,                    /* uint8 pointer to source data */             (uint8_t *)&flash_storage,  /* uint8 pointer to const destination */             length,                     /* number of bytes to write */             0);                         /* don't force write, but loop will keep retrying */     }     while (result != CYBLE_ERROR_OK); }
          • 2. Re: How to store a byte array to flash memory?
            rabiul.chowdhury.1

            Hi jrow,
            Thank you so much for your response. I still have some questions if you don't mind. 

            So if I want to retrieve the data I wrote, all I have to do is read the constant variable container? Also, I've read that writing to flash interferes with Bluetooth so it's good to attempt writing to flash when there is no Bluetooth device connected. When exactly should I try to write to flash? Would you recommend doing it after the event CYBLE_EVT_GAP_DEVICE_DISCONNECTED in the event handler?

            • 3. Re: How to store a byte array to flash memory?
              jrow

              Hi Rabiul,

                 

              >> So if I want to retrieve the data I wrote, all I have to do is read the constant variable container?

                 

              That is correct. In my previous example, you can directly read the flash_storage byte array in exactly the same way you would if it were non-const and stored in RAM. The compiler/CPU takes care of the memory access from flash vs. RAM transparently.

                 

              >> Also, I've read that writing to flash interferes with Bluetooth so it's good to attempt writing to flash when there is no Bluetooth device connected. When exactly should I try to write to flash?

                 

              I haven't encountered much trouble with flash writes during a connection, but that likely depends on CPU speed and connection parameters, among other things. If you want to play it extra safe, then yes--you would wait for the CYBLE_EVT_GAP_DEVICE_DISCONNECTED stack event to occur before storing in flash. Of course, this assumes that you can guarantee an execution flow of (1) advertise -> (2) connect -> (3) client writes data -> (4) client disconnects, so that the disconnection will always occur before you need to write new data. If your client needs to stay connected indefinitely or for a long time, and you need to make sure data is stored in flash before this much time elapses, then waiting for a disconnection might not be suitable.

                 

              You can try performing the flash write while connected anyway and see what the consequences are in your case; it might work fine for you.

              • 4. Re: How to store a byte array to flash memory?
                user_1377889

                Welcome in the forum!

                   

                At first, I hope you are talking about Bluetooth Low Energy (BLE) which is different from standard Bluetooth.

                   

                For storing small amounts of data there is a BLE Supervisory Flash

                   

                within the PSoC4 BLE devices. See example.

                   

                 

                   

                Bob

                1 of 1 people found this helpful
                • 5. Re: How to store a byte array to flash memory?
                  rabiul.chowdhury.1

                  Thank you Bob,

                     

                  Yes, I am indeed talking about Bluetooth Low Energy. What is the difference between supervisory flash and what jrow has recommended?

                  • 6. Re: How to store a byte array to flash memory?
                    user_1377889

                    @Jrow

                       

                    const uint8 flash_storage[16];
                    is not enough to guarantee that the variable is put into flash!!! It additionally needs an initialization to perform that.  Care must be taken so that the compiler does not "remember" the initialization values when referred to in the program.

                       

                     

                       

                    Bob

                    1 of 1 people found this helpful
                    • 7. Re: How to store a byte array to flash memory?
                      rabiul.chowdhury.1

                      It does not seem to be working. Bob, how can I initialize the array but not have it remember the initialization values?

                      • 8. Re: How to store a byte array to flash memory?
                        user_1377889

                        I used memcpy() to transfer flash data into sram.

                           

                         

                           

                        Bob

                        • 9. Re: How to store a byte array to flash memory?
                          dturney_1462731

                          I have the exact same issues/question, so I'm replying to follow.  When you say memcpy(), do you mean, on initializing, to copy the const variable to an application dynamic variable?

                          • 10. Re: How to store a byte array to flash memory?
                            user_1377889

                            copy the const variable to an application dynamic variable  Yes, exactly.

                               

                            I frequently use a struct to keep the information. one copy is in flash and a working copy is in sram. So I can initialize my sram struct from flash and when values changed I can update flash. The struct usually holds a "Magic Number" to indicate a valid struct and a "BurnCount" to keep track of how many times it was written to flash.

                               

                             

                               

                            Bob

                            1 of 1 people found this helpful
                            • 11. Re: How to store a byte array to flash memory?
                              kgrider_2306876

                              Bob,

                                 

                              Could you post a bit more of an example? I follow what you are saying, but am unclear on exactly how to do this. I read a value from a sensor and place the single byte in a variable. I write it to a pointer. I print the pointer using the UART so I know what the value is and it is correct. When I write that single byte with the CyBle_StoreAppData as Jrow shows, I get 0x00 when I read it back. My read is immediately after it is written.  I am guessing I do not have it 'initialized' as you state in post #7.

                              • 12. Re: How to store a byte array to flash memory?
                                user_1377889

                                Can you please post your complete project, so that we all can have a look at all of your settings. To do so, use
                                Creator->File->Create Workspace Bundle (minimal)
                                and attach the resulting file.

                                   

                                 

                                   

                                Bob

                                • 13. Re: How to store a byte array to flash memory?
                                  kgrider_2306876

                                  Bob,

                                     

                                  This is running with the CYALKIT-E02 beacon. Let me know if you see anything.

                                     

                                  k

                                  • 14. Re: How to store a byte array to flash memory?
                                    user_1377889

                                    Sorry, I was not referring to a CyBLE function, I was talking about Cy flash routines you can find in System Reference Guide.

                                       

                                     

                                       

                                    Bob

                                    1 2 Previous Next