]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_MPLAB/main1.c
4dc1989f83051a6ea73e668cbc0baa40fd383c4a
[freertos] / FreeRTOS / Demo / PIC18_MPLAB / main1.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  * Instead of the normal single demo application, the PIC18F demo is split\r
30  * into several smaller programs of which this is the first.  This enables the\r
31  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin\r
32  * devices require a more costly development platform and are not so readily\r
33  * available.\r
34  *\r
35  * The RTOSDemo1 project is configured for a PIC18F452 device.  Main1.c starts 5\r
36  * tasks (including the idle task).\r
37  *\r
38  * The first task runs at the idle priority.  It repeatedly performs a 32bit\r
39  * calculation and checks it's result against the expected value.  This checks\r
40  * that the temporary storage utilised by the compiler to hold intermediate\r
41  * results does not get corrupted when the task gets switched in and out.  See\r
42  * demo/common/minimal/integer.c for more information.\r
43  *\r
44  * The second and third tasks pass an incrementing value between each other on\r
45  * a message queue.  See demo/common/minimal/PollQ.c for more information.\r
46  *\r
47  * Main1.c also creates a check task.  This periodically checks that all the\r
48  * other tasks are still running and have not experienced any unexpected\r
49  * results.  If all the other tasks are executing correctly an LED is flashed\r
50  * once every mainCHECK_PERIOD milliseconds.  If any of the tasks have not\r
51  * executed, or report and error, the frequency of the LED flash will increase\r
52  * to mainERROR_FLASH_RATE.\r
53  *\r
54  * On entry to main an 'X' is transmitted.  Monitoring the serial port using a\r
55  * dumb terminal allows for verification that the device is not continuously\r
56  * being reset (no more than one 'X' should be transmitted).\r
57  *\r
58  * http://www.FreeRTOS.org contains important information on the use of the\r
59  * PIC18F port.\r
60  */\r
61 \r
62 /*\r
63 Changes from V2.0.0\r
64 \r
65         + Delay periods are now specified using variables and constants of\r
66           TickType_t rather than unsigned long.\r
67 */\r
68 \r
69 /* Scheduler include files. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 \r
73 /* Demo app include files. */\r
74 #include "PollQ.h"\r
75 #include "integer.h"\r
76 #include "partest.h"\r
77 #include "serial.h"\r
78 \r
79 /* The period between executions of the check task before and after an error\r
80 has been discovered.  If an error has been discovered the check task runs\r
81 more frequently - increasing the LED flash rate. */\r
82 #define mainNO_ERROR_CHECK_PERIOD               ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )\r
83 #define mainERROR_CHECK_PERIOD                  ( ( TickType_t ) 100 / portTICK_PERIOD_MS )\r
84 \r
85 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
86 priority. */\r
87 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
88 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
89 \r
90 /* The LED that is flashed by the check task. */\r
91 #define mainCHECK_TASK_LED                              ( 0 )\r
92 \r
93 /* Constants required for the communications.  Only one character is ever\r
94 transmitted. */\r
95 #define mainCOMMS_QUEUE_LENGTH                  ( 5 )\r
96 #define mainNO_BLOCK                                    ( ( TickType_t ) 0 )\r
97 #define mainBAUD_RATE                                   ( ( unsigned long ) 9600 )\r
98 \r
99 /*\r
100  * The task function for the "Check" task.\r
101  */\r
102 static void vErrorChecks( void *pvParameters );\r
103 \r
104 /*\r
105  * Checks the unique counts of other tasks to ensure they are still operational.\r
106  * Returns pdTRUE if an error is detected, otherwise pdFALSE.\r
107  */\r
108 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* Creates the tasks, then starts the scheduler. */\r
113 void main( void )\r
114 {\r
115         /* Initialise the required hardware. */\r
116         vParTestInitialise();\r
117         vPortInitialiseBlocks();\r
118 \r
119         /* Send a character so we have some visible feedback of a reset. */\r
120         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
121         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
122 \r
123         /* Start the standard demo tasks found in the demo\common directory. */\r
124         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
125         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
126 \r
127         /* Start the check task defined in this file. */\r
128         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
129 \r
130         /* Start the scheduler.  Will never return here. */\r
131         vTaskStartScheduler();\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 static void vErrorChecks( void *pvParameters )\r
136 {\r
137 TickType_t xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
138 portBASE_TYPE xErrorOccurred;\r
139 \r
140         /* Cycle for ever, delaying then checking all the other tasks are still\r
141         operating without error. */\r
142         for( ;; )\r
143         {\r
144                 /* Wait until it is time to check the other tasks. */\r
145                 vTaskDelay( xDelayTime );\r
146 \r
147                 /* Check all the other tasks are running, and running without ever\r
148                 having an error. */\r
149                 xErrorOccurred = prvCheckOtherTasksAreStillRunning();\r
150 \r
151                 /* If an error was detected increase the frequency of the LED flash. */\r
152                 if( xErrorOccurred == pdTRUE )\r
153                 {\r
154                         xDelayTime = mainERROR_CHECK_PERIOD;\r
155                 }\r
156 \r
157                 /* Flash the LED for visual feedback. */\r
158                 vParTestToggleLED( mainCHECK_TASK_LED );\r
159         }\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )\r
164 {\r
165 portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
166 \r
167         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
168         {\r
169                 xErrorHasOccurred = pdTRUE;\r
170         }\r
171 \r
172         if( xArePollingQueuesStillRunning() != pdTRUE )\r
173         {\r
174                 xErrorHasOccurred = pdTRUE;\r
175         }\r
176 \r
177         return xErrorHasOccurred;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 \r