simple pointer question CW5.7 9s08re16

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

simple pointer question CW5.7 9s08re16

2,472 Views
stevec
Contributor III
I want to store a char variable into an int address (storeing data byte to Flash) and call a routine

void di_in_ram(unsigned char command, unsigned int address, unsigned char data)

where "command" is put into one of the flash registers to tell it what action is required (erase or write)
"address" is the 16 bit address I want to write to
"data is the 8 bit byte to be written.

I know this involves pointers and I have read C books and tried various code which either produces compiler errors or fails to work (I won't bore you with them).

Could someone tell me how to achieve this. It's probably just two or three lines of code, but, for the life of me, I can't seem to get it sorted. It must be a Monday morning thing, or else I'm just getting too old!!!

Steve
Labels (1)
0 Kudos
6 Replies

328 Views
allawtterb
Contributor IV
I would change address to a pointer and change the address to point to an unsigned char (I am guess you are writing 1 byte at a time) so the function declaration would look like this:
void di_in_ram(unsigned char command, unsigned char *address, unsigned char data)
 
then if you want to write '$' to 0x400 you can cast 0x400 to be a pointer in the call
 
di_in_ram(WRITE_DATA,(unsigned int *)0x400,'$');
 
In function di_in_ram you then set what is pointed to by address to data with:
 
*address = data;
 
This is follow by the flash writing process that you seem to be aware of.
0 Kudos

328 Views
stevec
Contributor III
Thanks very much for that. You have defined the function as

void di_in_ram(unsigned char command, unsigned char *address, unsigned char data)

but used the call

di_in_ram(WRITE_DATA,(unsigned int *)0x400,'$');

was this meant to be (unsigned char *)0x400 or should the function be defined with unsigned int *address?

I am assuming it should be unsigned char * as it is a pointer to a char.

Regards,
Steve
0 Kudos

328 Views
allawtterb
Contributor IV
You are correct, when I put in the change to use an unsigned char instead of unsigned int I forgot to change the part that cast it.  If you don't want to cast every call you can just define the function as
Code:
void di_in_ram (unsigned char command, unsigned int address, unsigned char data){   unsigned char *p_address;   ....   p_address = (unsigned char *)address;   ....}

 
In this case you want the address to be an unsigned int, you can then call it without the cast:
di_in_ram(WRITE_DATA,0x400,'$');
0 Kudos

328 Views
CrasyCat
Specialist III
Hello
 
I am a little bit confused here.
 
You cannot write to flash using a simple assignment in C. You need to go through some flash programming routines.
 
writing to flash requires a special algorithm and adequate timing. So writing appropriate code here is not just a matter of programming in C.
 
Did you check the available application notes on the Freescale web page?
There might be some sample code for doing that there. Not sure you find it for RE16, but you might find a pretty similar MCU.
 
CrasyCat
0 Kudos

328 Views
stevec
Contributor III
Thanks for the reply.

I have a thread on "CW for 8 bit micros" which questioned writing to flash. I am using TN228 as the basis to write ROM code to RAM and execute from there. The routine "do_in_ram" mentioned here follows the methods detailed in the reference HC08 reference manual under FLASH application notes. I am aware of the stucture required to write to Flash.
0 Kudos

328 Views
stevec
Contributor III
All I am trying to do is to write a byte to a specified memory location, both of which are passed into the routine which handles operations on  the Flash. Firstly to erase the flash block, which requires a dummy byte to be written to the block to be erased. Secondly I need to then be able to write a byte into a location within that block. These operations are not stand alone but are involved in the routine that operates the flash access. The code for this access is run from RAM.
0 Kudos