benval wrote on Friday, October 17, 2014:
Hi!
I’m getting started with FreeRTOS and wrote a simple program that starts three threads that toggle an LED on a ARM Cortex-M4F.
Each thread also outputs their ID over UART, I’m using tiny printf for that.
To prevent the threads from writing over each other, I guard the printf with a semaphore.
static void my_task(void *args) {
TickType_t xNextWakeTime = xTaskGetTickCount();
int id = (int) args;
while (1) {
xSemaphoreTake(printf_semaphore, portMAX_DELAY);
printf("Hello from task %d!\n", id);
xSemaphoreGive(printf_semaphore);
/* Place this task in the blocked state until it is time to run again.
The block time is specified in ticks, the constant used converts ticks
to ms. While in the Blocked state this task will not consume any CPU
time. */
vTaskDelayUntil( &xNextWakeTime, (id + 1) * 200 / portTICK_PERIOD_MS);
switch (id) {
case 0:
RED_LED_TOGGLE;
break;
case 1:
GREEN_LED_TOGGLE;
break;
case 2:
BLUE_LED_TOGGLE;
break;
}
}
}
int main(void)
{
printf_semaphore = xSemaphoreCreateBinary();
xSemaphoreGive(printf_semaphore);
puts("Hello World!\n");
xTaskCreate(my_task, "task 1", configMINIMAL_STACK_SIZE, (void*) 0, tskIDLE_PRIORITY + 1, NULL);
xTaskCreate(my_task, "task 2", configMINIMAL_STACK_SIZE, (void*) 1, tskIDLE_PRIORITY + 1, NULL);
xTaskCreate(my_task, "task 3", configMINIMAL_STACK_SIZE, (void*) 2, tskIDLE_PRIORITY + 1, NULL);
vTaskStartScheduler();
}
This works as expected.
2014-10-17 15:57:09,334 - INFO # Hello World!
2014-10-17 15:57:09,336 - INFO # Hello from task 2!
2014-10-17 15:57:09,338 - INFO # Hello from task 0!
2014-10-17 15:57:09,339 - INFO # Hello from task 1!
2014-10-17 15:57:09,536 - INFO # Hello from task 0!
2014-10-17 15:57:09,736 - INFO # Hello from task 1!
2014-10-17 15:57:09,738 - INFO # Hello from task 0!
2014-10-17 15:57:09,935 - INFO # Hello from task 2!
2014-10-17 15:57:09,937 - INFO # Hello from task 0!
2014-10-17 15:57:10,136 - INFO # Hello from task 1!
2014-10-17 15:57:10,138 - INFO # Hello from task 0!
2014-10-17 15:57:10,336 - INFO # Hello from task 0!
2014-10-17 15:57:10,535 - INFO # Hello from task 2!
2014-10-17 15:57:10,537 - INFO # Hello from task 1!
2014-10-17 15:57:10,538 - INFO # Hello from task 0!
2014-10-17 15:57:10,736 - INFO # Hello from task 0!
2014-10-17 15:57:10,936 - INFO # Hello from task 1!
2014-10-17 15:57:10,938 - INFO # Hello from task 0!
2014-10-17 15:57:11,135 - INFO # Hello from task 2!
2014-10-17 15:57:11,137 - INFO # Hello from task 0!
2014-10-17 15:57:11,336 - INFO # Hello from task 1!
2014-10-17 15:57:11,338 - INFO # Hello from task 0!
Now instead of putting a semaphore around each printf, I would rather just add a wrapper around it.
void printf(char *fmt, ...) {
xSemaphoreTake(printf_semaphore, portMAX_DELAY);
va_list arg;
va_start(arg, fmt);
tfp_printf(fmt, arg);
va_end(arg);
xSemaphoreGive(printf_semaphore);
}
This however corrupts the threads’ local variable:
2014-10-17 16:03:29,602 - INFO # Hello World!
2014-10-17 16:03:29,604 - INFO # Hello from task 2060!
2014-10-17 16:03:29,605 - INFO # Hello from task 892!
2014-10-17 16:03:29,607 - INFO # Hello from task 1476!
2014-10-17 16:03:29,803 - INFO # Hello from task 892!
2014-10-17 16:03:30,003 - INFO # Hello from task 1476!
2014-10-17 16:03:30,005 - INFO # Hello from task 892!
2014-10-17 16:03:30,202 - INFO # Hello from task 2060!
2014-10-17 16:03:30,204 - INFO # Hello from task 892!
2014-10-17 16:03:30,403 - INFO # Hello from task 1476!
2014-10-17 16:03:30,405 - INFO # Hello from task 892!
2014-10-17 16:03:30,603 - INFO # Hello from task 892!
2014-10-17 16:03:30,802 - INFO # Hello from task 2060!
2014-10-17 16:03:30,804 - INFO # Hello from task 1476!
2014-10-17 16:03:30,806 - INFO # Hello from task 892!
2014-10-17 16:03:31,003 - INFO # Hello from task 892!
2014-10-17 16:03:31,203 - INFO # Hello from task 1476!
2014-10-17 16:03:31,205 - INFO # Hello from task 892!
2014-10-17 16:03:31,402 - INFO # Hello from task 2060!
What is going on here?