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