]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_MPLAB/main3.c
f0c6afb4cbf3629984299bf9aae4fb245ef161f3
[freertos] / FreeRTOS / Demo / PIC18_MPLAB / main3.c
1 /*\r
2     FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * THIS DEMO APPLICATION REQUIRES A LOOPBACK CONNECTOR TO BE FITTED TO THE PIC\r
68  * USART PORT - connect pin 2 to pin 3 on J2.\r
69  *\r
70  * Instead of the normal single demo application, the PIC18F demo is split\r
71  * into several smaller programs of which this is the third.  This enables the\r
72  * demo's to be executed on the RAM limited 40 pin devices.  The 64 and 80 pin\r
73  * devices require a more costly development platform and are not so readily\r
74  * available.\r
75  *\r
76  * The RTOSDemo3 project is configured for a PIC18F452 device.  Main3.c starts\r
77  * 5 tasks (including the idle task).\r
78  *\r
79  * The first task repeatedly transmits a string of characters on the PIC USART\r
80  * port.  The second task receives the characters, checking that the correct\r
81  * sequence is maintained (i.e. what is transmitted is identical to that\r
82  * received).  Each transmitted and each received character causes an LED to\r
83  * flash.  See demo/common/minimal/comtest. c for more information.\r
84  *\r
85  * The third task continuously performs a 32 bit calculation.  This is a good\r
86  * test of the context switch mechanism as the 8 bit architecture requires\r
87  * the use of several file registers to perform the 32 bit operations.  See\r
88  * demo/common/minimal/integer. c for more information.\r
89  *\r
90  * The third task is the check task.  This periodically checks that the other\r
91  * tasks are still running and have not experienced any errors.  If no errors\r
92  * have been reported by either the comms or integer tasks an LED is flashed\r
93  * with a frequency mainNO_ERROR_CHECK_PERIOD.  If an error is discovered the\r
94  * frequency is increased to mainERROR_FLASH_RATE.\r
95  *\r
96  * The check task also provides a visual indication of a system reset by\r
97  * flashing the one remaining LED (mainRESET_LED) when it starts.  After\r
98  * this initial flash mainRESET_LED should remain off.\r
99  *\r
100  * http://www.FreeRTOS.org contains important information on the use of the\r
101  * PIC18F port.\r
102  */\r
103 \r
104 /*\r
105 Changes from V2.0.0\r
106 \r
107         + Delay periods are now specified using variables and constants of\r
108           TickType_t rather than unsigned long.\r
109 */\r
110 \r
111 /* Scheduler include files. */\r
112 #include "FreeRTOS.h"\r
113 #include "task.h"\r
114 \r
115 /* Demo app include files. */\r
116 #include "partest.h"\r
117 #include "serial.h"\r
118 #include "comtest.h"\r
119 #include "integer.h"\r
120 \r
121 /* Priority definitions for the LED tasks.  Other tasks just use the idle\r
122 priority. */\r
123 #define mainCOMM_TEST_PRIORITY                  ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 2 )\r
124 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + ( unsigned portBASE_TYPE ) 3 )\r
125 \r
126 /* The period between executions of the check task before and after an error\r
127 has been discovered.  If an error has been discovered the check task runs\r
128 more frequently - increasing the LED flash rate. */\r
129 #define mainNO_ERROR_CHECK_PERIOD               ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )\r
130 #define mainERROR_CHECK_PERIOD                  ( ( TickType_t ) 100 / portTICK_PERIOD_MS )\r
131 \r
132 /* The period for which mainRESET_LED remain on every reset. */\r
133 #define mainRESET_LED_PERIOD                    ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
134 \r
135 /* The LED that is toggled whenever a character is transmitted.\r
136 mainCOMM_TX_RX_LED + 1 will be toggled every time a character is received. */\r
137 #define mainCOMM_TX_RX_LED                              ( ( unsigned portBASE_TYPE ) 2 )\r
138 \r
139 /* The LED that is flashed by the check task at a rate that indicates the\r
140 error status. */\r
141 #define mainCHECK_TASK_LED                              ( ( unsigned portBASE_TYPE ) 1 )\r
142 \r
143 /* The LED that is flashed once upon every reset. */\r
144 #define mainRESET_LED                                   ( ( unsigned portBASE_TYPE ) 0 )\r
145 \r
146 /* Constants required for the communications. */\r
147 #define mainCOMMS_QUEUE_LENGTH                  ( ( unsigned portBASE_TYPE ) 5 )\r
148 #define mainBAUD_RATE                                   ( ( unsigned long ) 57600 )\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /*\r
152  * Task function which periodically checks the other tasks for errors.  Flashes\r
153  * an LED at a rate that indicates whether an error has ever been detected.\r
154  */\r
155 static void vErrorChecks( void *pvParameters );\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /* Creates the tasks, then starts the scheduler. */\r
160 void main( void )\r
161 {\r
162         /* Initialise the required hardware. */\r
163         vParTestInitialise();\r
164 \r
165         /* Initialise the block memory allocator. */\r
166         vPortInitialiseBlocks();\r
167 \r
168         /* Start the standard comtest tasks as defined in demo/common/minimal. */\r
169         vAltStartComTestTasks( mainCOMM_TEST_PRIORITY, mainBAUD_RATE, mainCOMM_TX_RX_LED );\r
170 \r
171         /* Start the standard 32bit calculation task as defined in\r
172         demo/common/minimal. */\r
173         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
174 \r
175         /* Start the check task defined in this file. */\r
176         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
177 \r
178         /* Start the scheduler.  This will never return. */\r
179         vTaskStartScheduler();\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void vErrorChecks( void *pvParameters )\r
184 {\r
185 TickType_t xDelayTime = mainNO_ERROR_CHECK_PERIOD;\r
186 volatile unsigned long ulDummy = 3UL;\r
187 \r
188         /* Toggle the LED so we can see when a reset occurs. */\r
189         vParTestSetLED( mainRESET_LED, pdTRUE );\r
190         vTaskDelay( mainRESET_LED_PERIOD );\r
191         vParTestSetLED( mainRESET_LED, pdFALSE );\r
192 \r
193         /* Cycle for ever, delaying then checking all the other tasks are still\r
194         operating without error. */\r
195         for( ;; )\r
196         {\r
197                 /* Wait until it is time to check the other tasks. */\r
198                 vTaskDelay( xDelayTime );\r
199 \r
200                 /* Perform an integer calculation - just to ensure the registers\r
201                 get used.  The result is not important. */\r
202                 ulDummy *= 3UL;\r
203 \r
204                 /* Check all the other tasks are running, and running without ever\r
205                 having an error.  The delay period is lowered if an error is reported,\r
206                 causing the LED to flash at a higher rate. */\r
207                 if( xAreIntegerMathsTaskStillRunning() == pdFALSE )\r
208                 {\r
209                         xDelayTime = mainERROR_CHECK_PERIOD;\r
210                 }\r
211 \r
212                 if( xAreComTestTasksStillRunning() == pdFALSE )\r
213                 {\r
214                         xDelayTime = mainERROR_CHECK_PERIOD;\r
215                 }\r
216 \r
217                 /* Flash the LED for visual feedback.  The rate of the flash will\r
218                 indicate the health of the system. */\r
219                 vParTestToggleLED( mainCHECK_TASK_LED );\r
220         }\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r