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