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