]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_Rowley/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_Rowley / 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 /*\r
31  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
32  * documentation provides more details of the standard demo application tasks.\r
33  * In addition to the standard demo tasks, the following tasks and tests are\r
34  * defined and/or created within this file:\r
35  *\r
36  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
37  * using a free running timer to demonstrate the use of the\r
38  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
39  * service routine measures the number of processor clocks that occur between\r
40  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
41  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
42  * displayed on the OLED display by the 'OLED' task as described below.  The\r
43  * fast interrupt is configured and handled in the timertest.c source file.\r
44  *\r
45  * "OLED" task - the OLED task is a 'gatekeeper' task.  It is the only task that\r
46  * is permitted to access the display directly.  Other tasks wishing to write a\r
47  * message to the OLED send the message on a queue to the OLED task instead of\r
48  * accessing the OLED themselves.  The OLED task just blocks on the queue waiting\r
49  * for messages - waking and displaying the messages as they arrive.\r
50  *\r
51  * "Check" hook -  This only executes every five seconds from the tick hook.\r
52  * Its main function is to check that all the standard demo tasks are still\r
53  * operational.  Should any unexpected behaviour within a demo task be discovered\r
54  * the tick hook will write an error to the OLED (via the OLED task).  If all the\r
55  * demo tasks are executing with their expected behaviour then the check task\r
56  * writes PASS to the OLED (again via the OLED task), as described above.\r
57  *\r
58  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
59  * processing is performed in this task.\r
60  */\r
61 \r
62 \r
63 \r
64 \r
65 /*************************************************************************\r
66  * Please ensure to read http://www.freertos.org/portLM3Sxxxx_Eclipse.html\r
67  * which provides information on configuring and running this demo for the\r
68  * various Luminary Micro EKs.\r
69  *************************************************************************/\r
70 \r
71 \r
72 \r
73 \r
74 /* Standard includes. */\r
75 #include <stdio.h>\r
76 \r
77 /* Scheduler includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "queue.h"\r
81 #include "semphr.h"\r
82 \r
83 /* Hardware library includes. */\r
84 #include "hw_memmap.h"\r
85 #include "hw_types.h"\r
86 #include "hw_sysctl.h"\r
87 #include "sysctl.h"\r
88 #include "gpio.h"\r
89 #include "grlib.h"\r
90 #include "rit128x96x4.h"\r
91 #include "osram128x64x4.h"\r
92 #include "formike128x128x16.h"\r
93 \r
94 /* Demo app includes. */\r
95 #include "BlockQ.h"\r
96 #include "death.h"\r
97 #include "integer.h"\r
98 #include "blocktim.h"\r
99 #include "flash.h"\r
100 #include "partest.h"\r
101 #include "semtest.h"\r
102 #include "PollQ.h"\r
103 #include "lcd_message.h"\r
104 #include "bitmap.h"\r
105 #include "GenQTest.h"\r
106 #include "QPeek.h"\r
107 #include "recmutex.h"\r
108 #include "IntQueue.h"\r
109 #include "QueueSet.h"\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /* The time between cycles of the 'check' functionality (defined within the\r
114 tick hook. */\r
115 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
116 \r
117 /* Size of the stack allocated to the uIP task. */\r
118 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 3 )\r
119 \r
120 /* The OLED task uses the sprintf function so requires a little more stack too. */\r
121 #define mainOLED_TASK_STACK_SIZE                        ( configMINIMAL_STACK_SIZE + 50 )\r
122 \r
123 /* Task priorities. */\r
124 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
125 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
126 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
127 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
128 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
129 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
130 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
131 \r
132 /* The maximum number of message that can be waiting for display at any one\r
133 time. */\r
134 #define mainOLED_QUEUE_SIZE                                     ( 3 )\r
135 \r
136 /* Dimensions the buffer into which the jitter time is written. */\r
137 #define mainMAX_MSG_LEN                                         25\r
138 \r
139 /* The period of the system clock in nano seconds.  This is used to calculate\r
140 the jitter time in nano seconds. */\r
141 #define mainNS_PER_CLOCK                                        ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
142 \r
143 /* Constants used when writing strings to the display. */\r
144 #define mainCHARACTER_HEIGHT                            ( 9 )\r
145 #define mainMAX_ROWS_128                                        ( mainCHARACTER_HEIGHT * 14 )\r
146 #define mainMAX_ROWS_96                                         ( mainCHARACTER_HEIGHT * 10 )\r
147 #define mainMAX_ROWS_64                                         ( mainCHARACTER_HEIGHT * 7 )\r
148 #define mainFULL_SCALE                                          ( 15 )\r
149 #define ulSSI_FREQUENCY                                         ( 3500000UL )\r
150 \r
151 /*-----------------------------------------------------------*/\r
152 \r
153 /*\r
154  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
155  * this task.\r
156  */\r
157 extern void vuIP_Task( void *pvParameters );\r
158 \r
159 /*\r
160  * The display is written two by more than one task so is controlled by a\r
161  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
162  * access the display directly.  Other tasks wanting to display a message send\r
163  * the message to the gatekeeper.\r
164  */\r
165 static void vOLEDTask( void *pvParameters );\r
166 \r
167 /*\r
168  * Configure the hardware for the demo.\r
169  */\r
170 static void prvSetupHardware( void );\r
171 \r
172 /*\r
173  * Configures the high frequency timers - those used to measure the timing\r
174  * jitter while the real time kernel is executing.\r
175  */\r
176 extern void vSetupHighFrequencyTimer( void );\r
177 \r
178 /*\r
179  * The idle hook is used to run a test of the scheduler context switch\r
180  * mechanism.\r
181  */\r
182 void vApplicationIdleHook( void ) __attribute__((naked));\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /* The queue used to send messages to the OLED task. */\r
186 QueueHandle_t xOLEDQueue;\r
187 \r
188 /* The welcome text. */\r
189 const char * const pcWelcomeMessage = "   www.FreeRTOS.org";\r
190 \r
191 /* Variables used to detect the test in the idle hook failing. */\r
192 unsigned long ulIdleError = pdFALSE;\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /*************************************************************************\r
197  * Please ensure to read http://www.freertos.org/portLM3Sxxxx_Eclipse.html\r
198  * which provides information on configuring and running this demo for the\r
199  * various Luminary Micro EKs.\r
200  *************************************************************************/\r
201 int main( void )\r
202 {\r
203         prvSetupHardware();\r
204 \r
205         /* Create the queue used by the OLED task.  Messages for display on the OLED\r
206         are received via this queue. */\r
207         xOLEDQueue = xQueueCreate( mainOLED_QUEUE_SIZE, sizeof( xOLEDMessage ) );\r
208 \r
209         /* Create the uIP task if running on a processor that includes a MAC and\r
210         PHY. */\r
211         if( SysCtlPeripheralPresent( SYSCTL_PERIPH_ETH ) )\r
212         {\r
213                 xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
214         }\r
215 \r
216         /* Start the standard demo tasks. */\r
217         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
218     vCreateBlockTimeTasks();\r
219     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
220     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
221     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
222     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
223     vStartQueuePeekTasks();\r
224     vStartRecursiveMutexTasks();\r
225     vStartInterruptQueueTasks();\r
226     vStartQueueSetTasks();\r
227 \r
228         /* Start the tasks defined within this file/specific to this demo. */\r
229         xTaskCreate( vOLEDTask, "OLED", mainOLED_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
230 \r
231         /* The suicide tasks must be created last as they need to know how many\r
232         tasks were running prior to their creation in order to ascertain whether\r
233         or not the correct/expected number of tasks are running at any given time. */\r
234     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
235 \r
236         /* Configure the high frequency interrupt used to measure the interrupt\r
237         jitter time. */\r
238         vSetupHighFrequencyTimer();\r
239 \r
240         /* Start the scheduler. */\r
241         vTaskStartScheduler();\r
242 \r
243     /* Will only get here if there was insufficient memory to create the idle\r
244     task. */\r
245         for( ;; );\r
246         return 0;\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 void prvSetupHardware( void )\r
251 {\r
252     /* If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is\r
253     a workaround to allow the PLL to operate reliably. */\r
254     if( DEVICE_IS_REVA2 )\r
255     {\r
256         SysCtlLDOSet( SYSCTL_LDO_2_75V );\r
257     }\r
258 \r
259         /* Set the clocking to run from the PLL at 50 MHz */\r
260         SysCtlClockSet( SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ );\r
261 \r
262         /*      Enable Port F for Ethernet LEDs\r
263                 LED0        Bit 3   Output\r
264                 LED1        Bit 2   Output */\r
265         SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF );\r
266         GPIODirModeSet( GPIO_PORTF_BASE, (GPIO_PIN_2 | GPIO_PIN_3), GPIO_DIR_MODE_HW );\r
267         GPIOPadConfigSet( GPIO_PORTF_BASE, (GPIO_PIN_2 | GPIO_PIN_3 ), GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD );\r
268 \r
269         vParTestInitialise();\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 void vApplicationTickHook( void )\r
274 {\r
275 static xOLEDMessage xMessage = { "PASS" };\r
276 static unsigned long ulTicksSinceLastDisplay = 0;\r
277 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
278 \r
279         /* Called from every tick interrupt.  Have enough ticks passed to make it\r
280         time to perform our health status check again? */\r
281         ulTicksSinceLastDisplay++;\r
282         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
283         {\r
284                 ulTicksSinceLastDisplay = 0;\r
285 \r
286                 /* Has an error been found in any task? */\r
287                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
288                 {\r
289                         xMessage.pcMessage = "ERROR IN GEN Q";\r
290                 }\r
291                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
292                 {\r
293                         xMessage.pcMessage = "ERROR IN PEEK Q";\r
294                 }\r
295                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
296                 {\r
297                         xMessage.pcMessage = "ERROR IN BLOCK Q";\r
298                 }\r
299                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
300                 {\r
301                         xMessage.pcMessage = "ERROR IN BLOCK TIME";\r
302                 }\r
303             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
304             {\r
305                 xMessage.pcMessage = "ERROR IN SEMAPHORE";\r
306             }\r
307             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
308             {\r
309                 xMessage.pcMessage = "ERROR IN POLL Q";\r
310             }\r
311             else if( xIsCreateTaskStillRunning() != pdTRUE )\r
312             {\r
313                 xMessage.pcMessage = "ERROR IN CREATE";\r
314             }\r
315             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
316             {\r
317                 xMessage.pcMessage = "ERROR IN MATH";\r
318             }\r
319             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
320             {\r
321                 xMessage.pcMessage = "ERROR IN REC MUTEX";\r
322             }\r
323                 else if( ulIdleError != pdFALSE )\r
324                 {\r
325                         xMessage.pcMessage = "ERROR IN HOOK";\r
326                 }\r
327                 else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
328                 {\r
329                         xMessage.pcMessage = "ERROR IN INT QUEUE";\r
330                 }\r
331                 else if( xAreQueueSetTasksStillRunning() != pdTRUE )\r
332                 {\r
333                 xMessage.pcMessage = "ERROR IN QUEUE SET";\r
334                 }\r
335 \r
336                 /* Send the message to the OLED gatekeeper for display. */\r
337                 xHigherPriorityTaskWoken = pdFALSE;\r
338                 xQueueSendFromISR( xOLEDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
339         }\r
340 \r
341         /* Exercise the queue sets from an ISR. */\r
342     vQueueSetAccessQueueSetFromISR();\r
343 }\r
344 /*-----------------------------------------------------------*/\r
345 \r
346 void vOLEDTask( void *pvParameters )\r
347 {\r
348 xOLEDMessage xMessage;\r
349 unsigned long ulY, ulMaxY;\r
350 static char cMessage[ mainMAX_MSG_LEN ];\r
351 extern volatile unsigned long ulMaxJitter;\r
352 unsigned portBASE_TYPE uxUnusedStackOnEntry;\r
353 const unsigned char *pucImage;\r
354 \r
355 /* Functions to access the OLED.  The one used depends on the dev kit\r
356 being used. */\r
357 void ( *vOLEDInit )( unsigned long ) = NULL;\r
358 void ( *vOLEDStringDraw )( const char *, unsigned long, unsigned long, unsigned char ) = NULL;\r
359 void ( *vOLEDImageDraw )( const unsigned char *, unsigned long, unsigned long, unsigned long, unsigned long ) = NULL;\r
360 void ( *vOLEDClear )( void ) = NULL;\r
361 \r
362         /* Just for demo purposes. */\r
363         uxUnusedStackOnEntry = uxTaskGetStackHighWaterMark( NULL );\r
364 \r
365         /* Map the OLED access functions to the driver functions that are appropriate\r
366         for the evaluation kit being used. */\r
367         switch( HWREG( SYSCTL_DID1 ) & SYSCTL_DID1_PRTNO_MASK )\r
368         {\r
369                 case SYSCTL_DID1_PRTNO_6965     :\r
370                 case SYSCTL_DID1_PRTNO_2965     :       vOLEDInit = OSRAM128x64x4Init;\r
371                                                                                 vOLEDStringDraw = OSRAM128x64x4StringDraw;\r
372                                                                                 vOLEDImageDraw = OSRAM128x64x4ImageDraw;\r
373                                                                                 vOLEDClear = OSRAM128x64x4Clear;\r
374                                                                                 ulMaxY = mainMAX_ROWS_64;\r
375                                                                                 pucImage = pucBasicBitmap;\r
376                                                                                 break;\r
377 \r
378                 case SYSCTL_DID1_PRTNO_1968     :\r
379                 case SYSCTL_DID1_PRTNO_8962 :   vOLEDInit = RIT128x96x4Init;\r
380                                                                                 vOLEDStringDraw = RIT128x96x4StringDraw;\r
381                                                                                 vOLEDImageDraw = RIT128x96x4ImageDraw;\r
382                                                                                 vOLEDClear = RIT128x96x4Clear;\r
383                                                                                 ulMaxY = mainMAX_ROWS_96;\r
384                                                                                 pucImage = pucBasicBitmap;\r
385                                                                                 break;\r
386 \r
387                 default                                         :       vOLEDInit = vFormike128x128x16Init;\r
388                                                                                 vOLEDStringDraw = vFormike128x128x16StringDraw;\r
389                                                                                 vOLEDImageDraw = vFormike128x128x16ImageDraw;\r
390                                                                                 vOLEDClear = vFormike128x128x16Clear;\r
391                                                                                 ulMaxY = mainMAX_ROWS_128;\r
392                                                                                 pucImage = pucGrLibBitmap;\r
393                                                                                 break;\r
394         }\r
395 \r
396         ulY = ulMaxY;\r
397 \r
398         /* Initialise the OLED and display a startup message. */\r
399         vOLEDInit( ulSSI_FREQUENCY );\r
400         vOLEDStringDraw( "POWERED BY FreeRTOS", 0, 0, mainFULL_SCALE );\r
401         vOLEDImageDraw( pucImage, 0, mainCHARACTER_HEIGHT + 1, bmpBITMAP_WIDTH, bmpBITMAP_HEIGHT );\r
402 \r
403         for( ;; )\r
404         {\r
405                 /* Wait for a message to arrive that requires displaying. */\r
406                 xQueueReceive( xOLEDQueue, &xMessage, portMAX_DELAY );\r
407 \r
408                 /* Write the message on the next available row. */\r
409                 ulY += mainCHARACTER_HEIGHT;\r
410                 if( ulY >= ulMaxY )\r
411                 {\r
412                         ulY = mainCHARACTER_HEIGHT;\r
413                         vOLEDClear();\r
414                         vOLEDStringDraw( pcWelcomeMessage, 0, 0, mainFULL_SCALE );\r
415                 }\r
416 \r
417                 /* Display the message along with the maximum jitter time from the\r
418                 high priority time test. */\r
419                 sprintf( cMessage, "%s [%uns]", xMessage.pcMessage, ulMaxJitter * mainNS_PER_CLOCK );\r
420                 vOLEDStringDraw( cMessage, 0, ulY, mainFULL_SCALE );\r
421         }\r
422 }\r
423 /*-----------------------------------------------------------*/\r
424 \r
425 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
426 {\r
427         for( ;; );\r
428 }\r
429 /*-----------------------------------------------------------*/\r
430 \r
431 void vApplicationMallocFailedHook( void )\r
432 {\r
433         for( ;; );\r
434 }\r
435 /*-----------------------------------------------------------*/\r
436 \r
437 /* Just to keep the linker happy. */\r
438 void __error__( char *pcFilename, unsigned long ulLine )\r
439 {\r
440         for( ;; );\r
441 }\r
442 \r
443 int uipprintf( const char *fmt, ... )\r
444 {\r
445         return( 0 );\r
446 }\r