]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/WIN32-MSVC/main_blinky.c
Ensure both one-shot and auto-reload are written consistently with a hyphen in comments.
[freertos] / FreeRTOS / Demo / WIN32-MSVC / main_blinky.c
index a3683efe51012ef092aa91a2a8e0f2cf3babd1e7..e9ef5fb307bde514fb3d35644ef24aa9025c1173 100644 (file)
 /*\r
-    FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
-    All rights reserved\r
-\r
-    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
-\r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    FreeRTOS provides completely free yet professionally developed,    *\r
-     *    robust, strictly quality controlled, supported, and cross          *\r
-     *    platform software that has become a de facto standard.             *\r
-     *                                                                       *\r
-     *    Help yourself get started quickly and support the FreeRTOS         *\r
-     *    project by purchasing a FreeRTOS tutorial book, reference          *\r
-     *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
-     *                                                                       *\r
-     *    Thank you!                                                         *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
-    This file is part of the FreeRTOS distribution.\r
-\r
-    FreeRTOS is free software; you can redistribute it and/or modify it under\r
-    the terms of the GNU General Public License (version 2) as published by the\r
-    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
-\r
-    >>! NOTE: The modification to the GPL is included to allow you to distribute\r
-    >>! a combined work that includes FreeRTOS without being obliged to provide\r
-    >>! the source code for proprietary components outside of the FreeRTOS\r
-    >>! kernel.\r
-\r
-    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
-    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
-    FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
-    link: http://www.freertos.org/a00114.html\r
-\r
-    1 tab == 4 spaces!\r
-\r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    Having a problem?  Start by reading the FAQ "My application does   *\r
-     *    not run, what could be wrong?"                                     *\r
-     *                                                                       *\r
-     *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
-    http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
-    license and Real Time Engineers Ltd. contact details.\r
-\r
-    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
-    including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
-    compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
-\r
-    http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
-    Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
-    licenses offer ticketed support, indemnification and middleware.\r
-\r
-    http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
-    engineered and independently SIL3 certified version for use in safety and\r
-    mission critical applications that require provable dependability.\r
-\r
-    1 tab == 4 spaces!\r
-*/\r
+ * FreeRTOS Kernel V10.2.1\r
+ * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://www.FreeRTOS.org\r
+ * http://aws.amazon.com/freertos\r
+ *\r
+ * 1 tab == 4 spaces!\r
+ */\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: Windows will not be running the FreeRTOS demo threads continuously, so\r
+ * do not expect to get real time behaviour from the FreeRTOS Windows port, or\r
+ * this demo application.  Also, the timing information in the FreeRTOS+Trace\r
+ * logs have no meaningful units.  See the documentation page for the Windows\r
+ * port for further information:\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
  * NOTE 2:  This project provides two demo applications.  A simple blinky style\r
  * project, and a more comprehensive test and demo application.  The\r
  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
- * in main.c.  This file implements the simply blinky style version.\r
+ * in main.c.  This file implements the simply blinky version.  Console output\r
+ * is used in place of the normal LED toggling.\r
  *\r
  * NOTE 3:  This file only contains the source code that is specific to the\r
  * basic demo.  Generic functions, such FreeRTOS hook functions, are defined\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 periodic task that sends\r
