• AVR Freaks

Helpful Reply[Solved] P24H; Can't erase program memory

Author
Arie
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2010/09/16 02:20:39
  • Location: Drachten, Netherlands
  • Status: offline
2010/11/01 04:56:59 (permalink)
0

[Solved] P24H; Can't erase program memory

Hello,

i am trying to erase, and write to the program memory of a pic24HJ128GP504. But... it doesn't work :(
I've written a small test code, just to make sure these operations are working, but they just. dont.
I've also included a function to read from the program memory, this works perfectly.

So i was thinking it has something to do with write protection (as the project does compile/build).
I've checked the project (working in an existing project), but there wasn't any configuration setting that had something to do with code protection, so i added: _FGS(GSS_OFF & GCP_OFF & GWRP_OFF);
... Still nothing.

So can you please tell me if there is anything i'm missing? or is the code im using just wrong?
Thanks,
Arend.

I use C30, ICD3 (as debugger), MPLAB v8.56
I test the code using the watch window, a breakpoint, and the "debugger -> read"

  
#include "p24Hxxxx.h"
#include "libpic30.h"

void eraseProgMem(_prog_addressT startAddr)
{
    _erase_flash(startAddr);
}

void readProgMem(_prog_addressT startAddr, unsigned int nrOfInstructions)
{
    unsigned int highWord = 0;
    unsigned int lowWord = 0;

    unsigned int i = 0;
    for (i=0; i<nrOfInstructions; i++)
    {   
        TBLPAG = 0x0;
        lowWord = __builtin_tblrdl(startAddr+(i*2));
        highWord = __builtin_tblrdh(startAddr+(i*2));
    }
}

void doWriteTest()
{
    // For now, i use these addresses.
    _prog_addressT mAddr = 0x32D4; // Write to this addres
    _prog_addressT dAddr = 0x3140; // Start erasing from this address
   
    // Build test data
    int myData[_FLASH_ROW];
    int i = 0;
    for (i = 0; i < _FLASH_ROW; i++)
    {
        myData[i] = i;
    }

    _write_flash24(mAddr, myData);
    readProgMem((mAddr-0x200), _FLASH_ROW);
    eraseProgMem(dAddr);
    readProgMem(dAddr, 20);
}

post edited by Arie - 2010/11/03 02:57:19
#1
Arie
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2010/09/16 02:20:39
  • Location: Drachten, Netherlands
  • Status: offline
Re:P24H; Can't write to / erase program memory 2010/11/02 00:47:44 (permalink)
0
Hello again,

I think i fixed it. Atleast, im seeing changes in the program memory after a doWrite().
Here's what i now use, instead of the libpic30 function:


short doWrite(_prog_addressT mAddr, unsigned long mData)
{
    TBLPAG = 0x0;
    __builtin_tblwth(mAddr, ((mData&0xFF0000)>>0x10));
    __builtin_tblwtl(mAddr, (mData&0xFFFF));
    NVMCON = 0x4003;
    __builtin_write_NVM();
    while (NVMCONbits.WR) ;
    return NVMCONbits.WRERR;
}

post edited by Arie - 2010/11/02 02:32:48
#2
Arie
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2010/09/16 02:20:39
  • Location: Drachten, Netherlands
  • Status: offline
Re:P24H; Can't write to / erase program memory 2010/11/02 08:09:40 (permalink)
0
Right, so i've been trying to get the erasing to work aswell.. but no succes there. when i just run the program without breakpoints, it halts anyway (and can't properly restart without reprogramming).
If i use breakpoints, the code does continue (no halting, no need to reprogram) yet it doesnt actually erase the program memory.

Anyone have an idea what im doing wrong?

Arend.

The code (different from first post):

short doErase(unsigned long startAddr)
{
    TBLPAG = 0x0;
    __builtin_tblwtl(startAddr, 0xFFFF);
    NVMCON = 0x4042;
    __builtin_write_NVM();
    while (NVMCONbits.WR) ;
    return NVMCONbits.WRERR;
}

#3
aschen0866
Super Member
  • Total Posts : 4482
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
Re:P24H; Can't write to / erase program memory 2010/11/02 10:45:45 (permalink) ☄ Helpful
+2 (1)
Try this -
    #include <p24Hxxxx.h> 
   #include <libpic30.h>
   #include "GenericTypeDefs.h"
  
  
   void WritePage(const BYTE *ptrData, _prog_addressT dstP)
   {
      WORD r_count, inst_idx;   /* Row count and instruction count */
      DWORD_VAL temp; 
      long wrBuffer[_FLASH_ROW];
  
      /* Both _FLASH_PAGE and _FLASH_ROW are defined in libpic30.h */
      for (r_count = 0; r_count < (_FLASH_PAGE / _FLASH_ROW); r_count++)
      {
         for (inst_idx = 0; inst_idx < _FLASH_ROW; inst_idx++)
         {
            temp.v[0] = *ptrData++;
            temp.v[1] = *ptrData++;
            temp.v[2] = *ptrData++;
            temp.v[3] = 0;          /* Pad phantom byte with a zero */
            wrBuffer[inst_idx] = (long)temp.Val;
         }     
        
         _write_flash24(dstP, wrBuffer);
         dstP += _FLASH_ROW * 2;    /* Even address only */
      }
   }
  
   int main (void)
   {
      BYTE foo[256];
      int i;
     
      /* Dummy test data */
      for (i = 0; i < 256; i++)
         foo[i] = i;
        
      _erase_flash(0x3200);   /* Must be on the page boundry */
      WritePage(foo, 0x3200);
  
      while (1);   
      return 0;   
   }  

