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