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
  • 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
    }
    

Reply
  • 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
    }
    

Children