]> begriffs open source - cmsis-freertos/blob - Demo/PIC18_MPLAB/main1.c
osEventFlagsWait: Fix flag comparison
[cmsis-freertos] / Demo / PIC18_MPLAB / main1.c
1 /*
2  * FreeRTOS Kernel V10.2.1
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29  * Instead of the normal single demo application, the PIC18F demo is split
30  * into several smaller programs of which this is the first.  This enables the
31  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin
32  * devices require a more costly development platform and are not so readily
33  * available.
34  *
35  * The RTOSDemo1 project is configured for a PIC18F452 device.  Main1.c starts 5
36  * tasks (including the idle task).
37  *
38  * The first task runs at the idle priority.  It repeatedly performs a 32bit
39  * calculation and checks it's result against the expected value.  This checks
40  * that the temporary storage utilised by the compiler to hold intermediate
41  * results does not get corrupted when the task gets switched in and out.  See
42  * demo/common/minimal/integer.c for more information.
43  *
44  * The second and third tasks pass an incrementing value between each other on
45  * a message queue.  See demo/common/minimal/PollQ.c for more information.
46  *
47  * Main1.c also creates a check task.  This periodically checks that all the
48  * other tasks are still running and have not experienced any unexpected
49  * results.  If all the other tasks are executing correctly an LED is flashed
50  * once every mainCHECK_PERIOD milliseconds.  If any of the tasks have not
51  * executed, or report and error, the frequency of the LED flash will increase
52  * to mainERROR_FLASH_RATE.
53  *
54  * On entry to main an 'X' is transmitted.  Monitoring the serial port using a
55  * dumb terminal allows for verification that the device is not continuously
56  * being reset (no more than one 'X' should be transmitted).
57  *
58  * http://www.FreeRTOS.org contains important information on the use of the
59  * PIC18F port.
60  */
61
62 /*
63 Changes from V2.0.0
64
65         + Delay periods are now specified using variables and constants of
66           TickType_t rather than unsigned long.
67 */
68
69 /* Scheduler include files. */
70 #include "FreeRTOS.h"
71 #include "task.h"
72
73 /* Demo app include files. */
74 #include "PollQ.h"
75 #include "integer.h"
76 #include "partest.h"
77 #include "serial.h"
78
79 /* The period between executions of the check task before and after an error
80 has been discovered.  If an error has been discovered the check task runs
81 more frequently - increasing the LED flash rate. */
82 #define mainNO_ERROR_CHECK_PERIOD               ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )
83 #define mainERROR_CHECK_PERIOD                  ( ( TickType_t ) 100 / portTICK_PERIOD_MS )
84
85 /* Priority definitions for some of the tasks.  Other tasks just use the idle
86 priority. */
87 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )
88 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )
89
90 /* The LED that is flashed by the check task. */
91 #define mainCHECK_TASK_LED                              ( 0 )
92
93 /* Constants required for the communications.  Only one character is ever
94 transmitted. */
95 #define mainCOMMS_QUEUE_LENGTH                  ( 5 )
96 #define mainNO_BLOCK                                    ( ( TickType_t ) 0 )
97 #define mainBAUD_RATE                                   ( ( unsigned long ) 9600 )
98
99 /*
100  * The task function for the "Check" task.
101  */
102 static void vErrorChecks( void *pvParameters );
103
104 /*
105  * Checks the unique counts of other tasks to ensure they are still operational.
106  * Returns pdTRUE if an error is detected, otherwise pdFALSE.
107  */
108 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );
109
110 /*-----------------------------------------------------------*/
111
112 /* Creates the tasks, then starts the scheduler. */
113 void main( void )
114 {
115         /* Initialise the required hardware. */
116         vParTestInitialise();
117         vPortInitialiseBlocks();
118
119         /* Send a character so we have some visible feedback of a reset. */
120         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );
121         xSerialPutChar( NULL, 'X', mainNO_BLOCK );
122
123         /* Start the standard demo tasks found in the demo\common directory. */
124         vStartIntegerMathTasks( tskIDLE_PRIORITY );
125         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
126
127         /* Start the check task defined in this file. */
128         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
129
130         /* Start the scheduler.  Will never return here. */
131         vTaskStartScheduler();
132 }
133 /*-----------------------------------------------------------*/
134
135 static void vErrorChecks( void *pvParameters )
136 {
137 TickType_t xDelayTime = mainNO_ERROR_CHECK_PERIOD;
138 portBASE_TYPE xErrorOccurred;
139
140         /* Cycle for ever, delaying then checking all the other tasks are still
141         operating without error. */
142         for( ;; )
143         {
144                 /* Wait until it is time to check the other tasks. */
145                 vTaskDelay( xDelayTime );
146
147                 /* Check all the other tasks are running, and running without ever
148                 having an error. */
149                 xErrorOccurred = prvCheckOtherTasksAreStillRunning();
150
151                 /* If an error was detected increase the frequency of the LED flash. */
152                 if( xErrorOccurred == pdTRUE )
153                 {
154                         xDelayTime = mainERROR_CHECK_PERIOD;
155                 }
156
157                 /* Flash the LED for visual feedback. */
158                 vParTestToggleLED( mainCHECK_TASK_LED );
159         }
160 }
161 /*-----------------------------------------------------------*/
162
163 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )
164 {
165 portBASE_TYPE xErrorHasOccurred = pdFALSE;
166
167         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
168         {
169                 xErrorHasOccurred = pdTRUE;
170         }
171
172         if( xArePollingQueuesStillRunning() != pdTRUE )
173         {
174                 xErrorHasOccurred = pdTRUE;
175         }
176
177         return xErrorHasOccurred;
178 }
179 /*-----------------------------------------------------------*/
180
181