]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC24_MPLAB/main.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / PIC24_MPLAB / main.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
68  * documentation provides more details of the standard demo application tasks.\r
69  * In addition to the standard demo tasks, the following tasks and tests are\r
70  * defined and/or created within this file:\r
71  *\r
72  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
73  * using a free running timer to demonstrate the use of the \r
74  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt \r
75  * service routine measures the number of processor clocks that occur between\r
76  * each interrupt - and in so doing measures the jitter in the interrupt \r
77  * timing.  The maximum measured jitter time is latched in the usMaxJitter \r
78  * variable, and displayed on the LCD by the 'Check' as described below.  \r
79  * The fast interrupt is configured and handled in the timer_test.c source \r
80  * file.\r
81  *\r
82  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
83  * is permitted to access the LCD directly.  Other tasks wishing to write a\r
84  * message to the LCD send the message on a queue to the LCD task instead of \r
85  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting \r
86  * for messages - waking and displaying the messages as they arrive.  The LCD\r
87  * task is defined in lcd.c.  \r
88  * \r
89  * "Check" task -  This only executes every three seconds but has the highest \r
90  * priority so is guaranteed to get processor time.  Its main function is to \r
91  * check that all the standard demo tasks are still operational.  Should any\r
92  * unexpected behaviour within a demo task be discovered the 'check' task will\r
93  * write "FAIL #n" to the LCD (via the LCD task).  If all the demo tasks are \r
94  * executing with their expected behaviour then the check task writes the max\r
95  * jitter time to the LCD (again via the LCD task), as described above.\r
96  */\r
97 \r
98 /* Standard includes. */\r
99 #include <stdio.h>\r
100 \r
101 /* Scheduler includes. */\r
102 #include "FreeRTOS.h"\r
103 #include "task.h"\r
104 #include "queue.h"\r
105 #include "croutine.h"\r
106 \r
107 /* Demo application includes. */\r
108 #include "BlockQ.h"\r
109 #include "crflash.h"\r
110 #include "blocktim.h"\r
111 #include "integer.h"\r
112 #include "comtest2.h"\r
113 #include "partest.h"\r
114 #include "lcd.h"\r
115 #include "timertest.h"\r
116 \r
117 /* Demo task priorities. */\r
118 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
119 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
120 #define mainCOM_TEST_PRIORITY                           ( 2 )\r
121 \r
122 /* The check task may require a bit more stack as it calls sprintf(). */\r
123 #define mainCHECK_TAKS_STACK_SIZE                       ( configMINIMAL_STACK_SIZE * 2 )\r
124 \r
125 /* The execution period of the check task. */\r
126 #define mainCHECK_TASK_PERIOD                           ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )\r
127 \r
128 /* The number of flash co-routines to create. */\r
129 #define mainNUM_FLASH_COROUTINES                        ( 5 )\r
130 \r
131 /* Baud rate used by the comtest tasks. */\r
132 #define mainCOM_TEST_BAUD_RATE                          ( 19200 )\r
133 \r
134 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.\r
135 See the comtest.c file for more information. */\r
136 #define mainCOM_TEST_LED                                        ( 6 )\r
137 \r
138 /* The frequency at which the "fast interrupt test" interrupt will occur. */\r
139 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
140 \r
141 /* The number of processor clocks we expect to occur between each "fast\r
142 interrupt test" interrupt. */\r
143 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( configCPU_CLOCK_HZ / mainTEST_INTERRUPT_FREQUENCY )\r
144 \r
145 /* The number of nano seconds between each processor clock. */\r
146 #define mainNS_PER_CLOCK ( ( unsigned short ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
147 \r
148 /* Dimension the buffer used to hold the value of the maximum jitter time when\r
149 it is converted to a string. */\r
150 #define mainMAX_STRING_LENGTH                           ( 20 )\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /*\r
155  * The check task as described at the top of this file.\r
156  */\r
157 static void vCheckTask( void *pvParameters );\r
158 \r
159 /*\r
160  * Setup the processor ready for the demo.\r
161  */\r
162 static void prvSetupHardware( void );\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /* The queue used to send messages to the LCD task. */\r
167 static QueueHandle_t xLCDQueue;\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /*\r
172  * Create the demo tasks then start the scheduler.\r
173  */\r
174 int main( void )\r
175 {\r
176         /* Configure any hardware required for this demo. */\r
177         prvSetupHardware();\r
178 \r
179         /* Create the standard demo tasks. */\r
180         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );       \r
181         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
182         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );\r
183         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
184         vCreateBlockTimeTasks();\r
185 \r
186         /* Create the test tasks defined within this file. */\r
187         xTaskCreate( vCheckTask, "Check", mainCHECK_TAKS_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
188 \r
189         /* Start the task that will control the LCD.  This returns the handle\r
190         to the queue used to write text out to the task. */\r
191         xLCDQueue = xStartLCDTask();\r
192 \r
193         /* Start the high frequency interrupt test. */\r
194         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
195 \r
196         /* Finally start the scheduler. */\r
197         vTaskStartScheduler();\r
198 \r
199         /* Will only reach here if there is insufficient heap available to start\r
200         the scheduler. */\r
201         return 0;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 static void prvSetupHardware( void )\r
206 {\r
207         vParTestInitialise();\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void vCheckTask( void *pvParameters )\r
212 {\r
213 /* Used to wake the task at the correct frequency. */\r
214 TickType_t xLastExecutionTime; \r
215 \r
216 /* The maximum jitter time measured by the fast interrupt test. */\r
217 extern unsigned short usMaxJitter ;\r
218 \r
219 /* Buffer into which the maximum jitter time is written as a string. */\r
220 static char cStringBuffer[ mainMAX_STRING_LENGTH ];\r
221 \r
222 /* The message that is sent on the queue to the LCD task.  The first\r
223 parameter is the minimum time (in ticks) that the message should be\r
224 left on the LCD without being overwritten.  The second parameter is a pointer\r
225 to the message to display itself. */\r
226 xLCDMessage xMessage = { 0, cStringBuffer };\r
227 \r
228 /* Set to pdTRUE should an error be detected in any of the standard demo tasks. */\r
229 unsigned short usErrorDetected = pdFALSE;\r
230 \r
231         /* Remove compiler warnings. */\r
232         ( void ) pvParameters;\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