]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_IAR/main.c
Add in first STM32 demo.
[freertos] / Demo / CORTEX_STM32F103_IAR / main.c
1 /*\r
2         FreeRTOS.org V4.3.1 - Copyright (C) 2003-2007 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\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
38  * documentation provides more details of the standard demo application tasks.\r
39  * In addition to the standard demo tasks, the following tasks and tests are\r
40  * defined and/or created within this file:\r
41  *\r
42  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
43  * using a free running timer to demonstrate the use of the\r
44  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
45  * service routine measures the number of processor clocks that occur between\r
46  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
47  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
48  * displayed on the LCD by the 'Check' task as described below.  The\r
49  * fast interrupt is configured and handled in the timertest.c source file.\r
50  *\r
51  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
52  * is permitted to access the display directly.  Other tasks wishing to write a\r
53  * message to the LCD send the message on a queue to the LCD task instead of\r
54  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
55  * for messages - waking and displaying the messages as they arrive.\r
56  *\r
57  * "Check" task -  This only executes every five seconds but has the highest\r
58  * priority so is guaranteed to get processor time.  Its main function is to\r
59  * check that all the standard demo tasks are still operational.  Should any\r
60  * unexpected behaviour within a demo task be discovered the 'check' task will\r
61  * write an error to the LCD (via the LCD task).  If all the demo tasks are\r
62  * executing with their expected behaviour then the check task writes PASS\r
63  * along with the max jitter time to the LCD (again via the LCD task), as\r
64  * described above.\r
65  *\r
66  */\r
67 \r
68 /* Standard includes. */\r
69 #include <stdio.h>\r
70 \r
71 /* Scheduler includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "Task.h"\r
74 #include "Queue.h"\r
75 \r
76 /* Library includes. */\r
77 #include "stm32f10x_it.h"\r
78 \r
79 /* Demo app includes. */\r
80 #include "lcd.h"\r
81 #include "LCD_Message.h"\r
82 #include "BlockQ.h"\r
83 #include "death.h"\r
84 #include "integer.h"\r
85 #include "blocktim.h"\r
86 #include "partest.h"\r
87 #include "semtest.h"\r
88 #include "pollq.h"\r
89 #include "flash.h"\r
90 #include "comtest2.h"\r
91 \r
92 /* Task priorities. */\r
93 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
94 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
95 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
96 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
97 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
98 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1 )\r
99 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
100 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
101 \r
102 /* Constants related to the LCD. */\r
103 #define mainMAX_LINE                                            ( 240 )\r
104 #define mainROW_INCREMENT                                       ( 24 )\r
105 #define mainMAX_COLUMN                                          ( 20 )\r
106 #define mainCOLUMN_START                                        ( 319 )\r
107 #define mainCOLUMN_INCREMENT                            ( 16 )\r
108 \r
109 /* The maximum number of message that can be waiting for display at any one\r
110 time. */\r
111 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
112 \r
113 /* The check task uses the sprintf function so requires a little more stack. */\r
114 #define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
115 \r
116 /* Dimensions the buffer into which the jitter time is written. */\r
117 #define mainMAX_MSG_LEN                                         25\r
118 \r
119 /* The time between cycles of the 'check' task. */\r
120 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
121 \r
122 /* The number of nano seconds between each processor clock. */\r
123 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
124 \r
125 /* Baud rate used by the comtest tasks. */\r
126 #define mainCOM_TEST_BAUD_RATE          ( 115200 )\r
127 \r
128 /* The LED used by the comtest tasks. See the comtest.c file for more\r
129 information. */\r
130 #define mainCOM_TEST_LED                        ( 3 )\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 /*\r
135  * Configure the clocks, GPIO and other peripherals as required by the demo.\r
136  */\r
137 static void prvSetupHardware( void );\r
138 \r
139 /*\r
140  * Configure the LCD as required by the demo.\r
141  */\r
142 static void prvConfigureLCD( void );\r
143 \r
144 /*\r
145  * The LCD is written two by more than one task so is controlled by a\r
146  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
147  * access the LCD directly.  Other tasks wanting to display a message send\r
148  * the message to the gatekeeper.\r
149  */\r
150 static void vLCDTask( void *pvParameters );\r
151 \r
152 /*\r
153  * Retargets the C library printf function to the USART.\r
154  */\r
155 int fputc( int ch, FILE *f );\r
156 \r
157 /*\r
158  * Checks the status of all the demo tasks then prints a message to the\r
159  * display.  The message will be either PASS - and include in brackets the\r
160  * maximum measured jitter time (as described at the to of the file), or a\r
161  * message that describes which of the standard demo tasks an error has been\r
162  * discovered in.\r
163  *\r
164  * Messages are not written directly to the terminal, but passed to vLCDTask\r
165  * via a queue.\r
166  */\r
167 static void vCheckTask( void *pvParameters );\r
168 \r
169 /*\r
170  * Configures the timers and interrupts for the fast interrupt test as\r
171  * described at the top of this file.\r
172  */\r
173 extern void vSetupTimerTest( void );\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /* The queue used to send messages to the LCD task. */\r
178 xQueueHandle xLCDQueue;\r
179 \r
180 /*-----------------------------------------------------------*/\r
181 \r
182 int main( void )\r
183 {\r
184 #ifdef DEBUG\r
185   debug();\r
186 #endif\r
187 \r
188         prvSetupHardware();\r
189 \r
190         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
191         are received via this queue. */\r
192         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
193         \r
194         /* Start the standard demo tasks. */\r
195         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
196     vCreateBlockTimeTasks();\r
197     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
198     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
199     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
200         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
201         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
202 \r
203         /* Start the tasks defined within this file/specific to this demo. */\r
204     xTaskCreate( vCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );   \r
205         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
206 \r
207         /* The suicide tasks must be created last as they need to know how many\r
208         tasks were running prior to their creation in order to ascertain whether\r
209         or not the correct/expected number of tasks are running at any given time. */\r
210     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
211         \r
212         /* Configure the timers used by the fast interrupt timer test. */\r
213         vSetupTimerTest();\r
214         \r
215         /* Start the scheduler. */\r
216         vTaskStartScheduler();\r
217         \r
218         /* Will only get here if there was not enough heap space to create the\r
219         idle task. */\r
220         return 0;\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 void vLCDTask( void *pvParameters )\r
225 {\r
226 xLCDMessage xMessage;\r
227 \r
228         /* Initialise the LCD and display a startup message. */\r
229         prvConfigureLCD();\r
230         LCD_DrawMonoPict( ( unsigned portLONG * ) pcBitmap );\r
231 \r
232         for( ;; )\r
233         {\r
234                 /* Wait for a message to arrive that requires displaying. */\r
235                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
236 \r
237                 /* Display the message.  Print each message to a different position. */\r
238                 printf( ( portCHAR const * ) xMessage.pcMessage );\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 static void vCheckTask( void *pvParameters )\r
244 {\r
245 portTickType xLastExecutionTime;\r
246 xLCDMessage xMessage;\r
247 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];\r
248 extern unsigned portSHORT usMaxJitter;\r
249 \r
250         xLastExecutionTime = xTaskGetTickCount();\r
251         xMessage.pcMessage = cPassMessage;\r
252         \r
253     for( ;; )\r
254         {\r
255                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
256                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
257 \r
258                 /* Has an error been found in any task? */\r
259 \r
260         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
261                 {\r
262                         xMessage.pcMessage = "ERROR IN BLOCK Q\n";\r
263                 }\r
264                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
265                 {\r
266                         xMessage.pcMessage = "ERROR IN BLOCK TIME\n";\r
267                 }\r
268         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
269         {\r
270             xMessage.pcMessage = "ERROR IN SEMAPHORE\n";\r
271         }\r
272         else if( xArePollingQueuesStillRunning() != pdTRUE )\r
273         {\r
274             xMessage.pcMessage = "ERROR IN POLL Q\n";\r
275         }\r
276         else if( xIsCreateTaskStillRunning() != pdTRUE )\r
277         {\r
278             xMessage.pcMessage = "ERROR IN CREATE\n";\r
279         }\r
280         else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
281         {\r
282             xMessage.pcMessage = "ERROR IN MATH\n";\r
283         }\r
284                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
285                 {\r
286                         xMessage.pcMessage = "ERROR IN COM TEST\n";\r
287                 }                               \r
288                 else\r
289                 {\r
290                         sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );\r
291                 }\r
292 \r
293                 /* Send the message to the LCD gatekeeper for display. */\r
294                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
295         }\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 static void prvSetupHardware( void )\r
300 {\r
301         /* Start with the clocks in their expected state. */\r
302         RCC_DeInit();\r
303 \r
304         /* Enable HSE (high speed external clock). */\r
305         RCC_HSEConfig( RCC_HSE_ON );\r
306 \r
307         /* Wait till HSE is ready. */\r
308         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
309         {\r
310         }\r
311 \r
312         /* 2 wait states required on the flash. */\r
313         *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;\r
314 \r
315         /* HCLK = SYSCLK */\r
316         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
317 \r
318         /* PCLK2 = HCLK */\r
319         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
320 \r
321         /* PCLK1 = HCLK/2 */\r
322         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
323 \r
324         /* PLLCLK = 8MHz * 9 = 72 MHz. */\r
325         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );\r
326 \r
327         /* Enable PLL. */\r
328         RCC_PLLCmd( ENABLE );\r
329 \r
330         /* Wait till PLL is ready. */\r
331         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
332         {\r
333         }\r
334 \r
335         /* Select PLL as system clock source. */\r
336         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
337 \r
338         /* Wait till PLL is used as system clock source. */\r
339         while( RCC_GetSYSCLKSource() != 0x08 )\r
340         {\r
341         }\r
342 \r
343         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
344         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
345                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
346 \r
347         /* SPI2 Periph clock enable */\r
348         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
349 \r
350 \r
351         /* Set the Vector Table base address at 0x08000000 */\r
352         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
353 \r
354         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
355         \r
356         /* Configure HCLK clock as SysTick clock source. */\r
357         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
358         \r
359         vParTestInitialise();\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 static void prvConfigureLCD( void )\r
364 {\r
365 GPIO_InitTypeDef GPIO_InitStructure;\r
366 \r
367         /* Configure LCD Back Light (PA8) as output push-pull */\r
368         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;\r
369         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\r
370         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;\r
371         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
372 \r
373         /* Set the Backlight Pin */\r
374         GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);\r
375 \r
376         /* Initialize the LCD */\r
377         LCD_Init();\r
378 \r
379         /* Set the Back Color */\r
380         LCD_SetBackColor( White );\r
381 \r
382         /* Set the Text Color */\r
383         LCD_SetTextColor( 0x051F );\r
384 \r
385         LCD_Clear();\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 int fputc( int ch, FILE *f )\r
390 {\r
391 static unsigned portSHORT usColumn = 0, usRefColumn = mainCOLUMN_START;\r
392 static unsigned portCHAR ucLine = 0;\r
393 \r
394         if( ( usColumn == 0 ) && ( ucLine == 0 ) )\r
395         {\r
396                 LCD_Clear();\r
397         }\r
398 \r
399         if( ch != '\n' )\r
400         {\r
401                 /* Display one character on LCD */\r
402                 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );\r
403                 \r
404                 /* Decrement the column position by 16 */\r
405                 usRefColumn -= mainCOLUMN_INCREMENT;\r
406                 \r
407                 /* Increment the character counter */\r
408                 usColumn++;\r
409                 if( usColumn == mainMAX_COLUMN )\r
410                 {\r
411                         ucLine += mainROW_INCREMENT;\r
412                         usRefColumn = mainCOLUMN_START;\r
413                         usColumn = 0;\r
414                 }\r
415         }\r
416         else\r
417         {\r
418                 /* Move back to the first column of the next line. */\r
419                 ucLine += mainROW_INCREMENT;\r
420                 usRefColumn = mainCOLUMN_START;\r
421                 usColumn = 0;   \r
422         }\r
423 \r
424         /* Wrap back to the top of the display. */\r
425         if( ucLine >= mainMAX_LINE )\r
426         {\r
427                 ucLine = 0;\r
428         }\r
429         \r
430         return ch;\r
431 }\r
432 /*-----------------------------------------------------------*/\r
433 \r
434 #ifdef  DEBUG\r
435 /* Keep the linker happy. */\r
436 void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )\r
437 {\r
438         for( ;; )\r
439         {\r
440         }\r
441 }\r
442 #endif\r