]> begriffs open source - cmsis-freertos/blob - Demo/ARM9_STR91X_IAR/main.c
Set error state if no delay or already expired
[cmsis-freertos] / Demo / ARM9_STR91X_IAR / main.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
30         The processor MUST be in supervisor mode when vTaskStartScheduler is
31         called.  The demo applications included in the FreeRTOS.org download switch
32         to supervisor mode prior to main being called.  If you are not using one of
33         these demo application projects then ensure Supervisor mode is used.
34 */
35
36 /*
37  * Creates all the demo application tasks, then starts the scheduler.  The WEB
38  * documentation provides more details of the demo application tasks.
39  *
40  * A few tasks are created that are not part of the standard demo.  These are
41  * the 'LCD' task, the 'LCD Message' task, a WEB server task and the 'Check'
42  * task.
43  *
44  * The LCD task is the only task that accesses the LCD directly, so mutual
45  * exclusion is ensured.  Any task wishing to display text sends the LCD task
46  * a message containing a pointer to the string that should be displayed.
47  * The LCD task itself just blocks on a queue waiting for such a message to
48  * arrive - processing each in turn.
49  *
50  * The LCD Message task does nothing other than periodically send messages to
51  * the LCD task.  The messages originating from the LCD Message task are
52  * displayed on the top row of the LCD.
53  *
54  * The Check task only executes every three seconds but has the highest
55  * priority so is guaranteed to get processor time.  Its main function is to
56  * check that all the other tasks are still operational. Most tasks maintain
57  * a unique count that is incremented each time the task successfully completes
58  * a cycle of its function.  Should any error occur within such a task the
59  * count is permanently halted.  The check task sets a bit in an error status
60  * flag should it find any counter variable at a value that indicates an
61  * error has occurred.  The error flag value is converted to a string and sent
62  * to the LCD task for display on the bottom row on the LCD.
63  */
64
65 /* Standard includes. */
66 #include <stdio.h>
67
68 /* Library includes. */
69 #include "91x_lib.h"
70
71 /* Scheduler includes. */
72 #include "FreeRTOS.h"
73 #include "task.h"
74 #include "queue.h"
75
76 /* Demo application includes. */
77 #include "lcd.h"
78 #include "flash.h"
79 #include "integer.h"
80 #include "PollQ.h"
81 #include "BlockQ.h"
82 #include "semtest.h"
83 #include "dynamic.h"
84 #include "partest.h"
85 #include "flop.h"
86 #include "comtest2.h"
87 #include "serial.h"
88 #include "GenQTest.h"
89 #include "QPeek.h"
90
91 #ifdef STACK_LWIP
92         #include "BasicWEB.h"
93         #include "sys.h"
94 #endif
95
96 /* Priorities for the demo application tasks. */
97 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )
98 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )
99 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )
100 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )
101 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
102 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )
103 #define mainLCD_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )
104 #define mainMSG_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )
105 #define mainGENERIC_QUEUE_PRIORITY      ( tskIDLE_PRIORITY )
106
107 /* Delays used by the various tasks defined in this file. */
108 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
109 #define mainSTRING_WRITE_DELAY          ( 500 / portTICK_PERIOD_MS )
110 #define mainLCD_DELAY                           ( 20 / portTICK_PERIOD_MS )
111
112 /* Constants for the ComTest tasks. */
113 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )
114 #define mainCOM_TEST_LED                        ( 3 )
115
116 /* The maximum number of messages that can be pending to be written to the LCD. */
117 #define mainLCD_QUEUE_LEN                       ( 6 )
118
119 /* Dimension the buffer used to write the error flag string. */
120 #define mainMAX_FLAG_STRING_LEN         ( 32 )
121
122 /* The structure that is passed on the LCD message queue. */
123 typedef struct
124 {
125         char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */
126         portBASE_TYPE xRow;                             /*<< The row on which the message should be displayed. */
127 } xLCDMessage;
128 /*-----------------------------------------------------------*/
129
130 /*
131  * The task that executes at the highest priority and calls
132  * prvCheckOtherTasksAreStillRunning().  See the description at the top
133  * of the file.
134  */
135 static void vErrorChecks( void *pvParameters );
136
137 /*
138  * Configure the processor clock and ports.
139  */
140 static void prvSetupHardware( void );
141
142 /*
143  * Checks that all the demo application tasks are still executing without error
144  * - as described at the top of the file.  Called by vErrorChecks().
145  */
146 static void prvCheckOtherTasksAreStillRunning( void );
147
148 #ifdef STACK_UIP
149         /*
150          * The WEB server task prototype.  The task is created in this file but defined
151          * elsewhere.  STACK_UIP is defined when the uIP stack is used in preference
152          * to the lwIP stack.
153          */
154         extern void vuIP_Task(void *pvParameters);
155 #endif
156
157 /*
158  * The task that displays text on the LCD.
159  */
160 static void prvLCDTask( void * pvParameters );
161
162 /*
163  * The task that sends messages to be displayed on the top row of the LCD.
164  */
165 static void prvLCDMessageTask( void * pvParameters );
166
167 /*-----------------------------------------------------------*/
168
169 /* The queue used to pass messages to the LCD task. */
170 static QueueHandle_t xLCDQueue;
171
172 /* Error status flag. */
173 static unsigned long ulErrorFlags = 0;
174
175 /*-----------------------------------------------------------*/
176
177 /*
178  * Starts all the other tasks, then starts the scheduler.
179  */
180 void main( void )
181 {
182         #ifdef DEBUG
183                 debug();
184         #endif
185         
186         /* Setup any hardware that has not already been configured by the low
187         level init routines. */
188         prvSetupHardware();
189         /* Create the queue used to send data to the LCD task. */
190         xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );
191         
192         /* Start all the standard demo application tasks. */
193         vStartIntegerMathTasks( tskIDLE_PRIORITY );
194         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
195         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
196         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
197         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
198         vStartDynamicPriorityTasks();
199         vStartMathTasks( tskIDLE_PRIORITY );
200         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
201         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
202         vStartQueuePeekTasks(); 
203
204         /* Start the tasks which are defined in this file. */
205         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
206         xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );
207         xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );
208
209         /* Start either the uIP TCP/IP stack or the lwIP TCP/IP stack. */
210         #ifdef STACK_UIP
211                 /* Finally, create the WEB server task. */
212                 xTaskCreate( vuIP_Task, "uIP", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );
213         #endif
214
215         #ifdef STACK_LWIP       
216                 /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/
217                 vlwIPInit();
218                 sys_set_state(  ( signed char * ) "httpd", lwipBASIC_SERVER_STACK_SIZE );
219                 sys_thread_new( vBasicWEBServer, ( void * ) NULL, basicwebWEBSERVER_PRIORITY );
220                 sys_set_default_state();
221         #endif
222
223         /* Start the scheduler.
224
225         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
226         The processor MUST be in supervisor mode when vTaskStartScheduler is
227         called.  The demo applications included in the FreeRTOS.org download switch
228         to supervisor mode prior to main being called.  If you are not using one of
229         these demo application projects then ensure Supervisor mode is used here. */
230
231         vTaskStartScheduler();
232
233         /* We should never get here as control is now taken by the scheduler. */
234         for( ;; );
235 }
236 /*-----------------------------------------------------------*/
237
238 static void prvSetupHardware( void )
239 {
240         /* Configuration taken from the ST code.
241
242         Set Flash banks size & address */
243         FMI_BankRemapConfig( 4, 2, 0, 0x80000 );
244
245         /* FMI Waite States */
246         FMI_Config( FMI_READ_WAIT_STATE_2, FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE, FMI_LVD_ENABLE, FMI_FREQ_HIGH );
247
248         /* Configure the FPLL = 96MHz, and APB to 48MHz. */
249         SCU_PCLKDivisorConfig( SCU_PCLK_Div2 );
250         SCU_PLLFactorsConfig( 192, 25, 2 );
251         SCU_PLLCmd( ENABLE );
252         SCU_MCLKSourceConfig( SCU_MCLK_PLL );
253
254         WDG_Cmd( DISABLE );
255         VIC_DeInit();
256
257         /* GPIO8 clock source enable, used by the LCD. */
258         SCU_APBPeriphClockConfig(__GPIO8, ENABLE);
259         GPIO_DeInit(GPIO8);
260
261         /* GPIO 9 clock source enable, used by the LCD. */
262         SCU_APBPeriphClockConfig(__GPIO9, ENABLE);
263         GPIO_DeInit(GPIO9);
264
265         /* Enable VIC clock */
266         SCU_AHBPeriphClockConfig(__VIC, ENABLE);
267         SCU_AHBPeriphReset(__VIC, DISABLE);
268
269         /* Peripheral initialisation. */
270         vParTestInitialise();
271 }
272 /*-----------------------------------------------------------*/
273
274 static void vErrorChecks( void *pvParameters )
275 {
276 static char cCheckVal[ mainMAX_FLAG_STRING_LEN ];
277 char *pcFlagString;
278 xLCDMessage xMessageToSend;
279 TickType_t xLastWakeTime;
280 char *pcStringsToDisplay[] = {                                                                          
281                                                                         "Check status flag"
282                                                                  };
283
284         /* The parameters are not used in this task. */
285         ( void ) pvParameters;
286         
287         pcFlagString = &cCheckVal[ 0 ]; 
288
289         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
290         functions correctly. */
291         xLastWakeTime = xTaskGetTickCount();
292
293         /* Cycle for ever, delaying then checking all the other tasks are still
294         operating without error. */
295         for( ;; )
296         {
297                 /* Delay until it is time to execute again. */
298                 vTaskDelayUntil( &xLastWakeTime, mainCHECK_PERIOD );
299         
300                 /* Check all the other tasks to see if the error flag needs updating. */
301                 prvCheckOtherTasksAreStillRunning();
302                 
303                 /* Create a string indicating the error flag status. */
304                 sprintf( cCheckVal, "equals 0x%x       ", ulErrorFlags );
305                 xMessageToSend.xRow = Line2;
306
307                 /* Send the first part of the message to the LCD task. */
308                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ 0 ];
309                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );
310                 vTaskDelay( mainSTRING_WRITE_DELAY );
311
312                 /* Send the second part of the message to the LCD task. */
313                 xMessageToSend.ppcMessageToDisplay = &pcFlagString;
314                 xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 );
315         }
316 }
317 /*-----------------------------------------------------------*/
318
319 static void prvCheckOtherTasksAreStillRunning( void )
320 {
321         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
322         {
323                 ulErrorFlags |= 0x01;
324         }
325
326         if( xArePollingQueuesStillRunning() != pdTRUE )
327         {
328                 ulErrorFlags |=  0x02;
329         }
330
331         if( xAreSemaphoreTasksStillRunning() != pdTRUE )
332         {
333                 ulErrorFlags |= 0x04;
334         }
335
336         if( xAreBlockingQueuesStillRunning() != pdTRUE )
337         {
338                 ulErrorFlags |= 0x08;
339         }
340
341         if( xAreComTestTasksStillRunning() != pdTRUE )
342         {
343                 ulErrorFlags |= 0x10;
344         }
345
346         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
347         {
348                 ulErrorFlags |= 0x20;
349         }
350
351         if( xAreMathsTaskStillRunning() != pdTRUE )
352         {
353                 ulErrorFlags |= 0x40;
354         }
355         
356         if( xAreGenericQueueTasksStillRunning() != pdTRUE )
357         {
358                 ulErrorFlags |= 0x80;
359         }
360
361         if( xAreQueuePeekTasksStillRunning() != pdTRUE )
362         {
363                 ulErrorFlags |= 0x100;
364         }
365         
366 }
367 /*-----------------------------------------------------------*/
368
369 static void prvLCDMessageTask( void * pvParameters )
370 {
371 QueueHandle_t *pxLCDQueue;      
372 xLCDMessage xMessageToSend;
373 portBASE_TYPE xIndex = 0;
374
375 /* The strings that are written to the LCD. */
376 char *pcStringsToDisplay[] = {                                                                          
377                                                                         "IAR             ",
378                                                                         "STR912          ",
379                                                                         "Demo            ",
380                                                                         "www.FreeRTOS.org",
381                                                                         ""
382                                                                 };
383
384
385         /* To test the parameter passing mechanism, the queue on which messages are
386         posted is passed in as a parameter even though it is available as a file
387         scope variable anyway. */
388         pxLCDQueue = ( QueueHandle_t * ) pvParameters;
389
390         for( ;; )
391         {
392                 /* Wait until it is time to move onto the next string. */
393                 vTaskDelay( mainSTRING_WRITE_DELAY );           
394                 
395                 /* Configure the message object to send to the LCD task. */
396                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];
397                 xMessageToSend.xRow = Line1;
398                 
399                 /* Post the message to be displayed. */
400                 xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 );
401                 
402                 /* Move onto the next message, wrapping when necessary. */
403                 xIndex++;               
404                 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )
405                 {
406                         xIndex = 0;
407
408                         /* Delay longer before going back to the start of the messages. */
409                         vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
410                 }
411         }
412 }
413 /*-----------------------------------------------------------*/
414
415 void prvLCDTask( void * pvParameters )
416 {
417 QueueHandle_t *pxLCDQueue;
418 xLCDMessage xReceivedMessage;
419 char *pcString;
420
421         /* To test the parameter passing mechanism, the queue on which messages are
422         received is passed in as a parameter even though it is available as a file
423         scope variable anyway. */
424         pxLCDQueue = ( QueueHandle_t * ) pvParameters;
425
426         LCD_Init();
427
428         for( ;; )
429         {
430                 /* Wait for a message to arrive. */
431                 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )
432                 {
433                         /* Where is the string we are going to display? */
434                         pcString = *xReceivedMessage.ppcMessageToDisplay;
435                         LCD_DisplayString(xReceivedMessage.xRow, pcString, BlackText);
436
437                         /* The delay here is just to ensure the LCD task does not starve
438                         out lower priority tasks as writing to the LCD can take a long
439                         time. */
440                         vTaskDelay( mainLCD_DELAY );
441                 }
442         }
443 }
444 /*-----------------------------------------------------------*/