+ * the value 100 to the queue every 200 milliseconds (please read the notes\r
+ * above 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.\r
+ *\r
+ * Expected Behaviour:\r
+ * - The queue send task writes to the queue every 200ms, so every 200ms the\r
+ *   queue receive task will output a message indicating that data was received\r
+ *   on the queue from the queue send task.\r
+ * - The queue send software timer has a period of two seconds, and is reset\r
+ *   each time a key is pressed.  So if two seconds expire without a key being\r
+ *   pressed then the queue receive task will output a message indicating that\r
+ *   data was received on the queue from the queue send software timer.\r
+ *\r
+ * NOTE:  Console input and output relies on Windows system calls, which can\r
+ * interfere with the execution of the FreeRTOS Windows port.  This demo only\r
+ * uses Windows system call occasionally.  Heavier use of Windows system calls\r
+ * can crash the port.\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_RATE_MS constant. */\r
-#define mainQUEUE_SEND_FREQUENCY_MS                    ( 200 / portTICK_RATE_MS )\r
+/* The rate at which data is sent to the queue.  The times are converted from\r
+milliseconds to ticks using the pdMS_TO_TICKS() macro. */\r
+#define mainTASK_SEND_FREQUENCY_MS                     pdMS_TO_TICKS( 200UL )\r
+#define mainTIMER_SEND_FREQUENCY_MS                    pdMS_TO_TICKS( 2000UL )\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 at once. */\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
+/* 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
@@ -148,30 +117,50 @@ 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 xQueueHandle xQueue = NULL;\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
+/*** SEE THE COMMENTS AT THE TOP OF THIS FILE ***/\r
 void main_blinky( void )\r
 {\r
+const TickType_t xTimerPeriod = mainTIMER_SEND_FREQUENCY_MS;\r
+\r
        /* Create the queue. */\r
-       xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
+       xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );\r
 \r
        if( xQueue != NULL )\r
        {\r
                /* Start the two tasks as described in the comments at the top of this\r
                file. */\r
-               xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
-                                       "Rx",                                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
-                                       configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */\r
-                                       ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */\r
-                                       mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */\r
-                                       NULL );                                                                 /* The task handle is not required, so NULL is passed. */\r
+               xTaskCreate( prvQueueReceiveTask,                       /* The function that implements the task. */\r
+                                       "Rx",                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
+                                       configMINIMAL_STACK_SIZE,               /* The size of the stack to allocate to the task. */\r
+                                       NULL,                                                   /* The parameter passed to the task - not used in this simple case. */\r
+                                       mainQUEUE_RECEIVE_TASK_PRIORITY,/* The priority assigned to the task. */\r
+                                       NULL );                                                 /* The task handle is not required, so NULL is passed. */\r
+\r
+               xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
 \r
-               xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\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
+               xTimerStart( xTimer, 0 ); /* The scheduler has not started so use a block time of 0. */\r
 \r
                /* Start the tasks and timer running. */\r
                vTaskStartScheduler();\r
@@ -188,65 +177,98 @@ void main_blinky( void )
 \r
 static void prvQueueSendTask( void *pvParameters )\r
 {\r
-portTickType xNextWakeTime;\r
-const unsigned long ulValueToSend = 100UL;\r
+TickType_t xNextWakeTime;\r
+const TickType_t xBlockTime = 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
+       /* Prevent the compiler warning about the unused parameter. */\r
        ( void ) pvParameters;\r
 \r
-       /* Check the task parameter is as expected. */\r
-       configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );\r
-\r
        /* Initialise xNextWakeTime - this only needs to be done once. */\r
        xNextWakeTime = xTaskGetTickCount();\r
 \r
        for( ;; )\r
        {\r
                /* Place this task in the blocked state until it is time to run again.\r
-               The block time is specified in ticks, the constant used converts ticks\r
-               to ms.  While in the Blocked state this task will not consume any CPU\r
-               time. */\r
-               vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
+               The block time is specified in ticks, pdMS_TO_TICKS() was used to\r
+               convert a time specified in milliseconds into a time specified in ticks.\r
+               While in the Blocked state this task will not consume any CPU time. */\r
+               vTaskDelayUntil( &xNextWakeTime, xBlockTime );\r
 \r
                /* 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
+               write to the console.  0 is used as the block time so the send 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
+       /* This is the software timer callback function.  The software timer has a\r
+       period of two seconds and is reset each time a key is pressed.  This\r
+       callback function will execute if the timer expires, which will only happen\r
+       if a key is not pressed for two seconds. */\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
+uint32_t ulReceivedValue;\r
 \r
-       /* Remove compiler warning in the case that configASSERT() is not\r
-       defined. */\r
+       /* Prevent the compiler warning about the unused parameter. */\r
        ( void ) pvParameters;\r
 \r
-       /* Check the task parameter is as expected. */\r
-       configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );\r
-\r
        for( ;; )\r
        {\r
                /* Wait until something arrives in the queue - this task will block\r
                indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
-               FreeRTOSConfig.h. */\r
+               FreeRTOSConfig.h.  It will not use any CPU time while it is in the\r
+               Blocked state. */\r
                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 an 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.  However, note the comments at the top of\r
+               this file about the risks of making Windows system calls (such as\r
+               console output) from a FreeRTOS task. */\r
+               if( ulReceivedValue == mainVALUE_SENT_FROM_TASK )\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
+                       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
+                       /* 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