]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/IA32_flat_GCC_Galileo_Gen_2/Blinky_Demo/main_blinky.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / IA32_flat_GCC_Galileo_Gen_2 / Blinky_Demo / main_blinky.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /******************************************************************************\r
30  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
31  * project, and a more comprehensive test and demo application.  The\r
32  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
33  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
34  * in main.c.  This file implements the simply blinky style version.\r
35  *\r
36  * NOTE 2:  This file only contains the source code that is specific to the\r
37  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
38  * required to configure the hardware are defined in main.c.\r
39  ******************************************************************************\r
40  *\r
41  * See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage\r
42  * instructions.\r
43  *\r
44  * main_blinky() creates one queue, and two tasks.  It then starts the\r
45  * scheduler.\r
46  *\r
47  * The Queue Send Task:\r
48  * The queue send task is implemented by the prvQueueSendTask() function in\r
49  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
50  * block for 200 milliseconds, before sending the value 100 to the queue that\r
51  * was created within main_blinky().  Once the value is sent, the task loops\r
52  * back around to block for another 200 milliseconds...and so on.\r
53  *\r
54  * The Queue Receive Task:\r
55  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
56  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
57  * blocks on attempts to read data from the queue that was created within\r
58  * main_blinky().  When data is received, the task checks the value of the\r
59  * data, and if the value equals the expected 100, outputs a message to the COM\r
60  * port.  The 'block time' parameter passed to the queue receive function\r
61  * specifies that the task should be held in the Blocked state indefinitely to\r
62  * wait for data to be available on the queue.  The queue receive task will only\r
63  * leave the Blocked state when the queue send task writes to the queue.  As the\r
64  * queue send task writes to the queue every 200 milliseconds, the queue receive\r
65  * task leaves the Blocked state every 200 milliseconds, and therefore writes to\r
66  * the COM port every 200 milliseconds.\r
67  */\r
68 \r
69 /* Kernel includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include "semphr.h"\r
73 \r
74 /* Added Galileo SERIAL support */\r
75 #include "galileo_support.h"\r
76 \r
77 /* Priorities at which the tasks are created. */\r
78 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
79 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
80 \r
81 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
82 to ticks using the portTICK_PERIOD_MS constant. */\r
83 #define mainQUEUE_SEND_FREQUENCY_MS                     ( pdMS_TO_TICKS( 200 ) )\r
84 \r
85 /* The number of items the queue can hold.  This is 1 as the receive task\r
86 will remove items as they are added, meaning the send task should always find\r
87 the queue empty. */\r
88 #define mainQUEUE_LENGTH                                        ( 1 )\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /*\r
93  * The tasks as described in the comments at the top of this file.\r
94  */\r
95 static void prvQueueReceiveTask( void *pvParameters );\r
96 static void prvQueueSendTask( void *pvParameters );\r
97 \r
98 /*\r
99  * Called by main() to create the simply blinky style application if\r
100  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
101  */\r
102 void main_blinky( void );\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /* The queue used by both tasks. */\r
107 static QueueHandle_t xQueue = NULL;\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage\r
112 instructions. */\r
113 void main_blinky( void )\r
114 {\r
115         /* Create the queue. */\r
116         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );\r
117 \r
118         if( xQueue != NULL )\r
119         {\r
120                 /* Start the two tasks as described in the comments at the top of this\r
121                 file. */\r
122                 xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
123                                         "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
124                                         configMINIMAL_STACK_SIZE * 2,           /* The size of the stack to allocate to the task. */\r
125                                         NULL,                                                           /* The parameter passed to the task - not used in this case. */\r
126                                         mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
127                                         NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
128 \r
129                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE * 2, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
130 \r
131                 /* Start the tasks and timer running. */\r
132                 vTaskStartScheduler();\r
133         }\r
134 \r
135         /* If all is well, the scheduler will now be running, and the following\r
136         line will never be reached.  If the following line does execute, then\r
137         there was either insufficient FreeRTOS heap memory available for the idle\r
138         and/or timer tasks to be created, or vTaskStartScheduler() was called from\r
139         User mode.  See the memory management section on the FreeRTOS web site for\r
140         more details on the FreeRTOS heap http://www.freertos.org/a00111.html.  The\r
141         mode from which main() is called is set in the C start up code and must be\r
142         a privileged mode (not user mode). */\r
143         for( ;; );\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 static void prvQueueSendTask( void *pvParameters )\r
148 {\r
149 TickType_t xNextWakeTime;\r
150 const uint32_t ulValueToSend = 100UL;\r
151 \r
152         /* Remove compiler warning about unused parameter. */\r
153         ( void ) pvParameters;\r
154 \r
155         /* Initialise xNextWakeTime - this only needs to be done once. */\r
156         xNextWakeTime = xTaskGetTickCount();\r
157 \r
158         for( ;; )\r
159         {\r
160                 /* Place this task in the blocked state until it is time to run again. */\r
161                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
162 \r
163                 /* Send to the queue - causing the queue receive task to unblock and\r
164                 write to the COM port.  0 is used as the block time so the sending\r
165                 operation will not block - it shouldn't need to block as the queue\r
166                 should always be empty at this point in the code. */\r
167                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
168         }\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 static void prvQueueReceiveTask( void *pvParameters )\r
173 {\r
174 uint32_t ulReceivedValue, ulLEDStatus;\r
175 const uint32_t ulExpectedValue = 100UL;\r
176 \r
177         /* Remove compiler warning about unused parameter. */\r
178         ( void ) pvParameters;\r
179 \r
180         /* Initial cursor position to skip a line) */\r
181         g_printf_rcc( 5, 2, DEFAULT_SCREEN_COLOR, "LED on the Galileo board should be blinking." );\r
182 \r
183         for( ;; )\r
184         {\r
185                 /* Wait until something arrives in the queue - this task will block\r
186                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
187                 FreeRTOSConfig.h. */\r
188                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
189 \r
190                 /*  To get here something must have been received from the queue, but\r
191                 is it the expected value?  If it is, write a message to the COMP\r
192                 port. */\r
193                 if( ulReceivedValue == ulExpectedValue )\r
194                 {\r
195                         /* Toggle the LED, and also print the LED toggle state to the\r
196                         UART. */\r
197                         ulLEDStatus = ulBlinkLED();\r
198 \r
199                         /* Print the LED status */\r
200                         g_printf_rcc( 6, 2, DEFAULT_SCREEN_COLOR, "LED State = %d\r\n", ( int ) ulLEDStatus );\r
201                         ulReceivedValue = 0U;\r
202                 }\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r