]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/ConfigPerformance.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / ConfigPerformance.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * This file implements functions to access and manipulate the PIC32 hardware\r
77  * without reliance on third party library functions that may be liable to\r
78  * change.\r
79  */\r
80 \r
81 /* FreeRTOS includes. */\r
82 #include "FreeRTOS.h"\r
83 \r
84 /* Demo includes. */\r
85 #include "ConfigPerformance.h"\r
86 \r
87 /* Hardware specific definitions. */\r
88 #define hwCHECON_PREFEN_BITS                    ( 0x03UL << 0x04UL )\r
89 #define hwCHECON_WAIT_STAT_BITS                 ( 0x07UL << 0UL )\r
90 #define hwMAX_FLASH_SPEED                               ( 30000000UL )\r
91 #define hwPERIPHERAL_CLOCK_DIV_BY_2             ( 1UL << 0x13UL )\r
92 #define hwUNLOCK_KEY_0                                  ( 0xAA996655UL )\r
93 #define hwUNLOCK_KEY_1                                  ( 0x556699AAUL )\r
94 #define hwLOCK_KEY                                              ( 0x33333333UL )\r
95 #define hwGLOBAL_INTERRUPT_BIT                  ( 0x01UL )\r
96 #define hwBEV_BIT                                               ( 0x00400000 )\r
97 #define hwEXL_BIT                                               ( 0x00000002 )\r
98 #define hwIV_BIT                                                ( 0x00800000 )\r
99 \r
100 /*\r
101  * Set the flash wait states for the configured CPU clock speed.\r
102  */\r
103 static void prvConfigureWaitStates( void );\r
104 \r
105 /*\r
106  * Use a divisor of 2 on the peripheral bus.\r
107  */\r
108 static void prvConfigurePeripheralBus( void );\r
109 \r
110 /*\r
111  * Enable the cache.\r
112  */\r
113 static void __attribute__ ((nomips16)) prvKSeg0CacheOn( void );\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 void vHardwareConfigurePerformance( void )\r
118 {\r
119 unsigned long ulStatus;\r
120 #ifdef _PCACHE\r
121         unsigned long ulCacheStatus;\r
122 #endif\r
123 \r
124         /* Disable interrupts - not taskDISABLE_INTERRUPTS() cannot be used here as\r
125         FreeRTOS does not globally disable interrupt. */\r
126         ulStatus = _CP0_GET_STATUS();\r
127         _CP0_SET_STATUS( ulStatus & ~hwGLOBAL_INTERRUPT_BIT );\r
128 \r
129         prvConfigurePeripheralBus();\r
130         prvConfigureWaitStates();\r
131 \r
132         /* Disable DRM wait state. */\r
133         BMXCONCLR = _BMXCON_BMXWSDRM_MASK;\r
134 \r
135         #ifdef _PCACHE\r
136         {\r
137                 /* Read the current CHECON value. */\r
138                 ulCacheStatus = CHECON;\r
139 \r
140                 /* All the PREFEN bits are being set, so no need to clear first. */\r
141                 ulCacheStatus |= hwCHECON_PREFEN_BITS;\r
142 \r
143                 /* Write back the new value. */\r
144                 CHECON = ulCacheStatus;\r
145                 prvKSeg0CacheOn();\r
146         }\r
147         #endif\r
148 \r
149         /* Reset the status register back to its original value so the original\r
150         interrupt enable status is retored. */\r
151         _CP0_SET_STATUS( ulStatus );\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 void vHardwareUseMultiVectoredInterrupts( void )\r
156 {\r
157 unsigned long ulStatus, ulCause;\r
158 extern unsigned long _ebase_address[];\r
159 \r
160         /* Get current status. */\r
161         ulStatus = _CP0_GET_STATUS();\r
162 \r
163         /* Disable interrupts. */\r
164         ulStatus &= ~hwGLOBAL_INTERRUPT_BIT;\r
165 \r
166         /* Set BEV bit. */\r
167         ulStatus |= hwBEV_BIT;\r
168 \r
169         /* Write status back. */\r
170         _CP0_SET_STATUS( ulStatus );\r
171 \r
172         /* Setup EBase. */\r
173         _CP0_SET_EBASE( ( unsigned long ) _ebase_address );\r
174         \r
175         /* Space vectors by 0x20 bytes. */\r
176         _CP0_XCH_INTCTL( 0x20 );\r
177 \r
178         /* Set the IV bit in the CAUSE register. */\r
179         ulCause = _CP0_GET_CAUSE();\r
180         ulCause |= hwIV_BIT;\r
181         _CP0_SET_CAUSE( ulCause );\r
182 \r
183         /* Clear BEV and EXL bits in status. */\r
184         ulStatus &= ~( hwBEV_BIT | hwEXL_BIT );\r
185         _CP0_SET_STATUS( ulStatus );\r
186 \r
187         /* Set MVEC bit. */\r
188         INTCONbits.MVEC = 1;\r
189         \r
190         /* Finally enable interrupts again. */\r
191         ulStatus |= hwGLOBAL_INTERRUPT_BIT;\r
192         _CP0_SET_STATUS( ulStatus );\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void prvConfigurePeripheralBus( void )\r
197 {\r
198 unsigned long ulDMAStatus;\r
199 __OSCCONbits_t xOSCCONBits;\r
200 \r
201         /* Unlock after suspending. */\r
202         ulDMAStatus = DMACONbits.SUSPEND;\r
203         if( ulDMAStatus == 0 )\r
204         {\r
205                 DMACONSET = _DMACON_SUSPEND_MASK;\r
206 \r
207                 /* Wait until actually suspended. */\r
208                 while( DMACONbits.SUSPEND == 0 );\r
209         }\r
210 \r
211         SYSKEY = 0;\r
212         SYSKEY = hwUNLOCK_KEY_0;\r
213         SYSKEY = hwUNLOCK_KEY_1;\r
214 \r
215         /* Read to start in sync. */\r
216         xOSCCONBits.w = OSCCON;\r
217         xOSCCONBits.PBDIV = 0;\r
218         xOSCCONBits.w |= hwPERIPHERAL_CLOCK_DIV_BY_2;\r
219 \r
220         /* Write back. */\r
221         OSCCON = xOSCCONBits.w;\r
222 \r
223         /* Ensure the write occurred. */\r
224         xOSCCONBits.w = OSCCON;\r
225 \r
226         /* Lock again. */\r
227         SYSKEY = hwLOCK_KEY;\r
228 \r
229         /* Resume DMA activity. */\r
230         if( ulDMAStatus == 0 )\r
231         {\r
232                 DMACONCLR=_DMACON_SUSPEND_MASK;\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvConfigureWaitStates( void )\r
238 {\r
239 unsigned long ulInterruptStatus, ulSystemClock = configCPU_CLOCK_HZ - 1;\r
240 unsigned long ulWaitStates, ulCHECONVal;\r
241 \r
242         /* 1 wait state for every hwMAX_FLASH_SPEED MHz. */\r
243         ulWaitStates = 0;\r
244 \r
245         while( ulSystemClock > hwMAX_FLASH_SPEED )\r
246         {\r
247                 ulWaitStates++;\r
248                 ulSystemClock -= hwMAX_FLASH_SPEED;\r
249         }\r
250 \r
251         /* Obtain current CHECON value. */\r
252         ulCHECONVal = CHECON;\r
253 \r
254         /* Clear the wait state bits, then set the calculated wait state bits. */\r
255         ulCHECONVal &= ~hwCHECON_WAIT_STAT_BITS;\r
256         ulCHECONVal |= ulWaitStates;\r
257 \r
258         /* Write back the new value. */\r
259         CHECON = ulWaitStates;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 static void __attribute__ ((nomips16)) prvKSeg0CacheOn( void )\r
264 {\r
265 unsigned long ulValue;\r
266 \r
267         __asm volatile( "mfc0 %0, $16, 0" :  "=r"( ulValue ) );\r
268         ulValue = ( ulValue & ~0x07) | 0x03;\r
269         __asm volatile( "mtc0 %0, $16, 0" :: "r" ( ulValue ) );\r
270 }\r
271 \r
272 \r