]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Pearl_Gecko_Simplicity_Studio/main.c
99f5edc828ac15e2a43234a69114bc74ba0cd1ce
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Pearl_Gecko_Simplicity_Studio / main.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  * See http://www.freertos.org/EFM32-Giant-Gecko-Pearl-Gecko-tickless-RTOS-demo.html\r
30  *\r
31  * This project provides two demo applications.  A simple blinky style project\r
32  * that demonstrates low power tickless functionality, and a more comprehensive\r
33  * test and demo application.  The configCREATE_LOW_POWER_DEMO setting, which is\r
34  * defined in FreeRTOSConfig.h, is used to select between the two, and to select\r
35  * the clock used when demonstrating tickless functionality.\r
36  *\r
37  * The simply blinky low power demo is implemented and described in\r
38  * main_low_power.c.  The more comprehensive test and demo application is\r
39  * implemented and described in main_full.c.\r
40  *\r
41  * This file implements the code that is not demo specific, including the\r
42  * hardware setup and standard FreeRTOS hook functions.\r
43  *\r
44  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
45  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
46  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
47  *\r
48  */\r
49 \r
50 /* FreeRTOS includes. */\r
51 #include "FreeRTOS.h"\r
52 #include "task.h"\r
53 \r
54 /* SiLabs includes. */\r
55 #include "em_emu.h"\r
56 #include "bsp.h"\r
57 #include "bsp_trace.h"\r
58 #include "sleep.h"\r
59 \r
60 /*-----------------------------------------------------------*/\r
61 \r
62 /*\r
63  * Configure the hardware as necessary to run this demo.\r
64  */\r
65 static void prvSetupHardware( void );\r
66 \r
67 /*\r
68  * main_low_power() is used when configCREATE_LOW_POWER_DEMO is set to 1.\r
69  * main_full() is used when configCREATE_LOW_POWER_DEMO is set to 0.\r
70  */\r
71 #if( configCREATE_LOW_POWER_DEMO != 0 )\r
72         extern void main_low_power( void );\r
73 #else\r
74         extern void main_full( void );\r
75 #endif /* #if configCREATE_LOW_POWER_DEMO == 1 */\r
76 \r
77 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
78 within this file. */\r
79 void vApplicationMallocFailedHook( void );\r
80 void vApplicationIdleHook( void );\r
81 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
82 void vApplicationTickHook( void );\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 int main( void )\r
87 {\r
88         /*\r
89          * See the following link for instructions:\r
90          * http://www.freertos.org/EFM32-Giant-Gecko-Pearl-Gecko-tickless-RTOS-demo.html\r
91          */\r
92 \r
93         /* Configure the hardware ready to run the demo. */\r
94         prvSetupHardware();\r
95 \r
96         /* The mainCREATE_LOW_POWER_DEMO setting is described at the top\r
97         of this file. */\r
98         #if( configCREATE_LOW_POWER_DEMO != 0 )\r
99         {\r
100                 main_low_power();\r
101         }\r
102         #else\r
103         {\r
104                 main_full();\r
105         }\r
106         #endif\r
107 \r
108         /* Should not get here. */\r
109         return 0;\r
110 }\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 static void prvSetupHardware( void )\r
114 {\r
115 EMU_DCDCInit_TypeDef xDCDInit = EMU_DCDCINIT_STK_DEFAULT;\r
116 CMU_HFXOInit_TypeDef xHFXOInit = CMU_HFXOINIT_STK_DEFAULT;\r
117 \r
118         /* Chip errata */\r
119         CHIP_Init();\r
120 \r
121         /* Init DCDC regulator and HFXO with kit specific parameters */\r
122         EMU_DCDCInit( &xDCDInit );\r
123         CMU_HFXOInit( &xHFXOInit );\r
124 \r
125         /* Switch HFCLK to HFXO and disable HFRCO */\r
126         CMU_ClockSelectSet( cmuClock_HF, cmuSelect_HFXO );\r
127         CMU_OscillatorEnable( cmuOsc_HFRCO, false, false );\r
128 \r
129         /* Initialize LED driver. */\r
130         BSP_LedsInit();\r
131         BSP_LedSet( 0 );\r
132         BSP_LedClear( 1 );\r
133 }\r
134 /*-----------------------------------------------------------*/\r
135 \r
136 void vApplicationMallocFailedHook( void )\r
137 {\r
138         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
139         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
140         internally by FreeRTOS API functions that create tasks, queues, software\r
141         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
142         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
143 \r
144         /* Force an assert. */\r
145         configASSERT( ( volatile void * ) NULL );\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
150 {\r
151         ( void ) pcTaskName;\r
152         ( void ) pxTask;\r
153 \r
154         /* Run time stack overflow checking is performed if\r
155         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
156         function is called if a stack overflow is detected. */\r
157 \r
158         /* Force an assert. */\r
159         configASSERT( ( volatile void * ) NULL );\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void vApplicationIdleHook( void )\r
164 {\r
165 volatile size_t xFreeHeapSpace;\r
166 \r
167         /* This is just a trivial example of an idle hook.  It is called on each\r
168         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
169         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
170         memory management section on the http://www.FreeRTOS.org web site for memory\r
171         management options.  If there is a lot of heap memory free then the\r
172         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
173         RAM. */\r
174         xFreeHeapSpace = xPortGetFreeHeapSize();\r
175 \r
176         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
177         ( void ) xFreeHeapSpace;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void vApplicationTickHook( void )\r
182 {\r
183         /* The full demo includes tests that run from the tick hook. */\r
184         #if( configCREATE_LOW_POWER_DEMO == 0 )\r
185         {\r
186         extern void vFullDemoTickHook( void );\r
187 \r
188                 /* Some of the tests and demo tasks executed by the full demo include\r
189                 interaction from an interrupt - for which the tick interrupt is used\r
190                 via the tick hook function. */\r
191                 vFullDemoTickHook();\r
192         }\r
193         #endif\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
198 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
199 used by the Idle task. */\r
200 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
201 {\r
202 /* If the buffers to be provided to the Idle task are declared inside this\r
203 function then they must be declared static - otherwise they will be allocated on\r
204 the stack and so not exists after this function exits. */\r
205 static StaticTask_t xIdleTaskTCB;\r
206 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
207 \r
208         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
209         state will be stored. */\r
210         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
211 \r
212         /* Pass out the array that will be used as the Idle task's stack. */\r
213         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
214 \r
215         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
216         Note that, as the array is necessarily of type StackType_t,\r
217         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
218         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
223 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
224 to provide the memory that is used by the Timer service task. */\r
225 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
226 {\r
227 /* If the buffers to be provided to the Timer task are declared inside this\r
228 function then they must be declared static - otherwise they will be allocated on\r
229 the stack and so not exists after this function exits. */\r
230 static StaticTask_t xTimerTaskTCB;\r
231 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
232 \r
233         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
234         task's state will be stored. */\r
235         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
236 \r
237         /* Pass out the array that will be used as the Timer task's stack. */\r
238         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
239 \r
240         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
241         Note that, as the array is necessarily of type StackType_t,\r
242         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
243         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
244 }\r