]> begriffs open source - cmsis-freertos/blob - Demo/PIC32MEC14xx_MPLAB/src/main.c
This is a FreeRTOS header, not RTX.
[cmsis-freertos] / Demo / PIC32MEC14xx_MPLAB / src / main.c
1 /*
2  * FreeRTOS Kernel V10.0.1
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /******************************************************************************
29  * This project provides two demo applications.  A simple blinky style project,
30  * and a more comprehensive test and demo application.  The
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
32  * select between the two.  The simply blinky demo is implemented and described
33  * in main_blinky.c.  The more comprehensive test and demo application is
34  * implemented and described in main_full.c.
35  *
36  * This file implements the code that is not demo specific, including the
37  * hardware setup and FreeRTOS hook functions.
38  *****************************************************************************/
39
40 /* Kernel includes. */
41 #include "FreeRTOS.h"
42 #include "task.h"
43 #include "queue.h"
44 #include "timers.h"
45
46 /* Target includes. */
47 #include "appcfg.h"
48 #include "MEC14xx/mec14xx.h"
49 #include "MEC14xx/mec14xx_jtvic.h"
50 #include "MEC14xx/mec14xx_bbled.h"
51 #include "MEC14xx/mec14xx_girqs.h"
52
53 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
54 or 0 to run the more comprehensive test and demo application. */
55 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0
56
57 /*-----------------------------------------------------------*/
58
59 /*
60  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
61  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
62  */
63 extern void main_blinky( void );
64 extern void main_full( void );
65
66 /*
67  * Performs any hardware setup necessary.
68  */
69  static void __attribute__((nomips16)) prvSetupHardware( void );
70
71 /*
72  * Add some thread safety to the LED toggle function.
73  */
74 void vToggleLED( uint8_t ucLED );
75
76 /*-----------------------------------------------------------*/
77
78 int main( void )
79 {
80         /* Perform any hardware initialisation necessary. */
81         prvSetupHardware();
82
83         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
84         of this file. */
85         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1
86         {
87                 main_blinky();
88         }
89         #else
90         {
91                 main_full();
92         }
93         #endif
94
95     /* Should never be reached. */
96     return 0;
97 }
98 /*-----------------------------------------------------------*/
99
100 void vToggleLED( uint8_t ucLED )
101 {
102         taskENTER_CRITICAL();
103         {
104                 led_out_toggle( ucLED );
105         }
106         taskEXIT_CRITICAL();
107 }
108 /*-----------------------------------------------------------*/
109
110 static void __attribute__((nomips16)) prvSetupHardware( void )
111 {
112 volatile uint32_t ulTemp;
113
114         /* Interrupts are automatically re-enabled when the scheduler is started. */
115         __asm volatile( "di" );
116
117         /* Enable M14K Vector Pre-fetch: CP0.IntCtl b[22]=1
118            IRET (interrupt chaining): b[21]=1
119            Enable Auto-Prolog: b[14]=1 */
120         ulTemp = _CP0_GET_INTCTL();
121         ulTemp |= ( 1ul << 22 ) + ( 1ul << 21 ) + ( 1ul << 14 );
122         _CP0_SET_INTCTL( ulTemp );
123
124         /* Configure 32KHz for Switched Clock Source always ON
125            b[ 0 ] = XOSEL                     = 1
126            b[ 1 ] = EXT_32K_OSC_EN            = 1
127            b[ 2 ] = INT_32K_OSC_EN            = 1
128            b[ 3 ] = INT_32K_VTR_PWR_WELL_EMUL = 0
129            b[ 4 ] = 32K_SWITCHER_CTRL         = 0 */
130         VBAT_REGS->CLOCK_ENABLE = 0x07;
131
132         ulTemp = 256;
133         while (ulTemp--)
134         {
135                 __asm volatile( "NOP" );
136                 __asm volatile( "NOP" );
137                 __asm volatile( "NOP" );
138                 __asm volatile( "NOP" );
139         }
140
141         /* Disaggregate GIRQ23 & GIRQ24 for FreeRTOS.  Second parameter is a bit-map
142         for each GIRQ where
143           0 = Aggregated, 1 = Dis-aggregate
144           Bit position = GIRQ_Number - 8
145           Example: GIRQ23 ( 23 - 8 ) = 15
146           Dis-aggregate GIRQ23 & GIRQ24
147           The symbols JTVIC_DISAGR_BITMAP is generated in header file mec14xx_girqm.h
148
149           Each disaggregated interrupt handler is spaced 8-bytes apart starting at
150           base address for that GIRQ. */
151         jtvic_init( dflt_ih_table, ( JTVIC_DISAGR_BITMAP ), ( JTVIC_FLAG_DISAGR_SPACING_8 ) );
152
153         /* Initialise the LEDs. */
154         for( ulTemp = 0; ulTemp < LED_ID_MAX; ulTemp++ )
155         {
156                 led_sleep_en( ulTemp, ADISABLE );
157                 led_init( ulTemp );
158                 led_out_high( ulTemp );
159         }
160 }
161 /*-----------------------------------------------------------*/
162
163 void vApplicationMallocFailedHook( void )
164 {
165         /* vApplicationMallocFailedHook() will only be called if
166         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
167         function that will get called if a call to pvPortMalloc() fails.
168         pvPortMalloc() is called internally by the kernel whenever a task, queue,
169         timer or semaphore is created.  It is also called by various parts of the
170         demo application.  If heap_1.c or heap_2.c are used, then the size of the
171         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
172         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
173         to query the size of free heap space that remains (although it does not
174         provide information on how the remaining heap might be fragmented). */
175         taskDISABLE_INTERRUPTS();
176         for( ;; );
177 }
178 /*-----------------------------------------------------------*/
179
180 void vApplicationIdleHook( void )
181 {
182         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
183         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
184         task.  It is essential that code added to this hook function never attempts
185         to block in any way (for example, call xQueueReceive() with a block time
186         specified, or call vTaskDelay()).  If the application makes use of the
187         vTaskDelete() API function (as this demo application does) then it is also
188         important that vApplicationIdleHook() is permitted to return to its calling
189         function, because it is the responsibility of the idle task to clean up
190         memory allocated by the kernel to any task that has since been deleted. */
191 }
192 /*-----------------------------------------------------------*/
193
194 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
195 {
196         ( void ) pcTaskName;
197         ( void ) pxTask;
198
199         /* Run time task stack overflow checking is performed if
200         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook function is
201         called if a task stack overflow is detected.  Note the system/interrupt
202         stack is not checked. */
203         taskDISABLE_INTERRUPTS();
204         for( ;; );
205 }
206 /*-----------------------------------------------------------*/
207
208 void vApplicationTickHook( void )
209 {
210         /* This function will be called by each tick interrupt if
211         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
212         added here, but the tick hook is called from an interrupt context, so
213         code must not attempt to block, and only the interrupt safe FreeRTOS API
214         functions can be used (those that end in FromISR()). */
215 }
216 /*-----------------------------------------------------------*/
217
218 void vAssertCalled( const char * pcFile, unsigned long ulLine )
219 {
220 volatile char *pcFileName;
221 volatile unsigned long ulLineNumber;
222
223         /* Prevent things that are useful to view in the debugger from being
224         optimised away. */
225         pcFileName = ( char * ) pcFile;
226         ( void ) pcFileName;
227         ulLineNumber = ulLine;
228
229         /* Set ulLineNumber to 0 in the debugger to break out of this loop and
230         return to the line that triggered the assert. */
231         while( ulLineNumber != 0 )
232         {
233                 __asm volatile( "NOP" );
234                 __asm volatile( "NOP" );
235                 __asm volatile( "NOP" );
236                 __asm volatile( "NOP" );
237                 __asm volatile( "NOP" );
238         }
239 }
240