]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Kintex7_EthernetLite/RTOSDemo/src/Blinky_Demo/main_blinky.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / MicroBlaze_Kintex7_EthernetLite / RTOSDemo / src / 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 /******************************************************************************\r
31  * NOTE 1:  This project provides three demo applications.  A simple blinky\r
32  * style project, a more comprehensive test and demo application, and an\r
33  * lwIP example.  The mainSELECTED_APPLICATION setting in main.c is used to\r
34  * select between the three.  See the notes on using mainSELECTED_APPLICATION\r
35  * in main.c.  This file implements the simply blinky style version.\r
36  *\r
37  * NOTE 2:  This file only contains the source code that is specific to the\r
38  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
39  * required to configure the hardware are defined in main.c.\r
40  ******************************************************************************\r
41  *\r
42  * main_blinky() creates one queue, and two tasks.  It then starts the\r
43  * scheduler.\r
44  *\r
45  * The Queue Send Task:\r
46  * The queue send task is implemented by the prvQueueSendTask() function in\r
47  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
48  * block for 200 milliseconds, before sending the value 100 to the queue that\r
49  * was created within main_blinky().  Once the value is sent, the task loops\r
50  * back around to block for another 200 milliseconds...and so on.\r
51  *\r
52  * The Queue Receive Task:\r
53  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
54  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
55  * blocks on attempts to read data from the queue that was created within\r
56  * main_blinky().  When data is received, the task checks the value of the\r
57  * data, and if the value equals the expected 100, toggles an LED.  The 'block\r
58  * time' parameter passed to the queue receive function specifies that the\r
59  * task should be held in the Blocked state indefinitely to wait for data to\r
60  * be available on the queue.  The queue receive task will only leave the\r
61  * Blocked state when the queue send task writes to the queue.  As the queue\r
62  * send task writes to the queue every 200 milliseconds, the queue receive\r
63  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
64  * the LED every 200 milliseconds.\r
65  */\r
66 \r
67 /* Kernel includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "semphr.h"\r
71 \r
72 /* Standard demo includes. */\r
73 #include "partest.h"\r
74 \r
75 /* Priorities at which the tasks are created. */\r
76 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
77 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
78 \r
79 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
80 to ticks using the pdMS_TO_TICKS() macro. */\r
81 #define mainQUEUE_SEND_FREQUENCY_MS                     ( pdMS_TO_TICKS( 200UL ) )\r
82 \r
83 /* The number of items the queue can hold.  This is 1 as the receive task\r
84 will remove items as they are added, meaning the send task should always find\r
85 the queue empty. */\r
86 #define mainQUEUE_LENGTH                                        ( 1 )\r
87 \r
88 /* The LED toggled by the Rx task. */\r
89 #define mainTASK_LED                                            ( 0 )\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /*\r
94  * The tasks as described in the comments at the top of this file.\r
95  */\r
96 static void prvQueueReceiveTask( void *pvParameters );\r
97 static void prvQueueSendTask( void *pvParameters );\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* The queue used by both tasks. */\r
102 static QueueHandle_t xQueue = NULL;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 void main_blinky( void )\r
107 {\r
108         /* Create the queue. */\r
109         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );\r
110 \r
111         if( xQueue != NULL )\r
112         {\r
113                 /* Start the two tasks as described in the comments at the top of this\r
114                 file. */\r
115                 xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
116                                         "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
117                                         configMINIMAL_STACK_SIZE,                       /* The size of the stack to allocate to the task. */\r
118                                         NULL,                                                           /* The parameter passed to the task - not used in this case. */\r
119                                         mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
120                                         NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
121 \r
122                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
123 \r
124                 /* Start the tasks and timer running. */\r
125                 vTaskStartScheduler();\r
126         }\r
127 \r
128         /* If all is well, the scheduler will now be running, and the following\r
129         line will never be reached.  If the following line does execute, then\r
130         there was insufficient FreeRTOS heap memory available for the Idle and/or\r
131         timer tasks to be created.  See the memory management section on the\r
132         FreeRTOS web site for more details on the FreeRTOS heap\r
133         http://www.freertos.org/a00111.html. */\r
134         for( ;; );\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 static void prvQueueSendTask( void *pvParameters )\r
139 {\r
140 TickType_t xNextWakeTime;\r
141 const uint32_t ulValueToSend = 100UL;\r
142 \r
143         /* Remove compiler warning about unused parameter. */\r
144         ( void ) pvParameters;\r
145 \r
146         /* Initialise xNextWakeTime - this only needs to be done once. */\r
147         xNextWakeTime = xTaskGetTickCount();\r
148 \r
149         for( ;; )\r
150         {\r
151                 /* Place this task in the blocked state until it is time to run again. */\r
152                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
153 \r
154                 /* Send to the queue - causing the queue receive task to unblock and\r
155                 toggle the LED.  0 is used as the block time so the sending operation\r
156                 will not block - it shouldn't need to block as the queue should always\r
157                 be empty at this point in the code. */\r
158                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
159         }\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 static void prvQueueReceiveTask( void *pvParameters )\r
164 {\r
165 uint32_t ulReceivedValue;\r
166 const uint32_t ulExpectedValue = 100UL;\r
167 \r
168         /* Remove compiler warning about unused parameter. */\r
169         ( void ) pvParameters;\r
170 \r
171         for( ;; )\r
172         {\r
173                 /* Wait until something arrives in the queue - this task will block\r
174                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
175                 FreeRTOSConfig.h. */\r
176                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
177 \r
178                 /*  To get here something must have been received from the queue, but\r
179                 is it the expected value?  If it is, toggle the LED. */\r
180                 if( ulReceivedValue == ulExpectedValue )\r
181                 {\r
182                         vParTestToggleLED( mainTASK_LED );\r
183                         ulReceivedValue = 0U;\r
184                 }\r
185         }\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r