]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_WizC/Demo7/main.c
f81c25748900e9eb0f70fa41f30bd586bf9e8cc8
[freertos] / FreeRTOS / Demo / PIC18_WizC / Demo7 / main.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 Changes from V3.0.0\r
30 \r
31 Changes from V3.0.1\r
32 */\r
33 \r
34 /*\r
35  * Instead of the normal single demo application, the PIC18F demo is split\r
36  * into several smaller programs of which this is the seventh.  This enables the\r
37  * demo's to be executed on the RAM limited PIC-devices.\r
38  *\r
39  * The Demo7 project is configured for a PIC18F4620 device.  Main.c starts 10\r
40  * tasks (including the idle task). See the indicated files in the demo/common\r
41  * directory for more information.\r
42  *\r
43  * demo/common/minimal/flash.c:         Creates 3 tasks\r
44  * demo/common/minimal/death.c:         Creates 1 controltask and\r
45  *                                                                      creates/deletes 4 suicidaltasks\r
46  *\r
47  * Main.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 an 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  * wizC PIC18F port.\r
60  */\r
61 \r
62 /* Scheduler include files. */\r
63 #include <FreeRTOS.h>\r
64 #include <task.h>\r
65 \r
66 /* Demo app include files. */\r
67 #include "death.h"\r
68 #include "flash.h"\r
69 #include "partest.h"\r
70 #include "serial.h"\r
71 \r
72 /* The period between executions of the check task before and after an error\r
73 has been discovered.  If an error has been discovered the check task runs\r
74 more frequently - increasing the LED flash rate. */\r
75 #define mainNO_ERROR_CHECK_PERIOD       ( ( TickType_t ) 10000 / portTICK_PERIOD_MS )\r
76 #define mainERROR_CHECK_PERIOD          ( ( TickType_t )  1000 / portTICK_PERIOD_MS )\r
77 #define mainCHECK_TASK_LED                      ( ( unsigned char ) 3 )\r
78 \r
79 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
80 priority. */\r
81 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + ( unsigned char ) 2 )\r
82 #define mainLED_FLASH_PRIORITY          ( tskIDLE_PRIORITY + ( unsigned char ) 2 )\r
83 #define mainCREATOR_TASK_PRIORITY       ( tskIDLE_PRIORITY + ( unsigned char ) 1 )\r
84 \r
85 /* Constants required for the communications.  Only one character is ever\r
86 transmitted. */\r
87 #define mainCOMMS_QUEUE_LENGTH          ( ( unsigned char ) 5 )\r
88 #define mainNO_BLOCK                            ( ( TickType_t ) 0 )\r
89 #define mainBAUD_RATE                           ( ( unsigned long ) 57600 )\r
90 \r
91 /*\r
92  * The task function for the "Check" task.\r
93  */\r
94 static portTASK_FUNCTION_PROTO( vErrorChecks, pvParameters );\r
95 \r
96 /*\r
97  * Checks the unique counts of other tasks to ensure they are still operational.\r
98  * Returns pdTRUE if an error is detected, otherwise pdFALSE.\r
99  */\r
100 static char prvCheckOtherTasksAreStillRunning( void );\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /* Creates the tasks, then starts the scheduler. */\r
105 void main( void )\r
106 {\r
107         /* Initialise the required hardware. */\r
108         vParTestInitialise();\r
109 \r
110         /* Send a character so we have some visible feedback of a reset. */\r
111         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
112         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
113 \r
114         /* Start a few of the standard demo tasks found in the demo\common directory. */\r
115         vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );\r
116 \r
117         /* Start the check task defined in this file. */\r
118         xTaskCreate( vErrorChecks, "Check", portMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
119 \r
120         /* This task has to be created last as it keeps account of the number of tasks\r
121         it expects to see running. */\r
122         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
123 \r
124         /* Start the scheduler.  Will never return here. */\r
125         vTaskStartScheduler();\r
126 \r
127         while(1)        /* This point should never be reached. */\r
128         {\r
129         }\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 static portTASK_FUNCTION( vErrorChecks, pvParameters )\r
134 {\r
135 TickType_t xLastCheckTime;\r
136 TickType_t xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
137 char cErrorOccurred;\r
138 \r
139         /* We need to initialise xLastCheckTime prior to the first call to\r
140         vTaskDelayUntil(). */\r
141         xLastCheckTime = xTaskGetTickCount();\r
142 \r
143         /* Cycle for ever, delaying then checking all the other tasks are still\r
144         operating without error. */\r
145         for( ;; )\r
146         {\r
147                 /* Wait until it is time to check the other tasks again. */\r
148                 vTaskDelayUntil( &xLastCheckTime, xDelayTime );\r
149 \r
150                 /* Check all the other tasks are running, and running without ever\r
151                 having an error. */\r
152                 cErrorOccurred = prvCheckOtherTasksAreStillRunning();\r
153 \r
154                 /* If an error was detected increase the frequency of the LED flash. */\r
155                 if( cErrorOccurred == pdTRUE )\r
156                 {\r
157                         xDelayTime = mainERROR_CHECK_PERIOD;\r
158                 }\r
159 \r
160                 /* Flash the LED for visual feedback. */\r
161                 vParTestToggleLED( mainCHECK_TASK_LED );\r
162         }\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static char prvCheckOtherTasksAreStillRunning( void )\r
167 {\r
168         char cErrorHasOccurred = ( char ) pdFALSE;\r
169 \r
170         if( xIsCreateTaskStillRunning() != pdTRUE )\r
171         {\r
172                 cErrorHasOccurred = ( char ) pdTRUE;\r
173         }\r
174 \r
175         return cErrorHasOccurred;\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 \r