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