• AVR Freaks

Writing to flash

Author
seyyah
Super Member
  • Total Posts : 609
  • Reward points : 0
  • Joined: 2004/05/14 12:49:28
  • Status: offline
2009/03/02 01:12:37 (permalink)
0

Writing to flash

I was using eeprom as persistent memory before. I'll use flash memory for the first time for this purpose on pic24h. I have a few questions.
- Can we use every memory single byte independently?
- Should we erase the memory area before writing into it?
- How fast is it to write on compared to eeprom?
- What path should we follow in order to write to and read from a flash memory block in c30?
- Do the same properties and rules apply to dspic30f?
 
Thanks in advance.
#1

5 Replies Related Threads

    MBedder
    Circuit breaker
    • Total Posts : 6773
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    RE: Writing to flash 2009/03/02 01:33:18 (permalink)
    0
    I recommend this application note and library for the beginning. The dsPIC30F chips have dedicated EEPROM which can be used in a more straightforward way than emulation.There are special builtin functions in C30 which simpify the Flash read/write handling - __builtin_tblrdX(), __builtin_tblwtX(). They are documented in C30 User's Manual.
    #2
    friesen
    Super Member
    • Total Posts : 2065
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: online
    RE: Writing to flash 2009/03/02 08:04:53 (permalink)
    0
    - Can we use every memory single byte independently?

    Yes, but It is not as easy as using an eeprom.  When flash is erased, everything becomes a "1".  When you write flash, you are clearing specific ones to zeros.  If I understand it,(others may correct me here)flash endurance is affected on a bit level per change to each specific bit.  So yes you may use each byte independently, but you may have to jump through a couple hoops to get there.  It will be a little trouble to use the top instruction byte, though doable.

    Should we erase the memory area before writing into it?

    Yes you have to because of the above mentioned erasing only writing ones to the flash.  Writing flash does not write ones, only zeros.

    How fast is it to write on compared to eeprom?

    Not sure, but the processor does some funny things on write that you will need to study out.

    What path should we follow in order to write to and read from a flash memory block in c30?

    These are very generic details so you will have to get you manual out for the nitty gritty.

    #1 Erase a 1536 byte block of memory.  This is the smallest amount you can erase.  Make sure this is aligned on the proper boundary.

    #2 Write to program word of your choice, as little or as much as you wish.  In the case of implementing the eeprom emulation, you will continue to step up through memory as you save misc info. 


    With a little creativity you can create your own block of incrementing memory that has more flexibility than the application note.  The total size is somewhat limited.  I needed 10k or so of memory and the application software couldn't do it.  It just depends on your total read/write limits.  10,000 flash read/write limit isn't very much.



    Erik Friesen
    #3
    BulleTracker
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2008/12/29 02:47:52
    • Location: Normandy, FRANCE
    • Status: offline
    RE: Writing to flash 2009/03/11 18:56:08 (permalink)
    0
    Hello,
    Before using the emulation library, i would like to understand what is wrong in my code since i simply copied what i found either in C30 guide or in PIC24F Datasheets

    PROG_Read is fine and works !
    PROG_Write and PROG_Erase do not ?!

    Any body to help me ? thank's a lot in advance
    Philippe


    int   __attribute__ (( aligned (4), space (prog) )) FailSafe1 = 1;

    /*---------------------------------------------------------------------------*/
    void     PROG_Write(int data)
    {
    int offset;
    int i ;


       // Set up NVMCON to write one word of data EEPROM
       NVMCON = 0x4003;

       // Set up a pointer to the EEPROM location to be written
       __builtin_tblpage(&FailSafe1);
       offset = __builtin_tbloffset(&FailSafe1);

       // Write Data Value To Holding Latch
       __builtin_tblwtl(offset, data);

       // Disable Interrupts For 5 Instructions
       asm volatile ("disi #5");

       // Issue Unlock Sequence & Start Write Cycle
       __builtin_write_NVM();
    }
    /*---------------------------------------------------------------------------*/
    void     PROG_Erase()
    {
    int offset;


       // Set up NVMCON to erase one word of data EEPROM
       NVMCON = 0x4043;

       // Set up a pointer to the EEPROM location to be erased
       __builtin_tblpage(&FailSafe1);
       offset = __builtin_tbloffset(&FailSafe1);

       __builtin_tblwtl(offset, 0XFFFF);

       // Disable Interrupts For 5 Instructions
       asm volatile ("disi #5");
       // Issue Unlock Sequence & Start Write Cycle
       __builtin_write_NVM();
    }
    /*---------------------------------------------------------------------------*/
    void     PROG_Read()
    {
    int offset;
    int data,i ;

       // Set up a pointer to the EEPROM location to be read
       __builtin_tblpage(&FailSafe1);
       offset = __builtin_tbloffset(&FailSafe1);

       // Read the EEPROM data
       data = __builtin_tblrdl(offset);

    }
    post edited by BulleTracker - 2009/03/11 19:07:50
    #4
    _fragment_
    Super Member
    • Total Posts : 390
    • Reward points : 0
    • Joined: 2008/10/08 09:17:55
    • Location: Idaho
    • Status: offline
    RE: Writing to flash 2009/03/12 10:01:32 (permalink)
    0
    The answer will depend on which device you are using, PIC24F or PIC24H. You mention PIC24F datasheets, so you should know that (some, all?) PIC24F devices do not have data EEPROM.
     
    Dave
    #5
    BulleTracker
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2008/12/29 02:47:52
    • Location: Normandy, FRANCE
    • Status: offline
    RE: Writing to flash 2009/03/15 11:30:38 (permalink)
    0
    Hello,
    i gave a complete new working code on C30 compiler forum for writing to flash on PIC24F
    Philippe
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5