Hot!PIC24 Data EEPROM Emulation

Author
noob
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2017/10/17 05:08:19
  • Location: 0
  • Status: offline
2018/11/26 10:07:53 (permalink)
0

PIC24 Data EEPROM Emulation

hi,

I am using PIC24fj256ga702 controller in my project. I wanted to store some bytes data in a non volatile memory in case if there is a power loss.

I searched in the datasheet but i don't think that this particular controller comes with a separate flash data space. The application note AN1095 suggested that one can use the program memory to emulate a data EEPROM.

However, AN1095 seems to complicated. I tried to find examples of its implementation, but couldn't find any.


1. As i understand. the program memory works in pages of fixed length of 255 bytes. each of these pages have their own unique address. And pages have rows. these are used to write onto, however if i want to erase any row, I have to erase the whole page.

however their are some terms in the checklist, on page 14 of the AN1095 application note. which are very much confusing.
Can anyone explain what DATA_EE_BANKS, DATA_EE_SIZE, NUM_DATA_EE_PAGES is?

2. Right now i let pickit choose the memory locations on which to program onto. After the proggraming i see the following output on the pickit window on MPLAB X IDE

Connecting to MPLAB PICkit 3...

Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.51.08
Firmware type..............dsPIC33F/24F/24H

Target voltage detected
Target device PIC24FJ256GA702 found.
Device ID Revision = 3
DEVSN0 = 00000000
DEVSN1 = 00000000

Device Erased...

Programming...

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x23ff
Programming/Verify complete

 
as i understand this is the amount of memory space currently being occupied by my program. Should i manually specify the programming space, and allocate the rest to EEPROM Emulation?
is having a larger space better for endurance?

tl;dr Does anyone have an example of simple variable declaration, read, and write to/from program flash?
#1

