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