]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/ConfigPerformance.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / ConfigPerformance.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 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
73  * change.\r
74  */\r
75 \r
76 /* FreeRTOS includes. */\r
77 #include "FreeRTOS.h"\r
78 \r
79 /* Demo includes. */\r
80 #include "ConfigPerformance.h"\r
81 \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
94 \r
95 /*\r
96  * Set the flash wait states for the configured CPU clock speed.\r
97  */\r
98 static void prvConfigureWaitStates( void );\r
99 \r
100 /*\r
101  * Use a divisor of 2 on the peripheral bus.\r
102  */\r
103 static void prvConfigurePeripheralBus( void );\r
104 \r
105 /*\r
106  * Enable the cache.\r
107  */\r
108 static void __attribute__ ((nomips16)) prvKSeg0CacheOn( void );\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 void vHardwareConfigurePerformance( void )\r
113 {\r
114 unsigned long ulStatus;\r
115 #ifdef _PCACHE\r
116         unsigned long ulCacheStatus;\r
117 #endif\r
118 \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
123 \r
124         prvConfigurePeripheralBus();\r
125         prvConfigureWaitStates();\r
126 \r
127         /* Disable DRM wait state. */\r
128         BMXCONCLR = _BMXCON_BMXWSDRM_MASK;\r
129 \r
130         #ifdef _PCACHE\r
131         {\r
132                 /* Read the current CHECON value. */\r
133                 ulCacheStatus = CHECON;\r
134 \r
135                 /* All the PREFEN bits are being set, so no need to clear first. */\r
136                 ulCacheStatus |= hwCHECON_PREFEN_BITS;\r
137 \r
138                 /* Write back the new value. */\r
139                 CHECON = ulCacheStatus;\r
140                 prvKSeg0CacheOn();\r
141         }\r
142         #endif\r
143 \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
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 void vHardwareUseMultiVectoredInterrupts( void )\r
151 {\r
152 unsigned long ulStatus, ulCause;\r
153 extern unsigned long _ebase_address[];\r
154 \r
155         /* Get current status. */\r
156         ulStatus = _CP0_GET_STATUS();\r
157 \r
158         /* Disable interrupts. */\r
159         ulStatus &= ~hwGLOBAL_INTERRUPT_BIT;\r
160 \r
161         /* Set BEV bit. */\r
162         ulStatus |= hwBEV_BIT;\r
163 \r
164         /* Write status back. */\r
165         _CP0_SET_STATUS( ulStatus );\r
166 \r
167         /* Setup EBase. */\r
168         _CP0_SET_EBASE( ( unsigned long ) _ebase_address );\r
169         \r
170         /* Space vectors by 0x20 bytes. */\r
171         _CP0_XCH_INTCTL( 0x20 );\r
172 \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
177 \r
178         /* Clear BEV and EXL bits in status. */\r
179         ulStatus &= ~( hwBEV_BIT | hwEXL_BIT );\r
180         _CP0_SET_STATUS( ulStatus );\r
181 \r
182         /* Set MVEC bit. */\r
183         INTCONbits.MVEC = 1;\r
184         \r
185         /* Finally enable interrupts again. */\r
186         ulStatus |= hwGLOBAL_INTERRUPT_BIT;\r
187         _CP0_SET_STATUS( ulStatus );\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvConfigurePeripheralBus( void )\r
192 {\r
193 unsigned long ulDMAStatus;\r
194 __OSCCONbits_t xOSCCONBits;\r
195 \r
196         /* Unlock after suspending. */\r
197         ulDMAStatus = DMACONbits.SUSPEND;\r
198         if( ulDMAStatus == 0 )\r
199         {\r
200                 DMACONSET = _DMACON_SUSPEND_MASK;\r
201 \r
202                 /* Wait until actually suspended. */\r
203                 while( DMACONbits.SUSPEND == 0 );\r
204         }\r
205 \r
206         SYSKEY = 0;\r
207         SYSKEY = hwUNLOCK_KEY_0;\r
208         SYSKEY = hwUNLOCK_KEY_1;\r
209 \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
214 \r
215         /* Write back. */\r
216         OSCCON = xOSCCONBits.w;\r
217 \r
218         /* Ensure the write occurred. */\r
219         xOSCCONBits.w = OSCCON;\r
220 \r
221         /* Lock again. */\r
222         SYSKEY = hwLOCK_KEY;\r
223 \r
224         /* Resume DMA activity. */\r
225         if( ulDMAStatus == 0 )\r
226         {\r
227                 DMACONCLR=_DMACON_SUSPEND_MASK;\r
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void prvConfigureWaitStates( void )\r
233 {\r
234 unsigned long ulSystemClock = configCPU_CLOCK_HZ - 1;\r
235 unsigned long ulWaitStates, ulCHECONVal;\r
236 \r
237         /* 1 wait state for every hwMAX_FLASH_SPEED MHz. */\r
238         ulWaitStates = 0;\r
239 \r
240         while( ulSystemClock > hwMAX_FLASH_SPEED )\r
241         {\r
242                 ulWaitStates++;\r
243                 ulSystemClock -= hwMAX_FLASH_SPEED;\r
244         }\r
245 \r
246         /* Obtain current CHECON value. */\r
247         ulCHECONVal = CHECON;\r
248 \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
252 \r
253         /* Write back the new value. */\r
254         CHECON = ulWaitStates;\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void __attribute__ ((nomips16)) prvKSeg0CacheOn( void )\r
259 {\r
260 unsigned long ulValue;\r
261 \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
265 }\r
266 \r
267 \r