]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MEC14xx_MPLAB/src/main.c
81b5f589fbd17ff688b27ced990674da92402bb8
[freertos] / FreeRTOS / Demo / PIC32MEC14xx_MPLAB / src / 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 #include "queue.h"\r
45 #include "timers.h"\r
46 \r
47 /* Target includes. */\r
48 #include "appcfg.h"\r
49 #include "MEC14xx/mec14xx.h"\r
50 #include "MEC14xx/mec14xx_jtvic.h"\r
51 #include "MEC14xx/mec14xx_bbled.h"\r
52 #include "MEC14xx/mec14xx_girqs.h"\r
53 \r
54 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
55 or 0 to run the more comprehensive test and demo application. */\r
56 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 /*\r
61  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
62  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
63  */\r
64 extern void main_blinky( void );\r
65 extern void main_full( void );\r
66 \r
67 /*\r
68  * Performs any hardware setup necessary.\r
69  */\r
70  static void __attribute__((nomips16)) prvSetupHardware( void );\r
71 \r
72 /*\r
73  * Add some thread safety to the LED toggle function.\r
74  */\r
75 void vToggleLED( uint8_t ucLED );\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 int main( void )\r
80 {\r
81         /* Perform any hardware initialisation necessary. */\r
82         prvSetupHardware();\r
83 \r
84         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
85         of this file. */\r
86         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
87         {\r
88                 main_blinky();\r
89         }\r
90         #else\r
91         {\r
92                 main_full();\r
93         }\r
94         #endif\r
95 \r
96     /* Should never be reached. */\r
97     return 0;\r
98 }\r
99 /*-----------------------------------------------------------*/\r
100 \r
101 void vToggleLED( uint8_t ucLED )\r
102 {\r
103         taskENTER_CRITICAL();\r
104         {\r
105                 led_out_toggle( ucLED );\r
106         }\r
107         taskEXIT_CRITICAL();\r
108 }\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 static void __attribute__((nomips16)) prvSetupHardware( void )\r
112 {\r
113 volatile uint32_t ulTemp;\r
114 \r
115         /* Interrupts are automatically re-enabled when the scheduler is started. */\r
116         __asm volatile( "di" );\r
117 \r
118         /* Enable M14K Vector Pre-fetch: CP0.IntCtl b[22]=1\r
119            IRET (interrupt chaining): b[21]=1\r
120            Enable Auto-Prolog: b[14]=1 */\r
121         ulTemp = _CP0_GET_INTCTL();\r
122         ulTemp |= ( 1ul << 22 ) + ( 1ul << 21 ) + ( 1ul << 14 );\r
123         _CP0_SET_INTCTL( ulTemp );\r
124 \r
125         /* Configure 32KHz for Switched Clock Source always ON\r
126            b[ 0 ] = XOSEL                     = 1\r
127            b[ 1 ] = EXT_32K_OSC_EN            = 1\r
128            b[ 2 ] = INT_32K_OSC_EN            = 1\r
129            b[ 3 ] = INT_32K_VTR_PWR_WELL_EMUL = 0\r
130            b[ 4 ] = 32K_SWITCHER_CTRL         = 0 */\r
131         VBAT_REGS->CLOCK_ENABLE = 0x07;\r
132 \r
133         ulTemp = 256;\r
134         while (ulTemp--)\r
135         {\r
136                 __asm volatile( "NOP" );\r
137                 __asm volatile( "NOP" );\r
138                 __asm volatile( "NOP" );\r
139                 __asm volatile( "NOP" );\r
140         }\r
141 \r
142         /* Disaggregate GIRQ23 & GIRQ24 for FreeRTOS.  Second parameter is a bit-map\r
143         for each GIRQ where\r
144           0 = Aggregated, 1 = Dis-aggregate\r
145           Bit position = GIRQ_Number - 8\r
146           Example: GIRQ23 ( 23 - 8 ) = 15\r
147           Dis-aggregate GIRQ23 & GIRQ24\r
148           The symbols JTVIC_DISAGR_BITMAP is generated in header file mec14xx_girqm.h\r
149 \r
150           Each disaggregated interrupt handler is spaced 8-bytes apart starting at\r
151           base address for that GIRQ. */\r
152         jtvic_init( dflt_ih_table, ( JTVIC_DISAGR_BITMAP ), ( JTVIC_FLAG_DISAGR_SPACING_8 ) );\r
153 \r
154         /* Initialise the LEDs. */\r
155         for( ulTemp = 0; ulTemp < LED_ID_MAX; ulTemp++ )\r
156         {\r
157                 led_sleep_en( ulTemp, ADISABLE );\r
158                 led_init( ulTemp );\r
159                 led_out_high( ulTemp );\r
160         }\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 void vApplicationMallocFailedHook( void )\r
165 {\r
166         /* vApplicationMallocFailedHook() will only be called if\r
167         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
168         function that will get called if a call to pvPortMalloc() fails.\r
169         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
170         timer or semaphore is created.  It is also called by various parts of the\r
171         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
172         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
173         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
174         to query the size of free heap space that remains (although it does not\r
175         provide information on how the remaining heap might be fragmented). */\r
176         taskDISABLE_INTERRUPTS();\r
177         for( ;; );\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void vApplicationIdleHook( void )\r
182 {\r
183         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
184         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
185         task.  It is essential that code added to this hook function never attempts\r
186         to block in any way (for example, call xQueueReceive() with a block time\r
187         specified, or call vTaskDelay()).  If the application makes use of the\r
188         vTaskDelete() API function (as this demo application does) then it is also\r
189         important that vApplicationIdleHook() is permitted to return to its calling\r
190         function, because it is the responsibility of the idle task to clean up\r
191         memory allocated by the kernel to any task that has since been deleted. */\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
196 {\r
197         ( void ) pcTaskName;\r
198         ( void ) pxTask;\r
199 \r
200         /* Run time task stack overflow checking is performed if\r
201         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook function is\r
202         called if a task stack overflow is detected.  Note the system/interrupt\r
203         stack is not checked. */\r
204         taskDISABLE_INTERRUPTS();\r
205         for( ;; );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void vApplicationTickHook( void )\r
210 {\r
211         /* This function will be called by each tick interrupt if\r
212         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
213         added here, but the tick hook is called from an interrupt context, so\r
214         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
215         functions can be used (those that end in FromISR()). */\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
220 {\r
221 volatile char *pcFileName;\r
222 volatile unsigned long ulLineNumber;\r
223 \r
224         /* Prevent things that are useful to view in the debugger from being\r
225         optimised away. */\r
226         pcFileName = ( char * ) pcFile;\r
227         ( void ) pcFileName;\r
228         ulLineNumber = ulLine;\r
229 \r
230         /* Set ulLineNumber to 0 in the debugger to break out of this loop and\r
231         return to the line that triggered the assert. */\r
232         while( ulLineNumber != 0 )\r
233         {\r
234                 __asm volatile( "NOP" );\r
235                 __asm volatile( "NOP" );\r
236                 __asm volatile( "NOP" );\r
237                 __asm volatile( "NOP" );\r
238                 __asm volatile( "NOP" );\r
239         }\r
240 }\r
241 \r