]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC24_MPLAB/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PIC24_MPLAB / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
31  * documentation provides more details of the standard demo application tasks.\r
32  * In addition to the standard demo tasks, the following tasks and tests are\r
33  * defined and/or created within this file:\r
34  *\r
35  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
36  * using a free running timer to demonstrate the use of the \r
37  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt \r
38  * service routine measures the number of processor clocks that occur between\r
39  * each interrupt - and in so doing measures the jitter in the interrupt \r
40  * timing.  The maximum measured jitter time is latched in the usMaxJitter \r
41  * variable, and displayed on the LCD by the 'Check' as described below.  \r
42  * The fast interrupt is configured and handled in the timer_test.c source \r
43  * file.\r
44  *\r
45  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
46  * is permitted to access the LCD directly.  Other tasks wishing to write a\r
47  * message to the LCD send the message on a queue to the LCD task instead of \r
48  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting \r
49  * for messages - waking and displaying the messages as they arrive.  The LCD\r
50  * task is defined in lcd.c.  \r
51  * \r
52  * "Check" task -  This only executes every three seconds but has the highest \r
53  * priority so is guaranteed to get processor time.  Its main function is to \r
54  * check that all the standard demo tasks are still operational.  Should any\r
55  * unexpected behaviour within a demo task be discovered the 'check' task will\r
56  * write "FAIL #n" to the LCD (via the LCD task).  If all the demo tasks are \r
57  * executing with their expected behaviour then the check task writes the max\r
58  * jitter time to the LCD (again via the LCD task), as described above.\r
59  */\r
60 \r
61 /* Standard includes. */\r
62 #include <stdio.h>\r
63 \r
64 /* Scheduler includes. */\r
65 #include "FreeRTOS.h"\r
66 #include "task.h"\r
67 #include "queue.h"\r
68 #include "croutine.h"\r
69 \r
70 /* Demo application includes. */\r
71 #include "BlockQ.h"\r
72 #include "crflash.h"\r
73 #include "blocktim.h"\r
74 #include "integer.h"\r
75 #include "comtest2.h"\r
76 #include "partest.h"\r
77 #include "lcd.h"\r
78 #include "timertest.h"\r
79 \r
80 /* Demo task priorities. */\r
81 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
82 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
83 #define mainCOM_TEST_PRIORITY                           ( 2 )\r
84 \r
85 /* The check task may require a bit more stack as it calls sprintf(). */\r
86 #define mainCHECK_TAKS_STACK_SIZE                       ( configMINIMAL_STACK_SIZE * 2 )\r
87 \r
88 /* The execution period of the check task. */\r
89 #define mainCHECK_TASK_PERIOD                           ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )\r
90 \r
91 /* The number of flash co-routines to create. */\r
92 #define mainNUM_FLASH_COROUTINES                        ( 5 )\r
93 \r
94 /* Baud rate used by the comtest tasks. */\r
95 #define mainCOM_TEST_BAUD_RATE                          ( 19200 )\r
96 \r
97 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.\r
98 See the comtest.c file for more information. */\r
99 #define mainCOM_TEST_LED                                        ( 6 )\r
100 \r
101 /* The frequency at which the "fast interrupt test" interrupt will occur. */\r
102 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
103 \r
104 /* The number of processor clocks we expect to occur between each "fast\r
105 interrupt test" interrupt. */\r
106 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( configCPU_CLOCK_HZ / mainTEST_INTERRUPT_FREQUENCY )\r
107 \r
108 /* The number of nano seconds between each processor clock. */\r
109 #define mainNS_PER_CLOCK ( ( unsigned short ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
110 \r
111 /* Dimension the buffer used to hold the value of the maximum jitter time when\r
112 it is converted to a string. */\r
113 #define mainMAX_STRING_LENGTH                           ( 20 )\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * The check task as described at the top of this file.\r
119  */\r
120 static void vCheckTask( void *pvParameters );\r
121 \r
122 /*\r
123  * Setup the processor ready for the demo.\r
124  */\r
125 static void prvSetupHardware( void );\r
126 \r
127 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
128 within this file. */\r
129 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* The queue used to send messages to the LCD task. */\r
134 static QueueHandle_t xLCDQueue;\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 /*\r
139  * Create the demo tasks then start the scheduler.\r
140  */\r
141 int main( void )\r
142 {\r
143         /* Configure any hardware required for this demo. */\r
144         prvSetupHardware();\r
145 \r
146         /* Create the standard demo tasks. */\r
147         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );       \r
148         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
149         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );\r
150         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
151         vCreateBlockTimeTasks();\r
152 \r
153         /* Create the test tasks defined within this file. */\r
154         xTaskCreate( vCheckTask, "Check", mainCHECK_TAKS_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
155 \r
156         /* Start the task that will control the LCD.  This returns the handle\r
157         to the queue used to write text out to the task. */\r
158         xLCDQueue = xStartLCDTask();\r
159 \r
160         /* Start the high frequency interrupt test. */\r
161         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
162 \r
163         /* Finally start the scheduler. */\r
164         vTaskStartScheduler();\r
165 \r
166         /* Will only reach here if there is insufficient heap available to start\r
167         the scheduler. */\r
168         return 0;\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 static void prvSetupHardware( void )\r
173 {\r
174         vParTestInitialise();\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static void vCheckTask( void *pvParameters )\r
179 {\r
180 /* Used to wake the task at the correct frequency. */\r
181 TickType_t xLastExecutionTime; \r
182 \r
183 /* The maximum jitter time measured by the fast interrupt test. */\r
184 extern unsigned short usMaxJitter ;\r
185 \r
186 /* Buffer into which the maximum jitter time is written as a string. */\r
187 static char cStringBuffer[ mainMAX_STRING_LENGTH ];\r
188 \r
189 /* The message that is sent on the queue to the LCD task.  The first\r
190 parameter is the minimum time (in ticks) that the message should be\r
191 left on the LCD without being overwritten.  The second parameter is a pointer\r
192 to the message to display itself. */\r
193 xLCDMessage xMessage = { 0, cStringBuffer };\r
194 \r
195 /* Set to pdTRUE should an error be detected in any of the standard demo tasks. */\r
196 unsigned short usErrorDetected = pdFALSE;\r
197 \r
198         /* Remove compiler warnings. */\r
199         ( void ) pvParameters;\r
200 \r
201         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
202         works correctly. */\r
203         xLastExecutionTime = xTaskGetTickCount();\r
204 \r
205         for( ;; )\r
206         {\r
207                 /* Wait until it is time for the next cycle. */\r
208                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_TASK_PERIOD );\r
209 \r
210                 /* Has an error been found in any of the standard demo tasks? */\r
211 \r
212                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
213                 {\r
214                         usErrorDetected = pdTRUE;\r
215                         sprintf( cStringBuffer, "FAIL #1" );\r
216                 }\r
217         \r
218                 if( xAreComTestTasksStillRunning() != pdTRUE )\r
219                 {\r
220                         usErrorDetected = pdTRUE;\r
221                         sprintf( cStringBuffer, "FAIL #2" );\r
222                 }\r
223 \r
224                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
225                 {\r
226                         usErrorDetected = pdTRUE;\r
227                         sprintf( cStringBuffer, "FAIL #3" );\r
228                 }\r
229 \r
230                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
231                 {\r
232                         usErrorDetected = pdTRUE;\r
233                         sprintf( cStringBuffer, "FAIL #4" );\r
234                 }\r
235 \r
236                 if( usErrorDetected == pdFALSE )\r
237                 {\r
238                         /* No errors have been discovered, so display the maximum jitter\r
239                         timer discovered by the "fast interrupt test". */\r
240                         sprintf( cStringBuffer, "%dns max jitter", ( short ) ( usMaxJitter - mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ) * mainNS_PER_CLOCK );\r
241                 }\r
242 \r
243                 /* Send the message to the LCD gatekeeper for display. */\r
244                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
245         }\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 void vApplicationIdleHook( void )\r
250 {\r
251         /* Schedule the co-routines from within the idle task hook. */\r
252         vCoRoutineSchedule();\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
257 {\r
258         ( void ) pcTaskName;\r
259         ( void ) pxTask;\r
260 \r
261         /* Run time stack overflow checking is performed if\r
262         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
263         function is called if a stack overflow is detected. */\r
264         taskDISABLE_INTERRUPTS();\r
265         for( ;; );\r
266 }\r
267 \r