Unable use Segments, flash varibles

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

Unable use Segments, flash varibles

Jump to solution
1,557 Views
CarlFST60L_2nd
Contributor I

This is confusing me, so i thought i would ask for some help.
 
I have to write some code to use a flash table for variables, and want to be able to address them using their names, and put the data in the required segment.

(forgive me, i am reasonably new to C programming and Codewarrior, my background is 99% assembly code)

I do want to stay away from using actual address references due to the complex nature of the data that will be stored in flash.

I have modified the PRM file to this:

//************************************************************************************
/* This is a linker parameter file for the GP32 */

NAMES END /* CodeWarrior will pass all the needed files to the linker by command line. But here you may add your own files too. */

SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */

    ROM             =  READ_ONLY    0x9000 TO 0xFDFF;      //Moved from 0x8000 to 0x9000 to make room for flash data storage
    F_RAM           =  READ_ONLY    0x8000 TO 0x8FFF;      //Added read write block for flash data storage
    Z_RAM           =  READ_WRITE   0x0040 TO 0x00FF;
    RAM             =  READ_WRITE   0x0100 TO 0x023F;
    ROM_IMAGE       =  READ_ONLY    0xF000 TO 0xF2FF RELOCATE_TO 0x0180;
END

PLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */
    DEFAULT_RAM                         INTO  RAM;
    DEFAULT_ROM, ROM_VAR, STRINGS       INTO  ROM; /*  In case you want to use  as well, be sure the option -OnB=b is passed to the compiler. */
    _DATA_ZEROPAGE, MY_ZEROPAGE         INTO  Z_RAM;
    UserFlashData                       INTO  F_RAM;
   
ToCopyToRAM                         INTO  ROM_IMAGE;
END

STACKSIZE 0x50

VECTOR 0 _Startup /* Reset vector: this is the default entry point for an application. */
//************************************************************************************
C Code
//************************************************************************************

#include <hidef.h>                /* for EnableInterrupts macro */
#include "derivative.h"           /* include peripheral declarations */

#pragma DATA_SEG UserFlashData
char FlashByte1 = 0x08;                  //Byte to be stored in UserFlashData, initialise to 0x08
#pragma DATA_SEG DEFAULT

void main(void) {

 char* pFlash = (char*)&__SEG_START_UserFlashData

 //two ways of writing the same byte of flash to PTA (0x0000)

    PTA = *pFlash;  
   
PTA = FlashByte1;
}
//************************************************************************************

//I realize this code is completely useless and doesn’t do anything, but just wanted to illustrate what i am having problems with

When i compile this code, the assemble code is (PTA is at address 0x0000 on 68HC908GP32)
mov #0x08,0x00
(i want) it to be:
lda   0x8000
sta   0x00

I have tried many variants to this idea, and cannot get it to compile with the right assembly code.

I am only using the simulator at this point for testing, I have simulated my write/erase flash routines running from RAM with no problems, but now that im trying to actually modify data and use it from flash, i am having all sorts of problems.

All i want to be able to do is use the references to address varibles in flash (segments) using references, to create a table of variables in Flash, but not having and luck understanding how this all works.

 

 

 

Labels (1)
Tags (1)
0 Kudos
1 Solution
328 Views
CompilerGuru
NXP Employee
NXP Employee
First I guess that you got the immediate load when the FlashByte1 variable was defined as constant like

#pragma CONST_SEG UserFlashData
const char FlashByte1 = 0x08;                  //Byte to be stored in UserFlashData, initialise to 0x08
#pragma
CONST_SEG DEFAULT

I doubt that the compiler would do constant propagation on initialized variables :smileywink:.

So this takes us to the problem. We have a constant variable FlashByte1 with a known value and the compiler just tries to get better code by using the initialization value instead of the flash location.
So we have to change that the compiler is not doing that.

There is a compiler option for this porpose "
-OnCstVar".

With an rather old compiler that did not work, this did cause this thread

http://forums.freescale.com/freescale/board/message?board.id=CW816COMM&message.id=2704
(it's for the HC12 and banked, but just ignore that part, the rest aplies anyway)

which contains some hints about how to do this without that option.
BTW: please add the version of the tools you are using. In between the lines I see that you use at least 5.0, so much more recent.

So when thinking about it, you have a constant which can change anyway without the compiler knowing that. So I think it should be declared volatile, at least conceptually. Well having volatile and const at once is not that usual.

Daniel

BTW: In your prm, the line
>ROM_IMAGE       =  READ_ONLY    0xF000 TO 0xF2FF RELOCATE_TO 0x0180;
is telling to use the RAM area 0x0180..0x0480 at runtime. That obviouly wont work as there is no RAM at the upper end (or? did not check) and the lower end area is actually already in use by the RAM segment.

View solution in original post

0 Kudos
1 Reply
329 Views
CompilerGuru
NXP Employee
NXP Employee
First I guess that you got the immediate load when the FlashByte1 variable was defined as constant like

#pragma CONST_SEG UserFlashData
const char FlashByte1 = 0x08;                  //Byte to be stored in UserFlashData, initialise to 0x08
#pragma
CONST_SEG DEFAULT

I doubt that the compiler would do constant propagation on initialized variables :smileywink:.

So this takes us to the problem. We have a constant variable FlashByte1 with a known value and the compiler just tries to get better code by using the initialization value instead of the flash location.
So we have to change that the compiler is not doing that.

There is a compiler option for this porpose "
-OnCstVar".

With an rather old compiler that did not work, this did cause this thread

http://forums.freescale.com/freescale/board/message?board.id=CW816COMM&message.id=2704
(it's for the HC12 and banked, but just ignore that part, the rest aplies anyway)

which contains some hints about how to do this without that option.
BTW: please add the version of the tools you are using. In between the lines I see that you use at least 5.0, so much more recent.

So when thinking about it, you have a constant which can change anyway without the compiler knowing that. So I think it should be declared volatile, at least conceptually. Well having volatile and const at once is not that usual.

Daniel

BTW: In your prm, the line
>ROM_IMAGE       =  READ_ONLY    0xF000 TO 0xF2FF RELOCATE_TO 0x0180;
is telling to use the RAM area 0x0180..0x0480 at runtime. That obviouly wont work as there is no RAM at the upper end (or? did not check) and the lower end area is actually already in use by the RAM segment.

0 Kudos