This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Global & Static initialization problem

Hi,
I am developing a firmware for EZUSB FX based board. I am facing a small problem. I am not able to initialize a Global variable in my firmware. Even though I assign the global variable with 0 initial value, But the variable does not take that value. I am using SMALL memory model for my development.

my declerations looks like this :-
BYTE GlobalCount = 0;

and in my sof() I am using this variable and updating it and reusing it again in next sof().
Please advise ..

Rahul Gupta

  • Are you including in your project STARTUP.A51 (to initialize memory to zero) and INIT.A51 (to copy static initialized data from ROM to RAM)?

  • yeah i am including them !!
    But still global variable is not initialized

    I am just including these files in my project. Is there anything else which needs to be done here ...


    TIA
    Rahul

  • Look, whether the STARUP.A51 is the last File for the Compile and Link Process (in uV2 the last in the Tree).

  • it is the last file indeed ..!! but still ..no respite !!

    Another piece of information I want to add here is the function is a interrupt routine called sof().. and i want use the global variable across the Sof's (like for counting number of interrupts .. etc )

    Also, I have DSCR.A51, Init.A51 and STARTUP.A51 included in my projects in that order.


    TIA
    Rahul

  • Since GlobalCount is declared as follows:

    BYTE GlobalCount = 0;

    I have a few questions.

    1. Are you using LARGE memory model.

    2. Did you enable clearing of XDATA in your STARTUP code?

    3. Have you started the uVision Debugger (without running to main) and set a breakpoint on read or write accesses to GlobalCount? The debugger will halt execution whenever anything reads or writes this variable INCLUDING the initialization routines.

    Jon

  • Hi,

    1) Memory model is small
    2) My initialization is
    BYTE GlobalCount = 0;

    So, DO I have to clear XDATA in my STARTUP code.(I am not using xdata here)

    I am a beginner on 8051, So I might be making very basic mistake.
    Please Advise ...

    Thanks
    Rahul

  • So, DO I have to clear XDATA in my STARTUP code.(I am not using xdata here)

    If you're not using XDATA, then, NO.

    Here's what I'd do.

    1. In uVision, open your project options and do to the debug tab. Uncheck go til main under the simulation side (left side). Make sure the simulator radio button (top left) is checked.

    2. Start the debugger.

    3. Set a memory access breakpoint on GlobalCount. Type the following in the Command Window: bs readwrite GlobalCount.

    4. Run the program.

    Execution stops when anything reads or writes to GlobalCount. The first place that program execution stops should be in the startup code (because that clears all data memory to 0).

    Jon

  • I got the debugging thing and this is what I have found.
    1) GlobalCount is initialized to 0 in my startup code, main(), and even in TD_POLL(polling function).

    2) But in SOF() (interrupt routine) it was not zero and had some garbage value..... I couldn't verify this in simulation mode (I guess simulation mode does not support interrupts).

    My problem is I want this global to be 0 in my SOF() (interrupt routine) for the first time and then update it, so that it is again available in next SOF interrupt. ??

    TIA
    Rahul

  • Reduce your code to the minimum complete program that exhibits the problem then post it here.

  • GlobalCount is initialized to 0 in my startup code, main(), and even in TD_POLL(polling function).

    Sounds like the compiler and startup code are doing their jobs.

    But in SOF() (interrupt routine) it was not zero and had some garbage value.

    Sounds like it's getting trashed somewhere else in your program.

    Jon

  • here is the relevant code ...
    I have included init.A51 and STARTUP.A51 in the project in that order.

    static BYTE glbCount = 0; // global

    //Interrupt Routine
    void ISR_Sof(void) interrupt 0

    {

    BYTE i = 0;



    for (i=0;i<16;i++)

    {

    IN8DATA = glbCount;

    }



    glbCount = glbCount + 0x10;



    EZUSB_IRQ_CLEAR();

    USBIRQ = bmSOF; // Clear SOF IRQ

    }


    Now this ISR_SOF() starts with some arbit value and increment by 0x10 as desired.
    Interestingly,
    When glbCount is initialized with 0 the values returned by the Firmware (IN8DATA) are 20, 30 ,40... (i.e. the LS-nibble is 0)
    while if I initialize glbCount by 2 the values returned are 22, 32, 42 .. etc (i.e. the LS-nibble is 2).
    P.S. - the values 20, 30 . is an example, As I have told you initial value is garbage.

    Please Advise..
    Thanks
    Rahul Gupta

  • As, in code Posted above I never touch the GlobalCount in any of my functions.
    There should be any corruption.

    Is there any other way I can achieve the desiref functionality ???

    Thanks
    Rahul Gupta

  • oops! it's really bad! :-( :-(
    try to understand where this memory location was overwrited.
    1. Check .m51, where var is located.

    2 is there any array before or after this var?
    array index may be wrong, for example
    char x[7];
    x[i]= 0; with i>= 7;
    use x[8] and x[i&7] instead, especially for receiver buffer!

    3 did you use any pointer operation, like char *x;
    *x= value; x may be wrong.

    4 did you use sprintf ? If format is wrong, it may put data outside the buffer

  • On Analysing the problem further, This is the behaviour I have observed ....
    If I remove the code line viz. glbCount = glbCount + 0x10; in the Code attached below.
    The glbCount is initialized to 0.
    But if this line of code is there then I my glbCount can start
    It seems this particular line of code is corrupting the Initial value.
    If it is ... what is the alternate way to achieve the desired functionality ???

    TIA
    Rahul Gupta

    static BYTE glbCount = 0; // global

    //Interrupt Routine
    void ISR_Sof(void) interrupt 0
    {
    BYTE i = 0;
    for (i=0;i<16;i++)
    {
    IN8DATA = glbCount;
    }
    glbCount = glbCount + 0x10;
    EZUSB_IRQ_CLEAR();
    USBIRQ = bmSOF; // Clear SOF IRQ
    }

  • On Analysing the problem further, This is the behaviour I have observed ....
    If I remove the code line viz. glbCount = glbCount + 0x10; in the Code attached below.
    The glbCount is initialized to 0.
    But if this line of code is there then the glbCount can take any garbage value and increment subsiquently ...
    It seems this particular line of code is corrupting the Initial value.

    If it is ... what is the alternate way to achieve the desired functionality ???

    Attaching code for reference ... Please Advise !!

    static BYTE glbCount = 0; // global

    //Interrupt Routine
    void ISR_Sof(void) interrupt 0
    {
    BYTE i = 0;
    for (i=0;i<16;i++)
    {
    IN8DATA = glbCount;
    }
    glbCount = glbCount + 0x10;
    EZUSB_IRQ_CLEAR();
    USBIRQ = bmSOF; // Clear SOF IRQ
    }