]> git.sur5r.net Git - freertos/blob - Demo/ARM7_LPC2129_IAR/main.c
Update Cortex M3 ports to ensure 8 byte alignment.
[freertos] / Demo / ARM7_LPC2129_IAR / main.c
1 /*\r
2     FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 /*\r
56         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
57         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
58         called.  The demo applications included in the FreeRTOS.org download switch\r
59         to supervisor mode prior to main being called.  If you are not using one of\r
60         these demo application projects then ensure Supervisor mode is used.\r
61 */\r
62 \r
63 \r
64 /*\r
65  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
66  * documentation provides more details of the demo application tasks.\r
67  *\r
68  * Main.c also creates a task called "Check".  This only executes every three\r
69  * seconds but has the highest priority so is guaranteed to get processor time.\r
70  * Its main function is to check that all the other tasks are still operational.\r
71  * Each task (other than the "flash" tasks) maintains a unique count that is\r
72  * incremented each time the task successfully completes its function.  Should\r
73  * any error occur within such a task the count is permanently halted.  The\r
74  * check task inspects the count of each task to ensure it has changed since\r
75  * the last time the check task executed.  If all the count variables have\r
76  * changed all the tasks are still executing error free, and the check task\r
77  * toggles the onboard LED.  Should any task contain an error at any time\r
78  * the LED toggle rate will change from 3 seconds to 500ms.\r
79  *\r
80  */\r
81 \r
82 /* Standard includes. */\r
83 #include <stdlib.h>\r
84 \r
85 /* Scheduler includes. */\r
86 #include "FreeRTOS.h"\r
87 #include "task.h"\r
88 \r
89 /* Demo application includes. */\r
90 #include "flash.h"\r
91 #include "integer.h"\r
92 #include "PollQ.h"\r
93 #include "BlockQ.h"\r
94 #include "semtest.h"\r
95 #include "dynamic.h"\r
96 #include "partest.h"\r
97 #include "comtest2.h"\r
98 \r
99 /* Priorities for the demo application tasks. */\r
100 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
101 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
102 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
103 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
104 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
105 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
106 \r
107 /* Constants required by the 'Check' task. */\r
108 #define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
109 #define mainERROR_FLASH_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
110 #define mainCHECK_TASK_LED                      ( 7 )\r
111 \r
112 /* Constants for the ComTest tasks. */\r
113 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )\r
114 #define mainCOM_TEST_LED                        ( 4 )\r
115 #define mainTX_ENABLE                           ( ( unsigned long ) 0x0001 )\r
116 #define mainRX_ENABLE                           ( ( unsigned long ) 0x0004 )\r
117 \r
118 /* Constants to setup the PLL. */\r
119 #define mainPLL_MUL_5                           ( ( unsigned char ) 0x0004 )\r
120 #define mainPLL_DIV_1                           ( ( unsigned char ) 0x0000 )\r
121 #define mainPLL_ENABLE                          ( ( unsigned char ) 0x0001 )\r
122 #define mainPLL_CONNECT                         ( ( unsigned char ) 0x0003 )\r
123 #define mainPLL_FEED_BYTE1                      ( ( unsigned char ) 0xaa )\r
124 #define mainPLL_FEED_BYTE2                      ( ( unsigned char ) 0x55 )\r
125 #define mainPLL_LOCK                            ( ( unsigned long ) 0x0400 )\r
126 \r
127 /* Constants to setup the MAM. */\r
128 #define mainMAM_TIM_3                           ( ( unsigned char ) 0x03 )\r
129 #define mainMAM_MODE_FULL                       ( ( unsigned char ) 0x02 )\r
130 \r
131 /* Constants to setup the peripheral bus. */\r
132 #define mainBUS_CLK_FULL                        ( ( unsigned char ) 0x01 )\r
133 \r
134 /* And finally, constant to setup the port for the LED's. */\r
135 #define mainLED_TO_OUTPUT                       ( ( unsigned long ) 0xff0000 )\r
136 \r
137 /*\r
138  * The task that executes at the highest priority and calls\r
139  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
140  * of the file.\r
141  */\r
142 static void vErrorChecks( void *pvParameters );\r
143 \r
144 /*\r
145  * Configures the processor for use with this demo.\r
146  */\r
147 static void prvSetupHardware( void );\r
148 \r
149 /*\r
150  * Checks that all the demo application tasks are still executing without error\r
151  * - as described at the top of the file.\r
152  */\r
153 static long prvCheckOtherTasksAreStillRunning( void );\r
154 \r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /*\r
159  * Starts all the other tasks, then starts the scheduler.\r
160  */\r
161 void main( void )\r
162 {\r
163         /* Setup the processor. */\r
164         prvSetupHardware();\r
165 \r
166         /* Start all the standard demo application tasks. */\r
167         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
168         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
169         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
170         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
171         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
172         vStartDynamicPriorityTasks();\r
173         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
174                 \r
175         /* Start the check task - which is defined in this file. */\r
176         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
177 \r
178         /* Start the scheduler.\r
179 \r
180         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
181         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
182         called.  The demo applications included in the FreeRTOS.org download switch\r
183         to supervisor mode prior to main being called.  If you are not using one of\r
184         these demo application projects then ensure Supervisor mode is used here.\r
185         */\r
186         vTaskStartScheduler();\r
187 \r
188         /* We should never get here as control is now taken by the scheduler. */\r
189         return;\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 static void prvSetupHardware( void )\r
194 {\r
195         /* Setup the PLL to multiply the XTAL input by 5. */\r
196         PLLCFG = ( mainPLL_MUL_5 | mainPLL_DIV_1 );\r
197 \r
198         /* Activate the PLL by turning it on then feeding the correct sequence of\r
199         bytes. */\r
200         PLLCON = mainPLL_ENABLE;\r
201         PLLFEED = mainPLL_FEED_BYTE1;\r
202         PLLFEED = mainPLL_FEED_BYTE2;\r
203 \r
204         /* Wait for the PLL to lock... */\r
205         while( !( PLLSTAT & mainPLL_LOCK ) );\r
206 \r
207         /* ...before connecting it using the feed sequence again. */\r
208         PLLCON = mainPLL_CONNECT;\r
209         PLLFEED = mainPLL_FEED_BYTE1;\r
210         PLLFEED = mainPLL_FEED_BYTE2;\r
211 \r
212         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
213         PLL used.  It is possible faster overall performance could be obtained by\r
214         tuning the MAM and PLL settings. */\r
215         MAMTIM = mainMAM_TIM_3;\r
216         MAMCR = mainMAM_MODE_FULL;\r
217 \r
218         /* Setup the peripheral bus to be the same as the PLL output. */\r
219         APBDIV = mainBUS_CLK_FULL;\r
220         \r
221         /* Configure the RS2332 pins.  All other pins remain at their default of 0. */\r
222         PINSEL0 |= mainTX_ENABLE;\r
223         PINSEL0 |= mainRX_ENABLE;\r
224 \r
225         /* LED pins need to be output. */\r
226         IO1DIR = mainLED_TO_OUTPUT;\r
227 \r
228         /* Setup the peripheral bus to be the same as the PLL output. */\r
229         APBDIV = mainBUS_CLK_FULL;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void vErrorChecks( void *pvParameters )\r
234 {\r
235 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
236 \r
237         /* The parameters are not used in this task. */\r
238         ( void ) pvParameters;\r
239 \r
240         /* Cycle for ever, delaying then checking all the other tasks are still\r
241         operating without error.  If an error is detected then the delay period\r
242         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
243         the on board LED flash rate will increase. */\r
244 \r
245         for( ;; )\r
246         {\r
247                 /* Delay until it is time to execute again. */\r
248                 vTaskDelay( xDelayPeriod );\r
249         \r
250                 /* Check all the standard demo application tasks are executing without\r
251                 error. */\r
252                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
253                 {\r
254                         /* An error has been detected in one of the tasks - flash faster. */\r
255                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
256                 }\r
257                 \r
258                 vParTestToggleLED( mainCHECK_TASK_LED );\r
259         }\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 static long prvCheckOtherTasksAreStillRunning( void )\r
264 {\r
265 long lReturn = ( long ) pdPASS;\r
266 \r
267         /* Check all the demo tasks (other than the flash tasks) to ensure\r
268         that they are all still running, and that none of them have detected\r
269         an error. */\r
270 \r
271         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
272         {\r
273                 lReturn = ( long ) pdFAIL;\r
274         }\r
275 \r
276         if( xArePollingQueuesStillRunning() != pdTRUE )\r
277         {\r
278                 lReturn = ( long ) pdFAIL;\r
279         }\r
280 \r
281         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
282         {\r
283                 lReturn = ( long ) pdFAIL;\r
284         }\r
285 \r
286         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
287         {\r
288                 lReturn = ( long ) pdFAIL;\r
289         }\r
290 \r
291         if( xAreComTestTasksStillRunning() != pdTRUE )\r
292         {\r
293                 lReturn = ( long ) pdFAIL;\r
294         }\r
295 \r
296         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
297         {\r
298                 lReturn = ( long ) pdFAIL;\r
299         }\r
300 \r
301         return lReturn;\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 \r