(XC32) How to use L1 cache in PIC32MZ devices?

For PIC32 devices featuring an L1 data cache, data variables are by default allocated to the KSEG0 data memory region (kseg0_data_mem). The KSEG0 data memory region is considered cacheable and this makes the variables accessible through the L1 cache. So, the linker-allocated heap and stack are allocated to the KSEG0 region.

At reset, code execution occurs from KSEG1 using uncached access. This is necessary as the cache cannot be used until it is initialized by startup code. Once the cache is initialized, code execution can occur from KSEG0, which utilizes the cache for optimum performance. The default cache policy for the PIC32MZ is present in the startup code supplied with the compiler.

Since the default stack is allocated to the cached KSEG0 region, run-time allocation of uncached memory must come from the heap. The compiler provides two functional equivalents to malloc() and free() to create uncached variables at run-time. These functions are __pic32_alloc_coherent and __pic32_free_coherent. In a similar way, the coherent variable attribute will be allocated to the kseg1_data_mem region.

Buffer will be allocated in the KSEG1 region.

Example

© 2025 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.