/* Standard includes */ #include // prototype declarations for I/O functions /* FreeRTOS includes */ #include "FreeRTOS.h" #include "task.h" #include "queue.h" #define TASK_A_MSG (0xAAAA) #define TASK_B_MSG (0xBBBB) xQueueHandle xQueue; void prvTaskA (void* pvParameters) { unsigned portLONG ulVar = TASK_A_MSG; (void) pvParameters; // Just to stop compiler warnings. for (;;) { vTaskDelay(500); printf("Task A\n"); if (ulVar == TASK_A_MSG) ulVar = TASK_B_MSG; else ulVar = TASK_A_MSG; if( xQueue != 0 ) { // Send an unsigned long. Wait for 10 ticks for space to become // available if necessary. if( xQueueSend( xQueue, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS ) { // Failed to post the message, even after 10 ticks. for(;;); // trap, set break point here! } } vTaskDelay(500); } } void prvTaskB (void* pvParameters) { unsigned portLONG pxRxedMessage = 0; (void) pvParameters; // Just to stop compiler warnings. for (;;) { printf("Task B\n"); if( xQueue != 0 ) { // Receive a message on the created queue. Block for 10 ticks if a // message is not immediately available. if( xQueueReceive( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) ) { // pcRxedMessage now points to the struct AMessage variable posted // by vATask. if (pxRxedMessage == TASK_A_MSG) printf("R: TASK_A_MSG\n"); else if (pxRxedMessage == TASK_B_MSG) printf("R: TASK_B_MSG\n"); else printf("UNKNOWN MSG\n"); } } vTaskDelay(1000); } } int main (void) { portTickType xNextWakeTime; xNextWakeTime = xTaskGetTickCount(); xTaskCreate( prvTaskA, ( signed char * ) "TaskA", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL ); xTaskCreate( prvTaskB, ( signed char * ) "TaskB", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL ); // Create a queue capable of containing 10 unsigned long values. xQueue = xQueueCreate( 10, sizeof( unsigned portLONG ) ); if( xQueue == 0 ) { // Queue was not created and must not be used. for (;;) ; // trap } vTaskStartScheduler(); //should never get here printf("ERORR: vTaskStartScheduler returned!"); for (;;); } void vApplicationIdleHook( void ) { const unsigned long ulMSToSleep = 5; /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are tasks waiting to be terminated by the idle task. */ Sleep( ulMSToSleep ); } /*-----------------------------------------------------------*/ void vApplicationMallocFailedHook( void ) { /* Can be implemented if required, but probably not required in this environment and running this demo. */ } /*-----------------------------------------------------------*/ void vApplicationStackOverflowHook( void ) { /* Can be implemented if required, but not required in this environment and running this demo. */ } /*-----------------------------------------------------------*/ void vApplicationTickHook( void ) { /* Call the periodic timer test, which tests the timer API functions that can be called from an ISR. */ // vTimerPeriodicISRTests(); } /*-----------------------------------------------------------*/ void vAssertCalled( void ) { taskDISABLE_INTERRUPTS(); for( ;; ); }