No announcement yet.

Write variables to flash

  • Filter
  • Time
  • Show
Clear All
new posts

  • Write variables to flash

    I would like to have some variables that maintain their values through power cycles, I believe to do this I need to write the variables to flash.
    I have had a look at the app notes, but it only gives an example of writing a file from the micro SD to flash.

    How can I achieve this?

  • #2
    I've attached a program that uses flash storage in bank 0 as variable storage.

    Note that for a larger program the location in the bank will need to be moved.

    Of course you could also change it to use another bank.
    Attached Files


    • #3
      Thanks Mark!
      I have gotten this to work but have a few questions:
      1. Why are *block[1024] and *ndt[6] declared as pointers? Does it have something to do with the "dest" argument of flash_Copy? From the internal functions PDF: "dest Pointer to the destination. The destination pointer is byte aligned, so a str_Ptr must be raised to get correct address."
      2. The comments say "Must give a flashbyte > 'code size' + 17". Obviously this is only if using bank 0. And I realise you have to write a whole page at once. But just as an example, let's say my code size is 137 bytes, what is the first byte I can freely use in flash? Byte 154 or 155?
      3. What is the average time required for flash_WriteBlock to execute? I ask this because I'm writing to flash inside a pin counter OVFfunction that overflows every falling edge (counter is set to 0xFFFF every time it fires) and the pulses are up to 25Hz. If flash_WriteBlock takes a significant time to execute, I will have to consider writing to flash less often.


      • #4
        1. This isn't C, in 4DGL all the * does is to prevent a notice if the variable is indexed. Those two variables aren't indexed so the * is not needed, and is probably misleading (you, for example)
        2. 155, but, of course since the blocks are 2k aligned, it's higher than that
        3. I think you can write to flash that fast, try setting up a timer beforehand and reading it out afterwards to see how fast it is. But even so, it's not a good idea, Flash doesn't have an infinite life expectancy (10,000 cycles according to the datasheet), so try writing to it as infrequently as possible.


        • #5

          block and ndt should not be declared the way they are in the original sample, they should indeed be

          var block[1024], *hndl, ndt[6] ;
          as these are arrays, not pointers to arrays


          • #6
            Haha, thanks for correcting the information after so long, Mark! Great support!


            • #7
              I'm interested in the example code, but I can only see a blank page.


              • #8
                Hi everyone,

                I am programming an application were the user can preset some values from screen and have them after a power-off. I am using the method you described in this post, i.e. writing in a block to flash when the user want to change the present value and reading them from flash just after start.
                Which are the drawbacks of using flash memory? I guess one must be totally sure to not overwrite the program code itself. What but the number of cycles writing/reading in flash? Is there any limit?
                I need to "stored" just 20 bytes, is there another most efficient way to do it?
                The method is working, I am just curious about if it is the correct one for my application.

                Best regards


                • #9

                  The write flash limit is many tens of thousands of flash write cycles so you are very unlikely to hit this even with a display power cycled daily. It is good self contained efficient solution.

                  I hope this helps

                  Best regards