]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_Discovery_IAR/main_full.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_Discovery_IAR / main_full.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /******************************************************************************\r
29  * NOTE 1:  This project provides two demo applications.  A low power tickless\r
30  * project, and a more comprehensive test and demo application.  The\r
31  * configCREATE_LOW_POWER_DEMO setting in FreeRTOSConfig.h is used to\r
32  * select between the two.  See the notes on using\r
33  * configCREATE_LOW_POWER_DEMO in FreeRTOSConfig.h.  This file implements\r
34  * the comprehensive test and demo version.\r
35  *\r
36  * NOTE 2:  This file only contains the source code that is specific to the\r
37  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
38  * required to configure the hardware, are defined in main.c.\r
39  ******************************************************************************\r
40  *\r
41  * main_full() creates all the demo application tasks and a software timer, then\r
42  * starts the scheduler.  The web documentation provides more details of the\r
43  * standard demo application tasks, which provide no particular functionality,\r
44  * but do provide a good example of how to use the FreeRTOS API.\r
45  *\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  * "Check" timer - The check software timer period is initially set to three\r
50  * seconds.  The callback function associated with the check software timer\r
51  * checks that all the standard demo tasks are not only still executing, but\r
52  * are executing without reporting any errors.  If the check software timer\r
53  * discovers that a task has either stalled, or reported an error, then it\r
54  * changes its own execution period from the initial three seconds, to just\r
55  * 200ms.  The check software timer callback function also toggles an LED each\r
56  * time it is called.  This provides a visual indication of the system status:\r
57  * If the LED toggles every three seconds, then no issues have been discovered.\r
58  * If the LED toggles every 200ms, then an issue has been discovered with at\r
59  * least one task.\r
60  *\r
61  * See the documentation page for this demo on the FreeRTOS.org web site for\r
62  * full information, including hardware setup requirements.\r
63  */\r
64 \r
65 /* Standard includes. */\r
66 #include <stdio.h>\r
67 \r
68 /* Kernel includes. */\r
69 #include "FreeRTOS.h"\r
70 #include "task.h"\r
71 #include "timers.h"\r
72 #include "semphr.h"\r
73 \r
74 /* Standard demo application includes. */\r
75 #include "PollQ.h"\r
76 #include "semtest.h"\r
77 #include "dynamic.h"\r
78 #include "BlockQ.h"\r
79 #include "blocktim.h"\r
80 #include "countsem.h"\r
81 #include "GenQTest.h"\r
82 #include "recmutex.h"\r
83 \r
84 /* ST library functions. */\r
85 #include "stm32l1xx.h"\r
86 #include "discover_board.h"\r
87 #include "stm32l_discovery_lcd.h"\r
88 \r
89 /* Priorities for the demo application tasks. */\r
90 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
91 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
92 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
93 \r
94 /* A block time of zero simply means "don't block". */\r
95 #define mainDONT_BLOCK                                          ( 0UL )\r
96 \r
97 /* The period after which the check timer will expire providing no errors\r
98 have been reported by any of the standard demo tasks.  ms are converted to the\r
99 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
100 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
101 \r
102 /* The period at which the check timer will expire, in ms, if an error has been\r
103 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
104 in ticks using the portTICK_PERIOD_MS constant. */\r
105 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /*\r
110  * The check timer callback function, as described at the top of this file.\r
111  */\r
112 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
113 \r
114 /*\r
115  * Configure the LCD, then write welcome message.\r
116  */\r
117 static void prvConfigureLCD( void );\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 void main_full( void )\r
122 {\r
123 TimerHandle_t xCheckTimer = NULL;\r
124 \r
125         /* The LCD is only used in the Full demo. */\r
126         prvConfigureLCD();\r
127 \r
128         /* Start all the other standard demo/test tasks.  They have no particular\r
129         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
130         kernel port. */\r
131         vStartDynamicPriorityTasks();\r
132         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
133         vCreateBlockTimeTasks();\r
134         vStartCountingSemaphoreTasks();\r
135         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
136         vStartRecursiveMutexTasks();\r
137         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
138         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
139 \r
140         /* Create the software timer that performs the 'check' functionality,\r
141         as described at the top of this file. */\r
142         xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
143                                                                 ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
144                                                                 pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
145                                                                 ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
146                                                                 prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
147                                                           );\r
148 \r
149         if( xCheckTimer != NULL )\r
150         {\r
151                 xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
152         }\r
153 \r
154         /* Start the scheduler. */\r
155         vTaskStartScheduler();\r
156 \r
157         /* If all is well, the scheduler will now be running, and the following line\r
158         will never be reached.  If the following line does execute, then there was\r
159         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
160         to be created.  See the memory management section on the FreeRTOS web site\r
161         for more details. */\r
162         for( ;; );\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
167 {\r
168 static long lChangedTimerPeriodAlready = pdFALSE;\r
169 unsigned long ulErrorFound = pdFALSE;\r
170 \r
171         /* Check all the demo tasks to ensure they are all still running, and that\r
172         none have detected an error. */\r
173 \r
174         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
175         {\r
176                 ulErrorFound = pdTRUE;\r
177         }\r
178 \r
179         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
180         {\r
181                 ulErrorFound = pdTRUE;\r
182         }\r
183 \r
184         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
185         {\r
186                 ulErrorFound = pdTRUE;\r
187         }\r
188 \r
189         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
190         {\r
191                 ulErrorFound = pdTRUE;\r
192         }\r
193 \r
194         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
195         {\r
196                 ulErrorFound = pdTRUE;\r
197         }\r
198 \r
199         if( xArePollingQueuesStillRunning() != pdTRUE )\r
200         {\r
201                 ulErrorFound = pdTRUE;\r
202         }\r
203 \r
204         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
205         {\r
206                 ulErrorFound = pdTRUE;\r
207         }\r
208 \r
209         /* Toggle the check LED to give an indication of the system status.  If\r
210         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
211         everything is ok.  A faster toggle indicates an error. */\r
212         GPIO_TOGGLE( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );\r
213 \r
214         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
215         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
216         This will result in an increase in the rate at which mainCHECK_LED\r
217         toggles. */\r
218         if( ulErrorFound != pdFALSE )\r
219         {\r
220                 if( lChangedTimerPeriodAlready == pdFALSE )\r
221                 {\r
222                         lChangedTimerPeriodAlready = pdTRUE;\r
223 \r
224                         /* This call to xTimerChangePeriod() uses a zero block time.\r
225                         Functions called from inside of a timer callback function must\r
226                         *never* attempt to block. */\r
227                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
228                 }\r
229         }\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvConfigureLCD( void )\r
234 {\r
235 GPIO_InitTypeDef GPIO_InitStructure;\r
236 \r
237         /* Enable necessary clocks. */\r
238         RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC, ENABLE );\r
239         RCC_APB1PeriphClockCmd( RCC_APB1Periph_LCD, ENABLE );\r
240         PWR_RTCAccessCmd( ENABLE );\r
241         RCC_LSEConfig( ENABLE );\r
242         RCC_RTCCLKConfig( RCC_RTCCLKSource_LSE );\r
243         RCC_RTCCLKCmd( ENABLE );\r
244 \r
245         /* Configure Port A LCD Output pins as alternate function. */\r
246         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
247         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
248         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
249 \r
250         /* Select LCD alternate function for Port A LCD Output pins. */\r
251         GPIO_PinAFConfig( GPIOA, GPIO_PinSource1, GPIO_AF_LCD );\r
252         GPIO_PinAFConfig( GPIOA, GPIO_PinSource2, GPIO_AF_LCD );\r
253         GPIO_PinAFConfig( GPIOA, GPIO_PinSource3, GPIO_AF_LCD );\r
254         GPIO_PinAFConfig( GPIOA, GPIO_PinSource8, GPIO_AF_LCD );\r
255         GPIO_PinAFConfig( GPIOA, GPIO_PinSource9, GPIO_AF_LCD );\r
256         GPIO_PinAFConfig( GPIOA, GPIO_PinSource10, GPIO_AF_LCD );\r
257         GPIO_PinAFConfig( GPIOA, GPIO_PinSource15, GPIO_AF_LCD );\r
258 \r
259         /* Configure Port B LCD Output pins as alternate function */\r
260         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
261         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
262         GPIO_Init( GPIOB, &GPIO_InitStructure );\r
263 \r
264         /* Select LCD alternate function for Port B LCD Output pins */\r
265         GPIO_PinAFConfig( GPIOB, GPIO_PinSource3, GPIO_AF_LCD );\r
266         GPIO_PinAFConfig( GPIOB, GPIO_PinSource4, GPIO_AF_LCD );\r
267         GPIO_PinAFConfig( GPIOB, GPIO_PinSource5, GPIO_AF_LCD );\r
268         GPIO_PinAFConfig( GPIOB, GPIO_PinSource8, GPIO_AF_LCD );\r
269         GPIO_PinAFConfig( GPIOB, GPIO_PinSource9, GPIO_AF_LCD );\r
270         GPIO_PinAFConfig( GPIOB, GPIO_PinSource10, GPIO_AF_LCD );\r
271         GPIO_PinAFConfig( GPIOB, GPIO_PinSource11, GPIO_AF_LCD );\r
272         GPIO_PinAFConfig( GPIOB, GPIO_PinSource12, GPIO_AF_LCD );\r
273         GPIO_PinAFConfig( GPIOB, GPIO_PinSource13, GPIO_AF_LCD );\r
274         GPIO_PinAFConfig( GPIOB, GPIO_PinSource14, GPIO_AF_LCD );\r
275         GPIO_PinAFConfig( GPIOB, GPIO_PinSource15, GPIO_AF_LCD );\r
276 \r
277         /* Configure Port C LCD Output pins as alternate function */\r
278         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
279         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
280         GPIO_Init( GPIOC, &GPIO_InitStructure );\r
281 \r
282         /* Select LCD alternate function for Port B LCD Output pins */\r
283         GPIO_PinAFConfig( GPIOC, GPIO_PinSource0, GPIO_AF_LCD );\r
284         GPIO_PinAFConfig( GPIOC, GPIO_PinSource1, GPIO_AF_LCD );\r
285         GPIO_PinAFConfig( GPIOC, GPIO_PinSource2, GPIO_AF_LCD );\r
286         GPIO_PinAFConfig( GPIOC, GPIO_PinSource3, GPIO_AF_LCD );\r
287         GPIO_PinAFConfig( GPIOC, GPIO_PinSource6, GPIO_AF_LCD );\r
288         GPIO_PinAFConfig( GPIOC, GPIO_PinSource7, GPIO_AF_LCD );\r
289         GPIO_PinAFConfig( GPIOC, GPIO_PinSource8, GPIO_AF_LCD );\r
290         GPIO_PinAFConfig( GPIOC, GPIO_PinSource9, GPIO_AF_LCD );\r
291         GPIO_PinAFConfig( GPIOC, GPIO_PinSource10, GPIO_AF_LCD );\r
292         GPIO_PinAFConfig( GPIOC, GPIO_PinSource11, GPIO_AF_LCD );\r
293 \r
294         LCD_GLASS_Init();\r
295         LCD_GLASS_DisplayString( "F'RTOS" );\r
296 }\r
297 \r