]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RL78_multiple_IAR/main_full.c
23faee7b9219cba42be9a8ad37dcb88d79654577
[freertos] / FreeRTOS / Demo / RL78_multiple_IAR / 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         vCreateBlockTimeTasks();\r
186 \r
187         /* Create the RegTest tasks as described at the top of this file. */\r
188         xTaskCreate( prvRegTest1Entry,                          /* The function that implements the task. */\r
189                                  "Reg1",                                                /* Text name for the task - to assist debugging only, not used by the kernel. */\r
190                                  configMINIMAL_STACK_SIZE,              /* The size of the stack allocated to the task (in words, not bytes). */\r
191                                  mainREG_TEST_1_PARAMETER,      /* The parameter passed into the task. */\r
192                                  tskIDLE_PRIORITY,                              /* The priority at which the task will execute. */\r
193                                  NULL );                                                /* Used to pass the handle of the created task out to the function caller - not used in this case. */\r
194 \r
195         xTaskCreate( prvRegTest2Entry, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
196 \r
197         /* Create the software timer that performs the 'check' functionality,\r
198         as described at the top of this file. */\r
199         xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
200                                                                 ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
201                                                                 pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
202                                                                 ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
203                                                                 prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
204                                                           );\r
205 \r
206         /* Create the software timer that just increments a variable for demo\r
207         purposes. */\r
208         xDemoTimer = xTimerCreate(  "DemoTimer",/* A text name, purely to help debugging. */\r
209                                                                 ( 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
210                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
211                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
212                                                                 prvDemoTimerCallback                            /* The callback function that inspects the status of all the other tasks. */\r
213                                                           );\r
214 \r
215         /* Start both the check timer and the demo timer.  The timers won't actually\r
216         start until the scheduler is started. */\r
217         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
218         xTimerStart( xDemoTimer, mainDONT_BLOCK );\r
219 \r
220         /* Finally start the scheduler running. */\r
221         vTaskStartScheduler();\r
222 \r
223         /* If all is well execution will never reach here as the scheduler will be\r
224         running.  If this null loop is reached then it is likely there was\r
225         insufficient FreeRTOS heap available for the idle task and/or timer task to\r
226         be created.  See http://www.freertos.org/a00111.html. */\r
227         for( ;; );\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 static void prvDemoTimerCallback( TimerHandle_t xTimer )\r
232 {\r
233         /* Remove compiler warning about unused parameter. */\r
234         ( void ) xTimer;\r
235 \r
236         /* The demo timer has expired.  All it does is increment a variable.  The\r
237         period of the demo timer is relative to that of the check timer, so the\r
238         check timer knows how many times this variable should have been incremented\r
239         between each execution of the check timer's own callback. */\r
240         ulDemoSoftwareTimerCounter++;\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
245 {\r
246 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;\r
247 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
248 \r
249         /* Remove compiler warning about unused parameter. */\r
250         ( void ) xTimer;\r
251 \r
252         /* Inspect the status of the standard demo tasks. */\r
253         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
254         {\r
255                 xErrorStatus = pdFAIL;\r
256         }\r
257 \r
258         if( xArePollingQueuesStillRunning() != pdTRUE )\r
259         {\r
260                 xErrorStatus = pdFAIL;\r
261         }\r
262 \r
263         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
264         {\r
265                 xErrorStatus = pdFAIL;\r
266         }\r
267 \r
268         /* Indicate an error if either of the reg test loop counters have not\r
269         incremented since the last time this function was called. */\r
270         if( usLastRegTest1Counter == usRegTest1LoopCounter )\r
271         {\r
272                 xErrorStatus = pdFAIL;\r
273         }\r
274         else\r
275         {\r
276                 usLastRegTest1Counter = usRegTest1LoopCounter;\r
277         }\r
278 \r
279         if( usLastRegTest2Counter == usRegTest2LoopCounter )\r
280         {\r
281                 xErrorStatus = pdFAIL;\r
282         }\r
283         else\r
284         {\r
285                 usLastRegTest2Counter = usRegTest2LoopCounter;\r
286         }\r
287 \r
288         /* Ensure that the demo software timer has expired\r
289         mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between\r
290         each call of this function.  A critical section is not required to access\r
291         ulDemoSoftwareTimerCounter as the variable is only accessed from another\r
292         software timer callback, and only one software timer callback can be\r
293         executing at any time. */\r
294         if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||\r
295             ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )\r
296           )\r
297         {\r
298                 xErrorStatus = pdFAIL;\r
299         }\r
300         else\r
301         {\r
302                 ulDemoSoftwareTimerCounter = 0UL;\r
303         }\r
304 \r
305         if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )\r
306         {\r
307                 /* An error has occurred, but the timer's period has not yet been changed,\r
308                 change it now, and remember that it has been changed.  Shortening the\r
309                 timer's period means the LED will toggle at a faster rate, giving a\r
310                 visible indication that something has gone wrong. */\r
311                 xChangedTimerPeriodAlready = pdTRUE;\r
312 \r
313                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions\r
314                 called from inside of a timer callback function must *never* attempt to\r
315                 block. */\r
316                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
317         }\r
318 \r
319         /* Toggle the LED.  The toggle rate will depend on whether or not an error\r
320         has been found in any tasks. */\r
321         LED_BIT = !LED_BIT;\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 void vRegTestError( void )\r
326 {\r
327         /* Called by both reg test tasks if an error is found.  There is no way out\r
328         of this function so the loop counter of the calling task will stop\r
329         incrementing, which will result in the check timer signaling an error. */\r
330         for( ;; );\r
331 }\r
332 /*-----------------------------------------------------------*/\r
333 \r
334 static void prvRegTest1Entry( void *pvParameters )\r
335 {\r
336         /* If the parameter has its expected value then start the first reg test\r
337         task (this is only done to test that the RTOS port is correctly handling\r
338         task parameters. */\r
339         if( pvParameters == mainREG_TEST_1_PARAMETER )\r
340         {\r
341                 vRegTest1Task();\r
342         }\r
343         else\r
344         {\r
345                 vRegTestError();\r
346         }\r
347 \r
348         /* It is not possible to get here as neither of the two functions called\r
349         above will ever return. */\r
350 }\r
351 /*-----------------------------------------------------------*/\r
352 \r
353 static void prvRegTest2Entry( void *pvParameters )\r
354 {\r
355         /* If the parameter has its expected value then start the first reg test\r
356         task (this is only done to test that the RTOS port is correctly handling\r
357         task parameters. */\r
358         if( pvParameters == mainREG_TEST_2_PARAMETER )\r
359         {\r
360                 vRegTest2Task();\r
361         }\r
362         else\r
363         {\r
364                 vRegTestError();\r
365         }\r
366 \r
367         /* It is not possible to get here as neither of the two functions called\r
368         above will ever return. */\r
369 }\r
370 /*-----------------------------------------------------------*/\r
371 \r