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