]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_Primer_GCC/main.c
Update to use new port layer.
[freertos] / Demo / CORTEX_STM32F103_Primer_GCC / main.c
1 /*\r
2         FreeRTOS.org V4.7.2 - Copyright (C) 2003-2008 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 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 /*\r
44  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
45  * documentation provides more details of the standard demo application tasks.\r
46  * In addition to the standard demo tasks, the following tasks and tests are\r
47  * defined and/or created within this file:\r
48  *\r
49  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
50  * using a free running timer to demonstrate the use of the\r
51  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
52  * service routine measures the number of processor clocks that occur between\r
53  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
54  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
55  * displayed on the LCD by the 'Check' task as described below.  The\r
56  * fast interrupt is configured and handled in the timertest.c source file.\r
57  *\r
58  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
59  * is permitted to access the display directly.  Other tasks wishing to write a\r
60  * message to the LCD send the message on a queue to the LCD task instead of\r
61  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
62  * for messages - waking and displaying the messages as they arrive.  Messages\r
63  * can either be a text string to display, or an instruction to update MEMS\r
64  * input.  The MEMS input is used to display a ball that can be moved around\r
65  * LCD by tilting the STM32 Primer.  45% is taken as the neutral position.\r
66  *\r
67  * "Check" task -  This only executes every five seconds but has the highest\r
68  * priority so is guaranteed to get processor time.  Its main function is to\r
69  * check that all the standard demo tasks are still operational.  Should any\r
70  * unexpected behaviour within a demo task be discovered the 'check' task will\r
71  * write an error to the LCD (via the LCD task).  If all the demo tasks are\r
72  * executing with their expected behaviour then the check task writes PASS\r
73  * along with the max jitter time to the LCD (again via the LCD task), as\r
74  * described above.\r
75  *\r
76  * Tick Hook - A tick hook is provided just for demonstration purposes.  In \r
77  * this case it is used to periodically send an instruction to updated the\r
78  * MEMS input to the LCD task.\r
79  *\r
80  */\r
81 \r
82 /* CircleOS includes.  Some of the CircleOS peripheral functionality is \r
83 utilised, although CircleOS itself is not used. */\r
84 #include "circle.h"\r
85 \r
86 /* Standard includes. */\r
87 #include <string.h>\r
88 \r
89 /* Scheduler includes. */\r
90 #include "FreeRTOS.h"\r
91 #include "task.h"\r
92 #include "queue.h"\r
93 \r
94 /* Demo app includes. */\r
95 #include "BlockQ.h"\r
96 #include "blocktim.h"\r
97 #include "GenQTest.h"\r
98 #include "partest.h"\r
99 #include "QPeek.h"\r
100 \r
101 /* The bitmap used to display the FreeRTOS.org logo is stored in 16bit format\r
102 and therefore takes up a large proportion of the Flash space.  Setting this\r
103 parameter to 0 excludes the bitmap from the build, freeing up Flash space for\r
104 extra code. */\r
105 #define mainINCLUDE_BITMAP                                      1\r
106 \r
107 #if mainINCLUDE_BITMAP == 1\r
108         #include "bitmap.h"\r
109 #endif\r
110 \r
111 /* Task priorities. */\r
112 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
113 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
114 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
115 #define mainGEN_Q_PRIORITY                                      ( tskIDLE_PRIORITY + 0 )\r
116 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
117 \r
118 /* Splash screen related constants. */\r
119 #define mainBITMAP_Y                                            ( 38 )\r
120 #define mainBITMAP_X                                            ( 18 )\r
121 #define mainURL_Y                                                       ( 8 )\r
122 #define mainURL_X                                                       ( 78 )\r
123 #define mainSPLASH_SCREEN_DELAY         ( 2000 / portTICK_RATE_MS )\r
124 \r
125 /* Text drawing related constants. */\r
126 #define mainLCD_CHAR_HEIGHT                     ( 13 )\r
127 #define mainLCD_MAX_Y                           ( 110 )\r
128 \r
129 /* The maximum number of message that can be waiting for display at any one\r
130 time. */\r
131 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
132 \r
133 /* The check task uses the sprintf function so requires a little more stack. */\r
134 #define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
135 \r
136 /* The LCD task calls some of the CircleOS functions (for MEMS and LCD access),\r
137 these can require a larger stack. */\r
138 #define configLCD_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
139 \r
140 /* Dimensions the buffer into which the jitter time is written. */\r
141 #define mainMAX_MSG_LEN                                         25\r
142 \r
143 /* The time between cycles of the 'check' task. */\r
144 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
145 \r
146 /* The period at which the MEMS input should be updated. */\r
147 #define mainMEMS_DELAY                                          ( ( portTickType ) 100 / portTICK_RATE_MS )\r
148 \r
149 /* The rate at which the flash task toggles the LED. */\r
150 #define mainFLASH_DELAY                                         ( ( portTickType ) 1000 / portTICK_RATE_MS )\r
151 \r
152 /* The number of nano seconds between each processor clock. */\r
153 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
154 \r
155 /* The two types of message that can be sent to the LCD task. */\r
156 #define mainUPDATE_BALL_MESSAGE                         ( 0 )\r
157 #define mainWRITE_STRING_MESSAGE                        ( 1 )\r
158 \r
159 /* Type of the message sent to the LCD task. */\r
160 typedef struct\r
161 {\r
162         portBASE_TYPE xMessageType;\r
163         signed char *pcMessage;\r
164 } xLCDMessage;\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /*\r
169  * Configure the clocks, GPIO and other peripherals as required by the demo.\r
170  */\r
171 static void prvSetupHardware( void );\r
172 \r
173 /*\r
174  * The LCD is written two by more than one task so is controlled by a\r
175  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
176  * access the LCD directly.  Other tasks wanting to display a message send\r
177  * the message to the gatekeeper.\r
178  */\r
179 static void prvLCDTask( void *pvParameters );\r
180 \r
181 /*\r
182  * Checks the status of all the demo tasks then prints a message to the\r
183  * display.  The message will be either PASS - and include in brackets the\r
184  * maximum measured jitter time (as described at the to of the file), or a\r
185  * message that describes which of the standard demo tasks an error has been\r
186  * discovered in.\r
187  *\r
188  * Messages are not written directly to the terminal, but passed to prvLCDTask\r
189  * via a queue.\r
190  *\r
191  * The check task also receives instructions to update the MEMS input, which\r
192  * in turn can also lead to the LCD being updated.\r
193  */\r
194 static void prvCheckTask( void *pvParameters );\r
195 \r
196 /*\r
197  * Configures the timers and interrupts for the fast interrupt test as\r
198  * described at the top of this file.\r
199  */\r
200 extern void vSetupTimerTest( void );\r
201 \r
202 /*\r
203  * A cut down version of sprintf() used to percent the HUGE GCC library\r
204  * equivalent from being included in the binary image. \r
205  */\r
206 extern int sprintf(char *out, const char *format, ...);\r
207 \r
208 /*\r
209  * Simple toggle the LED periodically for timing verification.\r
210  */\r
211 static void prvFlashTask( void *pvParameters );\r
212 \r
213 /*-----------------------------------------------------------*/\r
214 \r
215 /* The queue used to send messages to the LCD task. */\r
216 xQueueHandle xLCDQueue;\r
217 \r
218 /*-----------------------------------------------------------*/\r
219 \r
220 int main( void )\r
221 {\r
222         #ifdef DEBUG\r
223                 debug();\r
224         #endif\r
225 \r
226         prvSetupHardware();\r
227 \r
228         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
229         are received via this queue. */\r
230         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
231         \r
232         /* Start the standard demo tasks. */\r
233         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
234     vCreateBlockTimeTasks();\r
235         vStartGenericQueueTasks( mainGEN_Q_PRIORITY );\r
236         vStartQueuePeekTasks();\r
237         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
238 \r
239         /* Start the tasks defined within this file/specific to this demo. */\r
240     xTaskCreate( prvCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL ); \r
241         xTaskCreate( prvLCDTask, ( signed portCHAR * ) "LCD", configLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
242         xTaskCreate( prvFlashTask, ( signed portCHAR * ) "Flash", configMINIMAL_STACK_SIZE, NULL, mainFLASH_TASK_PRIORITY, NULL );\r
243 \r
244         /* Configure the timers used by the fast interrupt timer test. */\r
245         vSetupTimerTest();\r
246         \r
247         /* Start the scheduler. */\r
248         vTaskStartScheduler();\r
249         \r
250         /* Will only get here if there was not enough heap space to create the\r
251         idle task. */\r
252         return 0;\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 void prvLCDTask( void *pvParameters )\r
257 {\r
258 xLCDMessage xMessage;\r
259 portCHAR cY = mainLCD_CHAR_HEIGHT;\r
260 const portCHAR * const pcString = "www.FreeRTOS.org";\r
261 const portCHAR * const pcBlankLine = "                  ";\r
262 \r
263         DRAW_Init();\r
264 \r
265         #if mainINCLUDE_BITMAP == 1\r
266                 DRAW_SetImage( pucImage, mainBITMAP_Y, mainBITMAP_X, bmpBITMAP_HEIGHT, bmpBITMAP_WIDTH );\r
267         #endif\r
268 \r
269         LCD_SetScreenOrientation( V9 );\r
270         DRAW_DisplayString( mainURL_Y, mainURL_X, pcString, strlen( pcString ) );\r
271         vTaskDelay( mainSPLASH_SCREEN_DELAY );\r
272         LCD_FillRect( 0, 0, CHIP_SCREEN_WIDTH, CHIP_SCREEN_HEIGHT, RGB_WHITE );\r
273 \r
274         for( ;; )\r
275         {\r
276                 /* Wait for a message to arrive that requires displaying. */\r
277                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
278 \r
279                 /* Check the message type. */\r
280                 if( xMessage.xMessageType == mainUPDATE_BALL_MESSAGE )\r
281                 {\r
282                         /* Read the MEMS and update the ball display on the LCD if required. */\r
283                         MEMS_Handler();\r
284                         POINTER_Handler();\r
285                 }\r
286                 else\r
287                 {\r
288                         /* A text string was sent.  First blank off the old text string, then\r
289                         draw the new text on the next line down. */\r
290                         DRAW_DisplayString( 0, cY, pcBlankLine, strlen( pcBlankLine ) );\r
291 \r
292                         cY -= mainLCD_CHAR_HEIGHT;\r
293                         if( cY <= ( mainLCD_CHAR_HEIGHT - 1 ) )\r
294                         {                       \r
295                                 /* Wrap the line onto which we are going to write the text. */\r
296                                 cY = mainLCD_MAX_Y;\r
297                         }\r
298                         \r
299                         /* Display the message. */\r
300                         DRAW_DisplayString( 0, cY, xMessage.pcMessage, strlen( xMessage.pcMessage ) );\r
301                 }\r
302         }\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 static void prvCheckTask( void *pvParameters )\r
307 {\r
308 portTickType xLastExecutionTime;\r
309 xLCDMessage xMessage;\r
310 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];\r
311 extern unsigned portSHORT usMaxJitter;\r
312 \r
313         /* Initialise the xLastExecutionTime variable on task entry. */\r
314         xLastExecutionTime = xTaskGetTickCount();\r
315 \r
316         /* Setup the message we are going to send to the LCD task. */\r
317         xMessage.xMessageType = mainWRITE_STRING_MESSAGE;\r
318         xMessage.pcMessage = cPassMessage;\r
319         \r
320     for( ;; )\r
321         {\r
322                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
323                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
324 \r
325                 /* Has an error been found in any task?   If so then point the text\r
326                 we are going to send to the LCD task to an error message instead of\r
327                 the PASS message. */\r
328                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
329                 {\r
330                         xMessage.pcMessage = "ERROR IN GEN Q";\r
331                 }\r
332         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
333                 {\r
334                         xMessage.pcMessage = "ERROR IN BLOCK Q";\r
335                 }\r
336                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
337                 {\r
338                         xMessage.pcMessage = "ERROR IN BLOCK TIME";\r
339                 }\r
340         else if( xArePollingQueuesStillRunning() != pdTRUE )\r
341         {\r
342             xMessage.pcMessage = "ERROR IN POLL Q";\r
343         }\r
344                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
345                 {\r
346                         xMessage.pcMessage = "ERROR IN PEEK Q";\r
347                 }\r
348                 else\r
349                 {\r
350                         /* No errors were found in any task, so send a pass message\r
351                         with the max measured jitter time also included (as per the\r
352                         fast interrupt test described at the top of this file and on\r
353                         the online documentation page for this demo application). */\r
354                         sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );\r
355                 }\r
356 \r
357                 /* Send the message to the LCD gatekeeper for display. */\r
358                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
359         }\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 void vApplicationTickHook( void )\r
364 {\r
365 static unsigned portLONG ulCallCount;\r
366 static const xLCDMessage xMemsMessage = { mainUPDATE_BALL_MESSAGE, NULL };\r
367 \r
368         /* Periodically send a message to the LCD task telling it to update\r
369         the MEMS input, and then if necessary the LCD. */\r
370         ulCallCount++;\r
371         if( ulCallCount >= mainMEMS_DELAY )\r
372         {\r
373                 ulCallCount = 0;\r
374                 xQueueSendFromISR( xLCDQueue, &xMemsMessage, pdFALSE );\r
375         }\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r
379 static void prvSetupHardware( void )\r
380 {\r
381         /* Start with the clocks in their expected state. */\r
382         RCC_DeInit();\r
383 \r
384         /* Enable HSE (high speed external clock). */\r
385         RCC_HSEConfig( RCC_HSE_ON );\r
386 \r
387         /* Wait till HSE is ready. */\r
388         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
389         {\r
390         }\r
391 \r
392         /* 2 wait states required on the flash. */\r
393         *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;\r
394 \r
395         /* HCLK = SYSCLK */\r
396         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
397 \r
398         /* PCLK2 = HCLK */\r
399         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
400 \r
401         /* PCLK1 = HCLK/2 */\r
402         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
403 \r
404         /* PLLCLK = 12MHz * 6 = 72 MHz. */\r
405         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );\r
406 \r
407         /* Enable PLL. */\r
408         RCC_PLLCmd( ENABLE );\r
409 \r
410         /* Wait till PLL is ready. */\r
411         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
412         {\r
413         }\r
414 \r
415         /* Select PLL as system clock source. */\r
416         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
417 \r
418         /* Wait till PLL is used as system clock source. */\r
419         while( RCC_GetSYSCLKSource() != 0x08 )\r
420         {\r
421         }\r
422 \r
423         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
424         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
425                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
426 \r
427         /* SPI2 Periph clock enable */\r
428         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
429 \r
430 \r
431         /* Set the Vector Table base address at 0x08000000 */\r
432         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
433 \r
434         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
435         \r
436         /* Configure HCLK clock as SysTick clock source. */\r
437         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
438         \r
439         /* Misc initialisation, including some of the CircleOS features.  Note\r
440         that CircleOS itself is not used. */\r
441         vParTestInitialise();\r
442         MEMS_Init();\r
443         POINTER_Init();\r
444         POINTER_SetMode( POINTER_RESTORE_LESS );\r
445 }\r
446 /*-----------------------------------------------------------*/\r
447 \r
448 static void prvFlashTask( void *pvParameters )\r
449 {\r
450 portTickType xLastExecutionTime;\r
451 \r
452         /* Initialise the xLastExecutionTime variable on task entry. */\r
453         xLastExecutionTime = xTaskGetTickCount();\r
454 \r
455     for( ;; )\r
456         {\r
457                 /* Simple toggle the LED periodically.  This just provides some timing\r
458                 verification. */\r
459                 vTaskDelayUntil( &xLastExecutionTime, mainFLASH_DELAY );\r
460                 vParTestToggleLED( 0 );\r
461         }\r
462 }\r
463 /*-----------------------------------------------------------*/\r
464 \r
465 void starting_delay( unsigned long ul )\r
466 {\r
467         vTaskDelay( ( portTickType ) ul );\r
468 }\r
469 \r
470 \r
471 \r