]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2129_Keil_RVDS/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / ARM7_LPC2129_Keil_RVDS / 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         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
31         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
32         called.  The demo applications included in the FreeRTOS.org download switch\r
33         to supervisor mode prior to main being called.  If you are not using one of\r
34         these demo application projects then ensure Supervisor mode is used.\r
35 */\r
36 \r
37 \r
38 /*\r
39  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
40  * documentation provides more details of the demo application tasks.\r
41  * \r
42  * Main.c also creates a task called "Check".  This only executes every three \r
43  * seconds but has the highest priority so is guaranteed to get processor time.  \r
44  * Its main function is to check that all the other tasks are still operational.\r
45  * Each task (other than the "flash" tasks) maintains a unique count that is \r
46  * incremented each time the task successfully completes its function.  Should \r
47  * any error occur within such a task the count is permanently halted.  The \r
48  * check task inspects the count of each task to ensure it has changed since\r
49  * the last time the check task executed.  If all the count variables have \r
50  * changed all the tasks are still executing error free, and the check task\r
51  * toggles the onboard LED.  Should any task contain an error at any time \r
52  * the LED toggle rate will change from 3 seconds to 500ms.\r
53  *\r
54  */\r
55 \r
56 /* Standard includes. */\r
57 #include <stdlib.h>\r
58 \r
59 /* Scheduler includes. */\r
60 #include "FreeRTOS.h"\r
61 #include "task.h"\r
62 \r
63 /* Demo application includes. */\r
64 #include "partest.h"\r
65 #include "flash.h"\r
66 #include "comtest2.h"\r
67 #include "serial.h"\r
68 #include "PollQ.h"\r
69 #include "BlockQ.h"\r
70 #include "semtest.h"\r
71 #include "dynamic.h"\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* Constants to setup I/O and processor. */\r
76 #define mainTX_ENABLE           ( ( unsigned long ) 0x00010000 )        /* UART1. */\r
77 #define mainRX_ENABLE           ( ( unsigned long ) 0x00040000 )        /* UART1. */\r
78 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
79 #define mainLED_TO_OUTPUT       ( ( unsigned long ) 0xff0000 )\r
80 \r
81 /* Constants for the ComTest demo application tasks. */\r
82 #define mainCOM_TEST_BAUD_RATE  ( ( unsigned long ) 115200 )\r
83 #define mainCOM_TEST_LED                ( 3 )\r
84 \r
85 /* Priorities for the demo application tasks. */\r
86 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
87 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
88 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
89 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
90 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
91 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
92 \r
93 /* Constants used by the "check" task.  As described at the head of this file\r
94 the check task toggles an LED.  The rate at which the LED flashes is used to\r
95 indicate whether an error has been detected or not.  If the LED toggles every\r
96 3 seconds then no errors have been detected.  If the rate increases to 500ms\r
97 then an error has been detected in at least one of the demo application tasks. */\r
98 #define mainCHECK_LED                           ( 7 )\r
99 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
100 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /*\r
105  * Checks that all the demo application tasks are still executing without error\r
106  * - as described at the top of the file.\r
107  */\r
108 static long prvCheckOtherTasksAreStillRunning( void );\r
109 \r
110 /*\r
111  * The task that executes at the highest priority and calls \r
112  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
113  * of the file.\r
114  */\r
115 static void vErrorChecks( void *pvParameters );\r
116 \r
117 /*\r
118  * Configure the processor for use with the Keil demo board.  This is very\r
119  * minimal as most of the setup is managed by the settings in the project\r
120  * file.\r
121  */\r
122 static void prvSetupHardware( void );\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 \r
127 \r
128 /*\r
129  * Application entry point:\r
130  * Starts all the other tasks, then starts the scheduler. \r
131  */\r
132 int main( void )\r
133 {\r
134         /* Setup the hardware for use with the Keil demo board. */\r
135         prvSetupHardware();\r
136 \r
137         /* Start the demo/test application tasks. */\r
138         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
139         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
140         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
141         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
142         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
143         vStartDynamicPriorityTasks();\r
144 \r
145         /* Start the check task - which is defined in this file.  This is the task\r
146         that periodically checks to see that all the other tasks are executing \r
147         without error. */\r
148         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
149 \r
150         /* Now all the tasks have been started - start the scheduler.\r
151 \r
152         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
153         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
154         called.  The demo applications included in the FreeRTOS.org download switch\r
155         to supervisor mode prior to main being called.  If you are not using one of\r
156         these demo application projects then ensure Supervisor mode is used here. */\r
157         vTaskStartScheduler();\r
158 \r
159         /* Should never reach here!  If you do then there was not enough heap\r
160         available for the idle task to be created. */\r
161         for( ;; );\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 static void vErrorChecks( void *pvParameters )\r
166 {\r
167 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
168 \r
169         /* Parameters are not used. */\r
170         ( void ) pvParameters;\r
171 \r
172         /* Cycle for ever, delaying then checking all the other tasks are still\r
173         operating without error.  If an error is detected then the delay period\r
174         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
175         the on board LED flash rate will increase.\r
176 \r
177         This task runs at the highest priority. */\r
178 \r
179         for( ;; )\r
180         {\r
181                 /* The period of the delay depends on whether an error has been \r
182                 detected or not.  If an error has been detected then the period\r
183                 is reduced to increase the LED flash rate. */\r
184                 vTaskDelay( xDelayPeriod );\r
185 \r
186                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
187                 {\r
188                         /* An error has been detected in one of the tasks - flash faster. */\r
189                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
190                 }\r
191 \r
192                 /* Toggle the LED before going back to wait for the next cycle. */\r
193                 vParTestToggleLED( mainCHECK_LED );\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 static void prvSetupHardware( void )\r
199 {\r
200         /* Perform the hardware setup required.  This is minimal as most of the\r
201         setup is managed by the settings in the project file. */\r
202 \r
203         /* Configure the UART1 pins.  All other pins remain at their default of 0. */\r
204         PINSEL0 |= mainTX_ENABLE;\r
205         PINSEL0 |= mainRX_ENABLE;\r
206 \r
207         /* LED pins need to be output. */\r
208         IODIR1 = mainLED_TO_OUTPUT;\r
209 \r
210         /* Setup the peripheral bus to be the same as the PLL output. */\r
211         VPBDIV = mainBUS_CLK_FULL;\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 static long prvCheckOtherTasksAreStillRunning( void )\r
216 {\r
217 long lReturn = pdPASS;\r
218 \r
219         /* Check all the demo tasks (other than the flash tasks) to ensure\r
220         that they are all still running, and that none of them have detected\r
221         an error. */\r
222         if( xAreComTestTasksStillRunning() != pdPASS )\r
223         {\r
224                 lReturn = pdFAIL;\r
225         }\r
226 \r
227         if( xArePollingQueuesStillRunning() != pdTRUE )\r
228         {\r
229                 lReturn = pdFAIL;\r
230         }\r
231 \r
232         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
233         {\r
234                 lReturn = pdFAIL;\r
235         }\r
236 \r
237         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
238         {\r
239                 lReturn = pdFAIL;\r
240         }\r
241 \r
242         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
243         {\r
244                 lReturn = pdFAIL;\r
245         }\r
246 \r
247         return lReturn;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 \r