]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_EFMG890F128_IAR/main.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / CORTEX_EFMG890F128_IAR / main.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 \r
68 /*\r
69  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
70  * documentation provides more details of the standard demo application tasks.\r
71  * In addition to the standard demo tasks, the following tasks and tests are\r
72  * defined and/or created within this file:\r
73  *\r
74  * "LCD test" task - the LCD task writes a continually repeating series of patterns\r
75  * to the LCD display.\r
76  *\r
77  * "LED test" task -  This is a very simple task that just turns on user LEDs\r
78  * 8 to 15 in turn, before turning them off again.\r
79  *\r
80  * "Check task" - The check task only runs every five seconds but has the highest\r
81  * priority so is guaranteed to get processing time.  Its main job is to inspect\r
82  * all the other standard demo tasks to ensure they are executing without error.\r
83  * The Check task will toggle LED 0 every five seconds while no errors exist,\r
84  * with the toggle frequency increasing to 200ms should an error be detected in\r
85  * any other task.\r
86  *\r
87  * Both the check task and the idle task place the processor into energy saving\r
88  * mode 1, which will be exited following each tick interrupt.  The check task\r
89  * is the highest priority task in the system, so while it is executing no other\r
90  * task will execute.  If the check task places the processor into a low power\r
91  * mode without blocking then the energy consumption as viewed on the Energy\r
92  * Micro Gecko board will go down noticeably as in effect no tasks will be running.\r
93  * The check task places the processor into low power mode for two out of every\r
94  * five seconds.  The current use of low power modes is very basic.  Future\r
95  * FreeRTOS releases will aim to make significant improvements.\r
96  *\r
97  */\r
98  /* Scheduler includes. */\r
99 #include "FreeRTOS.h"\r
100 #include "croutine.h"\r
101 #include "task.h"\r
102 #include "queue.h"\r
103 #include "semphr.h"\r
104 \r
105 /* Common demo application includes. */\r
106 #include "partest.h"\r
107 #include "GenQTest.h"\r
108 #include "QPeek.h"\r
109 #include "recmutex.h"\r
110 #include "semtest.h"\r
111 \r
112 /* Demo application includes. */\r
113 #include "lcdcontroller.h"\r
114 #include "ledtest.h"\r
115 #include "lcdtest.h"\r
116 #include "chip.h"\r
117 \r
118 /* Task priorities. */\r
119 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
120 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
121 #define mainGEN_Q_TASK_PRIORITY                 ( tskIDLE_PRIORITY )\r
122 #define mainSEMAPHORE_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
123 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
124 \r
125 /* A period of two seconds, adjusted to use the tick frequency. */\r
126 #define mainTWO_SECONDS                                 ( 2000 / portTICK_RATE_MS )\r
127 \r
128 /* The length of the delay between each cycle of the check task when an error\r
129 has / has not been detected. */\r
130 #define mainNO_ERROR_CHECK_FREQUENCY    ( 5000 / portTICK_RATE_MS )\r
131 #define mainERROR_CHECK_FREQUENCY               ( 200 / portTICK_RATE_MS )\r
132 \r
133 /* The LED that is toggled by the check task.  The rate of the toggle indicates\r
134 whether or not an error has been found, as defined by the\r
135 mainNO_ERROR_CHECK_FREQUENCY and mainERROR_CHECK_FREQUENCY definitions above. */\r
136 #define mainCHECK_LED                                   ( 0 )\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /*\r
141  * Configure the hardware as required by the demo.\r
142  */\r
143 static void prvSetupHardware( void );\r
144 \r
145 /*\r
146  * The check task as described at the top of this file.\r
147  */\r
148 static void prvCheckTask( void *pvParameters );\r
149 \r
150 /*\r
151  * Put the CPU into the least low power low power mode.\r
152  */\r
153 static void prvLowPowerMode1( void );\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 int main( void )\r
158 {\r
159         /* Perform the necessary hardware configuration. */\r
160         prvSetupHardware();\r
161 \r
162         /* Create the task that writes various text and patterns to the LCD. */\r
163         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );\r
164 \r
165         /* Create a task that writes to LEDs 8 to 15. */\r
166         xTaskCreate( vLEDTask, "LCDTask", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );\r
167 \r
168         /* Create some of the standard demo tasks.  These just test the port and\r
169         demonstrate how the FreeRTOS API can be used.  They do not provide any\r
170         specific functionality. */\r
171         vStartGenericQueueTasks( mainGEN_Q_TASK_PRIORITY );\r
172         vStartQueuePeekTasks();\r
173         vStartRecursiveMutexTasks();\r
174         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
175         \r
176         /* Create the check task as described at the top of this file. */\r
177         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
178         \r
179         /* Start the scheduler. */\r
180         vTaskStartScheduler();\r
181         \r
182         /* The scheduler should now be running the tasks so the following code should\r
183         never be reached.  If it is reached then there was insufficient heap space\r
184         for the idle task to be created.  In this case the heap size is set by\r
185         configTOTAL_HEAP_SIZE in FreeRTOSConfig.h. */\r
186         for( ;; );\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vApplicationIdleHook( void )\r
191 {\r
192         /* Use the idle task to place the CPU into a low power mode.  Greater power\r
193         saving could be achieved by not including any demo tasks that never block. */\r
194         prvLowPowerMode1();\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
199 {\r
200         /* This function will be called if a task overflows its stack, if\r
201         configCHECK_FOR_STACK_OVERFLOW != 0.  It might be that the function\r
202         parameters have been corrupted, depending on the severity of the stack\r
203         overflow.  When this is the case pxCurrentTCB can be inspected in the\r
204         debugger to find the offending task. */\r
205         for( ;; );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static void prvCheckTask( void *pvParameters )\r
210 {\r
211 portTickType xLastExecutionTime, xFrequency = mainNO_ERROR_CHECK_FREQUENCY;\r
212 long lCount;\r
213 \r
214         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
215         works correctly. */\r
216         xLastExecutionTime = xTaskGetTickCount();\r
217 \r
218         for( ;; )\r
219         {\r
220                 /* Perform this check at a frequency that indicates whether or not an\r
221                 error has been found. */\r
222                 vTaskDelayUntil( &xLastExecutionTime, xFrequency );\r
223                 \r
224                 /* Check all the other tasks are running without error. */\r
225                 if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
226                 {\r
227                         xFrequency = mainERROR_CHECK_FREQUENCY;\r
228                 }\r
229                 \r
230                 if( xAreQueuePeekTasksStillRunning() != pdPASS )\r
231                 {\r
232                         xFrequency = mainERROR_CHECK_FREQUENCY;\r
233                 }\r
234                 \r
235                 if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
236                 {\r
237                         xFrequency = mainERROR_CHECK_FREQUENCY;\r
238                 }\r
239 \r
240                 if( xAreSemaphoreTasksStillRunning() != pdPASS )\r
241                 {\r
242                         xFrequency = mainERROR_CHECK_FREQUENCY;\r
243                 }\r
244                 \r
245                 /* Toggle the LED to show that the check hook function is running.\r
246                 The toggle freequency will increase if an error has been found in any\r
247                 task. */\r
248                 vParTestToggleLED( mainCHECK_LED );\r
249                 \r
250                 /* Just loop around putting the processor into low power mode 1 for\r
251                 a while.  This is the highest priority task, and this loop does not\r
252                 cause it to block, so it will remain as the running task.  Each time it\r
253                 runs for the next two seconds it will simply put the processor to sleep.\r
254                 No other task will run so nothing else will happen.  This periodic two\r
255                 seconds of lower power should be viewable using the Advanced Energy\r
256                 Monitor on the Energy Micro Gecko board. */\r
257                 for( lCount = 0; lCount < mainTWO_SECONDS; lCount++ )\r
258                 {\r
259                         prvLowPowerMode1();\r
260                 }\r
261         }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 static void prvSetupHardware( void )\r
266 {\r
267         /* Initialise the LEDs. */\r
268         vParTestInitialise();\r
269 \r
270         /* Configure the LCD. */\r
271         LCD_Init( LCD );\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 static void prvLowPowerMode1( void )\r
276 {\r
277         /* Clear SLEEPDEEP for EM1 */\r
278         SCB->SCR &= ~( 1 << SCB_SCR_SLEEPDEEP_Pos );\r
279         \r
280         /* Power down. */\r
281         __WFI();\r
282 }\r
283 \r
284 \r