Electronic – Unable to use compiler built-in functions to write to dsPIC EEPROM

eeprommplabpic

As I mentioned here, I've been having issues with EEPROM on dsPIC30F6012A. Since my old EEPROM library was difficult to analyze and support, I went back to square one and rewrote it using the new (newer than my library!) C30 compiler built-in EEPROM routines. I took the Microchip demo code and tried to run it. Compiled, programmed, no problems, but it didn't seem to operate correctly. I made a modified version of their demo, also without success.

I run this program, read the chip's contents back into MPLAB X, and look at the contents of EEPROM. Erase operations work correctly, any addresses I erase comes back as 0xFFFF. But the write operations do nothing. I've tried several combinations, repeated writes, different addresses, nothing appears to be written.

#include <p30fxxxx.h>
#include <libpic30.h>

 _FOSC(CSW_FSCM_OFF & FRC_PLL16); 
_FWDT(WDT_OFF);                 /* Turn off the Watch-Dog Timer.  */
_FBORPOR(MCLR_EN & PWRT_OFF);   /* Enable MCLR reset pin and turn off the power-up timers. */
_FGS(CODE_PROT_OFF);            /* Disable Code Protection */

 typedef struct _eestruct {
    unsigned char testdata[10];
} eestruct;
 eestruct eedata __attribute__((space(eedata)));
 eestruct backup_eedata __attribute__((space(eedata)));

 int main(){

    _prog_addressT EE_addr;
    //_init_prog_address(EE_addr, eedata);
    EE_addr = 0x7FF000;

    int temp_word = 0x0102;
    _erase_eedata(EE_addr, _EE_WORD);
    _wait_eedata();
    _write_eedata_word(EE_addr, temp_word);
    _wait_eedata();

    while(1){
    ClrWdt();
    };
    return 0;
}

I've got four theories:

  1. Writes are taking place, and I'm not reading the data back correctly.
  2. Writes are not taking place, because something is wrong with my
    code.
  3. Writes are not taking place, because something is wrong with
    my project/build options.
  4. Writes are not taking place, because something is wrong with my hardware.

I've posted to the Microchip forums without result. I've opened a ticket with Microchip, and all they did was tell me to use/write an assembly call. They seem uninterested in whether/why these built-in function calls don't work.

Does anyone have any suggestions?

Best Answer

When in doubt, refactor.

It's not likely a hardware issue, but to rule it out, I would try replacing the C calls with the exact inline assembly instructions described in section 7 of the datasheet, following all of its recommendations (disabling interrupts, polling WR to test for doneness, etc.)

If you get it working, make your own function/library from the inline assembly and carry on.