Problem with C18 , global and static variables

Author
seth19
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2010/06/30 01:18:23
  • Location: 0
  • Status: offline
2010/07/20 02:45:41 (permalink)
0

Problem with C18 , global and static variables

Hi,

i am working on a microchip device, p18f46j50, with MPLAB and the C18 compilator and i have a problem with variables.

I have configure the MPLAB's build options -> MPLAB C18 -> default storage class = auto
In my project i have a global variable : unsigned char key_verification[7];
In my main program, i use a local variable : unsigned char a[7];

I have stored the same word at two different address (0xE000 ans 0x1300).
In the main i read the flash and stored it in key_verification and a the word:

ReadFlash( (unsigned long) 0xE000, (unsigned int) 7, (unsigned char *) key_verification);
ReadFlash(     (unsigned long)0x1300, (unsigned int) 7, (unsigned char *) a);



but when i build and execute the project, there is only the global variable key_verification which really has the word read.

The only solution i have found in order to make the local variable a read the word at 0x1300 is to declare it in static:

static unsigned char a[7];

But i don't understand why i have to do so.

If someone has an explanation, it would be greatwink


thanks


seth.

#1

7 Replies Related Threads

    crwilliams
    Super Member
    • Total Posts : 1663
    • Reward points : 0
    • Joined: 2005/06/24 07:56:24
    • Location: From UK now in PA
    • Status: offline
    Re:Problem with C18 , global and static variables 2010/07/20 06:31:05 (permalink)
    0
    I would initialize the array a where you declare it, perhaps:
     unsigned char a[7] = "123456";

    Run the code in the simulator. Then put a break point before and after ReadFlash(...,...,a);
    and look at array a in the Watch window.
    Look and see what is happening.
    Without seeing more of your code I can't tell what is happening.

    #2
    jtemples
    Super Member
    • Total Posts : 10074
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re:Problem with C18 , global and static variables 2010/07/20 14:57:34 (permalink)
    0
    Post some actual example code (code that can be compiled) that demonstrates the problem.

    And get rid of the casts.  They can mask errors.
    #3
    seth19
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2010/06/30 01:18:23
    • Location: 0
    • Status: offline
    Re:Problem with C18 , global and static variables 2010/07/21 06:35:22 (permalink)
    0
    hi,

    i am sorry i forgot to include my code. i am joining it this time.

    I would like to make just some precisions.
    If i don't define a in static, when i read the word, which is "tinka" by the way,
    a only get the 't' character.
    My project is a modification of the HID USB Bootloader from microchip. In the the memory zone of the bootloader i stored a identification key (at 0x1300) which i use to verify if the appliction i flash is valid. If the application is valid, the key is stored at 0xE000.

    This is working great only if a is static. 

    here is my main:

    /** V A R I A B L E S ********************************************************/
    #pragma romdata bootloader_identification_key=0x1300
    rom unsigned char bootloader_identification_key[]="tinka"; 
    #pragma udata
    word led_count;
    unsigned int pll_startup_counter; //Used for software delay while pll is starting up
    unsigned char verification_key[7];  // 
    /*****************************************************************************/



    /** P R I V A T E  P R O T O T Y P E S ***************************************/
    static void InitializeSystem(void);
    void USBTasks(void);
    void BlinkUSBStatus(void);
    void Main(void);
    void _entry (void);
    //externs
    extern void LongDelay(void);
    /******************************************************************************/



    void Main(void)
    {


        InitializeSystem(); //Some USB, I/O pins, and other initialization




    /***  Test of key_identification  ***/
    ReadFlash((unsigned long)0xE000,(unsigned int)7,(unsigned char *)verification_key); // read the identification key of the application which is stored at 0xE000

      { // ZONE de stack pour l'identification //

    /***   Local variables for identification    ***/
    static signed char identification_Valid; // if a is not static, the reading is not working
    static unsigned char a[7];
    static int cpt; // 
    /*****************************************************************/
    identification_Valid = 0;
        ReadFlash(     (unsigned long)0x1300, (unsigned int) 7, (unsigned char *) a); // read the identification key of the bootloader

     identification_Valid=strcmp( (const char*)verification_key, (const char*)a ); // we compar the keys
     if(identification_Valid ==0) // if equal ==> jump to the application
     {
    mLED_1_On();
    mLED_2_On();
    for(cpt=0;cpt<20000;cpt++)
    {
    cpt =cpt;
    }

    mLED_1_Off();
    mLED_2_Off();

      _asm
      goto 0x1400
      _endasm
     }
     else //else go back the flashing part
     {

       // to defin
              }


    /*******************************************************/
        while(1)
        {

    ClrWdt();
       USBTasks();        // Need to call USBTasks() periodically
        // it handles SETUP packets needed for enumeration

    BlinkUSBStatus(); //Blink the LEDs based on current USB state

       if((usb_device_state == CONFIGURED_STATE) && (UCONbits.SUSPND != 1))
       {
            ProcessIO();   // This is where all the actual bootloader related data transfer/self programming takes place
         }    



    }//end while
    }



    post edited by seth19 - 2010/07/21 07:02:46
    #4
    crwilliams
    Super Member
    • Total Posts : 1663
    • Reward points : 0
    • Joined: 2005/06/24 07:56:24
    • Location: From UK now in PA
    • Status: offline
    Re:Problem with C18 , global and static variables 2010/07/21 06:50:36 (permalink)
    0
    You declare array a in the  ZONE de stack pour l'identification block, which goes out of scope before you get to the while(1).

    Are you only using array a in ReadFlash(...) and identification_Valid=strcmp(...)?

    Where does verification_cle come from?

    Are you using the right version or strcmp for what you are doing (See C18 Library Help)?

    And as jtemples said, if you need those casts to make it work you are doing something wrong and then hiding the error by casting.



    post edited by crwilliams - 2010/07/21 06:55:03
    #5
    seth19
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2010/06/30 01:18:23
    • Location: 0
    • Status: offline
    Re:Problem with C18 , global and static variables 2010/07/21 07:12:59 (permalink)
    0
    Yes you're right but the array a is only use in the stack zone and so is the strcmp. 

    If a is out of scope in the while(1), i don't understand why it's problem because i only need it in the stack zone.

    Yes i only use array a in ReadFlash(...) and identification_Valid=strcmp(...).


    I am sorry, my code is not in english so i have made a translation for the post and i forgot to translate "verification_cle" == verification_key. wink


    The version of strcmp i'm using is the one microchip give.


    The cast dosn't seem to be the problem because whether or not i cast, the proble is the same, but i agree with you it's not very clean for code
    #6
    crwilliams
    Super Member
    • Total Posts : 1663
    • Reward points : 0
    • Joined: 2005/06/24 07:56:24
    • Location: From UK now in PA
    • Status: offline
    Re:Problem with C18 , global and static variables 2010/07/21 07:31:41 (permalink)
    0
    No, that's not the point.

    The point is: is the variable verification-cle stored in program memory, or RAM? Look in the library help - there are several versions (all Microchip!) of the strcmp function.

    If you don't understand why there are different versions, the fact is that with the C18 compile and link process they are necessary. (The compiler can't tell the linker where things are, so you need different functions).

     
    signed char strcmp( const char * str1, const char * str2 );
    signed char strcmppgm( const rom char * str1, const rom char * str2 );
    signed char strcmppgm2ram( const char * str1, const rom char * str2 );
    signed char strcmpram2pgm( const rom char * str1, const char * str2 );


    #7
    crwilliams
    Super Member
    • Total Posts : 1663
    • Reward points : 0
    • Joined: 2005/06/24 07:56:24
    • Location: From UK now in PA
    • Status: offline
    Re:Problem with C18 , global and static variables 2010/07/21 07:39:34 (permalink)
    0
    So, are you saying that ReadFlash(...) is supposed to take seven characters from Flash, starting at address 0x1300, and put them in array a? And that in flash, address 0x1300 contains the character 't', 0x1301 contains 'i', then 'n', 'k', and 'a'?

    And that when ReadFlash(...) returns
     a[0] is equal to t, but a[1] onwards do not contain i, n, k, and a?


    #8
    Jump to:
    © 2017 APG vNext Commercial Version 4.5