3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
28 /******************************************************************************
29 * This project provides two demo applications. A simple blinky style project
30 * that demonstrates low power tickless functionality, and a more comprehensive
31 * test and demo application. The configCREATE_LOW_POWER_DEMO setting, which is
32 * defined in FreeRTOSConfig.h, is used to select between the two. The simply
33 * blinky low power demo is implemented and described in main_low_power.c. The
34 * more comprehensive test and demo application is implemented and described in
37 * This file implements the code that is not demo specific, including the
38 * hardware setup and standard FreeRTOS hook functions.
40 * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
41 * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
42 * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
46 /* Scheduler include files. */
50 /* Hardware register addresses and value. */
51 #define mainVTOR ( * ( uint32_t * ) 0xE000ED08 )
52 #define mainNVIC_AUX_ACTLR ( * ( uint32_t * ) 0xE000E008 )
53 #define mainEC_INTERRUPT_CONTROL ( * ( volatile uint32_t * ) 0x4000FC18 )
54 #define mainMMCR_PCR_PROCESSOR_CLOCK_CONTROL ( * ( volatile uint32_t * )( 0x40080120 ) )
55 #define mainCPU_CLOCK_DIVIDER 1
57 /*-----------------------------------------------------------*/
60 * Configure the hardware as necessary to run this demo.
62 static void prvSetupHardware( void );
65 * main_low_power() is used when configCREATE_LOW_POWER_DEMO is set to 1.
66 * main_full() is used when configCREATE_LOW_POWER_DEMO is set to 0.
68 #if( configCREATE_LOW_POWER_DEMO == 1 )
70 extern void main_low_power( void );
74 extern void main_full( void );
76 /* Some of the tests and examples executed as part of the full demo make use
77 of the tick hook to call API functions from an interrupt context. */
78 extern void vFullDemoTickHook( void );
80 #endif /* #if configCREATE_LOW_POWER_DEMO == 1 */
82 /* Prototypes for the standard FreeRTOS callback/hook functions implemented
84 void vApplicationMallocFailedHook( void );
85 void vApplicationIdleHook( void );
86 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
87 void vApplicationTickHook( void );
89 /*-----------------------------------------------------------*/
91 /* The variable that is incremented to represent each LED toggle. On the
92 clicker hardware the LED state is set to the value of the least significant bit
93 of this variable. On other hardware, where an LED is not used, the LED just
94 keeps a count of the number of times the LED would otherwise have been toggled.
95 See the comments in main_low_power.c and main_full.c for information on the
96 expected LED toggle rate). */
97 volatile uint32_t ulLED = 0;
99 /*-----------------------------------------------------------*/
103 /* Configure the hardware ready to run the demo. */
106 /* The configCREATE_LOW_POWER_DEMO setting is described at the top
108 #if( configCREATE_LOW_POWER_DEMO == 1 )
118 /* Should not get here. */
121 /*-----------------------------------------------------------*/
123 static void prvSetupHardware( void )
125 /* Disable M4 write buffer: fix MEC1322 hardware bug. */
126 mainNVIC_AUX_ACTLR |= 0x07;
128 /* Set divider to 8 for 8MHz operation, MCLK in silicon chip is 64MHz,
130 mainMMCR_PCR_PROCESSOR_CLOCK_CONTROL = mainCPU_CLOCK_DIVIDER;
132 /* Enable alternative NVIC vectors. */
133 mainEC_INTERRUPT_CONTROL = pdTRUE;
135 /* Initialise the LED on the clicker board. */
136 GPIO_Digital_Output( &GPIO_PORT_150_157, _GPIO_PINMASK_4 | _GPIO_PINMASK_5 );
137 GPIO_OUTPUT_PIN_154_bit = 0;
138 GPIO_OUTPUT_PIN_155_bit = 0;
140 /*-----------------------------------------------------------*/
142 void vApplicationMallocFailedHook( void )
144 /* Called if a call to pvPortMalloc() fails because there is insufficient
145 free memory available in the FreeRTOS heap. pvPortMalloc() is called
146 internally by FreeRTOS API functions that create tasks, queues, software
147 timers, and semaphores. The size of the FreeRTOS heap is set by the
148 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
150 /* Force an assert. */
151 configASSERT( ( volatile void * ) NULL );
153 /*-----------------------------------------------------------*/
155 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
160 /* Run time stack overflow checking is performed if
161 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
162 function is called if a stack overflow is detected. */
164 /* Force an assert. */
165 configASSERT( ( volatile void * ) NULL );
167 /*-----------------------------------------------------------*/
169 void vApplicationIdleHook( void )
171 volatile size_t xFreeHeapSpace;
173 /* This is just a trivial example of an idle hook. It is called on each
174 cycle of the idle task. It must *NOT* attempt to block. In this case the
175 idle task just queries the amount of FreeRTOS heap that remains. See the
176 memory management section on the http://www.FreeRTOS.org web site for memory
177 management options. If there is a lot of heap memory free then the
178 configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
180 xFreeHeapSpace = xPortGetFreeHeapSize();
182 /* Remove compiler warning about xFreeHeapSpace being set but never used. */
183 ( void ) xFreeHeapSpace;
185 /*-----------------------------------------------------------*/
187 void vApplicationTickHook( void )
189 /* The full demo includes tests that run from the tick hook. */
190 #if( configCREATE_LOW_POWER_DEMO == 0 )
192 /* Some of the tests and demo tasks executed by the full demo include
193 interaction from an interrupt - for which the tick interrupt is used
194 via the tick hook function. */
199 /*-----------------------------------------------------------*/
201 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
202 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
203 used by the Idle task. */
204 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
206 /* If the buffers to be provided to the Idle task are declared inside this
207 function then they must be declared static - otherwise they will be allocated on
208 the stack and so not exists after this function exits. */
209 static StaticTask_t xIdleTaskTCB;
210 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
212 /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
213 state will be stored. */
214 *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
216 /* Pass out the array that will be used as the Idle task's stack. */
217 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
219 /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
220 Note that, as the array is necessarily of type StackType_t,
221 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
222 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
224 /*-----------------------------------------------------------*/
226 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
227 application must provide an implementation of vApplicationGetTimerTaskMemory()
228 to provide the memory that is used by the Timer service task. */
229 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
231 /* If the buffers to be provided to the Timer task are declared inside this
232 function then they must be declared static - otherwise they will be allocated on
233 the stack and so not exists after this function exits. */
234 static StaticTask_t xTimerTaskTCB;
235 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
237 /* Pass out a pointer to the StaticTask_t structure in which the Timer
238 task's state will be stored. */
239 *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
241 /* Pass out the array that will be used as the Timer task's stack. */
242 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
244 /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
245 Note that, as the array is necessarily of type StackType_t,
246 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
247 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;