post edited by aschen0866 - 2010/11/02 10:49:39
#4
Arie
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2010/09/16 02:20:39
  • Location: Drachten, Netherlands
  • Status: offline
Re:P24H; Can't write to / erase program memory 2010/11/03 02:56:38 (permalink)
0
Thank you for your reply, aschen!
That example is pretty clear to me, but not exactly what i need. I need to write one instruction (i receive some CAN messages containing data, which needs to be written to the program memory).

I had the writing fixed earlier, but the erasing is fixed now too.
Well, fixed.. was never broken. but i kind of "forgot" that erasing is page aligned, so thanks for pointing that out to me!

i also removed libpic30.h from my list of includes. I used my own defines to define _FLASH_PAGE n stuff. And i made my own functions for writing/erasing.
Why did i "reinvent" the wheel? because im a student, and i need to learn as much as possible about these subjects :D
#5
Antipodean
Super Member
  • Total Posts : 1724
  • Reward points : 0
  • Joined: 2008/12/09 10:19:08
  • Location: Didcot, United Kingdom
  • Status: online
Re:P24H; Can't write to / erase program memory 2010/11/03 04:04:56 (permalink)
+1 (1)
Arie

Why did i "reinvent" the wheel? because im a student, and i need to learn as much as possible about these subjects :D


Trouble is that will produce an ethic of always re-inventing the wheel instead of learning how to use the provided information. Showing you know how to use the provided information should involve just as much learning as it shows you have properly researched what is provided.

Do not use my alias in your message body when replying, your message will disappear ...

Alan
#6
imk
Junior Member
  • Total Posts : 95
  • Reward points : 0
  • Joined: 2007/01/05 16:41:49
  • Location: UK (North West)
  • Status: offline
Re:P24H; Can't write to / erase program memory 2010/11/04 08:38:11 (permalink)
0
Arie,
I wish you luck sorting this matter out as I have been trying to do it for days if not weeks now and most of the repiles/post are from people that understand/or think they understand the architecture with stuff that is just more confusing or just does not work.
 
I feel this is an are that PIC needs to do some work in and put out some examples that do more that just a couple of words. (what ever they may be) I simply want to pack of a 1536 byte structure into flash and load it back again and this is frankly like pulling teeth in pic24/dsp.
 
The post from aschen0866
void WritePage( unsigned char *ptrData , _prog_addressT dstP )
works really well and is a proper examples of how you do things.
I have tried to creat a
void ReadPage( unsigned char *ptrData , _prog_addressT dstP )
but it does not work so maybe he/she will pick up on this thread again and post one.
 
Then at least I can close out on this three year long project of mine.
 
Again all the best and good luck IMK
 
 
 
#7
aschen0866
Super Member
  • Total Posts : 4482
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
Re:P24H; Can't write to / erase program memory 2010/11/04 10:46:40 (permalink)
0
You can use this function to read anywhere in the program memory including DEVID and configuration words:

DWORD ReadPm(_prog_addressT addr)
{
   DWORD_VAL temp, pm_data;
   
   temp.Val = addr;
   TBLPAG = temp.word.HW;
   pm_data.word.HW = __builtin_tblrdh(temp.word.LW);
   pm_data.word.LW = __builtin_tblrdl(temp.word.LW);
   
   return pm_data.Val;



There are many other methods. Check out the attachment in Calum's Post#14.
#8
imk
Junior Member
  • Total Posts : 95
  • Reward points : 0
  • Joined: 2007/01/05 16:41:49
  • Location: UK (North West)
  • Status: offline
Re:P24H; Can't write to / erase program memory 2010/11/04 16:07:06 (permalink)
0
aschen0866

You can use this function to read anywhere in the program memory including DEVID and configuration words:

DWORD ReadPm(_prog_addressT addr)
{
  DWORD_VAL temp, pm_data;
  
  temp.Val = addr;
  TBLPAG = temp.word.HW;
  pm_data.word.HW = __builtin_tblrdh(temp.word.LW);
  pm_data.word.LW = __builtin_tblrdl(temp.word.LW);
  
  return pm_data.Val;



There are many other methods. Check out the attachment in Calum's Post#14.

Many thanks for this post and handy for reading a word when that all I need but the _memcpy_p2d24() is what I am looking for so many thanks for the clarity on the len issue. Sorry to all that I have not appriciated in this quest to solve this prog mem page write/read but been a long time on this project and being stalled at the end is most frustrating when you are only a couple of days away from completion. (Bit like being stuck at a red light when your trying to get to hospital to see your first born come it the world.)
 
Also the Calum post is very good and one to print and keep in the handy tips file.
 
All the best IMK
 
#9
Jump to:
© 2019 APG vNext Commercial Version 4.5