]> begriffs open source - freertos/blob - FreeRTOS/Demo/RL78_E2Studio_GCC/src/main_full.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / RL78_E2Studio_GCC / src / 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, along with an example of how to write an\r
39  * interrupt service routine, are defined in main.c.\r
40  ******************************************************************************\r
41  *\r
42  * main_full() creates all the demo application tasks and two software timers,\r
43  * then starts the scheduler.  The web documentation provides more details of\r
44  * the standard demo application tasks, which provide no particular\r
45  * functionality, 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, tests and\r
48  * timers are created within this file:\r
49  *\r
50  * "Reg test" tasks - These fill the registers with known values, then check\r
51  * that each register still contains its expected value.  Each task uses a\r
52  * different set of values.  The reg test tasks execute with a very low priority,\r
53  * so get preempted very frequently.  A register containing an unexpected value\r
54  * is indicative of an error in the context switching mechanism.\r
55  *\r
56  * The "Demo" Timer and Callback Function:\r
57  * The demo timer callback function does nothing more than increment a variable.\r
58  * The period of the demo timer is set relative to the period of the check timer\r
59  * (described below).  This allows the check timer to know how many times the\r
60  * demo timer callback function should execute between each execution of the\r
61  * check timer callback function.  The variable incremented in the demo timer\r
62  * callback function is used to determine how many times the callback function\r
63  * has executed.\r
64  *\r
65  * The "Check" Timer and Callback Function:\r
66  * The check timer period is initially set to three seconds.  The check timer\r
67  * callback function checks that all the standard demo tasks, the reg test\r
68  * tasks, and the demo timer are not only still executing, but are executing\r
69  * without reporting any errors.  If the check timer discovers that a task or\r
70  * timer has stalled, or reported an error, then it changes its own period from\r
71  * the initial three seconds, to just 200ms.  The check timer callback function\r
72  * also toggles an LED each time it is called.  This provides a visual\r
73  * indication of the system status:  If the LED toggles every three seconds,\r
74  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
75  * an issue has been discovered with at least one task.\r
76  *\r
77  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
78  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
79  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
80  *\r
81  */\r
82 \r
83 /* Scheduler include files. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 #include "timers.h"\r
87 \r
88 /* Standard demo includes. */\r
89 #include "dynamic.h"\r
90 #include "PollQ.h"\r
91 #include "blocktim.h"\r
92 \r
93 /* Hardware includes. */\r
94 #include "demo_specific_io.h"\r
95 \r
96 /* The period at which the check timer will expire, in ms, provided no errors\r
97 have been reported by any of the standard demo tasks.  ms are converted to the\r
98 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
99 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
100 \r
101 /* The period at which the check timer will expire, in ms, if an error has been\r
102 reported in one of the standard demo tasks, the check tasks, or the demo timer.\r
103 ms are converted to the equivalent in ticks using the portTICK_PERIOD_MS\r
104 constant. */\r
105 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
106 \r
107 /* These two definitions are used to set the period of the demo timer.  The demo\r
108 timer period is always relative to the check timer period, so the check timer\r
109 can determine if the demo timer has expired the expected number of times between\r
110 its own executions. */\r
111 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT       ( 100UL )\r
112 #define mainDEMO_TIMER_PERIOD_MS                        ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )\r
113 \r
114 /* A block time of zero simply means "don't block". */\r
115 #define mainDONT_BLOCK                                          ( 0U )\r
116 \r
117 /* Values that are passed as parameters into the reg test tasks (purely to\r
118 ensure task parameters are passed correctly). */\r
119 #define mainREG_TEST_1_PARAMETER                        ( ( void * ) 0x1234 )\r
120 #define mainREG_TEST_2_PARAMETER                        ( ( void * ) 0x5678 )\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * The 'check' timer callback function, as described at the top of this file.\r
126  */\r
127 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
128 \r
129 /*\r
130  * The 'demo' timer callback function, as described at the top of this file.\r
131  */\r
132 static void prvDemoTimerCallback( TimerHandle_t xTimer );\r
133 \r
134 /*\r
135  * Functions that define the RegTest tasks, as described at the top of this\r
136  * file.  The RegTest tasks are written (necessarily) in assembler.  Their\r
137  * entry points are written in C to allow for easy checking of the task\r
138  * parameter values.\r
139  */\r
140 extern void vRegTest1Task( void );\r
141 extern void vRegTest2Task( void );\r
142 static void prvRegTest1Entry( void *pvParameters );\r
143 static void prvRegTest2Entry( void *pvParameters );\r
144 \r
145 /*\r
146  * Called if a RegTest task discovers an error as a mechanism to stop the\r
147  * tasks loop counter incrementing (so the check task can detect that an\r
148  * error exists).\r
149  */\r
150 void vRegTestError( void );\r
151 \r
152 /*\r
153  * Called by main() to create the more comprehensive application if\r
154  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
155  */\r
156 void main_full( void );\r
157 \r
158 /*-----------------------------------------------------------*/\r
159 \r
160 /* Variables that are incremented on each cycle of the two reg tests to allow\r
161 the check timer to know that they are still executing. */\r
162 unsigned short usRegTest1LoopCounter = 0, usRegTest2LoopCounter;\r
163 \r
164 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
165 function. */\r
166 static TimerHandle_t xCheckTimer = NULL;\r
167 \r
168 /* The demo timer.  This uses prvDemoTimerCallback() as its callback function. */\r
169 static TimerHandle_t xDemoTimer = NULL;\r
170 \r
171 /* This variable is incremented each time the demo timer expires. */\r
172 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;\r
173 \r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void main_full( void )\r
177 {\r
178         /* Creates all the tasks and timers, then starts the scheduler. */\r
179 \r
180         /* First create the 'standard demo' tasks.  These are used to demonstrate\r
181         API functions being used and also to test the kernel port.  More information\r
182         is provided on the FreeRTOS.org WEB site. */\r
183 //      vStartDynamicPriorityTasks();\r
184 //      vStartPolledQueueTasks( tskIDLE_PRIORITY );\r
185 \r
186         /* Additional tasks can be added by un-commenting the line below on devices\r
187         with sufficient RAM for a larger heap (see configTOTAL_HEAP_SIZE in\r
188         FreeRTOSConfig.h). */\r
189         /*vCreateBlockTimeTasks();*/\r
190 \r
191         /* Create the RegTest tasks as described at the top of this file. */\r
192         xTaskCreate( prvRegTest1Entry,                  /* The function that implements the task. */\r
193                                  "Reg1",                                        /* Text name for the task - to assist debugging only, not used by the kernel. */\r
194                                  configMINIMAL_STACK_SIZE,      /* The size of the stack allocated to the task (in words, not bytes). */\r
195                                  mainREG_TEST_1_PARAMETER,  /* The parameter passed into the task. */\r
196                                  configMAX_PRIORITIES-1, /*tskIDLE_PRIORITY,*/                  /* The priority at which the task will execute. */\r
197                                  NULL );                                        /* Used to pass the handle of the created task out to the function caller - not used in this case. */\r
198 \r
199 //      xTaskCreate( prvRegTest2Entry, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
200 \r
201         /* Create the software timer that performs the 'check' functionality,\r
202         as described at the top of this file. */\r
203 //      xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
204 //                                                              ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
205 //                                                              pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
206 //                                                              ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
207 //                                                              prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
208 //                                                        );\r
209 \r
210         /* Create the software timer that just increments a variable for demo\r
211         purposes. */\r
212 //      xDemoTimer = xTimerCreate( "DemoTimer",                                         /* A text name, purely to help debugging. */\r
213 //                                                              ( mainDEMO_TIMER_PERIOD_MS ),   /* The timer period, in this case it is always calculated relative to the check timer period (see the definition of mainDEMO_TIMER_PERIOD_MS). */\r
214 //                                                              pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
215 //                                                              ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
216 //                                                              prvDemoTimerCallback                    /* The callback function that inspects the status of all the other tasks. */\r
217 //                                                        );\r
218 \r
219         /* Start both the check timer and the demo timer.  The timers won't actually\r
220         start until the scheduler is started. */\r
221 //      xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
222 //      xTimerStart( xDemoTimer, mainDONT_BLOCK );\r
223 \r
224         /* Finally start the scheduler running. */\r
225         vTaskStartScheduler();\r
226 \r
227 extern void my_function( void );\r
228         my_function();\r
229 \r
230         /* If all is well execution will never reach here as the scheduler will be\r
231         running.  If this null loop is reached then it is likely there was\r
232         insufficient FreeRTOS heap available for the idle task and/or timer task to\r
233         be created.  See http://www.freertos.org/a00111.html. */\r
234         for( ;; );\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void prvDemoTimerCallback( TimerHandle_t xTimer )\r
239 {\r
240         /* Remove compiler warning about unused parameter. */\r
241         ( void ) xTimer;\r
242 \r
243         /* The demo timer has expired.  All it does is increment a variable.  The\r
244         period of the demo timer is relative to that of the check timer, so the\r
245         check timer knows how many times this variable should have been incremented\r
246         between each execution of the check timer's own callback. */\r
247         ulDemoSoftwareTimerCounter++;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
252 {\r
253 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;\r
254 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
255 \r
256         /* Remove compiler warning about unused parameter. */\r
257         ( void ) xTimer;\r
258 \r
259         /* Inspect the status of the standard demo tasks. */\r
260         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
261         {\r
262                 xErrorStatus = pdFAIL;\r
263         }\r
264 \r
265         if( xArePollingQueuesStillRunning() != pdTRUE )\r
266         {\r
267                 xErrorStatus = pdFAIL;\r
268         }\r
269 \r
270         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
271         {\r
272                 /* Un-comment this line if the block time tasks are included in the\r
273                 demo. */\r
274                 /* xErrorStatus = pdFAIL; */\r
275         }\r
276 \r
277         /* Indicate an error if either of the reg test loop counters have not\r
278         incremented since the last time this function was called. */\r
279         if( usLastRegTest1Counter == usRegTest1LoopCounter )\r
280         {\r
281                 xErrorStatus = pdFAIL;\r
282         }\r
283         else\r
284         {\r
285                 usLastRegTest1Counter = usRegTest1LoopCounter;\r
286         }\r
287 \r
288         if( usLastRegTest2Counter == usRegTest2LoopCounter )\r
289         {\r
290                 xErrorStatus = pdFAIL;\r
291         }\r
292         else\r
293         {\r
294                 usLastRegTest2Counter = usRegTest2LoopCounter;\r
295         }\r
296 \r
297         /* Ensure that the demo software timer has expired\r
298         mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between\r
299         each call of this function.  A critical section is not required to access\r
300         ulDemoSoftwareTimerCounter as the variable is only accessed from another\r
301         software timer callback, and only one software timer callback can be\r
302         executing at any time. */\r
303         if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||\r
304             ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )\r
305           )\r
306         {\r
307                 xErrorStatus = pdFAIL;\r
308         }\r
309         else\r
310         {\r
311                 ulDemoSoftwareTimerCounter = 0UL;\r
312         }\r
313 \r
314         if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )\r
315         {\r
316                 /* An error has occurred, but the timer's period has not yet been changed,\r
317                 change it now, and remember that it has been changed.  Shortening the\r
318                 timer's period means the LED will toggle at a faster rate, giving a\r
319                 visible indication that something has gone wrong. */\r
320                 xChangedTimerPeriodAlready = pdTRUE;\r
321 \r
322                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions\r
323                 called from inside of a timer callback function must *never* attempt to\r
324                 block. */\r
325                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
326         }\r
327 \r
328         /* Toggle the LED.  The toggle rate will depend on whether or not an error\r
329         has been found in any tasks. */\r
330         LED_BIT = !LED_BIT;\r
331 }\r
332 /*-----------------------------------------------------------*/\r
333 \r
334 void vRegTestError( void )\r
335 {\r
336         /* Called by both reg test tasks if an error is found.  There is no way out\r
337         of this function so the loop counter of the calling task will stop\r
338         incrementing, which will result in the check timer signaling an error. */\r
339         for( ;; );\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 static void prvRegTest1Entry( void *pvParameters )\r
344 {\r
345         /* If the parameter has its expected value then start the first reg test\r
346         task (this is only done to test that the RTOS port is correctly handling\r
347         task parameters. */\r
348         if( pvParameters == mainREG_TEST_1_PARAMETER )\r
349         {\r
350                 vRegTest1Task();\r
351         }\r
352         else\r
353         {\r
354                 vRegTestError();\r
355         }\r
356 \r
357         /* It is not possible to get here as neither of the two functions called\r
358         above will ever return. */\r
359 }\r
360 /*-----------------------------------------------------------*/\r
361 \r
362 static void prvRegTest2Entry( void *pvParameters )\r
363 {\r
364         /* If the parameter has its expected value then start the first reg test\r
365         task (this is only done to test that the RTOS port is correctly handling\r
366         task parameters. */\r
367         if( pvParameters == mainREG_TEST_2_PARAMETER )\r
368         {\r
369                 vRegTest2Task();\r
370         }\r
371         else\r
372         {\r
373                 vRegTestError();\r
374         }\r
375 \r
376         /* It is not possible to get here as neither of the two functions called\r
377         above will ever return. */\r
378 }\r
379 /*-----------------------------------------------------------*/\r
380 \r