• AVR Freaks

Hot![Resolved] dsPIC30F2010 EEPROM doesn't want to write

Author
rickford66
Super Member
  • Total Posts : 472
  • Reward points : 0
  • Joined: 2008/06/23 14:21:30
  • Location: 0
  • Status: offline
2020/01/22 12:20:58 (permalink)
0

[Resolved] dsPIC30F2010 EEPROM doesn't want to write

I have some code that I ported over from an app that used a dsPIC30F4XXX device.  It writes to the EEPROM.  It's giving me some grief as it's not working for the dsPIC30F2010.  One curious thing is that, when I step through the code, NVMCON doesn't seem to show bit 15 as set.  Thinking it may be a timing thing and is just going away too fast, I put a read of the NVMCON to a variable right after the set bit 15 instruction, and even at full speed, it doesn't show as set there either.  Here is the code.  All interrupts are disabled when executing this code.  Ideas?  Btw, the read back at the end works, as I can manually insert values into EEPROM with the debugger and it reads them correctly.
 
//returns 0 if successful, valid addresses are 0 to 1023 inclusive
int writeToEEPROM(int eeAddress, int eeData)
{
 #define OFFSET 0x007FFC00 //EEPROM addresses are 0x7FFC00 to 7FFFFE
 int tempAddress;
 long realAddress;
 int tryCount;
 int testData;

 realAddress = (long)eeAddress + OFFSET; //add offset to EEPROM address
 if ((realAddress > 0x007FFFFE) || (realAddress < 0x007FFC00))
  return 1; //invalid address, indicate error, get out

 for(tryCount = 0; tryCount < 5; tryCount++) //try to write 5 times before giving up
 {
  asm("clrwdt");
  NVMCON = 0x4044; //select EEPROM word, erase, wren
  NVMADRU = (int)((realAddress >> 16) & 0x0000007F);
  NVMADR = (int)(realAddress & 0x0000FFFF);
  //assembly code
  asm("mov #0x55,W2");
  asm("mov W2,NVMKEY");
  asm("mov #0xAA,W2");
  asm("mov W2,NVMKEY");
  asm("bset NVMCON,#15");
  asm("nop");
  asm("nop");
  //done with assembly
  while(NVMCONbits.WR == 1) //wait for EEPROM to finish erase cycle
   asm("clrwdt");

  //done with erase, now do write
  NVMCON = 0x4004; //write one word, write, wren
  TBLPAG = (int)((realAddress >> 16) & 0x0000007F); //set up page
  tempAddress = (int)(realAddress & 0x0000FFFF); //lose page info
  //assembly code
  asm("mov %0,W1" : "+g"(tempAddress));
  asm("mov %0,W2" : "+g"(eeData));
  asm("tblwtl W2,[W1]");
  asm("mov #0x55,W2");
  asm("mov W2,NVMKEY");
  asm("mov #0xAA,W2");
  asm("mov W2,NVMKEY");
  asm("bset NVMCON,#15");
  asm("nop");
  asm("nop");
  //done with assembly
  while(NVMCONbits.WR == 1) //wait for EEPROM to finish write cycle
   asm("clrwdt");

  testData = readFromEEPROM(eeAddress); //test to see if written correctly
  if (testData == eeData)
   return 0; //all went well, indicate so by returning 0
 }
 return 1; //there was an error, indicate so by returning 1
}

 
post edited by rickford66 - 2020/01/27 10:46:16
#1

6 Replies Related Threads

    rickford66
    Super Member
    • Total Posts : 472
    • Reward points : 0
    • Joined: 2008/06/23 14:21:30
    • Location: 0
    • Status: offline
    Re: dsPIC30F2010 EEPROM doesn't want to write 2020/01/24 15:34:54 (permalink)
    0
    In my init code, I have some lines that have been there forever throughout many projects.  They are to disable modules in the processor to save power.  The  following line is the source of my problem.  I don't know what it does.  Perhaps it's not supported on this processor and turns something else off instead.  Anyone know what this does?
     
    PMD2bits.IC8MD = 1;
    #2
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dsPIC30F2010 EEPROM doesn't want to write 2020/01/24 16:15:03 (permalink)
    0
    That is most strange.
    Your datasheet does not mention any PMD registers, but they certainly appear in the p30F2010.h header that comes with XC16.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11577
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: dsPIC30F2010 EEPROM doesn't want to write 2020/01/24 21:59:35 (permalink)
    0
    I'm guessing that if your PIC actually had PMD, that would disable input capture 8, which obviously has nothing to do with EEPROM.
     
    Have you looked at what address is being affected by writing to the bogus PMD2bits?
    #4
    rickford66
    Super Member
    • Total Posts : 472
    • Reward points : 0
    • Joined: 2008/06/23 14:21:30
    • Location: 0
    • Status: offline
    Re: dsPIC30F2010 EEPROM doesn't want to write 2020/01/27 08:32:02 (permalink)
    0
    Disassembly listing...
    469:                PMD2bits.IC8MD = 1;
      0CEA  A8E773     bset.b 0x0773,#7
     
    0x773 is in the middle of program space.  The other thing that's awkward about this is I've never run into a situation like this where the compiler didn't object.  I see the declarations of PMD2 in the header file.  What in the world is a declaration like that doing in the header file for the 30f2010 if the 30f2010 has no support of it?
    #5
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dsPIC30F2010 EEPROM doesn't want to write 2020/01/27 12:24:48 (permalink)
    0
    rickford66
    0x773 is in the middle of program space. 

    No it's not.
    Are you forgetting that PIC's are "Harvard Architecture", with separate address busses for program and data space?
    0x773 is up the top end of the SFR space. See "FIGURE 3-6: DATA SPACE MEMORY MAP" in the datasheet.
     

    The other thing that's awkward about this is I've never run into a situation like this where the compiler didn't object.  I see the declarations of PMD2 in the header file.  What in the world is a declaration like that doing in the header file for the 30f2010 if the 30f2010 has no support of it?

    It doesn't object because of that definition in the header file.
    It would seem to be a mistake on Microchip's part that it is there.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    rickford66
    Super Member
    • Total Posts : 472
    • Reward points : 0
    • Joined: 2008/06/23 14:21:30
    • Location: 0
    • Status: offline
    Re: dsPIC30F2010 EEPROM doesn't want to write 2020/01/27 12:35:24 (permalink)
    0
    You're right, I did forget.  I'll have to look into this.  Thanks.
    #7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5