]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / PIC32MX_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 \r
48 /* Hardware specific includes. */\r
49 #include "ConfigPerformance.h"\r
50 \r
51 /* Core configuratin fuse settings */\r
52 #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF\r
53 #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2\r
54 #pragma config CP = OFF, BWP = OFF, PWP = OFF\r
55 \r
56 /* Additional config fuse settings for other supported processors */\r
57 #if defined(__32MX460F512L__)\r
58         #pragma config UPLLEN = OFF\r
59 #elif defined(__32MX795F512L__)\r
60         #pragma config UPLLEN = OFF\r
61         #pragma config FSRSSEL = PRIORITY_7\r
62 #endif\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
67 or 0 to run the more comprehensive test and demo application. */\r
68 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /*\r
73  * Set up the hardware ready to run this demo.\r
74  */\r
75 static void prvSetupHardware( void );\r
76 \r
77 /*\r
78  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
79  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
80  */\r
81 extern void main_blinky( void );\r
82 extern void main_full( void );\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /*\r
87  * Create the demo tasks then start the scheduler.\r
88  */\r
89 int main( void )\r
90 {\r
91         /* Prepare the hardware to run this demo. */\r
92         prvSetupHardware();\r
93 \r
94         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
95         of this file. */\r
96         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
97         {\r
98                 main_blinky();\r
99         }\r
100         #else\r
101         {\r
102                 main_full();\r
103         }\r
104         #endif\r
105 \r
106         return 0;\r
107 }\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 static void prvSetupHardware( void )\r
111 {\r
112         /* Configure the hardware for maximum performance. */\r
113         vHardwareConfigurePerformance();\r
114 \r
115         /* Setup to use the external interrupt controller. */\r
116         vHardwareUseMultiVectoredInterrupts();\r
117 \r
118         portDISABLE_INTERRUPTS();\r
119 \r
120         /* Setup the digital IO for the LED's. */\r
121         vParTestInitialise();\r
122 }\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void vApplicationMallocFailedHook( void )\r
126 {\r
127         /* vApplicationMallocFailedHook() will only be called if\r
128         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
129         function that will get called if a call to pvPortMalloc() fails.\r
130         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
131         timer or semaphore is created.  It is also called by various parts of the\r
132         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
133         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
134         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
135         to query the size of free heap space that remains (although it does not\r
136         provide information on how the remaining heap might be fragmented). */\r
137         taskDISABLE_INTERRUPTS();\r
138         for( ;; );\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vApplicationIdleHook( void )\r
143 {\r
144         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
145         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
146         task.  It is essential that code added to this hook function never attempts\r
147         to block in any way (for example, call xQueueReceive() with a block time\r
148         specified, or call vTaskDelay()).  If the application makes use of the\r
149         vTaskDelete() API function (as this demo application does) then it is also\r
150         important that vApplicationIdleHook() is permitted to return to its calling\r
151         function, because it is the responsibility of the idle task to clean up\r
152         memory allocated by the kernel to any task that has since been deleted. */\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
157 {\r
158         ( void ) pcTaskName;\r
159         ( void ) pxTask;\r
160 \r
161         /* Run time task stack overflow checking is performed if\r
162         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook function is \r
163         called if a task stack overflow is detected.  Note the system/interrupt\r
164         stack is not checked. */\r
165         taskDISABLE_INTERRUPTS();\r
166         for( ;; );\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 void vApplicationTickHook( void )\r
171 {\r
172         /* This function will be called by each tick interrupt if\r
173         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
174         added here, but the tick hook is called from an interrupt context, so\r
175         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
176         functions can be used (those that end in FromISR()). */\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 void _general_exception_handler( unsigned long ulCause, unsigned long ulStatus )\r
181 {\r
182         /* This overrides the definition provided by the kernel.  Other exceptions \r
183         should be handled here. */\r
184         for( ;; );\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
189 {\r
190 volatile unsigned long ul = 0;\r
191 \r
192         ( void ) pcFile;\r
193         ( void ) ulLine;\r
194 \r
195         __asm volatile( "di" );\r
196         {\r
197                 /* Set ul to a non-zero value using the debugger to step out of this\r
198                 function. */\r
199                 while( ul == 0 )\r
200                 {\r
201                         portNOP();\r
202                 }\r
203         }\r
204         __asm volatile( "ei" );\r
205 }\r