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