]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/WIN32-MSVC/main_blinky.c
MSP430:
[freertos] / FreeRTOS / Demo / WIN32-MSVC / main_blinky.c
index f6291913c3cb2f1de228987a068aa5d29f902082..d2048f4469a4e8b7a308c2ef86fd26f43de1179b 100644 (file)
 */\r
 \r
 /******************************************************************************\r
- * NOTE 1: The Win32 port is a simulation (or is that emulation?) only!  Do not\r
- * expect to get real time behaviour from the Win32 port or this demo\r
- * application.  It is provided as a convenient development and demonstration\r
- * test bed only.  This was tested using Windows XP on a dual core laptop.\r
- *\r
- * Windows will not be running the FreeRTOS simulator threads continuously, so\r
- * the timing information in the FreeRTOS+Trace logs have no meaningful units.\r
- * See the documentation page for the Windows simulator for an explanation of\r
- * the slow timing:\r
+ * NOTE 1: Do not expect to get real time behaviour from the Win32 port or this\r
+ * demo application.  It is provided as a convenient development and\r
+ * demonstration test bed only.  Windows will not be running the FreeRTOS\r
+ * threads continuously, so the timing information in the FreeRTOS+Trace logs\r
+ * have no meaningful units.  See the documentation page for the Windows\r
+ * simulator for further explanation:\r
  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
  *\r
  * in main.c.\r
  ******************************************************************************\r
  *\r
- * main_blinky() creates one queue, and two tasks.  It then starts the\r
- * scheduler.\r
+ * main_blinky() creates one queue, one software timer, and two tasks.  It then\r
+ * starts the scheduler.\r
  *\r
  * The Queue Send Task:\r
  * The queue send task is implemented by the prvQueueSendTask() function in\r
- * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
- * block for 200 (simulated as far as the scheduler is concerned, but in\r
- * reality much longer - see notes above) milliseconds, before sending the\r
- * value 100 to the queue that was created within main_blinky().  Once the\r
- * value is sent, the task loops back around to block for another 200\r
- * (simulated) milliseconds.\r
+ * this file.  It uses vTaskDelayUntil() to create a period task that sends the\r
+ * value 100 to the queue every 200 milliseconds (please read the notes above\r
+ * regarding the accuracy of timing under Windows).\r
+ *\r
+ * The Queue Send Software Timer:\r
+ * The timer is a one-shot timer that is reset by a key press.  The timer's\r
+ * period is set to two seconds - if the timer expires then its callback\r
+ * function writes the value 200 to the queue.  The callback function is\r
+ * implemented by prvQueueSendTimerCallback() within this file.\r
  *\r
  * The Queue Receive Task:\r
  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
- * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
- * blocks on attempts to read data from the queue that was created within\r
- * main_blinky().  When data is received, the task checks the value of the\r
- * data, and if the value equals the expected 100, outputs a message.  The\r
- * 'block time' parameter passed to the queue receive function specifies that\r
- * the task should be held in the Blocked state indefinitely to wait for data\r
- * to be available on the queue.  The queue receive task will only leave the\r
- * Blocked state when the queue send task writes to the queue.  As the queue\r
- * send task writes to the queue every 200 (simulated - see notes above)\r
- * milliseconds, the queue receive task leaves the Blocked state every 200\r
- * milliseconds, and therefore outputs a message every 200 milliseconds.\r
+ * in this file.  prvQueueReceiveTask() waits for data to arrive on the queue.\r
+ * When data is received, the task checks the value of the data, then outputs a\r
+ * message to indicate if the data came from the queue send task or the queue\r
+ * send software timer.  As the queue send task writes to the queue every 200ms,\r
+ * the queue receive task will print a message indicating that it received data\r
+ * from the queue send task every 200ms.  The queue receive task will print a\r
+ * message indicating that it received data from the queue send software timer\r
+ * 2 seconds after a key was last pressed.\r
  */\r
 \r
 /* Standard includes. */\r
 #include <stdio.h>\r
+#include <conio.h>\r
 \r
 /* Kernel includes. */\r
 #include "FreeRTOS.h"\r
 #include "task.h"\r
+#include "timers.h"\r
 #include "semphr.h"\r
 \r
 /* Priorities at which the tasks are created. */\r
 #define mainQUEUE_RECEIVE_TASK_PRIORITY                ( tskIDLE_PRIORITY + 2 )\r
 #define        mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
 \r
-/* The rate at which data is sent to the queue.  The 200ms value is converted\r
-to ticks using the portTICK_PERIOD_MS constant. */\r
-#define mainQUEUE_SEND_FREQUENCY_MS                    ( 200 / portTICK_PERIOD_MS )\r
+/* The rate at which data is sent to the queue.  The times are converted from\r
+milliseconds to ticks by the pdMS_TO_TICKS() macro where they are used. */\r
+#define mainTASK_SEND_FREQUENCY_MS                     200\r
+#define mainTIMER_SEND_FREQUENCY_MS                    2000\r
 \r
-/* The number of items the queue can hold.  This is 1 as the receive task\r
-will remove items as they are added, meaning the send task should always find\r
-the queue empty. */\r
-#define mainQUEUE_LENGTH                                       ( 1 )\r
+/* The number of items the queue can hold. */\r
+#define mainQUEUE_LENGTH                                       ( 2 )\r
 \r
 /* Values passed to the two tasks just to check the task parameter\r
 functionality. */\r
 #define mainQUEUE_SEND_PARAMETER                       ( 0x1111UL )\r
 #define mainQUEUE_RECEIVE_PARAMETER                    ( 0x22UL )\r
 \r
