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