]> git.sur5r.net Git - freertos/blob - Demo/ARM7_STR75x_GCC/main.c
259716962eca9faa39e0b217b367f17767a27981
[freertos] / Demo / ARM7_STR75x_GCC / main.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
56  * documentation provides more details of the demo application tasks.\r
57  *\r
58  * In addition to the standard demo tasks there are two tasks defined within\r
59  * this file:\r
60  *\r
61  * 1 - The check task\r
62  * The 'check' task is responsible for ensuring that all the standard demo\r
63  * tasks are executing as expected.  It only executes every three seconds, but\r
64  * has the highest priority within the system so is guaranteed to get execution\r
65  * time.  Any errors discovered by the check task are latched until the\r
66  * processor is reset.  At the end of each cycle the check task sends either\r
67  * a pass or fail message to the 'print' task for display on the LCD.\r
68  *\r
69  * 2 - The print task\r
70  * The print task is the LCD 'gatekeeper'.  That is, it is the only task that\r
71  * should access the LCD directly so is always guaranteed exclusive (and\r
72  * therefore consistent) access.  The print task simply blocks on a queue\r
73  * to wait for messages from other tasks wishing to display text on the LCD.\r
74  * When a message arrives it displays its contents on the LCD then blocks to\r
75  * wait again.\r
76  */\r
77 \r
78 /* ST includes. */\r
79 #include "lcd.h"\r
80 \r
81 /* Kernel includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "queue.h"\r
85 \r
86 /* Demo application includes. */\r
87 #include "partest.h"\r
88 #include "flash.h"\r
89 #include "integer.h"\r
90 #include "blocktim.h"\r
91 #include "BlockQ.h"\r
92 #include "comtest2.h"\r
93 #include "dynamic.h"\r
94 \r
95 /* Demo application task priorities. */\r
96 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
97 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
98 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
99 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
100 #define mainLCD_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
101 \r
102 /* How often should we check the other tasks? */\r
103 #define mainCHECK_TASK_CYCLE_TIME       ( 3000 )\r
104 \r
105 /* The maximum offset into the pass and fail strings sent to the LCD.  An\r
106 offset is used a simple method of using a different column each time a message\r
107 is written to the LCD. */\r
108 #define mainMAX_WRITE_COLUMN            ( 14 )\r
109 \r
110 /* Baud rate used by the comtest tasks. */\r
111 #define mainCOM_TEST_BAUD_RATE          ( 19200 )\r
112 \r
113 /* The LED used by the comtest tasks. See the comtest.c file for more\r
114 information. */\r
115 #define mainCOM_TEST_LED                        ( 3 )\r
116 \r
117 /* The number of messages that can be queued for display on the LCD at any one\r
118 time. */\r
119 #define mainLCD_QUEUE_LENGTH            ( 2 )\r
120 \r
121 /* The time to wait when sending to mainLCD_QUEUE_LENGTH. */\r
122 #define mainNO_DELAY                            ( 0 )\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /* The type that is posted to the LCD queue. */\r
127 typedef struct LCD_MESSAGE\r
128 {\r
129         unsigned char *pucString; /* Points to the string to be displayed. */\r
130         unsigned char ucLine;     /* The line of the LCD that should be used. */\r
131 } LCDMessage;\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 /*\r
136  * The task that executes at the highest priority and checks the operation of\r
137  * all the other tasks in the system.  See the description at the top of the\r
138  * file.\r
139  */\r
140 static void vCheckTask( void *pvParameters );\r
141 \r
142 /*\r
143  * ST provided routine to configure the processor.\r
144  */\r
145 static void prvSetupHardware(void);\r
146 \r
147 /*\r
148  * The only task that should access the LCD.  Other tasks wanting to write\r
149  * to the LCD should send a message of type LCDMessage containing the\r
150  * information to display to the print task.  The print task simply blocks\r
151  * waiting for the arrival of such messages, displays the message, then blocks\r
152  * again.\r
153  */\r
154 static void vPrintTask( void *pvParameters );\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /* The queue used to communicate with the LCD print task. */\r
159 static xQueueHandle xLCDQueue;\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /* Create all the demo application tasks, then start the scheduler. */\r
164 int main( void )\r
165 {\r
166         /* Perform any hardware setup necessary. */\r
167         prvSetupHardware();\r
168         vParTestInitialise();\r
169 \r
170         /* Create the queue used to communicate with the LCD print task. */\r
171         xLCDQueue = xQueueCreate( mainLCD_QUEUE_LENGTH, sizeof( LCDMessage ) ); \r
172         \r
173         /* Create the standard demo application tasks.  See the WEB documentation\r
174         for more information on these tasks. */\r
175         vCreateBlockTimeTasks();\r
176         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
177         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
178         vStartDynamicPriorityTasks();\r
179         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
180         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
181         \r
182         /* Create the tasks defined within this file. */\r
183         xTaskCreate( vPrintTask, ( signed char * ) "LCD", configMINIMAL_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );\r
184         xTaskCreate( vCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
185         \r
186         vTaskStartScheduler();\r
187         \r
188         /* Execution will only reach here if there was insufficient heap to\r
189         start the scheduler. */\r
190         return 0;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static void vCheckTask( void *pvParameters )\r
195 {\r
196 static unsigned long ulErrorDetected = pdFALSE; \r
197 portTickType xLastExecutionTime;\r
198 unsigned char *ucErrorMessage = ( unsigned char * )"              FAIL";\r
199 unsigned char *ucSuccessMessage = ( unsigned char * )"              PASS";\r
200 unsigned portBASE_TYPE uxColumn = mainMAX_WRITE_COLUMN;\r
201 LCDMessage xMessage;\r
202 \r
203         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
204         works correctly. */\r
205         xLastExecutionTime = xTaskGetTickCount();\r
206 \r
207         for( ;; )\r
208         {\r
209                 /* Wait until it is time for the next cycle. */\r
210                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_TASK_CYCLE_TIME );\r
211 \r
212                 /* Has an error been found in any of the standard demo tasks? */\r
213                 \r
214                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
215                 {\r
216                         ulErrorDetected = pdTRUE;\r
217                 }\r
218 \r
219                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
220                 {\r
221                         ulErrorDetected = pdTRUE;\r
222                 }\r
223 \r
224                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
225                 {\r
226                         ulErrorDetected = pdTRUE;\r
227                 }\r
228                 \r
229                 if( xAreComTestTasksStillRunning() != pdTRUE )\r
230                 {\r
231                         ulErrorDetected = pdTRUE;\r
232                 }               \r
233                 \r
234                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
235                 {\r
236                         ulErrorDetected = pdTRUE;\r
237                 }               \r
238         \r
239                 /* Calculate the LCD line on which we would like the message to\r
240                 be displayed.  The column variable is used for convenience as\r
241                 it is incremented each cycle anyway. */\r
242                 xMessage.ucLine = ( unsigned char ) ( uxColumn & 0x01 );\r
243 \r
244                 /* The message displayed depends on whether an error was found or\r
245                 not.  Any discovered error is latched.  Here the column variable\r
246                 is used as an index into the text string as a simple way of moving\r
247                 the text from column to column. */              \r
248                 if( ulErrorDetected == pdFALSE )\r
249                 {\r
250                         xMessage.pucString = ucSuccessMessage + uxColumn;\r
251                 }\r
252                 else\r
253                 {\r
254                         xMessage.pucString = ucErrorMessage + uxColumn;                 \r
255                 }               \r
256 \r
257                 /* Send the message to the print task for display. */\r
258                 xQueueSend( xLCDQueue, ( void * ) &xMessage, mainNO_DELAY );\r
259                 \r
260                 /* Make sure the message is printed in a different column the next\r
261                 time around. */\r
262                 uxColumn--;\r
263                 if( uxColumn == 0 )\r
264                 {\r
265                         uxColumn = mainMAX_WRITE_COLUMN;\r
266                 }\r
267         }\r
268 }\r
269 \r
270 /*-----------------------------------------------------------*/\r
271 \r
272 static void vPrintTask( void *pvParameters )\r
273 {\r
274 LCDMessage xMessage;\r
275 \r
276         for( ;; )\r
277         {\r
278                 /* Wait until a message arrives. */\r
279                 while( xQueueReceive( xLCDQueue, ( void * ) &xMessage, portMAX_DELAY ) != pdPASS );\r
280                 \r
281                 /* The message contains the text to display, and the line on which the\r
282                 text should be displayed. */\r
283                 LCD_Clear();\r
284                 LCD_DisplayString( xMessage.ucLine, xMessage.pucString, BlackText );\r
285         }\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 static void prvSetupHardware(void)\r
290 {\r
291 ErrorStatus OSC4MStartUpStatus01;       \r
292 \r
293         /* ST provided routine. */\r
294 \r
295         /* MRCC system reset */\r
296         MRCC_DeInit();\r
297         \r
298         /* Wait for OSC4M start-up */\r
299         OSC4MStartUpStatus01 = MRCC_WaitForOSC4MStartUp();\r
300         \r
301         if(OSC4MStartUpStatus01 == SUCCESS)\r
302         {\r
303                 /* Set HCLK to 60MHz */\r
304                 MRCC_HCLKConfig(MRCC_CKSYS_Div1);\r
305                 \r
306                 /* Set CKTIM to 60MHz */\r
307                 MRCC_CKTIMConfig(MRCC_HCLK_Div1);\r
308                 \r
309                 /* Set PCLK to 30MHz */\r
310                 MRCC_PCLKConfig(MRCC_CKTIM_Div2);\r
311                 \r
312                 /* Enable Flash Burst mode */\r
313                 CFG_FLASHBurstConfig(CFG_FLASHBurst_Enable);\r
314                 \r
315                 /* Set CK_SYS to 60 MHz */\r
316                 MRCC_CKSYSConfig(MRCC_CKSYS_OSC4MPLL, MRCC_PLL_Mul_15);\r
317         }\r
318         \r
319         /* GPIO pins optimized for 3V3 operation */\r
320         MRCC_IOVoltageRangeConfig(MRCC_IOVoltageRange_3V3);\r
321         \r
322         /* GPIO clock source enable */\r
323         MRCC_PeripheralClockConfig(MRCC_Peripheral_GPIO, ENABLE);\r
324         \r
325         /* EXTIT clock source enable */\r
326         MRCC_PeripheralClockConfig(MRCC_Peripheral_EXTIT, ENABLE);\r
327         /* TB clock source enable */\r
328         MRCC_PeripheralClockConfig(MRCC_Peripheral_TB, ENABLE);\r
329         \r
330         /* Initialize the demonstration menu */\r
331         LCD_Init();\r
332         \r
333         LCD_DisplayString(Line1, ( unsigned char * ) "www.FreeRTOS.org", BlackText);\r
334         LCD_DisplayString(Line2, ( unsigned char * ) "  STR750 Demo  ", BlackText);\r
335         \r
336         EIC_IRQCmd(ENABLE);\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r