]> begriffs open source - cmsis-freertos/blob - Demo/PIC32MZ_MPLAB/main.c
Set error state if no delay or already expired
[cmsis-freertos] / Demo / PIC32MZ_MPLAB / main.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 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  * and a more comprehensive test and demo application.  The
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
32  * select between the two.  The simply blinky demo is implemented and described
33  * in main_blinky.c.  The more comprehensive test and demo application is
34  * implemented and described in main_full.c.
35  *
36  * This file implements the code that is not demo specific, including the
37  * hardware setup and FreeRTOS hook functions.
38  */
39
40 /* Kernel includes. */
41 #include "FreeRTOS.h"
42 #include "task.h"
43
44 /* Standard demo includes. */
45 #include "partest.h"
46 #include "QueueOverwrite.h"
47 #include "QueueSet.h"
48 #include "EventGroupsDemo.h"
49
50 /* Hardware specific includes. */
51 #include "ConfigPerformance.h"
52
53 /* Core configuration fuse settings */
54 #if defined(__32MZ2048ECM144) || defined(__32MZ2048ECH144)
55         #pragma config FMIIEN = OFF, FETHIO = OFF, PGL1WAY = OFF, PMDL1WAY = OFF, IOL1WAY = OFF, FUSBIDIO = OFF
56         #pragma config FNOSC = SPLL, FSOSCEN = OFF, IESO = OFF, POSCMOD = EC
57         #pragma config OSCIOFNC = OFF, FCKSM = CSECMD, FWDTEN = OFF, FDMTEN = OFF
58         #pragma config DMTINTV = WIN_127_128, WDTSPGM = STOP, WINDIS= NORMAL
59         #pragma config WDTPS = PS1048576, FWDTWINSZ = WINSZ_25, DMTCNT = DMT31
60         #pragma config FPLLIDIV = DIV_3, FPLLRNG = RANGE_13_26_MHZ, FPLLICLK = PLL_POSC
61         #pragma config FPLLMULT = MUL_50, FPLLODIV = DIV_2, UPLLFSEL = FREQ_12MHZ, UPLLEN = OFF
62         #pragma config EJTAGBEN = NORMAL, DBGPER = PG_ALL, FSLEEP = OFF, FECCCON = OFF_UNLOCKED
63         #pragma config BOOTISA = MIPS32, TRCEN = ON, ICESEL = ICS_PGx2, JTAGEN = OFF, DEBUG = ON
64         #pragma config CP = OFF
65 #elif defined(__32MZ2048EFM144) || defined(__32MZ2048EFH144)
66         #pragma config FMIIEN = OFF, FETHIO = OFF, PGL1WAY = OFF, PMDL1WAY = OFF, IOL1WAY = OFF, FUSBIDIO = OFF
67         #pragma config FNOSC = SPLL, FSOSCEN = OFF, IESO = OFF, POSCMOD = EC
68         #pragma config OSCIOFNC = OFF, FCKSM = CSECMD, FWDTEN = OFF, FDMTEN = OFF
69         #pragma config DMTINTV = WIN_127_128, WDTSPGM = STOP, WINDIS= NORMAL
70         #pragma config WDTPS = PS1048576, FWDTWINSZ = WINSZ_25, DMTCNT = DMT31
71         #pragma config FPLLIDIV = DIV_3, FPLLRNG = RANGE_13_26_MHZ, FPLLICLK = PLL_POSC
72         #pragma config FPLLMULT = MUL_50, FPLLODIV = DIV_2, UPLLFSEL = FREQ_12MHZ
73         #pragma config EJTAGBEN = NORMAL, DBGPER = PG_ALL, FSLEEP = OFF, FECCCON = OFF_UNLOCKED
74         #pragma config BOOTISA = MIPS32, TRCEN = ON, ICESEL = ICS_PGx2, JTAGEN = OFF, DEBUG = ON
75         #pragma config CP = OFF
76 #endif
77
78 /*-----------------------------------------------------------*/
79
80 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
81 or 0 to run the more comprehensive test and demo application. */
82 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0
83
84 /*-----------------------------------------------------------*/
85
86 /*
87  * Set up the hardware ready to run this demo.
88  */
89 static void prvSetupHardware( void );
90
91 /*
92  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
93  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
94  */
95 extern void main_blinky( void );
96 extern void main_full( void );
97
98 /*-----------------------------------------------------------*/
99
100 /*
101  * Create the demo tasks then start the scheduler.
102  */
103 int main( void )
104 {
105         /* Prepare the hardware to run this demo. */
106         prvSetupHardware();
107
108         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
109         of this file. */
110         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1
111         {
112                 main_blinky();
113         }
114         #else
115         {
116                 main_full();
117         }
118         #endif
119
120         return 0;
121 }
122 /*-----------------------------------------------------------*/
123
124 static void prvSetupHardware( void )
125 {
126         /* Configure the hardware for maximum performance. */
127         vHardwareConfigurePerformance();
128
129         /* Setup to use the external interrupt controller. */
130         vHardwareUseMultiVectoredInterrupts();
131
132         portDISABLE_INTERRUPTS();
133
134         /* Setup the digital IO for the LED's. */
135         vParTestInitialise();
136 }
137 /*-----------------------------------------------------------*/
138
139 void vApplicationMallocFailedHook( void )
140 {
141         /* vApplicationMallocFailedHook() will only be called if
142         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
143         function that will get called if a call to pvPortMalloc() fails.
144         pvPortMalloc() is called internally by the kernel whenever a task, queue,
145         timer or semaphore is created.  It is also called by various parts of the
146         demo application.  If heap_1.c or heap_2.c are used, then the size of the
147         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
148         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
149         to query the size of free heap space that remains (although it does not
150         provide information on how the remaining heap might be fragmented). */
151         taskDISABLE_INTERRUPTS();
152         for( ;; );
153 }
154 /*-----------------------------------------------------------*/
155
156 void vApplicationIdleHook( void )
157 {
158         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
159         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
160         task.  It is essential that code added to this hook function never attempts
161         to block in any way (for example, call xQueueReceive() with a block time
162         specified, or call vTaskDelay()).  If the application makes use of the
163         vTaskDelete() API function (as this demo application does) then it is also
164         important that vApplicationIdleHook() is permitted to return to its calling
165         function, because it is the responsibility of the idle task to clean up
166         memory allocated by the kernel to any task that has since been deleted. */
167 }
168 /*-----------------------------------------------------------*/
169
170 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
171 {
172         ( void ) pcTaskName;
173         ( void ) pxTask;
174
175         /* Run time task stack overflow checking is performed if
176         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook function is
177         called if a task stack overflow is detected.  Note the system/interrupt
178         stack is not checked. */
179         taskDISABLE_INTERRUPTS();
180         for( ;; );
181 }
182 /*-----------------------------------------------------------*/
183
184 void vApplicationTickHook( void )
185 {
186         /* This function will be called by each tick interrupt if
187         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
188         added here, but the tick hook is called from an interrupt context, so
189         code must not attempt to block, and only the interrupt safe FreeRTOS API
190         functions can be used (those that end in FromISR()). */
191
192         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
193         {
194                 /* Call the periodic queue overwrite from ISR demo. */
195                 vQueueOverwritePeriodicISRDemo();
196
197                 /* Call the queue set ISR test function. */
198                 vQueueSetAccessQueueSetFromISR();
199
200                 /* Exercise event groups from interrupts. */
201                 vPeriodicEventGroupsProcessing();
202         }
203         #endif
204 }
205 /*-----------------------------------------------------------*/
206
207 extern void vAssertCalled( const char * pcFile, unsigned long ulLine )
208 {
209 volatile char *pcFileName;
210 volatile unsigned long ulLineNumber;
211
212         /* Prevent things that are useful to view in the debugger from being
213         optimised away. */
214         pcFileName = ( char * ) pcFile;
215         ( void ) pcFileName;
216         ulLineNumber = ulLine;
217
218         /* Set ulLineNumber to 0 in the debugger to break out of this loop and
219         return to the line that triggered the assert. */
220         while( ulLineNumber != 0 )
221         {
222                 __asm volatile( "NOP" );
223                 __asm volatile( "NOP" );
224                 __asm volatile( "NOP" );
225                 __asm volatile( "NOP" );
226                 __asm volatile( "NOP" );
227         }
228 }
229 /*-----------------------------------------------------------*/
230
231 /* This function overrides the normal _weak_ generic handler. */
232 void _general_exception_handler(void)
233 {
234 static enum {
235         EXCEP_IRQ = 0,  /* interrupt */
236         EXCEP_AdEL = 4, /* address error exception (load or ifetch) */
237         EXCEP_AdES,     /* address error exception (store) */
238         EXCEP_IBE,              /* bus error (ifetch) */
239         EXCEP_DBE,              /* bus error (load/store) */
240         EXCEP_Sys,              /* syscall */
241         EXCEP_Bp,               /* breakpoint */
242         EXCEP_RI,               /* reserved instruction */
243         EXCEP_CpU,              /* coprocessor unusable */
244         EXCEP_Overflow, /* arithmetic overflow */
245         EXCEP_Trap,     /* trap (possible divide by zero) */
246         EXCEP_FPE = 15, /* floating point exception */
247         EXCEP_IS1 = 16, /* implementation specfic 1 */
248         EXCEP_CEU,              /* CorExtend Unuseable */
249         EXCEP_C2E,              /* coprocessor 2 */
250         EXCEP_DSPDis = 26   /* DSP module disabled */
251 } _excep_code;
252
253 static unsigned long _epc_code;
254 static unsigned long _excep_addr;
255
256         asm volatile( "mfc0 %0,$13" : "=r" (_epc_code) );
257         asm volatile( "mfc0 %0,$14" : "=r" (_excep_addr) );
258
259         _excep_code = ( _epc_code & 0x0000007C ) >> 2;
260
261     for( ;; )
262         {
263                 /* prevent compiler warning */
264                 (void) _excep_code;
265
266                 /* Examine _excep_code to identify the type of exception.  Examine
267                 _excep_addr to find the address that caused the exception */
268                 LATHSET = 0x0007;
269                 Nop();
270                 Nop();
271                 Nop();
272         }
273 }
274 /*-----------------------------------------------------------*/
275