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