This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How do I know which flash page to erase to subsequently write to it?

I have been asked to write an application to be able to change some variables during runtime and be able to store them in non-volatile memory (flash). I read the C8051f507 datasheet and the process to erase and write to flash looks really straightforward. My question is: How do I know which page to erase?

Thanks for your time,

Fer

Parents
  • Well - sometime you are going to make a decision where to store your variables that you want to be able to modify. When you have decided on which page to store these variables, you will also know which page to erase to modify them.

    You obviously either have to set up the project so the linker places the variables at the decided flash page. Or you have to instead make use of a pointer to as struct and just initialize the pointer to the address of the page.

    Just remember one thing - what happens if the power is lost or something else happens when you erase that page? Is it bad if you lose the values of these variables?

Reply
  • Well - sometime you are going to make a decision where to store your variables that you want to be able to modify. When you have decided on which page to store these variables, you will also know which page to erase to modify them.

    You obviously either have to set up the project so the linker places the variables at the decided flash page. Or you have to instead make use of a pointer to as struct and just initialize the pointer to the address of the page.

    Just remember one thing - what happens if the power is lost or something else happens when you erase that page? Is it bad if you lose the values of these variables?

Children
  • How do you tell the linker to store those variables (in my case, nine variables) in a specific flash page and nothing else so that when I erase the page I only erase those variables and nothing else?

  • How do you tell the linker to store those variables (in my case, nine variables) in a specific flash page and nothing else so that when I erase the page I only erase those variables and nothing else?
    I like to do such stuff in assembler it is a lot easier using means that do not try to protect you from yourself.

    anyhow it can be done in the assembler, the compiler or the linker.

    and nothing else?
    the only way I know of to do so is to fill the page up.

    Erik

  • Thanks for your replies.

    So far I have been able to erase a page and write a byte to that page; however, I have only been able to implement one function at the time, when I try to call both functions it seems that only the function that is called first is executed. I have included both of the functions to see if somebody can tell me why they are not executing when they are called one after the other.

    Thanks

    void FLASH_ByteWrite (void)
    {
            unsigned char xdata * pwrite;           // FLASH write pointer
    
            pwrite = (unsigned char xdata *) FLASH_WRITE_READ_ERASE;
            PSCTL = 0x01;     // PSWE = 1 which enables writes
            FLKEY = 0xA5;     // Key Sequence 1
            FLKEY = 0xF1;     // Key Sequence 2
            VDM0CN = 0xA0;    // Enable VDD monitor and high threshold
            RSTSRC = 0x02;    // Enable VDD monitor as a reset source
            *pwrite = 0xAA;   // Write the byte
            PSCTL &= ~0x03;   // PSWE = 0 which disable writes
    
    }
    
    void FLASH_PageErase (void)
    {
            unsigned char xdata * pwrite; // FLASH write pointer
    
            pwrite = (unsigned char xdata *) FLASH_WRITE_READ_ERASE;
            PSCTL = 0x03;    // PSWE = 1; PSEE = 1
            FLKEY = 0xA5;    // Key Sequence 1
            FLKEY = 0xF1;    // Key Sequence 2
            VDM0CN = 0xA0;   // Enable VDD monitor and high threshold
            RSTSRC = 0x02;   // Enable VDD monitor as a reset source
            *pwrite = 0;     // Initiate page erase
            PSCTL &= ~0x03;  // PSWE = 0; PSEE = 0
    }
    

  • ... waiting for erase complete before writing?

  • I tried putting different delays in between function calls but I'm still seeing the same problem. I am suspecting that I am getting an undesirable reset right after the erase but I haven't been able to figure out why this is happening.

  • UPDATE

    I used an oscilloscope to trace how far my code was getting an I found out that the processor resets right after

     *pwrite = 0; // Initiate page erase
    

    .

    Does anybody has an idea why this is happening?

    Thanks

  • Does anybody has an idea why this is happening?
    yes, your puppy bites, disable the watchdog - unless you can set the timeout long enough.

    I tried putting different delays in between function calls
    does the chip you have not seen fit to reveale not have an "erase complete" flag. doing it with delays is not a good idea.

    Since you have not revealed your chip, if it does not allow disabling the WD and you can not set a long enough time, you will need to run without it.

    Erik