]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_M4F_CEC1302_MikroC/main.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / CORTEX_M4F_CEC1302_MikroC / main.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
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
35  * main_full.c.
36  *
37  * This file implements the code that is not demo specific, including the
38  * hardware setup and standard FreeRTOS hook functions.
39  *
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!
43  *
44  */
45
46 /* Scheduler include files. */
47 #include "FreeRTOS.h"
48 #include "task.h"
49
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
56
57 /*-----------------------------------------------------------*/
58
59 /*
60  * Configure the hardware as necessary to run this demo.
61  */
62 static void prvSetupHardware( void );
63
64 /*
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.
67  */
68 #if( configCREATE_LOW_POWER_DEMO == 1 )
69
70         extern void main_low_power( void );
71
72 #else
73
74         extern void main_full( void );
75
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 );
79
80 #endif /* #if configCREATE_LOW_POWER_DEMO == 1 */
81
82 /* Prototypes for the standard FreeRTOS callback/hook functions implemented
83 within this file. */
84 void vApplicationMallocFailedHook( void );
85 void vApplicationIdleHook( void );
86 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
87 void vApplicationTickHook( void );
88
89 /*-----------------------------------------------------------*/
90
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;
98
99 /*-----------------------------------------------------------*/
100
101 int main( void )
102 {
103         /* Configure the hardware ready to run the demo. */
104         prvSetupHardware();
105
106         /* The configCREATE_LOW_POWER_DEMO setting is described at the top
107         of this file. */
108         #if( configCREATE_LOW_POWER_DEMO == 1 )
109         {
110                 main_low_power();
111         }
112         #else
113         {
114                 main_full();
115         }
116         #endif
117
118         /* Should not get here. */
119         return 0;
120 }
121 /*-----------------------------------------------------------*/
122
123 static void prvSetupHardware( void )
124 {
125         /* Disable M4 write buffer: fix MEC1322 hardware bug. */
126         mainNVIC_AUX_ACTLR |= 0x07;
127
128         /* Set divider to 8 for 8MHz operation, MCLK in silicon chip is 64MHz,
129         CPU=MCLK/Divider. */
130         mainMMCR_PCR_PROCESSOR_CLOCK_CONTROL = mainCPU_CLOCK_DIVIDER;
131
132         /* Enable alternative NVIC vectors. */
133         mainEC_INTERRUPT_CONTROL = pdTRUE;
134
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;
139 }
140 /*-----------------------------------------------------------*/
141
142 void vApplicationMallocFailedHook( void )
143 {
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. */
149
150         /* Force an assert. */
151         configASSERT( ( volatile void * ) NULL );
152 }
153 /*-----------------------------------------------------------*/
154
155 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
156 {
157         ( void ) pcTaskName;
158         ( void ) pxTask;
159
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. */
163
164         /* Force an assert. */
165         configASSERT( ( volatile void * ) NULL );
166 }
167 /*-----------------------------------------------------------*/
168
169 void vApplicationIdleHook( void )
170 {
171 volatile size_t xFreeHeapSpace;
172
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
179         RAM. */
180         xFreeHeapSpace = xPortGetFreeHeapSize();
181
182         /* Remove compiler warning about xFreeHeapSpace being set but never used. */
183         ( void ) xFreeHeapSpace;
184 }
185 /*-----------------------------------------------------------*/
186
187 void vApplicationTickHook( void )
188 {
189         /* The full demo includes tests that run from the tick hook. */
190         #if( configCREATE_LOW_POWER_DEMO == 0 )
191         {
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. */
195                 vFullDemoTickHook();
196         }
197         #endif
198 }
199 /*-----------------------------------------------------------*/
200
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 )
205 {
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 ];
211
212         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
213         state will be stored. */
214         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
215
216         /* Pass out the array that will be used as the Idle task's stack. */
217         *ppxIdleTaskStackBuffer = uxIdleTaskStack;
218
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;
223 }
224 /*-----------------------------------------------------------*/
225
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 )
230 {
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 ];
236
237         /* Pass out a pointer to the StaticTask_t structure in which the Timer
238         task's state will be stored. */
239         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
240
241         /* Pass out the array that will be used as the Timer task's stack. */
242         *ppxTimerTaskStackBuffer = uxTimerTaskStack;
243
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;
248 }
249
250