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