]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_WizC/Demo4/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PIC18_WizC / Demo4 / main.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 Changes from V3.0.0\r
31 \r
32 Changes from V3.0.1\r
33 */\r
34 \r
35 /*\r
36  * Instead of the normal single demo application, the PIC18F demo is split\r
37  * into several smaller programs of which this is the fourth.  This enables the\r
38  * demo's to be executed on the RAM limited PIC-devices.\r
39  *\r
40  * The Demo4 project is configured for a PIC18F4620 device.  Main.c starts 11\r
41  * tasks (including the idle task). See the indicated files in the demo/common\r
42  * directory for more information.\r
43  *\r
44  * demo/common/minimal/integer.c:       Creates 1 task\r
45  * demo/common/minimal/dynamic.c:       Creates 5 tasks\r
46  * demo/common/minimal/flash.c:         Creates 3 tasks\r
47  *\r
48  * Main.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 an 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  * wizC PIC18F port.\r
61  */\r
62 \r
63 /* Scheduler include files. */\r
64 #include <FreeRTOS.h>\r
65 #include <task.h>\r
66 \r
67 /* Demo app include files. */\r
68 #include "integer.h"\r
69 #include "dynamic.h"\r
70 #include "flash.h"\r
71 #include "partest.h"\r
72 #include "serial.h"\r
73 \r
74 /* The period between executions of the check task before and after an error\r
75 has been discovered.  If an error has been discovered the check task runs\r
76 more frequently - increasing the LED flash rate. */\r
77 #define mainNO_ERROR_CHECK_PERIOD       ( ( TickType_t ) 10000 / portTICK_PERIOD_MS )\r
78 #define mainERROR_CHECK_PERIOD          ( ( TickType_t )  1000 / portTICK_PERIOD_MS )\r
79 #define mainCHECK_TASK_LED                      ( ( unsigned char ) 3 )\r
80 \r
81 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
82 priority. */\r
83 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + ( unsigned char ) 3 )\r
84 #define mainLED_FLASH_PRIORITY  ( tskIDLE_PRIORITY + ( unsigned char ) 2 )\r
85 #define mainINTEGER_PRIORITY    ( tskIDLE_PRIORITY + ( unsigned char ) 0 )\r
86 \r
87 /* Constants required for the communications.  Only one character is ever\r
88 transmitted. */\r
89 #define mainCOMMS_QUEUE_LENGTH          ( ( unsigned char ) 5 )\r
90 #define mainNO_BLOCK                            ( ( TickType_t ) 0 )\r
91 #define mainBAUD_RATE                           ( ( unsigned long ) 57600 )\r
92 \r
93 /*\r
94  * The task function for the "Check" task.\r
95  */\r
96 static portTASK_FUNCTION_PROTO( vErrorChecks, pvParameters );\r
97 \r
98 /*\r
99  * Checks the unique counts of other tasks to ensure they are still operational.\r
100  * Returns pdTRUE if an error is detected, otherwise pdFALSE.\r
101  */\r
102 static char prvCheckOtherTasksAreStillRunning( void );\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /* Creates the tasks, then starts the scheduler. */\r
107 void main( void )\r
108 {\r
109         /* Initialise the required hardware. */\r
110         vParTestInitialise();\r
111 \r
112         /* Send a character so we have some visible feedback of a reset. */\r
113         xSerialPortInitMinimal( mainBAUD_RATE, mainCOMMS_QUEUE_LENGTH );\r
114         xSerialPutChar( NULL, 'X', mainNO_BLOCK );\r
115 \r
116         /* Start the standard demo tasks found in the demo\common directory. */\r
117         vStartIntegerMathTasks( mainINTEGER_PRIORITY);\r
118         vStartDynamicPriorityTasks();\r
119         vStartLEDFlashTasks( mainLED_FLASH_PRIORITY );\r
120 \r
121         /* Start the check task defined in this file. */\r
122         xTaskCreate( vErrorChecks, "Check", portMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\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 \r
167 static char prvCheckOtherTasksAreStillRunning( void )\r
168 {\r
169         char cErrorHasOccurred = ( char ) pdFALSE;\r
170 \r
171         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
172         {\r
173                 cErrorHasOccurred = ( char ) pdTRUE;\r
174         }\r
175 \r
176         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
177         {\r
178                 cErrorHasOccurred = ( char ) pdTRUE;\r
179         }\r
180 \r
181         return cErrorHasOccurred;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 \r