]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D3x_Xplained_IAR/blinky_demo/main_blinky.c
9053e7bea30707d4c0887a087487b51929324646
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D3x_Xplained_IAR / blinky_demo / main_blinky.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /******************************************************************************\r
97  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
98  * project, and a more comprehensive test and demo application.  The\r
99  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
100  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
101  * in main.c.  This file implements the simply blinky style version.\r
102  *\r
103  * NOTE 2:  This file only contains the source code that is specific to the\r
104  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
105  * required to configure the hardware are defined in main.c.\r
106  ******************************************************************************\r
107  *\r
108  * main_blinky() creates one queue, and two tasks.  It then starts the\r
109  * scheduler.\r
110  *\r
111  * The Queue Send Task:\r
112  * The queue send task is implemented by the prvQueueSendTask() function in\r
113  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
114  * block for 200 milliseconds, before sending the value 100 to the queue that\r
115  * was created within main_blinky().  Once the value is sent, the task loops\r
116  * back around to block for another 200 milliseconds...and so on.\r
117  *\r
118  * The Queue Receive Task:\r
119  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
120  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
121  * blocks on attempts to read data from the queue that was created within\r
122  * main_blinky().  When data is received, the task checks the value of the\r
123  * data, and if the value equals the expected 100, toggles an LED.  The 'block\r
124  * time' parameter passed to the queue receive function specifies that the\r
125  * task should be held in the Blocked state indefinitely to wait for data to\r
126  * be available on the queue.  The queue receive task will only leave the\r
127  * Blocked state when the queue send task writes to the queue.  As the queue\r
128  * send task writes to the queue every 200 milliseconds, the queue receive\r
129  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
130  * the LED every 200 milliseconds.\r
131  */\r
132 \r
133 /* Kernel includes. */\r
134 #include "FreeRTOS.h"\r
135 #include "task.h"\r
136 #include "semphr.h"\r
137 \r
138 /* Standard demo includes. */\r
139 #include "partest.h"\r
140 \r
141 /* Priorities at which the tasks are created. */\r
142 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
143 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
144 \r
145 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
146 to ticks using the portTICK_PERIOD_MS constant. */\r
147 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
148 \r
149 /* The number of items the queue can hold.  This is 1 as the receive task\r
150 will remove items as they are added, meaning the send task should always find\r
151 the queue empty. */\r
152 #define mainQUEUE_LENGTH                                        ( 1 )\r
153 \r
154 /* The LED toggled by the Rx task. */\r
155 #define mainTASK_LED                                            ( 0 )\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /*\r
160  * Called by main when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1 in\r
161  * main.c.\r
162  */\r
163 void main_blinky( void );\r
164 \r
165 /*\r
166  * The tasks as described in the comments at the top of this file.\r
167  */\r
168 static void prvQueueReceiveTask( void *pvParameters );\r
169 static void prvQueueSendTask( void *pvParameters );\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 /* The queue used by both tasks. */\r
174 static QueueHandle_t xQueue = NULL;\r
175 \r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void main_blinky( void )\r
179 {\r
180         /* Create the queue. */\r
181         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );\r
182 \r
183         if( xQueue != NULL )\r
184         {\r
185                 /* Start the two tasks as described in the comments at the top of this\r
186                 file. */\r
187                 xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
188                                         "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
189                                         configMINIMAL_STACK_SIZE,                       /* The size of the stack to allocate to the task. */\r
190                                         NULL,                                                           /* The parameter passed to the task - not used in this case. */\r
191                                         mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
192                                         NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
193 \r
194                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
195 \r
196                 /* Start the tasks and timer running. */\r
197                 vTaskStartScheduler();\r
198         }\r
199 \r
200         /* If all is well, the scheduler will now be running, and the following\r
201         line will never be reached.  If the following line does execute, then\r
202         there was either insufficient FreeRTOS heap memory available for the idle\r
203         and/or timer tasks to be created, or vTaskStartScheduler() was called from\r
204         User mode.  See the memory management section on the FreeRTOS web site for\r
205         more details on the FreeRTOS heap http://www.freertos.org/a00111.html.  The\r
206         mode from which main() is called is set in the C start up code and must be\r
207         a privileged mode (not user mode). */\r
208         for( ;; );\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static void prvQueueSendTask( void *pvParameters )\r
213 {\r
214 TickType_t xNextWakeTime;\r
215 const unsigned long ulValueToSend = 100UL;\r
216 \r
217         /* Remove compiler warning about unused parameter. */\r
218         ( void ) pvParameters;\r
219 \r
220         /* Initialise xNextWakeTime - this only needs to be done once. */\r
221         xNextWakeTime = xTaskGetTickCount();\r
222 \r
223         for( ;; )\r
224         {\r
225                 /* Place this task in the blocked state until it is time to run again. */\r
226                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
227 \r
228                 /* Send to the queue - causing the queue receive task to unblock and\r
229                 toggle the LED.  0 is used as the block time so the sending operation\r
230                 will not block - it shouldn't need to block as the queue should always\r
231                 be empty at this point in the code. */\r
232                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvQueueReceiveTask( void *pvParameters )\r
238 {\r
239 unsigned long ulReceivedValue;\r
240 const unsigned long ulExpectedValue = 100UL;\r
241 \r
242         /* Remove compiler warning about unused parameter. */\r
243         ( void ) pvParameters;\r
244 \r
245         for( ;; )\r
246         {\r
247                 /* Wait until something arrives in the queue - this task will block\r
248                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
249                 FreeRTOSConfig.h. */\r
250                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
251 \r
252                 /*  To get here something must have been received from the queue, but\r
253                 is it the expected value?  If it is, toggle the LED. */\r
254                 if( ulReceivedValue == ulExpectedValue )\r
255                 {\r
256                         vParTestToggleLED( mainTASK_LED );\r
257                         ulReceivedValue = 0U;\r
258                 }\r
259         }\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r