]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430FR5969_LaunchPad_IAR_CCS/Full_Demo/main_full.c
6259003d866dd37973f19ef5bc739ce5bf6b84ac
[freertos] / FreeRTOS / Demo / MSP430X_MSP430FR5969_LaunchPad_IAR_CCS / Full_Demo / main_full.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2018 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.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /******************************************************************************\r
29  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
30  * project, and a more comprehensive test and demo application.  The\r
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
32  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
33  * in main.c.  This file implements the comprehensive test and demo version.\r
34  *\r
35  * NOTE 2:  This file only contains the source code that is specific to the\r
36  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
37  * required to configure the hardware, are defined in main.c.\r
38  *\r
39  ******************************************************************************\r
40  *\r
41  * main_full() creates all the demo application tasks and software timers, then\r
42  * starts the scheduler.  The web documentation provides more details of the\r
43  * standard demo application tasks, which provide no particular functionality,\r
44  * but do provide a good example of how to use the FreeRTOS API.\r
45  *\r
46  * In addition to the standard demo tasks, the following tasks and tests are\r
47  * defined and/or created within this file:\r
48  *\r
49  * "Reg test" tasks - These fill both the microcontroller registers with known\r
50  * values, then check that each register maintains its expected value for the\r
51  * lifetime of the task.  Each task uses a different set of values.  The reg\r
52  * test tasks execute with a very low priority, so get preempted very\r
53  * frequently.  A register containing an unexpected value is indicative of an\r
54  * error in the context switching mechanism.\r
55  *\r
56  * "Check" task - The check task period is initially set to three seconds.  The\r
57  * task checks that all the standard demo tasks, and the register check tasks,\r
58  * are not only still executing, but are executing without reporting any errors.\r
59  * If the check task discovers that a task has either stalled, or reported an\r
60  * error, then it changes its own execution period from the initial three\r
61  * seconds, to just 200ms.  The check task also toggles an LED each time it is\r
62  * called.  This provides a visual indication of the system status:  If the LED\r
63  * toggles every three seconds, then no issues have been discovered.  If the LED\r
64  * toggles every 200ms, then an issue has been discovered with at least one\r
65  * task.\r
66  */\r
67 \r
68 /* Standard includes. */\r
69 #include <stdio.h>\r
70 \r
71 /* Kernel includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 #include "timers.h"\r
75 #include "semphr.h"\r
76 \r
77 /* Standard demo application includes. */\r
78 #include "dynamic.h"\r
79 #include "blocktim.h"\r
80 #include "countsem.h"\r
81 #include "GenQTest.h"\r
82 #include "recmutex.h"\r
83 #include "partest.h"\r
84 #include "EventGroupsDemo.h"\r
85 #include "TaskNotify.h"\r
86 \r
87 /* Priorities for the check task, as described at the top of this file. */\r
88 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
89 \r
90 /* Parameters for the task that handles the UART command console. */\r
91 #define mainCOMMAND_CONSOLE_TASK_PRIORITY       ( tskIDLE_PRIORITY )\r
92 #define mainCOMMAND_CONSOLE_STACK_SIZE          ( configMINIMAL_STACK_SIZE * 2 )\r
93 \r
94 /* The LED used by the check timer as described at the top of this file. */\r
95 #define mainCHECK_LED                                           ( 0 )\r
96 \r
97 /* The period after which the check timer will expire, in ms, provided no errors\r
98 have been reported by any of the standard demo tasks.  ms are converted to the\r
99 equivalent in ticks using the pdMS_TO_TICKS() macro. */\r
100 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 3000 )\r
101 \r
102 /* The period at which the check timer will expire, in ms, if an error has been\r
103 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
104 in ticks using the pdMS_TO_TICKS() macro. */\r
105 #define mainERROR_CHECK_TASK_PERIOD             pdMS_TO_TICKS( 200 )\r
106 \r
107 /* Parameters that are passed into the register check tasks solely for the\r
108 purpose of ensuring parameters are passed into tasks correctly. */\r
109 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x1234 )\r
110 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x8765 )\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /*\r
115  * Called by main() to run the full demo (as opposed to the blinky demo) when\r
116  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
117  */\r
118 void main_full( void );\r
119 \r
120 /*\r
121  * The check task, as described at the top of this file.\r
122  */\r
123 static void prvCheckTask( void *pvParameters );\r
124 \r
125 /*\r
126  * Register check tasks, as described at the top of this file.  The nature of\r
127  * these files necessitates that they are written in an assembly file, but the\r
128  * entry points are kept in the C file for the convenience of checking the task\r
129  * parameter.\r
130  */\r
131 static void prvRegTestTaskEntry1( void *pvParameters );\r
132 extern void vRegTest1Implementation( void );\r
133 static void prvRegTestTaskEntry2( void *pvParameters );\r
134 extern void vRegTest2Implementation( void );\r
135 \r
136 /* Starts the 'standard' UART command console task.  UART 0 is used at 19200\r
137 baud. */\r
138 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
139 \r
140 /* Registers a set of example commands that can be used in the command\r
141 console. */\r
142 void vRegisterSampleCLICommands( void );\r
143 \r
144 /*-----------------------------------------------------------*/\r
145 \r
146 /* The following two variables are used to communicate the status of the\r
147 register check tasks to the check task.  If the variables keep incrementing,\r
148 then the register check tasks have not discovered any errors.  If a variable\r
149 stops incrementing, then an error has been found. */\r
150 volatile uint16_t usRegTest1LoopCounter = 0UL, usRegTest2LoopCounter = 0UL;\r
151 \r
152 /* cOutputBuffer is used by FreeRTOS+CLI.  It is declared here so the\r
153 persistent qualifier can be used.  For the buffer to be declared here, rather\r
154 than in FreeRTOS_CLI.c, configAPPLICATION_PROVIDES_cOutputBuffer must be set to\r
155 1 in FreeRTOSConfig.h. */\r
156 #ifdef __ICC430__\r
157         __persistent                                                    /* IAR version. */\r
158 #else\r
159         #pragma PERSISTENT( cOutputBuffer )     /* CCS version. */\r
160 #endif\r
161 char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ] = { 0 };\r
162 \r
163 /* Used for maintaining a 32-bit run time stats counter from a 16-bit timer. */\r
164 volatile uint32_t ulRunTimeCounterOverflows = 0;\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 void main_full( void )\r
169 {\r
170         /* Start all the standard demo/test tasks.  They have no particular\r
171         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
172         kernel port. */\r
173         vStartDynamicPriorityTasks();\r
174         vCreateBlockTimeTasks();\r
175         vStartCountingSemaphoreTasks();\r
176         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
177         vStartRecursiveMutexTasks();\r
178         vStartEventGroupTasks();\r
179         vStartTaskNotifyTask();\r
180 \r
181         /* Create the register check tasks, as described at the top of this     file */\r
182         xTaskCreate( prvRegTestTaskEntry1,                      /* Task entry point. */\r
183                                  "Reg1",                                                /* Text name for the task - not used by the kernel. */\r
184                                  configMINIMAL_STACK_SIZE,              /* Stack to allocate to the task - in words not bytes! */\r
185                                  mainREG_TEST_TASK_1_PARAMETER, /* The parameter passed into the task. */\r
186                                  tskIDLE_PRIORITY,                              /* The task's priority. */\r
187                                  NULL );                                                /* Task handle is not needed, so NULL is passed. */\r
188 \r
189         xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
190 \r
191         /* Create the task that performs the 'check' functionality, as described at\r
192         the top of this file. */\r
193         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
194 \r
195         /* Register an example set of CLI commands, then start the task that manages\r
196         the CLI using a UART for input and output. */\r
197         vRegisterSampleCLICommands();\r
198         vUARTCommandConsoleStart( mainCOMMAND_CONSOLE_STACK_SIZE, mainCOMMAND_CONSOLE_TASK_PRIORITY );\r
199 \r
200         /* Start the scheduler. */\r
201         vTaskStartScheduler();\r
202 \r
203         /* If all is well, the scheduler will now be running, and the following\r
204         line will never be reached.  If the following line does execute, then\r
205         there was either insufficient FreeRTOS heap memory available for the idle\r
206         and/or timer tasks to be created.  See the memory management section on the\r
207         FreeRTOS web site for more details on the FreeRTOS heap\r
208         http://www.freertos.org/a00111.html. */\r
209         for( ;; );\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 static void prvCheckTask( void *pvParameters )\r
214 {\r
215 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
216 TickType_t xLastExecutionTime;\r
217 static uint16_t usLastRegTest1Value = 0, usLastRegTest2Value = 0;\r
218 uint16_t usErrorFound = pdFALSE;\r
219 \r
220         /* Just to stop compiler warnings. */\r
221         ( void ) pvParameters;\r
222 \r
223         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
224         works correctly. */\r
225         xLastExecutionTime = xTaskGetTickCount();\r
226 \r
227         /* Cycle for ever, delaying then checking all the other tasks are still\r
228         operating without error.  An on-board LED is toggled on each iteration.\r
229         If an error is detected then the delay period is decreased from\r
230         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
231         effect of increasing the rate at which the on-board LED toggles, and in so\r
232         doing gives visual feedback of the system status. */\r
233         for( ;; )\r
234         {\r
235                 /* Delay until it is time to execute again. */\r
236                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
237 \r
238                 /* Check all the demo tasks to ensure they are all still running, and\r
239                 that none have detected an error. */\r
240                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
241                 {\r
242                         usErrorFound = 1UL << 0UL;\r
243                 }\r
244 \r
245                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
246                 {\r
247                         usErrorFound = 1UL << 1UL;\r
248                 }\r
249 \r
250                 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
251                 {\r
252                         usErrorFound = 1UL << 2UL;\r
253                 }\r
254 \r
255                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
256                 {\r
257                         usErrorFound = 1UL << 3UL;\r
258                 }\r
259 \r
260                 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
261                 {\r
262                         usErrorFound = 1UL << 4UL;\r
263                 }\r
264 \r
265                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
266                 {\r
267                         usErrorFound = 1UL << 5UL;\r
268                 }\r
269 \r
270                 if( xAreTaskNotificationTasksStillRunning() != pdPASS )\r
271                 {\r
272                         usErrorFound = 1UL << 6UL;\r
273                 }\r
274 \r
275                 /* Check that the register test 1 task is still running. */\r
276                 if( usLastRegTest1Value == usRegTest1LoopCounter )\r
277                 {\r
278                         usErrorFound = 1UL << 7UL;\r
279                 }\r
280                 usLastRegTest1Value = usRegTest1LoopCounter;\r
281 \r
282                 /* Check that the register test 2 task is still running. */\r
283                 if( usLastRegTest2Value == usRegTest2LoopCounter )\r
284                 {\r
285                         usErrorFound = 1UL << 8UL;\r
286                 }\r
287                 usLastRegTest2Value = usRegTest2LoopCounter;\r
288 \r
289                 /* Toggle the check LED to give an indication of the system status.  If\r
290                 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then\r
291                 everything is ok.  A faster toggle indicates an error. */\r
292                 vParTestToggleLED( mainCHECK_LED );\r
293 \r
294                 if( usErrorFound != pdFALSE )\r
295                 {\r
296                         /* An error has been detected in one of the tasks - flash the LED\r
297                         at a higher frequency to give visible feedback that something has\r
298                         gone wrong (it might just be that the loop back connector required\r
299                         by the comtest tasks has not been fitted). */\r
300                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
301                 }\r
302         }\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 static void prvRegTestTaskEntry1( void *pvParameters )\r
307 {\r
308         /* Although the regtest task is written in assembler, its entry point is\r
309         written in C for convenience of checking the task parameter is being passed\r
310         in correctly. */\r
311         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
312         {\r
313                 /* Start the part of the test that is written in assembler. */\r
314                 vRegTest1Implementation();\r
315         }\r
316 \r
317         /* The following line will only execute if the task parameter is found to\r
318         be incorrect.  The check task will detect that the regtest loop counter is\r
319         not being incremented and flag an error. */\r
320         vTaskDelete( NULL );\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 static void prvRegTestTaskEntry2( void *pvParameters )\r
325 {\r
326         /* Although the regtest task is written in assembler, its entry point is\r
327         written in C for convenience of checking the task parameter is being passed\r
328         in correctly. */\r
329         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
330         {\r
331                 /* Start the part of the test that is written in assembler. */\r
332                 vRegTest2Implementation();\r
333         }\r
334 \r
335         /* The following line will only execute if the task parameter is found to\r
336         be incorrect.  The check task will detect that the regtest loop counter is\r
337         not being incremented and flag an error. */\r
338         vTaskDelete( NULL );\r
339 }\r
340 /*-----------------------------------------------------------*/\r
341 \r
342 void vConfigureTimerForRunTimeStats( void )\r
343 {\r
344         /* Configure a timer that is used as the time base for run time stats.  See\r
345         http://www.freertos.org/rtos-run-time-stats.html */\r
346 \r
347         /* Ensure the timer is stopped. */\r
348         TA1CTL = 0;\r
349 \r
350         /* Start up clean. */\r
351         TA1CTL |= TACLR;\r
352 \r
353         /* Run the timer from the ACLK/8, continuous mode, interrupt enable. */\r
354         TA1CTL = TASSEL_1 | ID__8 | MC__CONTINUOUS | TAIE;\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 #pragma vector=TIMER1_A1_VECTOR\r
359 __interrupt void v4RunTimeStatsTimerOverflow( void )\r
360 {\r
361         TA1CTL &= ~TAIFG;\r
362         \r
363         /* 16-bit overflow, so add 17th bit. */\r
364         ulRunTimeCounterOverflows += 0x10000;\r
365         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
366 }\r
367 \r
368 \r
369 \r
370 \r