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