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