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