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