1
votes

I don't understand why freertos do not allow to change extern variables (myData) here is my poject


file main.c

uint16_t Mydata = 0;

main()
{
    System_Init();
    xTaskCreate(Task1, (const char*)"Task1", 100, NULL, 4, NULL);   
    xTaskCreate(Task2, (const char*)"Task2", 100, NULL, 3, NULL);   
    vTaskStartScheduler();
}

file Task1.c

extern uint16_t Mydata;

void Task1(void *p)
{ 
    while(1)
    {
        vTaskDelay(10);
        printf("Result: %d", Mydata);
    }
}

file Task2.c

extern uint16_t Mydata;

void Task2(void *p)
{
    while(1)
    {
       Mydata++;
       vTaskDelay(10);
    }
}

but the result is never correct

most of result is like "13842930", "-18234952", or something like that !

can anyone tell me why? (I'm sorry because of my bad English) thank for your help !

1

1 Answers

2
votes
  1. You cannot use %d for printing a value of uint16_t. %d expects an int. You either have to cast your value to int in the printf() call (printf("Result: %d", (int)Mydata);), or use proper specifier (printf("Result: %" PRIu16 "", Mydata);). The second solution might not be supported by your toolchain.

http://en.cppreference.com/w/cpp/io/c/fprintf

  1. Your variable should be declared volatile, otherwise compiler may as well read it once an never update the value from RAM.