]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2129_IAR/main.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / ARM7_LPC2129_IAR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /*\r
31         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
32         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
33         called.  The demo applications included in the FreeRTOS.org download switch\r
34         to supervisor mode prior to main being called.  If you are not using one of\r
35         these demo application projects then ensure Supervisor mode is used.\r
36 */\r
37 \r
38 \r
39 /*\r
40  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
41  * documentation provides more details of the demo application tasks.\r
42  *\r
43  * Main.c also creates a task called "Check".  This only executes every three\r
44  * seconds but has the highest priority so is guaranteed to get processor time.\r
45  * Its main function is to check that all the other tasks are still operational.\r
46  * Each task (other than the "flash" tasks) maintains a unique count that is\r
47  * incremented each time the task successfully completes its function.  Should\r
48  * any error occur within such a task the count is permanently halted.  The\r
49  * check task inspects the count of each task to ensure it has changed since\r
50  * the last time the check task executed.  If all the count variables have\r
51  * changed all the tasks are still executing error free, and the check task\r
52  * toggles the onboard LED.  Should any task contain an error at any time\r
53  * the LED toggle rate will change from 3 seconds to 500ms.\r
54  *\r
55  */\r
56 \r
57 /* Standard includes. */\r
58 #include <stdlib.h>\r
59 \r
60 /* Scheduler includes. */\r
61 #include "FreeRTOS.h"\r
62 #include "task.h"\r
63 \r
64 /* Demo application includes. */\r
65 #include "flash.h"\r
66 #include "integer.h"\r
67 #include "PollQ.h"\r
68 #include "BlockQ.h"\r
69 #include "semtest.h"\r
70 #include "dynamic.h"\r
71 #include "partest.h"\r
72 #include "comtest2.h"\r
73 \r
74 /* Priorities for the demo application tasks. */\r
75 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
76 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
77 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
78 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
79 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
80 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
81 \r
82 /* Constants required by the 'Check' task. */\r
83 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
84 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
85 #define mainCHECK_TASK_LED                      ( 7 )\r
86 \r
87 /* Constants for the ComTest tasks. */\r
88 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )\r
89 #define mainCOM_TEST_LED                        ( 4 )\r
90 #define mainTX_ENABLE                           ( ( unsigned long ) 0x0001 )\r
91 #define mainRX_ENABLE                           ( ( unsigned long ) 0x0004 )\r
92 \r
93 /* Constants to setup the PLL. */\r
94 #define mainPLL_MUL_5                           ( ( unsigned char ) 0x0004 )\r
95 #define mainPLL_DIV_1                           ( ( unsigned char ) 0x0000 )\r
96 #define mainPLL_ENABLE                          ( ( unsigned char ) 0x0001 )\r
97 #define mainPLL_CONNECT                         ( ( unsigned char ) 0x0003 )\r
98 #define mainPLL_FEED_BYTE1                      ( ( unsigned char ) 0xaa )\r
99 #define mainPLL_FEED_BYTE2                      ( ( unsigned char ) 0x55 )\r
100 #define mainPLL_LOCK                            ( ( unsigned long ) 0x0400 )\r
101 \r
102 /* Constants to setup the MAM. */\r
103 #define mainMAM_TIM_3                           ( ( unsigned char ) 0x03 )\r
104 #define mainMAM_MODE_FULL                       ( ( unsigned char ) 0x02 )\r
105 \r
106 /* Constants to setup the peripheral bus. */\r
107 #define mainBUS_CLK_FULL                        ( ( unsigned char ) 0x01 )\r
108 \r
109 /* And finally, constant to setup the port for the LED's. */\r
110 #define mainLED_TO_OUTPUT                       ( ( unsigned long ) 0xff0000 )\r
111 \r
112 /*\r
113  * The task that executes at the highest priority and calls\r
114  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
115  * of the file.\r
116  */\r
117 static void vErrorChecks( void *pvParameters );\r
118 \r
119 /*\r
120  * Configures the processor for use with this demo.\r
121  */\r
122 static void prvSetupHardware( void );\r
123 \r
124 /*\r
125  * Checks that all the demo application tasks are still executing without error\r
126  * - as described at the top of the file.\r
127  */\r
128 static long prvCheckOtherTasksAreStillRunning( void );\r
129 \r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /*\r
134  * Starts all the other tasks, then starts the scheduler.\r
135  */\r
136 void main( void )\r
137 {\r
138         /* Setup the processor. */\r
139         prvSetupHardware();\r
140 \r
141         /* Start all the standard demo application tasks. */\r
142         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
143         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
144         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
145         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
146         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
147         vStartDynamicPriorityTasks();\r
148         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
149                 \r
150         /* Start the check task - which is defined in this file. */\r
151         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
152 \r
153         /* Start the scheduler.\r
154 \r
155         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
156         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
157         called.  The demo applications included in the FreeRTOS.org download switch\r
158         to supervisor mode prior to main being called.  If you are not using one of\r
159         these demo application projects then ensure Supervisor mode is used here.\r
160         */\r
161         vTaskStartScheduler();\r
162 \r
163         /* We should never get here as control is now taken by the scheduler. */\r
164         return;\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 static void prvSetupHardware( void )\r
169 {\r
170         /* Setup the PLL to multiply the XTAL input by 5. */\r
171         PLLCFG = ( mainPLL_MUL_5 | mainPLL_DIV_1 );\r
172 \r
173         /* Activate the PLL by turning it on then feeding the correct sequence of\r
174         bytes. */\r
175         PLLCON = mainPLL_ENABLE;\r
176         PLLFEED = mainPLL_FEED_BYTE1;\r
177         PLLFEED = mainPLL_FEED_BYTE2;\r
178 \r
179         /* Wait for the PLL to lock... */\r
180         while( !( PLLSTAT & mainPLL_LOCK ) );\r
181 \r
182         /* ...before connecting it using the feed sequence again. */\r
183         PLLCON = mainPLL_CONNECT;\r
184         PLLFEED = mainPLL_FEED_BYTE1;\r
185         PLLFEED = mainPLL_FEED_BYTE2;\r
186 \r
187         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
188         PLL used.  It is possible faster overall performance could be obtained by\r
189         tuning the MAM and PLL settings. */\r
190         MAMTIM = mainMAM_TIM_3;\r
191         MAMCR = mainMAM_MODE_FULL;\r
192 \r
193         /* Setup the peripheral bus to be the same as the PLL output. */\r
194         APBDIV = mainBUS_CLK_FULL;\r
195         \r
196         /* Configure the RS2332 pins.  All other pins remain at their default of 0. */\r
197         PINSEL0 |= mainTX_ENABLE;\r
198         PINSEL0 |= mainRX_ENABLE;\r
199 \r
200         /* LED pins need to be output. */\r
201         IO1DIR = mainLED_TO_OUTPUT;\r
202 \r
203         /* Setup the peripheral bus to be the same as the PLL output. */\r
204         APBDIV = mainBUS_CLK_FULL;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void vErrorChecks( void *pvParameters )\r
209 {\r
210 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
211 \r
212         /* The parameters are not used in this task. */\r
213         ( void ) pvParameters;\r
214 \r
215         /* Cycle for ever, delaying then checking all the other tasks are still\r
216         operating without error.  If an error is detected then the delay period\r
217         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
218         the on board LED flash rate will increase. */\r
219 \r
220         for( ;; )\r
221         {\r
222                 /* Delay until it is time to execute again. */\r
223                 vTaskDelay( xDelayPeriod );\r
224         \r
225                 /* Check all the standard demo application tasks are executing without\r
226                 error. */\r
227                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
228                 {\r
229                         /* An error has been detected in one of the tasks - flash faster. */\r
230                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
231                 }\r
232                 \r
233                 vParTestToggleLED( mainCHECK_TASK_LED );\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static long prvCheckOtherTasksAreStillRunning( void )\r
239 {\r
240 long lReturn = ( long ) pdPASS;\r
241 \r
242         /* Check all the demo tasks (other than the flash tasks) to ensure\r
243         that they are all still running, and that none of them have detected\r
244         an error. */\r
245 \r
246         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
247         {\r
248                 lReturn = ( long ) pdFAIL;\r
249         }\r
250 \r
251         if( xArePollingQueuesStillRunning() != pdTRUE )\r
252         {\r
253                 lReturn = ( long ) pdFAIL;\r
254         }\r
255 \r
256         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
257         {\r
258                 lReturn = ( long ) pdFAIL;\r
259         }\r
260 \r
261         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
262         {\r
263                 lReturn = ( long ) pdFAIL;\r
264         }\r
265 \r
266         if( xAreComTestTasksStillRunning() != pdTRUE )\r
267         {\r
268                 lReturn = ( long ) pdFAIL;\r
269         }\r
270 \r
271         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
272         {\r
273                 lReturn = ( long ) pdFAIL;\r
274         }\r
275 \r
276         return lReturn;\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 \r