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

Code or compiler problem?? Need help!

Hi,
I've been working with keil uvision 4 for almost 3 years or more, coding for Cortex-M3.
I recently bump into a problem while testing my code.
For some crazy reason a function of my doesn't work unless I declare the variables as statics.
And I say it is crazy because it only happens in one function.
If I get the same code inside the function and put in main() it works perfectly, without being necessary to declare the variable as static.
No optimization is done.
Hope one of you guys could help me.
Otherwise I'll have to start declaring all local variables as "static".
Best regards,
Rodrigo Barros

PS: the function is described bellow.

//This function works
void func2(int *x, int *y)
{
        uint32_t a[1024];
        uint8_t buffer[64];

        a[1000] = *x;
        a[1023] = *y;

        //send_msg() sends a msg through USB
        send_msg("\rfunc2 begin.\n\0", strlen("\rfunc2 begin.\n\0"));

        for(a[1000] = 0; a[1000]< 5; a[1000]++)
        {
                sprintf( (char *) buffer, (char *)("func2 1.%i.\0"), a[1000]);
                send_msg( buffer, strlen(buffer) );
                cleanBuffer(buffer, 64);
                delay(100 MHZ, 0.5);            //wait for 500ms
        }


        for(a[1023]=0; a[1023]<5; a[1023]++)
        {
                sprintf( (char *) buffer, (char *)("func2 2.%i.\0"),a[1023]);
                send_msg( (char *) buffer, (char *) strlen(buffer) );
                cleanBuffer(buffer, 64);
                delay(100 MHZ, 0.5);            //wait for 500ms
        }

        send_msg("\rfunc2 end.\n\0", strlen("\rfunc2 end.\n\0"));
}


//This function doesnt work
void func1(void)
{

        uint8_t buffer[64] = {};
        static uint32_t a = 0;
        uint32_t b = 0;

        send_msg("\rfunc1 begin.\n\0", strlen("\rfunc1 begin.\n\0"));

        for(a = 0; a < 5; a++)
        {
                sprintf( (char *) buffer, (char *)("func1 1.%i.\0"), a);
                send_msg( buffer, strlen(buffer) );
                cleanBuffer(buffer, 64);
                delay(100 MHZ, 0.5);
        }


        for(b=0; b<5; b++)
        {
                sprintf( (char *) buffer, (char *)("func1 2.%i.\0"), a);
                send_msg( buffer, strlen(buffer) );
                cleanBuffer(buffer, 64);
                delay(100 MHZ, 0.5);
        }

        send_msg("\rfunc1 end.\n\0", strlen("\rfunc1 end.\n\0"));

}


int main(void)
{

        char data_received;

        init_hardware();

        while(1)
        {
                data_received = read_byte_from_serial();
                switch(data_received)
                {
                        case 0x00:
                        {
                                func2();        //      This will print all the
                                                //      messages 10 messages from the
                                                //      two for() loops

                                func1();        //      This one will only print the
                                                //      messages with the interator
                                                //      which variable is static (a).
                                                //      When it gets to the second for()
                                                //      loop it olny increments one time...
                                                //      and than get stucked forever inside
                                                //      the loop.

                        }
                        break;

                        //switch case continues
                }
        }

        //main() continues

}