]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MEC14xx_MPLAB/src/main.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / PIC32MEC14xx_MPLAB / src / main.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * This project provides two demo applications.  A simple blinky style project,\r
72  * and a more comprehensive test and demo application.  The\r
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
74  * select between the two.  The simply blinky demo is implemented and described\r
75  * in main_blinky.c.  The more comprehensive test and demo application is\r
76  * implemented and described in main_full.c.\r
77  *\r
78  * This file implements the code that is not demo specific, including the\r
79  * hardware setup and FreeRTOS hook functions.\r
80  *****************************************************************************/\r
81 \r
82 /* Kernel includes. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 #include "queue.h"\r
86 #include "timers.h"\r
87 \r
88 /* Target includes. */\r
89 #include "appcfg.h"\r
90 #include "MEC14xx/mec14xx.h"\r
91 #include "MEC14xx/mec14xx_jtvic.h"\r
92 #include "MEC14xx/mec14xx_bbled.h"\r
93 #include "MEC14xx/mec14xx_girqs.h"\r
94 \r
95 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
96 or 0 to run the more comprehensive test and demo application. */\r
97 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /*\r
102  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
103  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
104  */\r
105 extern void main_blinky( void );\r
106 extern void main_full( void );\r
107 \r
108 /*\r
109  * Performs any hardware setup necessary.\r
110  */\r
111  static void __attribute__((nomips16)) prvSetupHardware( void );\r
112 \r
113 /*\r
114  * Add some thread safety to the LED toggle function.\r
115  */\r
116 void vToggleLED( uint8_t ucLED );\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 int main( void )\r
121 {\r
122         /* Perform any hardware initialisation necessary. */\r
123         prvSetupHardware();\r
124 \r
125         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
126         of this file. */\r
127         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
128         {\r
129                 main_blinky();\r
130         }\r
131         #else\r
132         {\r
133                 main_full();\r
134         }\r
135         #endif\r
136 \r
137     /* Should never be reached. */\r
138     return 0;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vToggleLED( uint8_t ucLED )\r
143 {\r
144         taskENTER_CRITICAL();\r
145         {\r
146                 led_out_toggle( ucLED );\r
147         }\r
148         taskEXIT_CRITICAL();\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 static void __attribute__((nomips16)) prvSetupHardware( void )\r
153 {\r
154 volatile uint32_t ulTemp;\r
155 \r
156         /* Interrupts are automatically re-enabled when the scheduler is started. */\r
157         __asm volatile( "di" );\r
158 \r
159         /* Enable M14K Vector Pre-fetch: CP0.IntCtl b[22]=1\r
160            IRET (interrupt chaining): b[21]=1\r
161            Enable Auto-Prolog: b[14]=1 */\r
162         ulTemp = _CP0_GET_INTCTL();\r
163         ulTemp |= ( 1ul << 22 ) + ( 1ul << 21 ) + ( 1ul << 14 );\r
164         _CP0_SET_INTCTL( ulTemp );\r
165 \r
166         /* Configure 32KHz for Switched Clock Source always ON\r
167            b[ 0 ] = XOSEL                     = 1\r
168            b[ 1 ] = EXT_32K_OSC_EN            = 1\r
169            b[ 2 ] = INT_32K_OSC_EN            = 1\r
170            b[ 3 ] = INT_32K_VTR_PWR_WELL_EMUL = 0\r
171            b[ 4 ] = 32K_SWITCHER_CTRL         = 0 */\r
172         VBAT_REGS->CLOCK_ENABLE = 0x07;\r
173 \r
174         ulTemp = 256;\r
175         while (ulTemp--)\r
176         {\r
177                 __asm volatile( "NOP" );\r
178                 __asm volatile( "NOP" );\r
179                 __asm volatile( "NOP" );\r
180                 __asm volatile( "NOP" );\r
181         }\r
182 \r
183         /* Disaggregate GIRQ23 & GIRQ24 for FreeRTOS.  Second parameter is a bit-map\r
184         for each GIRQ where\r
185           0 = Aggregated, 1 = Dis-aggregate\r
186           Bit position = GIRQ_Number - 8\r
187           Example: GIRQ23 ( 23 - 8 ) = 15\r
188           Dis-aggregate GIRQ23 & GIRQ24\r
189           The symbols JTVIC_DISAGR_BITMAP is generated in header file mec14xx_girqm.h\r
190 \r
191           Each disaggregated interrupt handler is spaced 8-bytes apart starting at\r
192           base address for that GIRQ. */\r
193         jtvic_init( dflt_ih_table, ( JTVIC_DISAGR_BITMAP ), ( JTVIC_FLAG_DISAGR_SPACING_8 ) );\r
194 \r
195         /* Initialise the LEDs. */\r
196         for( ulTemp = 0; ulTemp < LED_ID_MAX; ulTemp++ )\r
197         {\r
198                 led_sleep_en( ulTemp, ADISABLE );\r
199                 led_init( ulTemp );\r
200                 led_out_high( ulTemp );\r
201         }\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 void vApplicationMallocFailedHook( void )\r
206 {\r
207         /* vApplicationMallocFailedHook() will only be called if\r
208         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
209         function that will get called if a call to pvPortMalloc() fails.\r
210         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
211         timer or semaphore is created.  It is also called by various parts of the\r
212         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
213         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
214         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
215         to query the size of free heap space that remains (although it does not\r
216         provide information on how the remaining heap might be fragmented). */\r
217         taskDISABLE_INTERRUPTS();\r
218         for( ;; );\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vApplicationIdleHook( void )\r
223 {\r
224         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
225         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
226         task.  It is essential that code added to this hook function never attempts\r
227         to block in any way (for example, call xQueueReceive() with a block time\r
228         specified, or call vTaskDelay()).  If the application makes use of the\r
229         vTaskDelete() API function (as this demo application does) then it is also\r
230         important that vApplicationIdleHook() is permitted to return to its calling\r
231         function, because it is the responsibility of the idle task to clean up\r
232         memory allocated by the kernel to any task that has since been deleted. */\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
237 {\r
238         ( void ) pcTaskName;\r
239         ( void ) pxTask;\r
240 \r
241         /* Run time task stack overflow checking is performed if\r
242         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook function is\r
243         called if a task stack overflow is detected.  Note the system/interrupt\r
244         stack is not checked. */\r
245         taskDISABLE_INTERRUPTS();\r
246         for( ;; );\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 void vApplicationTickHook( void )\r
251 {\r
252         /* This function will be called by each tick interrupt if\r
253         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
254         added here, but the tick hook is called from an interrupt context, so\r
255         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
256         functions can be used (those that end in FromISR()). */\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
261 {\r
262 volatile char *pcFileName;\r
263 volatile unsigned long ulLineNumber;\r
264 \r
265         /* Prevent things that are useful to view in the debugger from being\r
266         optimised away. */\r
267         pcFileName = ( char * ) pcFile;\r
268         ( void ) pcFileName;\r
269         ulLineNumber = ulLine;\r
270 \r
271         /* Set ulLineNumber to 0 in the debugger to break out of this loop and\r
272         return to the line that triggered the assert. */\r
273         while( ulLineNumber != 0 )\r
274         {\r
275                 __asm volatile( "NOP" );\r
276                 __asm volatile( "NOP" );\r
277                 __asm volatile( "NOP" );\r
278                 __asm volatile( "NOP" );\r
279                 __asm volatile( "NOP" );\r
280         }\r
281 }\r
282 \r