]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_Discovery_IAR/main_full.c
373b98434631edd5f0f21cdb5e00271068bcabc0
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_Discovery_IAR / main_full.c
1 /*\r
2     FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /******************************************************************************\r
67  * NOTE 1:  This project provides two demo applications.  A low power tickless\r
68  * project, and a more comprehensive test and demo application.  The\r
69  * configCREATE_LOW_POWER_DEMO setting in FreeRTOSConfig.h is used to\r
70  * select between the two.  See the notes on using\r
71  * configCREATE_LOW_POWER_DEMO in FreeRTOSConfig.h.  This file implements\r
72  * the comprehensive test and demo version.\r
73  *\r
74  * NOTE 2:  This file only contains the source code that is specific to the\r
75  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
76  * required to configure the hardware, are defined in main.c.\r
77  ******************************************************************************\r
78  *\r
79  * main_full() creates all the demo application tasks and a software timer, then\r
80  * starts the scheduler.  The web documentation provides more details of the\r
81  * standard demo application tasks, which provide no particular functionality,\r
82  * but do provide a good example of how to use the FreeRTOS API.\r
83  *\r
84  * In addition to the standard demo tasks, the following tasks and tests are\r
85  * defined and/or created within this file:\r
86  *\r
87  * "Check" timer - The check software timer period is initially set to three\r
88  * seconds.  The callback function associated with the check software timer\r
89  * checks that all the standard demo tasks are not only still executing, but\r
90  * are executing without reporting any errors.  If the check software timer\r
91  * discovers that a task has either stalled, or reported an error, then it\r
92  * changes its own execution period from the initial three seconds, to just\r
93  * 200ms.  The check software timer callback function also toggles an LED each\r
94  * time it is called.  This provides a visual indication of the system status:\r
95  * If the LED toggles every three seconds, then no issues have been discovered.\r
96  * If the LED toggles every 200ms, then an issue has been discovered with at\r
97  * least one task.\r
98  *\r
99  * See the documentation page for this demo on the FreeRTOS.org web site for\r
100  * full information, including hardware setup requirements.\r
101  */\r
102 \r
103 /* Standard includes. */\r
104 #include <stdio.h>\r
105 \r
106 /* Kernel includes. */\r
107 #include "FreeRTOS.h"\r
108 #include "task.h"\r
109 #include "timers.h"\r
110 #include "semphr.h"\r
111 \r
112 /* Standard demo application includes. */\r
113 #include "PollQ.h"\r
114 #include "semtest.h"\r
115 #include "dynamic.h"\r
116 #include "BlockQ.h"\r
117 #include "blocktim.h"\r
118 #include "countsem.h"\r
119 #include "GenQTest.h"\r
120 #include "recmutex.h"\r
121 \r
122 /* ST library functions. */\r
123 #include "stm32l1xx.h"\r
124 #include "discover_board.h"\r
125 #include "stm32l_discovery_lcd.h"\r
126 \r
127 /* Priorities for the demo application tasks. */\r
128 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
129 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
130 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
131 \r
132 /* A block time of zero simply means "don't block". */\r
133 #define mainDONT_BLOCK                                          ( 0UL )\r
134 \r
135 /* The period after which the check timer will expire providing no errors\r
136 have been reported by any of the standard demo tasks.  ms are converted to the\r
137 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
138 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
139 \r
140 /* The period at which the check timer will expire, in ms, if an error has been\r
141 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
142 in ticks using the portTICK_PERIOD_MS constant. */\r
143 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
144 \r
145 /*-----------------------------------------------------------*/\r
146 \r
147 /*\r
148  * The check timer callback function, as described at the top of this file.\r
149  */\r
150 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
151 \r
152 /*\r
153  * Configure the LCD, then write welcome message.\r
154  */\r
155 static void prvConfigureLCD( void );\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void main_full( void )\r
160 {\r
161 TimerHandle_t xCheckTimer = NULL;\r
162 \r
163         /* The LCD is only used in the Full demo. */\r
164         prvConfigureLCD();\r
165 \r
166         /* Start all the other standard demo/test tasks.  They have not particular\r
167         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
168         kernel port. */\r
169         vStartDynamicPriorityTasks();\r
170         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
171         vCreateBlockTimeTasks();\r
172         vStartCountingSemaphoreTasks();\r
173         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
174         vStartRecursiveMutexTasks();\r
175         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
176         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
177 \r
178         /* Create the software timer that performs the 'check' functionality,\r
179         as described at the top of this file. */\r
180         xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
181                                                                 ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
182                                                                 pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
183                                                                 ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
184                                                                 prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
185                                                           );\r
186 \r
187         if( xCheckTimer != NULL )\r
188         {\r
189                 xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
190         }\r
191 \r
192         /* Start the scheduler. */\r
193         vTaskStartScheduler();\r
194 \r
195         /* If all is well, the scheduler will now be running, and the following line\r
196         will never be reached.  If the following line does execute, then there was\r
197         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
198         to be created.  See the memory management section on the FreeRTOS web site\r
199         for more details. */\r
200         for( ;; );\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
205 {\r
206 static long lChangedTimerPeriodAlready = pdFALSE;\r
207 unsigned long ulErrorFound = pdFALSE;\r
208 \r
209         /* Check all the demo tasks to ensure they are all still running, and that\r
210         none have detected an error. */\r
211 \r
212         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
213         {\r
214                 ulErrorFound = pdTRUE;\r
215         }\r
216 \r
217         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
218         {\r
219                 ulErrorFound = pdTRUE;\r
220         }\r
221 \r
222         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
223         {\r
224                 ulErrorFound = pdTRUE;\r
225         }\r
226 \r
227         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
228         {\r
229                 ulErrorFound = pdTRUE;\r
230         }\r
231 \r
232         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
233         {\r
234                 ulErrorFound = pdTRUE;\r
235         }\r
236 \r
237         if( xArePollingQueuesStillRunning() != pdTRUE )\r
238         {\r
239                 ulErrorFound = pdTRUE;\r
240         }\r
241 \r
242         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
243         {\r
244                 ulErrorFound = pdTRUE;\r
245         }\r
246 \r
247         /* Toggle the check LED to give an indication of the system status.  If\r
248         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
249         everything is ok.  A faster toggle indicates an error. */\r
250         GPIO_TOGGLE( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );\r
251 \r
252         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
253         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
254         This will result in an increase in the rate at which mainCHECK_LED\r
255         toggles. */\r
256         if( ulErrorFound != pdFALSE )\r
257         {\r
258                 if( lChangedTimerPeriodAlready == pdFALSE )\r
259                 {\r
260                         lChangedTimerPeriodAlready = pdTRUE;\r
261 \r
262                         /* This call to xTimerChangePeriod() uses a zero block time.\r
263                         Functions called from inside of a timer callback function must\r
264                         *never* attempt to block. */\r
265                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
266                 }\r
267         }\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 static void prvConfigureLCD( void )\r
272 {\r
273 GPIO_InitTypeDef GPIO_InitStructure;\r
274 \r
275         /* Enable necessary clocks. */\r
276         RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC, ENABLE );\r
277         RCC_APB1PeriphClockCmd( RCC_APB1Periph_LCD, ENABLE );\r
278         PWR_RTCAccessCmd( ENABLE );\r
279         RCC_LSEConfig( ENABLE );\r
280         RCC_RTCCLKConfig( RCC_RTCCLKSource_LSE );\r
281         RCC_RTCCLKCmd( ENABLE );\r
282 \r
283         /* Configure Port A LCD Output pins as alternate function. */\r
284         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_8 | GPIO_Pin_9 |GPIO_Pin_10 |GPIO_Pin_15;\r
285         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
286         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
287 \r
288         /* Select LCD alternate function for Port A LCD Output pins. */\r
289         GPIO_PinAFConfig( GPIOA, GPIO_PinSource1, GPIO_AF_LCD );\r
290         GPIO_PinAFConfig( GPIOA, GPIO_PinSource2, GPIO_AF_LCD );\r
291         GPIO_PinAFConfig( GPIOA, GPIO_PinSource3, GPIO_AF_LCD );\r
292         GPIO_PinAFConfig( GPIOA, GPIO_PinSource8, GPIO_AF_LCD );\r
293         GPIO_PinAFConfig( GPIOA, GPIO_PinSource9, GPIO_AF_LCD );\r
294         GPIO_PinAFConfig( GPIOA, GPIO_PinSource10, GPIO_AF_LCD );\r
295         GPIO_PinAFConfig( GPIOA, GPIO_PinSource15, GPIO_AF_LCD );\r
296 \r
297         /* Configure Port B LCD Output pins as alternate function */\r
298         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;\r
299         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
300         GPIO_Init( GPIOB, &GPIO_InitStructure );\r
301 \r
302         /* Select LCD alternate function for Port B LCD Output pins */\r
303         GPIO_PinAFConfig( GPIOB, GPIO_PinSource3, GPIO_AF_LCD );\r
304         GPIO_PinAFConfig( GPIOB, GPIO_PinSource4, GPIO_AF_LCD );\r
305         GPIO_PinAFConfig( GPIOB, GPIO_PinSource5, GPIO_AF_LCD );\r
306         GPIO_PinAFConfig( GPIOB, GPIO_PinSource8, GPIO_AF_LCD );\r
307         GPIO_PinAFConfig( GPIOB, GPIO_PinSource9, GPIO_AF_LCD );\r
308         GPIO_PinAFConfig( GPIOB, GPIO_PinSource10, GPIO_AF_LCD );\r
309         GPIO_PinAFConfig( GPIOB, GPIO_PinSource11, GPIO_AF_LCD );\r
310         GPIO_PinAFConfig( GPIOB, GPIO_PinSource12, GPIO_AF_LCD );\r
311         GPIO_PinAFConfig( GPIOB, GPIO_PinSource13, GPIO_AF_LCD );\r
312         GPIO_PinAFConfig( GPIOB, GPIO_PinSource14, GPIO_AF_LCD );\r
313         GPIO_PinAFConfig( GPIOB, GPIO_PinSource15, GPIO_AF_LCD );\r
314 \r
315         /* Configure Port C LCD Output pins as alternate function */\r
316         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |GPIO_Pin_11 ;\r
317         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
318         GPIO_Init( GPIOC, &GPIO_InitStructure );\r
319 \r
320         /* Select LCD alternate function for Port B LCD Output pins */\r
321         GPIO_PinAFConfig( GPIOC, GPIO_PinSource0, GPIO_AF_LCD );\r
322         GPIO_PinAFConfig( GPIOC, GPIO_PinSource1, GPIO_AF_LCD );\r
323         GPIO_PinAFConfig( GPIOC, GPIO_PinSource2, GPIO_AF_LCD );\r
324         GPIO_PinAFConfig( GPIOC, GPIO_PinSource3, GPIO_AF_LCD );\r
325         GPIO_PinAFConfig( GPIOC, GPIO_PinSource6, GPIO_AF_LCD );\r
326         GPIO_PinAFConfig( GPIOC, GPIO_PinSource7, GPIO_AF_LCD );\r
327         GPIO_PinAFConfig( GPIOC, GPIO_PinSource8, GPIO_AF_LCD );\r
328         GPIO_PinAFConfig( GPIOC, GPIO_PinSource9, GPIO_AF_LCD );\r
329         GPIO_PinAFConfig( GPIOC, GPIO_PinSource10, GPIO_AF_LCD );\r
330         GPIO_PinAFConfig( GPIOC, GPIO_PinSource11, GPIO_AF_LCD );\r
331 \r
332         LCD_GLASS_Init();\r
333         LCD_GLASS_DisplayString( "F'RTOS" );\r
334 }\r
335 \r