CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Erasing RAM with pointer

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
E_Blue



Joined: 13 Apr 2011
Posts: 403

View user's profile Send private message

Erasing RAM with pointer
PostPosted: Tue Dec 03, 2019 12:06 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Tue Dec 03, 2019 1:13 pm     Reply with quote

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: 403

View user's profile Send private message

PostPosted: Tue Dec 03, 2019 1:20 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Tue Dec 03, 2019 1:56 pm     Reply with quote

memset is a standard C function. Should be available in any C.
E_Blue



Joined: 13 Apr 2011
Posts: 403

View user's profile Send private message

PostPosted: Tue Dec 03, 2019 2:10 pm     Reply with quote

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: 403

View user's profile Send private message

PostPosted: Tue Dec 03, 2019 2:17 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Tue Dec 03, 2019 2:33 pm     Reply with quote

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: 403

View user's profile Send private message

PostPosted: Wed Dec 04, 2019 8:32 am     Reply with quote

Ok, thanks!
I saw that variable on MPLAB X IDE RAM map but I thought that was something from MPLAB itself.
_________________
Electric Blue
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group