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