]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_MPLAB/main3.c
c746dbdc702227af146ec3bcbb18fa572f4f773a
[freertos] / FreeRTOS / Demo / PIC18_MPLAB / main3.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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  * THIS DEMO APPLICATION REQUIRES A LOOPBACK CONNECTOR TO BE FITTED TO THE PIC\r
30  * USART PORT - connect pin 2 to pin 3 on J2.\r
31  *\r
32  * Instead of the normal single demo application, the PIC18F demo is split\r
33  * into several smaller programs of which this is the third.  This enables the\r
34  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin\r
35  * devices require a more costly development platform and are not so readily\r
36  * available.\r
37  *\r
38  * The RTOSDemo3 project is configured for a PIC18F452 device.  Main3.c starts\r
39  * 5 tasks (including the idle task).\r
40  *\r
41  * The first task repeatedly transmits a string of characters on the PIC USART\r
42  * port.  The second task receives the characters, checking that the correct\r
43  * sequence is maintained (i.e. what is transmitted is identical to that\r
44  * received).  Each transmitted and each received character causes an LED to\r
45  * flash.  See demo/common/minimal/comtest. c for more information.\r
46  *\r
47  * The third task continuously performs a 32 bit calculation.  This is a good\r
48  * test of the context switch mechanism as the 8 bit architecture requires\r
49  * the use of several file registers to perform the 32 bit operations.  See\r
50  * demo/common/minimal/integer. c for more information.\r
51  *\r
52  * The third task is the check task.  This periodically checks that the other\r
53  * tasks are still running and have not experienced any errors.  If no errors\r
54  * have been reported by either the comms or integer tasks an LED is flashed\r
55  * with a frequency mainNO_ERROR_CHECK_PERIOD.  If an error is discovered the\r
56  * frequency is increased to mainERROR_FLASH_RATE.\r
57  *\r
58  * The check task also provides a visual indication of a system reset by\r
59  * flashing the one remaining LED (mainRESET_LED) when it starts.  After\r
60  * this initial flash mainRESET_LED should remain off.\r
61  *\r
62  * http://www.FreeRTOS.org contains important information on the use of the\r
63  * PIC18F port.\r
64  */\r
65 \r
66 /*\r
67 Changes from V2.0.0\r
68 \r
69         + Delay periods are now specified using variables and constants of\r
70           TickType_t rather than unsigned long.\r
71 */\r
72 \r
73 /* Scheduler include files. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 \r
77 /* Demo app include files. */\r
78 #include "partest.h"\r
79 #include "serial.h"\r
80 #include "comtest.h"\r
81 #include "integer.h"\r
82 \r
83 /* Priority definitions for the LED tasks.  Other tasks just use the idle\r
84 priority. */\r
85 #define mainCOMM_TEST_PRIORITY                  ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 2 )\r
86 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 3 )\r
87 \r
88 /* The period between executions of the check task before and after an error\r
89 has been discovered.  If an error has been discovered the check task runs\r
90 more frequently - increasing the LED flash rate. */\r
91 #define mainNO_ERROR_CHECK_PERIOD               ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )\r
92 #define mainERROR_CHECK_PERIOD                  ( ( TickType_t ) 100 / portTICK_PERIOD_MS )\r
93 \r
94 /* The period for which mainRESET_LED remain on every reset. */\r
95 #define mainRESET_LED_PERIOD                    ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
96 \r
97 /* The LED that is toggled whenever a character is transmitted.\r
98 mainCOMM_TX_RX_LED + 1 will be toggled every time a character is received. */\r
99 #define mainCOMM_TX_RX_LED                              ( ( unsigned portBASE_TYPE ) 2 )\r
100 \r
101 /* The LED that is flashed by the check task at a rate that indicates the\r
102 error status. */\r
103 #define mainCHECK_TASK_LED                              ( ( unsigned portBASE_TYPE ) 1 )\r
104 \r
105 /* The LED that is flashed once upon every reset. */\r
106 #define mainRESET_LED                                   ( ( unsigned portBASE_TYPE ) 0 )\r
107 \r
108 /* Constants required for the communications. */\r
109 #define mainCOMMS_QUEUE_LENGTH                  ( ( unsigned portBASE_TYPE ) 5 )\r
110 #define mainBAUD_RATE                                   ( ( unsigned long ) 57600 )\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /*\r
114  * Task function which periodically checks the other tasks for errors.  Flashes\r
115  * an LED at a rate that indicates whether an error has ever been detected.\r
116  */\r
117 static void vErrorChecks( void *pvParameters );\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /* Creates the tasks, then starts the scheduler. */\r
122 void main( void )\r
123 {\r
124         /* Initialise the required hardware. */\r
125         vParTestInitialise();\r
126 \r
127         /* Initialise the block memory allocator. */\r
128         vPortInitialiseBlocks();\r
129 \r
130         /* Start the standard comtest tasks as defined in demo/common/minimal. */\r
131         vAltStartComTestTasks( mainCOMM_TEST_PRIORITY, mainBAUD_RATE, mainCOMM_TX_RX_LED );\r
132 \r
133         /* Start the standard 32bit calculation task as defined in\r
134         demo/common/minimal. */\r
135         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
136 \r
137         /* Start the check task defined in this file. */\r
138         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
139 \r
140         /* Start the scheduler.  This will never return. */\r
141         vTaskStartScheduler();\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void vErrorChecks( void *pvParameters )\r
146 {\r
147 TickType_t xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
148 volatile unsigned long ulDummy = 3UL;\r
149 \r
150         /* Toggle the LED so we can see when a reset occurs. */\r
151         vParTestSetLED( mainRESET_LED, pdTRUE );\r
152         vTaskDelay( mainRESET_LED_PERIOD );\r
153         vParTestSetLED( mainRESET_LED, pdFALSE );\r
154 \r
155         /* Cycle for ever, delaying then checking all the other tasks are still\r
156         operating without error. */\r
157         for( ;; )\r
158         {\r
159                 /* Wait until it is time to check the other tasks. */\r
160                 vTaskDelay( xDelayTime );\r
161 \r
162                 /* Perform an integer calculation - just to ensure the registers\r
163                 get used.  The result is not important. */\r
164                 ulDummy *= 3UL;\r
165 \r
166                 /* Check all the other tasks are running, and running without ever\r
167                 having an error.  The delay period is lowered if an error is reported,\r
168                 causing the LED to flash at a higher rate. */\r
169                 if( xAreIntegerMathsTaskStillRunning() == pdFALSE )\r
170                 {\r
171                         xDelayTime = mainERROR_CHECK_PERIOD;\r
172                 }\r
173 \r
174                 if( xAreComTestTasksStillRunning() == pdFALSE )\r
175                 {\r
176                         xDelayTime = mainERROR_CHECK_PERIOD;\r
177                 }\r
178 \r
179                 /* Flash the LED for visual feedback.  The rate of the flash will\r
180                 indicate the health of the system. */\r
181                 vParTestToggleLED( mainCHECK_TASK_LED );\r
182         }\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r