11 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 17002
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: PIC24 Data EEPROM Emulation 2018/11/26 15:28:22 (permalink)
    4 (1)
    1. You understand correctly.
     
    If you are not trying to write Random bytes, you can save directly to the Flash skipping the Library.
    The Flash memory is grouped in to Erase pages.  The Number of "Words" in a Page is listed in the Data sheet.
    Each Erase page is arranged in to one or smaller groups that can be written to at once.  Many Chips also have a Word write (for a PIC24 24 bits)  Note the lower 16 bits can be read in ordinary C  (But need the Flash API to be written)
    a Write changes a 1 to a Zero.  So it is possible to write more than one time without erasing if you only need 1 to 0 transitions. 
    #2
    noob
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2017/10/17 05:08:19
    • Location: 0
    • Status: offline
    Re: PIC24 Data EEPROM Emulation 2018/11/27 02:01:55 (permalink)
    0
    NKurzman
    1. You understand correctly.
     
    If you are not trying to write Random bytes, you can save directly to the Flash skipping the Library.
    The Flash memory is grouped in to Erase pages.  The Number of "Words" in a Page is listed in the Data sheet.
    Each Erase page is arranged in to one or smaller groups that can be written to at once.  Many Chips also have a Word write (for a PIC24 24 bits)  Note the lower 16 bits can be read in ordinary C  (But need the Flash API to be written)
    a Write changes a 1 to a Zero.  So it is possible to write more than one time without erasing if you only need 1 to 0 transitions. 




    I do intend to save data on runtime, i think that is what you mean be random?

    i would really appreciate if you have an example code
     
    #3
    qhb
    Superb Member
    • Total Posts : 9196
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: PIC24 Data EEPROM Emulation 2018/11/27 02:36:09 (permalink)
    0
    By "random", they mean the ability to modify individual words anywhere in a block, not writing a whole page in one go.
    #4
    Mysil
    Super Member
    • Total Posts : 3206
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: PIC24 Data EEPROM Emulation 2018/11/27 03:15:43 (permalink)
    4 (1)
    Hi,
    The complicated parts of AN1095 is about wear leveling,
    that is to not destroy the Flash memory if many updates are to be stored.
    Actual Flash memory writing and erase are a few small functions that are called inside code in AN1095.
     
    For PIC24... devices, functions to erase, write and read data in flash memory, may be written in assembly language.
    You may call these functions from C language code in your own program code.
     
    DATA_EE_BANKS, DATA_EE_SIZE, NUM_DATA_EE_PAGES are macro definitions that are used to configure the code in AN1095 on a high level, they specify what area of Flash memory to be used for AN1095 data storage.
    Those macro definitions have little to do with low level functions to write and read data in flash memory.
     
    There is a complete implementation of AN1095 in the zipfile with project code that belong together with AN1095,
    there should also be example application in the same code package:
     https://www.microchip.com/wwwAppNotes/AppNotes.aspx?appnote=en530593 
    Use: DEE Emulation 16-bit 3.0    for your PIC24 device.
     
       Mysil
     
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 17002
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: PIC24 Data EEPROM Emulation 2018/11/27 07:51:04 (permalink)
    0
    The details of how you are saving data matter in this case. For example calibration data that is saved all at one time can be done via the flash API. User settings that are done infrequently are a possibly. If you are changing many bytes at many times and my exceed the number of erase cycles, then you need the App Note.
    #6
    noob
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2017/10/17 05:08:19
    • Location: 0
    • Status: offline
    Re: PIC24 Data EEPROM Emulation 2018/11/27 16:43:52 (permalink)
    0
    NKurzman
    The details of how you are saving data matter in this case. For example calibration data that is saved all at one time can be done via the flash API. User settings that are done infrequently are a possibly. If you are changing many bytes at many times and my exceed the number of erase cycles, then you need the App Note.


    Hi
    the data is usage hours of a filter. If the controller detects power loss, it will use the onboard small power source to save the usage hours, a few other bytes of data.
    on the next power up, the data is to be read. and the usage hours variables are to be initialized with the data stored in the emulated eeprom.


    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 17002
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: PIC24 Data EEPROM Emulation 2018/11/27 18:17:00 (permalink)
    0
    Then you can any of the ways including an external eeprom.

    But you need to have two copies of the data.
    Write then new one Check it is good, then erase the old one. This insures you can not loose all the data.
    And save periodically to avoid loosing to much time.
    #8
    noob
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2017/10/17 05:08:19
    • Location: 0
    • Status: offline
    Re: PIC24 Data EEPROM Emulation 2018/12/03 15:57:29 (permalink)
    0
    so i finally got round to writing some functions following the tutorial
     
    #define pagelength 1024             //page 42, one erase block 1024 instruction words
    #define rowlength 128               //one write block = 128 instruction words.
    /*
     * program memory upper boundary (instruction words) 0x02AFFE,
     * +2, i.e. 0x02B000, start of address
     * page length 1024 instruction words(erase block)
     * write block 128 instruction block; 255 bytes.
     */
    #define address 0x2B002             // might have less than 255 blocks for writing data

    uint8_t Rambuffer[pagelength * 2];
    uint8_t Rowbuffer[rowlength * 2];       //one row of data, that can be written once. 255 bytes.

    void readFlashPage(void)
    {
        int offset, i;
        TBLPAG = __builtin_tblpage (address);    //returns the page number of the memory address received as a parameter. For table instructions the returned value is placed in TBLPAG
        offset = __builtin_tbloffset (address);  //returns the offset from the base address for a memory location whose address is passed as a parameter. The return value of this function is passed as a parameter to table read and table write instructions
        offset = offset & 0xF800; //set to the base of page
        for(i = 0; i<(pagelength * 2); i++){
            Rambuffer[i++] = __builtin_tblrdl(offset);  //returns the lower 16 bits of the memory address specified by TBLPAG and the offset parameter(calls TBLRDL instruction)
            Rambuffer[i] = __builtin_tblrdh(offset);    //returns the upper 8-bits of the memory adddress specified by TBLPAG and the offset parameter(calls TBLRDL instruction)
            offset = offset + 2;
        }
    }

    void eraseFlashPage(void){
        int offset;
        NVMADRU = __builtin_tblpage(address);
        offset = __builtin_tbloffset(address);
        NVMADR = (offset & 0xF800); // for page size of 1024 PM words

        //set WREN and page Erase in NVMCON
        NVMCON = 0x4003;

        __builtin_disi(6);      //disable interrupts for next six instructions
        __builtin_write_NVM();  //intiate write process
    }

    void rowFlashWrite(void){
        int offset, i;
        TBLPAG = 0xFA;   // base address of write latches 0xFA0000h till 0xFA00FEh

        //load row of data into write latches
        offset = 0;
        for (i = 0; i < rowlength * 2 ; i++){
            __builtin_tblwtl(offset, Rowbuffer[i++]);
            __builtin_tblwth(offset, Rowbuffer[i]);
            offset+=2;
        }

        //set the destination address into the NVM address registers

        NVMADRU = __builtin_tblpage(address);
        offset = __builtin_tbloffset(address);
        NVMADR = (offset & 0xF800);   // for page size of 1024 PM words

        //set WREN and enable row write in NVMCON

        NVMCON = 0x4002;

        __builtin_disi(6); // disable interrupts for 6 instruction cycles
        __builtin_write_NVM(); // initate write process
    }

    void rowFlashRead(void){
        int offset, i;
        TBLPAG = __builtin_tblpage (address);    //returns the page number of the memory address received as a parameter. For table instructions the returned value is placed in TBLPAG
        offset = __builtin_tbloffset (address);  //returns the offset from the base address for a memory location whose address is passed as a parameter. The return value of this function is passed as a parameter to table read and table write instructions
        for(i = 0; i<(rowlength * 2); i++){
            Rowbuffer[i++] = __builtin_tblrdl(offset);  //returns the lower 16 bits of the memory address specified by TBLPAG and the offset parameter(calls TBLRDL instruction)
            Rowbuffer[i] = __builtin_tblrdh(offset);    //returns the upper 8-bits of the memory adddress specified by TBLPAG and the offset parameter(calls TBLRDL instruction)
            offset = offset + 2;
        }
    }


    but i am getting this error when i try to build it
     
    make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
    make[1]: Entering directory 'C:/Users/HP/MPLABXProjects/Rollman/emulate.X'
    make  -f nbproject/Makefile-default.mk dist/default/production/emulate.X.production.hex
    make[2]: Entering directory 'C:/Users/HP/MPLABXProjects/Rollman/emulate.X'
    "C:\Program Files (x86)\Microchip\xc16\v1.35\bin\xc16-gcc.exe"   main.c  -o build/default/production/main.o  -c -mcpu=24FJ256GA702  -MMD -MF "build/default/production/main.o.d"      -mno-eds-warn  -g -omf=elf -DXPRJ_default=default  -legacy-libc    -O0 -msmart-io=1 -Wall -msfr-warn=off  
    nbproject/Makefile-default.mk:155: recipe for target 'build/default/production/main.o' failed
    make[2]: Leaving directory 'C:/Users/HP/MPLABXProjects/Rollman/emulate.X'
    nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
    make[1]: Leaving directory 'C:/Users/HP/MPLABXProjects/Rollman/emulate.X'
    nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
    main.c: In function 'readFlashPage':
    main.c:83:32: error: Argument to __builtin_tblpage() is not the address
    of an object in a code, psv, or eedata section;
    the object must not be qualified with any form of index
    main.c:84:34: error: Argument to __builtin_tbloffset() is not the address
    of an object in a code, psv, or eedata section;
    the object must not be qualified with any form of index
    main.c: In function 'eraseFlashPage':
    main.c:95:32: error: Argument to __builtin_tblpage() is not the address
    of an object in a code, psv, or eedata section;
    the object must not be qualified with any form of index
    main.c:96:33: error: Argument to __builtin_tbloffset() is not the address
    of an object in a code, psv, or eedata section;
    the object must not be qualified with any form of index
    main.c: In function 'rowFlashWrite':
    main.c:120:32: error: Argument to __builtin_tblpage() is not the address
    of an object in a code, psv, or eedata section;
    the object must not be qualified with any form of index
    main.c:121:33: error: Argument to __builtin_tbloffset() is not the address
    of an object in a code, psv, or eedata section;
    the object must not be qualified with any form of index
    main.c: In function 'rowFlashRead':
    main.c:134:32: error: Argument to __builtin_tblpage() is not the address
    of an object in a code, psv, or eedata section;
    the object must not be qualified with any form of index
    main.c:135:34: error: Argument to __builtin_tbloffset() is not the address
    of an object in a code, psv, or eedata section;
    the object must not be qualified with any form of index
    make[2]: *** [build/default/production/main.o] Error 255
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2

    BUILD FAILED (exit value 2, total time: 573ms)

    how do i resolve this problem?
    also, can you please see the code, and tell me what i am doing wrong
    thanks

    p.s. i think i fixed it by
    const unsigned int __attribute__ ((space(prog), address (0x6000))) table[rowlength * 2];
    and passing table to __builtin_tblpage.
     
     
    but now the functions dont work as they should,
    nothing gets written, or if it does, i only read ones....
    i really need some help here.
    post edited by noob - 2018/12/05 08:56:22
    #9
    Kenneth_L
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2017/08/20 18:34:46
    • Location: 0
    • Status: offline
    Re: PIC24 Data EEPROM Emulation 2019/01/09 11:02:22 (permalink)
    0
    You might want to download the latest PIC24 plugin described in plug http://ww1.microchip.com/downloads/en/DeviceDoc/release_notes_pic24_dspic_pic32mm_v1_95.pdf
     
    It has these libraries...
    #10
    Nicholas Lindan
    Starting Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2017/03/06 21:25:34
    • Location: Cleveland, Ohio
    • Status: offline
    Re: PIC24 Data EEPROM Emulation 2019/01/10 13:30:58 (permalink)
    0
    You can also use an outboard EEPROM.  A 4kb (512 byte, 8 pin package) device costs less than $0.15 in ones / $0.10 in quantity (Digikey).  EEPROMs usually have 4 million or so write cycles, are random read/write access and interface over I2C.

    Nicholas Lindan
    Cleveland Engineering Design, LLC
    #11
    IvanP
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2014/11/14 05:00:13
    • Location: 0
    • Status: offline
    Re: PIC24 Data EEPROM Emulation 2019/01/24 06:37:49 (permalink)
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5