]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_CEC1302_Keil_GCC/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M4F_CEC1302_Keil_GCC / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /******************************************************************************\r
30  * See http://www.freertos.org/Microchip_CEC1302_ARM_Cortex-M4F_Low_Power_Demo.html\r
31  *\r
32  * This project provides two demo applications.  A simple blinky style project\r
33  * that demonstrates low power tickless functionality, and a more comprehensive\r
34  * test and demo application.  The configCREATE_LOW_POWER_DEMO setting, which is\r
35  * defined in FreeRTOSConfig.h, is used to select between the two.  The simply\r
36  * blinky low power demo is implemented and described in main_low_power.c.  The\r
37  * more comprehensive test and demo application is implemented and described in\r
38  * main_full.c.\r
39  *\r
40  * The simple blinky demo uses aggregated interrupts.  The full demo uses\r
41  * disaggregated interrupts.\r
42  *\r
43  * This file implements the code that is not demo specific, including the\r
44  * hardware setup and standard FreeRTOS hook functions.\r
45  *\r
46  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
47  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
48  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
49  *\r
50  */\r
51 \r
52 /* Scheduler include files. */\r
53 #include "FreeRTOS.h"\r
54 #include "task.h"\r
55 \r
56 /* Hardware register addresses. */\r
57 #define mainVTOR                                        ( * ( uint32_t * ) 0xE000ED08 )\r
58 #define mainNVIC_AUX_ACTLR                      ( * ( uint32_t * ) 0xE000E008 )\r
59 #define mainEC_INTERRUPT_CONTROL        ( * ( volatile uint32_t * ) 0x4000FC18 )\r
60 \r
61 /*-----------------------------------------------------------*/\r
62 \r
63 /*\r
64  * Configure the hardware as necessary to run this demo.\r
65  */\r
66 static void prvSetupHardware( void );\r
67 \r
68 /*\r
69  * main_low_power() is used when configCREATE_LOW_POWER_DEMO is set to 1.\r
70  * main_full() is used when configCREATE_LOW_POWER_DEMO is set to 0.\r
71  */\r
72 #if( configCREATE_LOW_POWER_DEMO == 1 )\r
73 \r
74         extern void main_low_power( void );\r
75 \r
76 #else\r
77 \r
78         extern void main_full( void );\r
79 \r
80         /* Some of the tests and examples executed as part of the full demo make use\r
81         of the tick hook to call API functions from an interrupt context. */\r
82         extern void vFullDemoTickHook( void );\r
83 \r
84 #endif /* #if configCREATE_LOW_POWER_DEMO == 1 */\r
85 \r
86 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
87 within this file. */\r
88 void vApplicationMallocFailedHook( void );\r
89 void vApplicationIdleHook( void );\r
90 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
91 void vApplicationTickHook( void );\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* The variable that is incremented to represent each LED toggle.  On the\r
96 clicker hardware the LED state is set to the value of the least significant bit\r
97 of this variable.  On other hardware, where an LED is not used, the LED just\r
98 keeps a count of the number of times the LED would otherwise have been toggled.\r
99 See the comments in main_low_power.c and main_full.c for information on the\r
100 expected LED toggle rate). */\r
101 volatile uint32_t ulLED = 0;\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 int main( void )\r
106 {\r
107         /* See http://www.freertos.org/Microchip_CEC1302_ARM_Cortex-M4F_Low_Power_Demo.html */\r
108 \r
109         /* Configure the hardware ready to run the demo. */\r
110         prvSetupHardware();\r
111 \r
112         /* The configCREATE_LOW_POWER_DEMO setting is described at the top\r
113         of this file. */\r
114         #if( configCREATE_LOW_POWER_DEMO == 1 )\r
115         {\r
116                 /* The low power demo also demonstrated aggregated interrupts, so clear\r
117                 the interrupt control register to disable the alternative NVIC vectors. */\r
118                 mainEC_INTERRUPT_CONTROL = pdFALSE;\r
119 \r
120                 main_low_power();\r
121         }\r
122         #else\r
123         {\r
124                 /* The full demo also demonstrated disaggregated interrupts, so set the\r
125                 interrupt control register to enable the alternative NVIC vectors. */\r
126                 mainEC_INTERRUPT_CONTROL = pdTRUE;\r
127 \r
128                 main_full();\r
129         }\r
130         #endif\r
131 \r
132         /* Should not get here. */\r
133         return 0;\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 static void prvSetupHardware( void )\r
138 {\r
139 extern void system_set_ec_clock( void );\r
140 extern unsigned long __Vectors[];\r
141 \r
142         /* Disable M4 write buffer: fix CEC1302 hardware bug. */\r
143         mainNVIC_AUX_ACTLR |= 0x07;\r
144 \r
145         system_set_ec_clock();\r
146 \r
147         /* Assuming downloading code via the debugger - so ensure the hardware\r
148         is using the vector table downloaded with the application. */\r
149         mainVTOR = ( uint32_t ) __Vectors;\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 void vApplicationMallocFailedHook( void )\r
154 {\r
155         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
156         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
157         internally by FreeRTOS API functions that create tasks, queues, software\r
158         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
159         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
160 \r
161         /* Force an assert. */\r
162         configASSERT( ( volatile void * ) NULL );\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
167 {\r
168         ( void ) pcTaskName;\r
169         ( void ) pxTask;\r
170 \r
171         /* Run time stack overflow checking is performed if\r
172         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
173         function is called if a stack overflow is detected. */\r
174 \r
175         /* Force an assert. */\r
176         configASSERT( ( volatile void * ) NULL );\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 void vApplicationIdleHook( void )\r
181 {\r
182 volatile size_t xFreeHeapSpace;\r
183 \r
184         /* This is just a trivial example of an idle hook.  It is called on each\r
185         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
186         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
187         memory management section on the http://www.FreeRTOS.org web site for memory\r
188         management options.  If there is a lot of heap memory free then the\r
189         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
190         RAM. */\r
191         xFreeHeapSpace = xPortGetFreeHeapSize();\r
192 \r
193         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
194         ( void ) xFreeHeapSpace;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vApplicationTickHook( void )\r
199 {\r
200         /* The full demo includes tests that run from the tick hook. */\r
201         #if( configCREATE_LOW_POWER_DEMO == 0 )\r
202         {\r
203                 /* Some of the tests and demo tasks executed by the full demo include\r
204                 interaction from an interrupt - for which the tick interrupt is used\r
205                 via the tick hook function. */\r
206                 vFullDemoTickHook();\r
207         }\r
208         #endif\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
213 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
214 used by the Idle task. */\r
215 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
216 {\r
217 /* If the buffers to be provided to the Idle task are declared inside this\r
218 function then they must be declared static - otherwise they will be allocated on\r
219 the stack and so not exists after this function exits. */\r
220 static StaticTask_t xIdleTaskTCB;\r
221 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
222 \r
223         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
224         state will be stored. */\r
225         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
226 \r
227         /* Pass out the array that will be used as the Idle task's stack. */\r
228         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
229 \r
230         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
231         Note that, as the array is necessarily of type StackType_t,\r
232         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
233         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
238 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
239 to provide the memory that is used by the Timer service task. */\r
240 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
241 {\r
242 /* If the buffers to be provided to the Timer task are declared inside this\r
243 function then they must be declared static - otherwise they will be allocated on\r
244 the stack and so not exists after this function exits. */\r
245 static StaticTask_t xTimerTaskTCB;\r
246 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
247 \r
248         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
249         task's state will be stored. */\r
250         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
251 \r
252         /* Pass out the array that will be used as the Timer task's stack. */\r
253         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
254 \r
255         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
256         Note that, as the array is necessarily of type StackType_t,\r
257         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
258         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
259 }\r
260 \r
261 \r