]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC24_MPLAB/main.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Demo / PIC24_MPLAB / main.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*\r
68  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
69  * documentation provides more details of the standard demo application tasks.\r
70  * In addition to the standard demo tasks, the following tasks and tests are\r
71  * defined and/or created within this file:\r
72  *\r
73  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
74  * using a free running timer to demonstrate the use of the \r
75  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt \r
76  * service routine measures the number of processor clocks that occur between\r
77  * each interrupt - and in so doing measures the jitter in the interrupt \r
78  * timing.  The maximum measured jitter time is latched in the usMaxJitter \r
79  * variable, and displayed on the LCD by the 'Check' as described below.  \r
80  * The fast interrupt is configured and handled in the timer_test.c source \r
81  * file.\r
82  *\r
83  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
84  * is permitted to access the LCD directly.  Other tasks wishing to write a\r
85  * message to the LCD send the message on a queue to the LCD task instead of \r
86  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting \r
87  * for messages - waking and displaying the messages as they arrive.  The LCD\r
88  * task is defined in lcd.c.  \r
89  * \r
90  * "Check" task -  This only executes every three seconds but has the highest \r
91  * priority so is guaranteed to get processor time.  Its main function is to \r
92  * check that all the standard demo tasks are still operational.  Should any\r
93  * unexpected behaviour within a demo task be discovered the 'check' task will\r
94  * write "FAIL #n" to the LCD (via the LCD task).  If all the demo tasks are \r
95  * executing with their expected behaviour then the check task writes the max\r
96  * jitter time to the LCD (again via the LCD task), as described above.\r
97  */\r
98 \r
99 /* Standard includes. */\r
100 #include <stdio.h>\r
101 \r
102 /* Scheduler includes. */\r
103 #include "FreeRTOS.h"\r
104 #include "task.h"\r
105 #include "queue.h"\r
106 #include "croutine.h"\r
107 \r
108 /* Demo application includes. */\r
109 #include "BlockQ.h"\r
110 #include "crflash.h"\r
111 #include "blocktim.h"\r
112 #include "integer.h"\r
113 #include "comtest2.h"\r
114 #include "partest.h"\r
115 #include "lcd.h"\r
116 #include "timertest.h"\r
117 \r
118 /* Demo task priorities. */\r
119 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
120 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
121 #define mainCOM_TEST_PRIORITY                           ( 2 )\r
122 \r
123 /* The check task may require a bit more stack as it calls sprintf(). */\r
124 #define mainCHECK_TAKS_STACK_SIZE                       ( configMINIMAL_STACK_SIZE * 2 )\r
125 \r
126 /* The execution period of the check task. */\r
127 #define mainCHECK_TASK_PERIOD                           ( ( portTickType ) 3000 / portTICK_RATE_MS )\r
128 \r
129 /* The number of flash co-routines to create. */\r
130 #define mainNUM_FLASH_COROUTINES                        ( 5 )\r
131 \r
132 /* Baud rate used by the comtest tasks. */\r
133 #define mainCOM_TEST_BAUD_RATE                          ( 19200 )\r
134 \r
135 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.\r
136 See the comtest.c file for more information. */\r
137 #define mainCOM_TEST_LED                                        ( 6 )\r
138 \r
139 /* The frequency at which the "fast interrupt test" interrupt will occur. */\r
140 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
141 \r
142 /* The number of processor clocks we expect to occur between each "fast\r
143 interrupt test" interrupt. */\r
144 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( configCPU_CLOCK_HZ / mainTEST_INTERRUPT_FREQUENCY )\r
145 \r
146 /* The number of nano seconds between each processor clock. */\r
147 #define mainNS_PER_CLOCK ( ( unsigned short ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
148 \r
149 /* Dimension the buffer used to hold the value of the maximum jitter time when\r
150 it is converted to a string. */\r
151 #define mainMAX_STRING_LENGTH                           ( 20 )\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 /*\r
156  * The check task as described at the top of this file.\r
157  */\r
158 static void vCheckTask( void *pvParameters );\r
159 \r
160 /*\r
161  * Setup the processor ready for the demo.\r
162  */\r
163 static void prvSetupHardware( void );\r
164 \r
165 /*-----------------------------------------------------------*/\r
166 \r
167 /* The queue used to send messages to the LCD task. */\r
168 static xQueueHandle xLCDQueue;\r
169 \r
170 /*-----------------------------------------------------------*/\r
171 \r
172 /*\r
173  * Create the demo tasks then start the scheduler.\r
174  */\r
175 int main( void )\r
176 {\r
177         /* Configure any hardware required for this demo. */\r
178         prvSetupHardware();\r
179 \r
180         /* Create the standard demo tasks. */\r
181         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );       \r
182         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
183         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );\r
184         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
185         vCreateBlockTimeTasks();\r
186 \r
187         /* Create the test tasks defined within this file. */\r
188         xTaskCreate( vCheckTask, ( signed char * ) "Check", mainCHECK_TAKS_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
189 \r
190         /* Start the task that will control the LCD.  This returns the handle\r
191         to the queue used to write text out to the task. */\r
192         xLCDQueue = xStartLCDTask();\r
193 \r
194         /* Start the high frequency interrupt test. */\r
195         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
196 \r
197         /* Finally start the scheduler. */\r
198         vTaskStartScheduler();\r
199 \r
200         /* Will only reach here if there is insufficient heap available to start\r
201         the scheduler. */\r
202         return 0;\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void prvSetupHardware( void )\r
207 {\r
208         vParTestInitialise();\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static void vCheckTask( void *pvParameters )\r
213 {\r
214 /* Used to wake the task at the correct frequency. */\r
215 portTickType xLastExecutionTime; \r
216 \r
217 /* The maximum jitter time measured by the fast interrupt test. */\r
218 extern unsigned short usMaxJitter ;\r
219 \r
220 /* Buffer into which the maximum jitter time is written as a string. */\r
221 static char cStringBuffer[ mainMAX_STRING_LENGTH ];\r
222 \r
223 /* The message that is sent on the queue to the LCD task.  The first\r
224 parameter is the minimum time (in ticks) that the message should be\r
225 left on the LCD without being overwritten.  The second parameter is a pointer\r
226 to the message to display itself. */\r
227 xLCDMessage xMessage = { 0, cStringBuffer };\r
228 \r
229 /* Set to pdTRUE should an error be detected in any of the standard demo tasks. */\r
230 unsigned short usErrorDetected = pdFALSE;\r
231 \r
232         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
233         works correctly. */\r
234         xLastExecutionTime = xTaskGetTickCount();\r
235 \r
236         for( ;; )\r
237         {\r
238                 /* Wait until it is time for the next cycle. */\r
239                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_TASK_PERIOD );\r
240 \r
241                 /* Has an error been found in any of the standard demo tasks? */\r
242 \r
243                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
244                 {\r
245                         usErrorDetected = pdTRUE;\r
246                         sprintf( cStringBuffer, "FAIL #1" );\r
247                 }\r
248         \r
249                 if( xAreComTestTasksStillRunning() != pdTRUE )\r
250                 {\r
251                         usErrorDetected = pdTRUE;\r
252                         sprintf( cStringBuffer, "FAIL #2" );\r
253                 }\r
254 \r
255                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
256                 {\r
257                         usErrorDetected = pdTRUE;\r
258                         sprintf( cStringBuffer, "FAIL #3" );\r
259                 }\r
260 \r
261                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
262                 {\r
263                         usErrorDetected = pdTRUE;\r
264                         sprintf( cStringBuffer, "FAIL #4" );\r
265                 }\r
266 \r
267                 if( usErrorDetected == pdFALSE )\r
268                 {\r
269                         /* No errors have been discovered, so display the maximum jitter\r
270                         timer discovered by the "fast interrupt test". */\r
271                         sprintf( cStringBuffer, "%dns max jitter", ( short ) ( usMaxJitter - mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ) * mainNS_PER_CLOCK );\r
272                 }\r
273 \r
274                 /* Send the message to the LCD gatekeeper for display. */\r
275                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
276         }\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 void vApplicationIdleHook( void )\r
281 {\r
282         /* Schedule the co-routines from within the idle task hook. */\r
283         vCoRoutineSchedule();\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r