+/* The values sent to the queue receive task from the queue send task and the\r
+queue send software timer respectively. */\r
+#define mainVALUE_SENT_FROM_TASK                       ( 100UL )\r
+#define mainVALUE_SENT_FROM_TIMER                      ( 200UL )\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
@@ -152,15 +155,25 @@ functionality. */
 static void prvQueueReceiveTask( void *pvParameters );\r
 static void prvQueueSendTask( void *pvParameters );\r
 \r
+/*\r
+ * The callback function executed when the software timer expires.\r
+ */\r
+static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle );\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 /* The queue used by both tasks. */\r
 static QueueHandle_t xQueue = NULL;\r
 \r
+/* A software timer that is started from the tick hook. */\r
+static TimerHandle_t xTimer = NULL;\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 void main_blinky( void )\r
 {\r
+const TickType_t xTimerPeriod = pdMS_TO_TICKS( mainTIMER_SEND_FREQUENCY_MS );\r
+\r
        /* Create the queue. */\r
        xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
 \r
@@ -177,6 +190,13 @@ void main_blinky( void )
 \r
                xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
 \r
+               /* Create the software timer, but don't start it yet. */\r
+               xTimer = xTimerCreate( "Timer",                         /* The text name assigned to the software timer - for debug only as it is not used by the kernel. */\r
+                                                               xTimerPeriod,           /* The period of the software timer in ticks. */\r
+                                                               pdFALSE,                        /* xAutoReload is set to pdFALSE, so this is a one shot timer. */\r
+                                                               NULL,                           /* The timer's ID is not used. */\r
+                                                               prvQueueSendTimerCallback );/* The function executed when the timer expires. */\r
+\r
                /* Start the tasks and timer running. */\r
                vTaskStartScheduler();\r
        }\r
@@ -193,8 +213,8 @@ void main_blinky( void )
 static void prvQueueSendTask( void *pvParameters )\r
 {\r
 TickType_t xNextWakeTime;\r
-const unsigned long ulValueToSend = 100UL;\r
-const TickType_t xBlockTime = pdMS_TO_TICKS( mainQUEUE_SEND_FREQUENCY_MS );\r
+const TickType_t xBlockTime = pdMS_TO_TICKS( mainTASK_SEND_FREQUENCY_MS );\r
+const uint32_t ulValueToSend = mainVALUE_SENT_FROM_TASK;\r
 \r
        /* Remove compiler warning in the case that configASSERT() is not\r
        defined. */\r
@@ -217,12 +237,26 @@ const TickType_t xBlockTime = pdMS_TO_TICKS( mainQUEUE_SEND_FREQUENCY_MS );
                /* Send to the queue - causing the queue receive task to unblock and\r
                toggle the LED.  0 is used as the block time so the sending operation\r
                will not block - it shouldn't need to block as the queue should always\r
-               be empty at this point in the code. */\r
+               have at least one space at this point in the code. */\r
                xQueueSend( xQueue, &ulValueToSend, 0U );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle )\r
+{\r
+const uint32_t ulValueToSend = mainVALUE_SENT_FROM_TIMER;\r
+\r
+       /* Avoid compiler warnings resulting from the unused parameter. */\r
+       ( void ) xTimerHandle;\r
+\r
+       /* Send to the queue - causing the queue receive task to unblock and\r
+       write out a message.  This function is called from the timer/daemon task, so\r
+       must not block.  Hence the block time is set to 0. */\r
+       xQueueSend( xQueue, &ulValueToSend, 0U );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 static void prvQueueReceiveTask( void *pvParameters )\r
 {\r
 unsigned long ulReceivedValue;\r
@@ -242,16 +276,34 @@ unsigned long ulReceivedValue;
                xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
 \r
                /*  To get here something must have been received from the queue, but\r
-               is it the expected value?  If it is, toggle the LED. */\r
-               if( ulReceivedValue == 100UL )\r
+               is it the expected value?  Normally calling printf() from a task is not\r
+               a good idea.  Here there is lots of stack space and only one task is\r
+               using console IO so it is ok. */\r
+               if( ulReceivedValue == mainVALUE_SENT_FROM_TASK )\r
+               {\r
+                       printf( "Message received from task\r\n" );\r
+               }\r
+               else if( ulReceivedValue == mainVALUE_SENT_FROM_TIMER )\r
+               {\r
+                       printf( "Message received from software timer\r\n" );\r
+               }\r
+               else\r
+               {\r
+                       printf( "Unexpected message\r\n" );\r
+               }\r
+\r
+               /* Reset the timer if a key has been pressed.  The timer will write\r
+               mainVALUE_SENT_FROM_TIMER to the queue when it expires. */\r
+               if( _kbhit() != 0 )\r
                {\r
-                       /* Normally calling printf() from a task is not a good idea.  Here\r
-                       there is lots of stack space and only one task is using console  IO\r
-                       so it is ok. */\r
-                       printf( "Message received\r\n" );\r
-                       ulReceivedValue = 0U;\r
+                       /* Remove the key from the input buffer. */\r
+                       ( void ) _getch();\r
+\r
+                       /* Reset the software timer. */\r
+                       xTimerReset( xTimer, portMAX_DELAY );\r
                }\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+\r