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