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