]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_WizC/Demo6/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PIC18_WizC / Demo6 / 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 sixth.  This enables the\r
38  * demo's to be executed on the RAM limited PIC-devices.\r
39  *\r
40  * The Demo6 project is configured for a PIC18F4620 device.  Main.c starts 4\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/comtest.c:       Creates 2 tasks\r
45  * ATTENTION: Comtest needs a loopback-connector on the serial port.\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  * http://www.FreeRTOS.org contains important information on the use of the\r
55  * wizC PIC18F port.\r
56  */\r
57 \r
58 /* Scheduler include files. */\r
59 #include <FreeRTOS.h>\r
60 #include <task.h>\r
61 \r
62 /* Demo app include files. */\r
63 #include "partest.h"\r
64 #include "serial.h"\r
65 #include "comtest.h"\r
66 \r
67 /* The period between executions of the check task before and after an error\r
68 has been discovered.  If an error has been discovered the check task runs\r
69 more frequently - increasing the LED flash rate. */\r
70 #define mainNO_ERROR_CHECK_PERIOD       ( ( TickType_t ) 10000 / portTICK_PERIOD_MS )\r
71 #define mainERROR_CHECK_PERIOD          ( ( TickType_t )  1000 / portTICK_PERIOD_MS )\r
72 #define mainCHECK_TASK_LED                      ( ( unsigned char ) 3 )\r
73 \r
74 /* Priority definitions for some of the tasks.  Other tasks just use the idle\r
75 priority. */\r
76 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + ( unsigned char ) 2 )\r
77 #define mainCOMM_TEST_PRIORITY  ( tskIDLE_PRIORITY + ( unsigned char ) 1 )\r
78 \r
79 /* The LED that is toggled whenever a character is transmitted.\r
80 mainCOMM_TX_RX_LED + 1 will be toggled every time a character is received. */\r
81 #define mainCOMM_TX_RX_LED              ( ( unsigned char ) 0 )\r
82 \r
83 /* Constants required for the communications. */\r
84 #define mainBAUD_RATE                   ( ( unsigned long ) 57600 )\r
85 \r
86 /*\r
87  * The task function for the "Check" task.\r
88  */\r
89 static portTASK_FUNCTION_PROTO( vErrorChecks, pvParameters );\r
90 \r
91 /*\r
92  * Checks the unique counts of other tasks to ensure they are still operational.\r
93  * Returns pdTRUE if an error is detected, otherwise pdFALSE.\r
94  */\r
95 static char prvCheckOtherTasksAreStillRunning( void );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* Creates the tasks, then starts the scheduler. */\r
100 void main( void )\r
101 {\r
102         /* Initialise the required hardware. */\r
103         vParTestInitialise();\r
104 \r
105         /* Start a few of the standard demo tasks found in the demo\common directory. */\r
106         vAltStartComTestTasks( mainCOMM_TEST_PRIORITY, mainBAUD_RATE, mainCOMM_TX_RX_LED );\r
107 \r
108         /* Start the check task defined in this file. */\r
109         xTaskCreate( vErrorChecks, "Check", portMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
110 \r
111         /* Start the scheduler.  Will never return here. */\r
112         vTaskStartScheduler();\r
113 \r
114         while(1)        /* This point should never be reached. */\r
115         {\r
116         }\r
117 }\r
118 /*-----------------------------------------------------------*/\r
119 \r
120 static portTASK_FUNCTION( vErrorChecks, pvParameters )\r
121 {\r
122 TickType_t xLastCheckTime;\r
123 TickType_t xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
124 char cErrorOccurred;\r
125 \r
126         /* We need to initialise xLastCheckTime prior to the first call to\r
127         vTaskDelayUntil(). */\r
128         xLastCheckTime = xTaskGetTickCount();\r
129 \r
130         /* Cycle for ever, delaying then checking all the other tasks are still\r
131         operating without error. */\r
132         for( ;; )\r
133         {\r
134                 /* Wait until it is time to check the other tasks again. */\r
135                 vTaskDelayUntil( &xLastCheckTime, xDelayTime );\r
136 \r
137                 /* Check all the other tasks are running, and running without ever\r
138                 having an error. */\r
139                 cErrorOccurred = prvCheckOtherTasksAreStillRunning();\r
140 \r
141                 /* If an error was detected increase the frequency of the LED flash. */\r
142                 if( cErrorOccurred == pdTRUE )\r
143                 {\r
144                         xDelayTime = mainERROR_CHECK_PERIOD;\r
145                 }\r
146 \r
147                 /* Flash the LED for visual feedback. */\r
148                 vParTestToggleLED( mainCHECK_TASK_LED );\r
149         }\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 static char prvCheckOtherTasksAreStillRunning( void )\r
154 {\r
155         char cErrorHasOccurred = ( char ) pdFALSE;\r
156 \r
157         if( xAreComTestTasksStillRunning() != pdTRUE )\r
158         {\r
159                 cErrorHasOccurred = ( char ) pdTRUE;\r
160         }\r
161 \r
162         return cErrorHasOccurred;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 \r