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