]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/main_blinky.c
Add the queue set test to the SAM4S-EK Atmel Studio code.
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / main_blinky.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /******************************************************************************\r
70  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
71  * project, and a more comprehensive test and demo application.  The\r
72  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
73  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
74  * in main.c.  This file implements the simply blinky style version.\r
75  *\r
76  * NOTE 2:  This file only contains the source code that is specific to the\r
77  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
78  * required to configure the hardware, are defined in main.c.\r
79  ******************************************************************************\r
80  *\r
81  * main_blinky() creates one queue, and two tasks.  It then starts the\r
82  * scheduler.\r
83  *\r
84  * The Queue Send Task:\r
85  * The queue send task is implemented by the prvQueueSendTask() function in\r
86  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
87  * block for 200 milliseconds, before sending the value 100 to the queue that\r
88  * was created within main_blinky().  Once the value is sent, the task loops\r
89  * back around to block for another 200 milliseconds.\r
90  *\r
91  * The Queue Receive Task:\r
92  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
93  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
94  * blocks on attempts to read data from the queue that was created within\r
95  * main_blinky().  When data is received, the task checks the value of the\r
96  * data, and if the value equals the expected 100, toggles the LED.  The 'block\r
97  * time' parameter passed to the queue receive function specifies that the\r
98  * task should be held in the Blocked state indefinitely to wait for data to\r
99  * be available on the queue.  The queue receive task will only leave the\r
100  * Blocked state when the queue send task writes to the queue.  As the queue\r
101  * send task writes to the queue every 200 milliseconds, the queue receive\r
102  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
103  * the LED every 200 milliseconds.\r
104  */\r
105 \r
106 /* Standard includes. */\r
107 #include <stdio.h>\r
108 \r
109 /* Kernel includes. */\r
110 #include "FreeRTOS.h"\r
111 #include "task.h"\r
112 #include "semphr.h"\r
113 \r
114 /* Atmel library includes. */\r
115 #include "asf.h"\r
116 \r
117 /* Common demo includes. */\r
118 #include "partest.h"\r
119 \r
120 /* Priorities at which the tasks are created. */\r
121 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
122 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
123 \r
124 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
125 to ticks using the portTICK_RATE_MS constant. */\r
126 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
127 \r
128 /* The number of items the queue can hold.  This is 1 as the receive task\r
129 will remove items as they are added, meaning the send task should always find\r
130 the queue empty. */\r
131 #define mainQUEUE_LENGTH                                        ( 1 )\r
132 \r
133 /* Values passed to the two tasks just to check the task parameter \r
134 functionality. */\r
135 #define mainQUEUE_SEND_PARAMETER                        ( 0x1111UL )\r
136 #define mainQUEUE_RECEIVE_PARAMETER                     ( 0x22UL )\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /*\r
141  * The tasks as described in the comments at the top of this file.\r
142  */\r
143 static void prvQueueReceiveTask( void *pvParameters );\r
144 static void prvQueueSendTask( void *pvParameters );\r
145 \r
146 /*\r
147  * Called by main() to create the simply blinky style application if\r
148  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
149  */\r
150 void main_blinky( void );\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /* The queue used by both tasks. */\r
155 static xQueueHandle xQueue = NULL;\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void main_blinky( void )\r
160 {\r
161         /* Create the queue. */\r
162         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
163 \r
164         if( xQueue != NULL )\r
165         {\r
166                 /* Start the two tasks as described in the comments at the top of this\r
167                 file. */\r
168                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
169                                         ( signed char * ) "Rx",                                 /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
170                                         configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */\r
171                                         ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */\r
172                                         mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */\r
173                                         NULL );                                                                 /* The task handle is not required, so NULL is passed. */\r
174 \r
175                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
176 \r
177                 /* Start the tasks and timer running. */\r
178                 vTaskStartScheduler();\r
179         }\r
180 \r
181         /* If all is well, the scheduler will now be running, and the following\r
182         line will never be reached.  If the following line does execute, then\r
183         there was insufficient FreeRTOS heap memory available for the idle and/or\r
184         timer tasks     to be created.  See the memory management section on the\r
185         FreeRTOS web site for more details. */\r
186         for( ;; );\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 static void prvQueueSendTask( void *pvParameters )\r
191 {\r
192 portTickType xNextWakeTime;\r
193 const unsigned long ulValueToSend = 100UL;\r
194 \r
195         /* Check the task parameter is as expected. */\r
196         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );\r
197 \r
198         /* Initialise xNextWakeTime - this only needs to be done once. */\r
199         xNextWakeTime = xTaskGetTickCount();\r
200 \r
201         for( ;; )\r
202         {\r
203                 /* Place this task in the blocked state until it is time to run again.\r
204                 The block time is specified in ticks, the constant used converts ticks\r
205                 to ms.  While in the Blocked state this task will not consume any CPU\r
206                 time. */\r
207                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
208 \r
209                 /* Send to the queue - causing the queue receive task to unblock and\r
210                 toggle the LED.  0 is used as the block time so the sending operation\r
211                 will not block - it shouldn't need to block as the queue should always\r
212                 be empty at this point in the code. */\r
213                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
214         }\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvQueueReceiveTask( void *pvParameters )\r
219 {\r
220 unsigned long ulReceivedValue;\r
221 \r
222         /* Check the task parameter is as expected. */\r
223         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );\r
224 \r
225         for( ;; )\r
226         {\r
227                 /* Wait until something arrives in the queue - this task will block\r
228                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
229                 FreeRTOSConfig.h. */\r
230                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
231 \r
232                 /*  To get here something must have been received from the queue, but\r
233                 is it the expected value?  If it is, toggle the LED. */\r
234                 if( ulReceivedValue == 100UL )\r
235                 {\r
236                         vParTestToggleLED( 0 );\r
237                         ulReceivedValue = 0U;\r
238                 }\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void vApplicationTickHook( void )\r
244 {\r
245         /* This function will be called by each tick interrupt if\r
246         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
247         added here, but the tick hook is called from an interrupt context, so\r
248         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
249         functions can be used (those that end in FromISR()). */\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r