View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
Erasing RAM with pointer |
Posted: Tue Dec 03, 2019 12:06 pm |
|
|
I need to clean all the RAM from 0x802 to 0x7FFF so I created this routine to do it but did not erase anything.
Code: | void CleanRAM()
{
unsigned int16 *p;
#locate p=0x800;
for(p=0x802;p<0x8000;p+=2)
{
&p=0;
}
}
|
I want to know what I'm doing wrong.
MPLAB X IDE v5.30
CCS v5.091
PIC24FJ1024GB606 _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Tue Dec 03, 2019 1:13 pm |
|
|
Several things:
Why are you locating p. Not needed and gains nothing.
Then key thing is wrong operation. & means take the address of. You
need *, which means to access what is pointed 'to'.
Code: |
void CleanRAM()
{
unsigned int16 *p;
for(p=0x802;p<0x8000;p+=2)
{
*p=0;
}
}
|
Why not just use memset?.
memset(0x800, 0, 0x77FD); |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue Dec 03, 2019 1:20 pm |
|
|
I'm locating p at 0x800 because this routine will erase(set to zero) all the RAM.
So, if I don't locate p outside the address range to be erased I will get an infinite loop.
I was not aware about memset instruction and I want to have a compatible code with other compilers not just CCS.
I'm simulating in MPLAB X IDE your code and doesn't set to zero the address pointed by p.
I will try it wit a real device. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Tue Dec 03, 2019 1:56 pm |
|
|
memset is a standard C function. Should be available in any C. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue Dec 03, 2019 2:10 pm |
|
|
Now it seems to work but only between 0x850 to 0x7FC4
From 0x7FC6 and after it doesn't set to zero.
Also, I'm getting a trap reset(code 16), so the routine never ends.
I will try with memset and see if I get a trap error. _________________ Electric Blue |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue Dec 03, 2019 2:17 pm |
|
|
memset work and I don't get trap error but doesn't clean anything beyond 0x7FC6; I don't understand why, maybe is a forbidden area(?). _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Tue Dec 03, 2019 2:33 pm |
|
|
There is an issue with writing to the very top of RAM. This is where the
stack is physically stored. You should limit your upper address to _STACK_
which is the internal variable describing the start of the stack. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Wed Dec 04, 2019 8:32 am |
|
|
Ok, thanks!
I saw that variable on MPLAB X IDE RAM map but I thought that was something from MPLAB itself. _________________ Electric Blue |
|
|
|