]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_ATSAM4E_Atmel_Studio/src/main_blinky.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M4F_ATSAM4E_Atmel_Studio / src / 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 demo application that makes use of\r
32  * FreeRTOS_CLI, FreeRTOS+UDP and FreeRTOS+FAT SL.  The\r
33  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
34  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\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, two tasks and one software timer.  It then\r
43  * starts the 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.  The task sits in a loop that sends a value to the queue every\r
48  * 200 milliseconds.\r
49  *\r
50  * The Queue Receive Task:\r
51  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
52  * in this file.  The task sits in a loop that blocks on the queue to wait for\r
53  * data to arrive (it does not use any CPU time while it is in the Blocked\r
54  * state), toggling an LED each time it receives the value sent by the queue\r
55  * send task.  As the queue send task writes to the queue every 200 milliseconds\r
56  * the LED will toggle every 200 milliseconds.\r
57  */\r
58 \r
59 \r
60 /* Kernel includes. */\r
61 #include "FreeRTOS.h"\r
62 #include "task.h"\r
63 #include "queue.h"\r
64 #include "timers.h"\r
65 \r
66 /* Demo application includes. */\r
67 #include "partest.h"\r
68 \r
69 /* Priorities at which the tasks are created. */\r
70 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
71 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
72 \r
73 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
74 to ticks using the portTICK_PERIOD_MS constant. */\r
75 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
76 \r
77 /* The number of items the queue can hold.  This is 1 as the receive task\r
78 will remove items as they are added, meaning the send task should always find\r
79 the queue empty. */\r
80 #define mainQUEUE_LENGTH                                        ( 1 )\r
81 \r
82 /* The period of the blinky software timer.  The period is specified in ms and\r
83 converted to ticks using the portTICK_PERIOD_MS constant. */\r
84 #define mainBLINKY_TIMER_PERIOD                         ( 50 / portTICK_PERIOD_MS )\r
85 \r
86 /* A block time of zero simply means "don't block". */\r
87 #define mainDONT_BLOCK                                          ( 0 )\r
88 \r
89 /* The LEDs toggled by the timer callback and queue receive task respectively. */\r
90 #define mainTIMER_LED                                           0\r
91 #define mainTASK_LED                                            1\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /*\r
96  * The tasks as described in the comments at the top of this file.\r
97  */\r
98 static void prvQueueReceiveTask( void *pvParameters );\r
99 static void prvQueueSendTask( void *pvParameters );\r
100 \r
101 /*\r
102  * The callback function for the blinky software timer, as described at the top\r
103  * of this file.\r
104  */\r
105 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );\r
106 \r
107 /*\r
108  * Called by main() to create the simply blinky style application if\r
109  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
110  */\r
111 void main_blinky( void );\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 void main_blinky( void )\r
116 {\r
117 TimerHandle_t xTimer;\r
118 QueueHandle_t xQueue;\r
119 \r
120         /* Create the queue. */\r
121         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
122 \r
123         if( xQueue != NULL )\r
124         {\r
125                 /* Start the two tasks as described in the comments at the top of this\r
126                 file. */\r
127                 xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
128                                         "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
129                                         configMINIMAL_STACK_SIZE,                       /* The size of the stack to allocate to the task. */\r
130                                         ( void * ) xQueue,                                      /* Pass the queue into the task using the task parameter. */\r
131                                         mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
132                                         NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
133 \r
134                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) xQueue, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
135 \r
136                 /* Create the blinky software timer as described at the top of this\r
137                 file. */\r
138                 xTimer = xTimerCreate(  "Blinky",                                       /* A text name, purely to help debugging. */\r
139                                                                 ( mainBLINKY_TIMER_PERIOD ),/* The timer period. */\r
140                                                                 pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
141                                                                 ( void * ) 0,                           /* The ID is not used, so can be set to anything. */\r
142                                                                 prvBlinkyTimerCallback );       /* The callback function that inspects the status of all the other tasks. */\r
143 \r
144                 configASSERT( xTimer );\r
145 \r
146                 if( xTimer != NULL )\r
147                 {\r
148                         xTimerStart( xTimer, mainDONT_BLOCK );\r
149                 }\r
150 \r
151                 /* Start the tasks and timer running. */\r
152                 vTaskStartScheduler();\r
153         }\r
154 \r
155         /* If all is well, the scheduler will now be running, and the following\r
156         line will never be reached.  If the following line does execute, then\r
157         there was insufficient FreeRTOS heap memory available for the idle and/or\r
158         timer tasks     to be created.  See the memory management section on the\r
159         FreeRTOS web site for more details. */\r
160         for( ;; );\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 static void prvQueueSendTask( void *pvParameters )\r
165 {\r
166 TickType_t xNextWakeTime;\r
167 const unsigned long ulValueToSend = 100UL;\r
168 QueueHandle_t xQueue;\r
169 \r
170         /* The handle of the queue is passed in using the task's parameter. */\r
171         xQueue = ( QueueHandle_t ) pvParameters;\r
172 \r
173         /* Initialise xNextWakeTime - this only needs to be done once. */\r
174         xNextWakeTime = xTaskGetTickCount();\r
175 \r
176         for( ;; )\r
177         {\r
178                 /* Place this task in the blocked state until it is time to run again.\r
179                 The block time is specified in ticks, the constant used converts ticks\r
180                 to ms.  While in the Blocked state this task will not consume any CPU\r
181                 time. */\r
182                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
183 \r
184                 /* Send to the queue - causing the queue receive task to unblock and\r
185                 toggle the LED.  0 is used as the block time so the sending operation\r
186                 will not block - it shouldn't need to block as the queue should always\r
187                 be empty at this point in the code. */\r
188                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
189         }\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 static void prvQueueReceiveTask( void *pvParameters )\r
194 {\r
195 unsigned long ulReceivedValue;\r
196 QueueHandle_t xQueue;\r
197 \r
198         /* The queue is passed in as the task's parameter. */\r
199         xQueue = ( QueueHandle_t ) pvParameters;\r
200 \r
201         for( ;; )\r
202         {\r
203                 /* Wait until something arrives in the queue - this task will block\r
204                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
205                 FreeRTOSConfig.h. */\r
206                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
207 \r
208                 /*  To get here something must have been received from the queue, but\r
209                 is it the expected value?  If it is, toggle the LED. */\r
210                 if( ulReceivedValue == 100UL )\r
211                 {\r
212                         vParTestToggleLED( mainTASK_LED );\r
213                         ulReceivedValue = 0U;\r
214                 }\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )\r
220 {\r
221         /* Avoid compiler warnings. */\r
222         ( void ) xTimer;\r
223 \r
224         /* This function is called when the blinky software time expires.  All the\r
225         function does is toggle the LED.  LED mainTIMER_LED should therefore toggle\r
226         with the period set by mainBLINKY_TIMER_PERIOD. */\r
227         vParTestToggleLED( mainTIMER_LED );\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r