2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
71 * This file implements functions to access and manipulate the PIC32 hardware
\r
72 * without reliance on third party library functions that may be liable to
\r
76 /* FreeRTOS includes. */
\r
77 #include "FreeRTOS.h"
\r
79 /* Demo includes. */
\r
80 #include "ConfigPerformance.h"
\r
82 /* Hardware specific definitions. */
\r
83 #define hwCHECON_PREFEN_BITS ( 0x03UL << 0x04UL )
\r
84 #define hwCHECON_WAIT_STAT_BITS ( 0x07UL << 0UL )
\r
85 #define hwMAX_FLASH_SPEED ( 30000000UL )
\r
86 #define hwPERIPHERAL_CLOCK_DIV_BY_2 ( 1UL << 0x13UL )
\r
87 #define hwUNLOCK_KEY_0 ( 0xAA996655UL )
\r
88 #define hwUNLOCK_KEY_1 ( 0x556699AAUL )
\r
89 #define hwLOCK_KEY ( 0x33333333UL )
\r
90 #define hwGLOBAL_INTERRUPT_BIT ( 0x01UL )
\r
91 #define hwBEV_BIT ( 0x00400000 )
\r
92 #define hwEXL_BIT ( 0x00000002 )
\r
93 #define hwIV_BIT ( 0x00800000 )
\r
96 * Set the flash wait states for the configured CPU clock speed.
\r
98 static void prvConfigureWaitStates( void );
\r
101 * Use a divisor of 2 on the peripheral bus.
\r
103 static void prvConfigurePeripheralBus( void );
\r
106 * Enable the cache.
\r
108 static void __attribute__ ((nomips16)) prvKSeg0CacheOn( void );
\r
110 /*-----------------------------------------------------------*/
\r
112 void vHardwareConfigurePerformance( void )
\r
114 unsigned long ulStatus;
\r
116 unsigned long ulCacheStatus;
\r
119 /* Disable interrupts - note taskDISABLE_INTERRUPTS() cannot be used here as
\r
120 FreeRTOS does not globally disable interrupt. */
\r
121 ulStatus = _CP0_GET_STATUS();
\r
122 _CP0_SET_STATUS( ulStatus & ~hwGLOBAL_INTERRUPT_BIT );
\r
124 prvConfigurePeripheralBus();
\r
125 prvConfigureWaitStates();
\r
127 /* Disable DRM wait state. */
\r
128 BMXCONCLR = _BMXCON_BMXWSDRM_MASK;
\r
132 /* Read the current CHECON value. */
\r
133 ulCacheStatus = CHECON;
\r
135 /* All the PREFEN bits are being set, so no need to clear first. */
\r
136 ulCacheStatus |= hwCHECON_PREFEN_BITS;
\r
138 /* Write back the new value. */
\r
139 CHECON = ulCacheStatus;
\r
144 /* Reset the status register back to its original value so the original
\r
145 interrupt enable status is retored. */
\r
146 _CP0_SET_STATUS( ulStatus );
\r
148 /*-----------------------------------------------------------*/
\r
150 void vHardwareUseMultiVectoredInterrupts( void )
\r
152 unsigned long ulStatus, ulCause;
\r
153 extern unsigned long _ebase_address[];
\r
155 /* Get current status. */
\r
156 ulStatus = _CP0_GET_STATUS();
\r
158 /* Disable interrupts. */
\r
159 ulStatus &= ~hwGLOBAL_INTERRUPT_BIT;
\r
162 ulStatus |= hwBEV_BIT;
\r
164 /* Write status back. */
\r
165 _CP0_SET_STATUS( ulStatus );
\r
168 _CP0_SET_EBASE( ( unsigned long ) _ebase_address );
\r
170 /* Space vectors by 0x20 bytes. */
\r
171 _CP0_XCH_INTCTL( 0x20 );
\r
173 /* Set the IV bit in the CAUSE register. */
\r
174 ulCause = _CP0_GET_CAUSE();
\r
175 ulCause |= hwIV_BIT;
\r
176 _CP0_SET_CAUSE( ulCause );
\r
178 /* Clear BEV and EXL bits in status. */
\r
179 ulStatus &= ~( hwBEV_BIT | hwEXL_BIT );
\r
180 _CP0_SET_STATUS( ulStatus );
\r
182 /* Set MVEC bit. */
\r
183 INTCONbits.MVEC = 1;
\r
185 /* Finally enable interrupts again. */
\r
186 ulStatus |= hwGLOBAL_INTERRUPT_BIT;
\r
187 _CP0_SET_STATUS( ulStatus );
\r
189 /*-----------------------------------------------------------*/
\r
191 static void prvConfigurePeripheralBus( void )
\r
193 unsigned long ulDMAStatus;
\r
194 __OSCCONbits_t xOSCCONBits;
\r
196 /* Unlock after suspending. */
\r
197 ulDMAStatus = DMACONbits.SUSPEND;
\r
198 if( ulDMAStatus == 0 )
\r
200 DMACONSET = _DMACON_SUSPEND_MASK;
\r
202 /* Wait until actually suspended. */
\r
203 while( DMACONbits.SUSPEND == 0 );
\r
207 SYSKEY = hwUNLOCK_KEY_0;
\r
208 SYSKEY = hwUNLOCK_KEY_1;
\r
210 /* Read to start in sync. */
\r
211 xOSCCONBits.w = OSCCON;
\r
212 xOSCCONBits.PBDIV = 0;
\r
213 xOSCCONBits.w |= hwPERIPHERAL_CLOCK_DIV_BY_2;
\r
216 OSCCON = xOSCCONBits.w;
\r
218 /* Ensure the write occurred. */
\r
219 xOSCCONBits.w = OSCCON;
\r
222 SYSKEY = hwLOCK_KEY;
\r
224 /* Resume DMA activity. */
\r
225 if( ulDMAStatus == 0 )
\r
227 DMACONCLR=_DMACON_SUSPEND_MASK;
\r
230 /*-----------------------------------------------------------*/
\r
232 static void prvConfigureWaitStates( void )
\r
234 unsigned long ulSystemClock = configCPU_CLOCK_HZ - 1;
\r
235 unsigned long ulWaitStates, ulCHECONVal;
\r
237 /* 1 wait state for every hwMAX_FLASH_SPEED MHz. */
\r
240 while( ulSystemClock > hwMAX_FLASH_SPEED )
\r
243 ulSystemClock -= hwMAX_FLASH_SPEED;
\r
246 /* Obtain current CHECON value. */
\r
247 ulCHECONVal = CHECON;
\r
249 /* Clear the wait state bits, then set the calculated wait state bits. */
\r
250 ulCHECONVal &= ~hwCHECON_WAIT_STAT_BITS;
\r
251 ulCHECONVal |= ulWaitStates;
\r
253 /* Write back the new value. */
\r
254 CHECON = ulWaitStates;
\r
256 /*-----------------------------------------------------------*/
\r
258 static void __attribute__ ((nomips16)) prvKSeg0CacheOn( void )
\r
260 unsigned long ulValue;
\r
262 __asm volatile( "mfc0 %0, $16, 0" : "=r"( ulValue ) );
\r
263 ulValue = ( ulValue & ~0x07) | 0x03;
\r
264 __asm volatile( "mtc0 %0, $16, 0" :: "r" ( ulValue ) );
\r