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