User Guide for MICROCHIP models including: TB3308 Handling Cache Coherency Issues at Runtime Using Cache Maintenance, TB3308, Handling Cache Coherency Issues at Runtime Using Cache Maintenance, Runtime Using Cache Maintenance, Cache Maintenance
File Info : application/pdf, 12 Pages, 313.22KB
DocumentDocumentTB3308 Handling Cache Coherency Issues at Runtime Using Cache Maintenance Operations on PIC32MZ MCUs Using MPLAB Harmony v3 Introduction The cache coherency issue is inevitable on applications running on microcontrollers (MCUs) that have cacheable memory regions, which use a Direct Memory Access (DMA) for data transfer operations. This is due to the CPU performing a read/write operation from the cache while the DMA transfers data between the peripheral and physical memory. One of the methods to handle cache coherency requires the application to manage the cache at run-time using the cache maintenance operations. MPLAB® Harmony v3 provides cache maintenance Application Program Interfaces (APIs) for PIC32MZ devices. This document explains how an application can manage cache coherency issues at run time by using cache management APIs under MPLAB Harmony v3. Note: The concepts discussed in this document are common for all PIC32MZ MCUs. The PIC32MZ EF is used as an example to discuss the concepts. © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 1 TB3308 Description 1. Description The following figure illustrates the cache coherency issue as observed when the DMA reads from the SRAM on the PIC32MZ EF MCU. Figure 1-1. Memory-to-Peripheral Transfer (DMA Reads from SRAM) CPU Write Back Write Allocate Cache WR "ABCDEFGH" 1 SRAM DMA 3 WR RD 2 TxBuffer "12345678" Peripheral The application submits a request to transfer the data buffer TxBuffer value `ABCDEFGH' to the peripheral. The CPU populates the DMA write buffer (TxBuffer) with the data to be written `ABCDEFGH' to the peripheral. However, due to the set cache policy Write Back and Write Allocate, the DMA write buffer (TxBuffer) may not be immediately written to the main memory, and the written data may remain in the data cache. The DMA write buffer (TxBuffer) in the main memory still contains the old value of `12345678'. When the DMA is triggered to initiate the memory-to-peripheral transfer, the DMA reads the buffer (TxBuffer) from the main memory as `12345678'. As a result, the DMA ends up transferring stale data to the peripheral. The following figure illustrates the cache coherency issue observed when the DMA writes to the SRAM. Figure 1-2. Peripheral-to-Memory Transfer (DMA Writes to SRAM) CPU DMA 1 RD Write Back Write Allocate RD Cache "ABCDEFGH" 3 SSRRAAMM WR 2 RxBuffer ""1122334455667788"" Peripheral The application submits a request to receive data in the RxBuffer with a value of `12345678' from the peripheral. The DMA populates the RxBuffer with a value of `12345678' in the SRAM. However, the data cache is not updated, and it continues to hold the previous data. When the CPU reads the RxBuffer, it ends up reading the previous value contained in the buffer as `ABCDEFGH'. © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 2 TB3308 Description 1.1 Handling Cache Coherency The two methods to handle the cache coherency issues are illustrated in Memory to Peripheral Transfer (DMA Reads from SRAM) and Peripheral-to-Memory Transfer (DMA Writes to SRAM). These methods involve the application managing the cache at run-time using the maintenance operations. The operations include the ability to perform these actions: · Invalidate the cache: Marks the cache lines as invalid. Subsequent access forces the data to be copied from the main memory to the cache. · Clean the cache: Writes the cache lines, which are marked as dirty, back to the main memory To handle the cache coherency discussed in Memory to Peripheral Transfer (DMA Reads from SRAM), perform the following actions: 1. The application fills the write data buffer (TxBuffer) with a value `ABCEDFGH'. Due to the default cache policy (Write Back and Write Allocate), the written data may be in the cache. Figure 1-3. Populate Write Buffer CPU WR Cache "ABCDEFGH" 1 2. Flush the write data buffer (TxBuffer) with the value `ABCDEFGH' to the main memory by calling the clean cache API. Figure 1-4. Flush Write Buffer CPU 2 Clean Cache Cache "ABCDEFGH" SRAM TxBuffer "ABCDEFGH" 3. The application submits a request to transfer data from the TxBuffer with a value of `ABCDEFGH' to the peripheral. Figure 1-5. Write to Peripheral SRAM DMA 4 WR Peripheral WR 3 TxBuffer "ABCDEFGH" © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 3 TB3308 Description To handle the cache coherency as discussed in Peripheral to Memory Transfer (DMA Writes to SRAM), follow these steps: 1. The application calls the Invalidate cache API to mark the cache lines as invalid. Figure 1-6. Invalidate Cache CPU 1 ! Cache Invalidate Cache "ABCDEFGH" 2. The application submits a request to receive data in the RxBuffer with a value of `12345678' from the peripheral. 3. The DMA populates the RxBuffer with a value of `12345678' in the SRAM. 4. Because the cache line corresponding to the RxBuffer is in an invalid state, a read access by the CPU results in the RxBuffer being copied from the main memory into the data cache. Figure 1-7. Handle Peripheral-to-Memory Transfer Cache Coherency CPU DMA 2 RD 4 RD Cache "12345678" WR 3 SSRRAAMM RxBuffer ""1122334455667788"" Peripheral © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 4 TB3308 Implementation 2. Implementation 2.1 Configuration In an MPLAB Harmony v3 project for the PIC32 MZ EF, the cache maintenance operations are enabled by the MPLAB Harmony v3 Configurator (MHC) or MPLAB Code Configurator (MCC). In MHC or MCC, the configuration setting can be found under project graph > System > MIPS Configuration > Cache. Figure 2-1. MHC Cache Configuration © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 5 TB3308 Implementation 2.2 Data-Cache Maintenance APIs MPLAB Harmony v3 provides the following data-cache maintenance APIs: Table 2-1. Data-Cache Maintenance APIs Name Description DCACHE_INVALIDATE (void) Invalidates the entire data cache before enabling it. DCACHE_CLEAN_BY_ADDR (uint32_t addr, size_t sz) Write back and invalidate an address range in the data cache. DCACHE_INVALIDATE_BY_ADDR (uint32_t addr, size_t sz) Invalidate an address range in the data cache. DCACHE_CLEAN_INVALIDATE_BY_ADDR (uint32_t addr, size_t sz) Write back and invalidate an address range in the data cache. Notes: 1. MCU specific data and instruction cache maintenance APIs are available as a peripheral library (PLIB) plib_cache.c. Refer to the PLIB application example at: https://github.com/Microchip-MPLAB-Harmony/csp_apps_pic32mz_ef/tree/master/apps/cache/ cache_maintenance. 2. When using the cache clean and cache invalidate by address APIs: addr: Must be aligned to the cache line size boundary. This means that the DMA buffer address must be aligned to the 16-byte boundary. dsize: Must be a multiple of the cache line size. This means that the DMA buffer size must be a multiple of 16- bytes. © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 6 TB3308 Implementation 2.3 Example The following code example demonstrates the usage of data-cache maintenance APIs along with the MPLAB Harmony v3 DMA peripheral library APIs to read and write data over the UART interface. char __attribute__ ((aligned (16))) messageStart[] = "**** CACHE maintenance demo with UART ****\r\n\ **** Type a buffer of 10 characters and observe it echo back ****\r\n\ **** LED toggles on each time the buffer is echoed ****\r\n"; char __attribute__ ((aligned (16))) receiveBuffer[BUFFER_SIZE] = {}; char __attribute__ ((aligned (16))) echoBuffer[BUFFER_SIZE] = {}; char __attribute__ ((aligned (16))) messageError[BUFFER_SIZE] = "**** UART error occurred ****\r\n"; int main ( void ) { /* Initialize all peripherals and modules */ While(1) { if(readStatus == true) { readStatus = false; /* Fill the TxBuffer with the data to be transmitted. The TxBuffer may be in D-cache */ memcpy(echoBuffer, receiveBuffer, READ_SIZE); echoBuffer[READ_SIZE] = '\r'; echoBuffer[(READ_SIZE + 1)] = '\n'; DCACHE_CLEAN_BY_ADDR((uint32_t)echoBuffer, sizeof(echoBuffer)); DMAC_ChannelTransfer(DMAC_CHANNEL_0, echoBuffer, READ_SIZE+2, (const void *)&U2TXREG, 1, 1); LED_TOGGLE(); } else if(writeStatus == true) { writeStatus = false; /* Invalidate cache lines having received buffer before using it * to load the latest data in the actual memory to the cache */ DCACHE_INVALIDATE_BY_ADDR((uint32_t)receiveBuffer, sizeof(receiveBuffer)); DMAC_ChannelTransfer(DMAC_CHANNEL_1, (const void *)&U2RXREG, 1, receiveBuffer, READ_SIZE, 1); } } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); } For detailed source code, refer to the PLIB application example at: https://github.com/Microchip-MPLAB-Harmony/csp_apps_pic32mz_ef/tree/master/apps/cache/cache_maintenance. Note: The cache coherency issues discussed above can also be handled at link time by using the coherent variable attribute to the data buffer in contention. unsigned int __attribute__((coherent)) buffer[1024]; In this code, the compiler allocates (at link time) the 1024 element in the non-cacheable memory region KSEG1. © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 7 TB3308 References 3. References The following documents are listed as resources. For additional information on cache coherency and related Microchip products, refer to the Microchip Website, or contact a local Microchip sales representative. · Using L1 Cache on PIC32MZ Devices · PIC32MZ EF Cache Maintenance PLIB Example · MPLAB Harmony v3 Quick Docs package provides standalone help pages for users to get started developing applications on Microchip's 32-bit SAM and PIC32 MCUs. Download the quick_docs repository and start with the index.html file available in the docs folder. The online version is available at: microchip-mplab-harmony.github.io/quick_docs/. · MPLAB Harmony v3 landing web page: www.microchip.com/mplab/mplab-harmony © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 8 TB3308 The Microchip Website Microchip provides online support via our website at www.microchip.com/. This website is used to make files and information easily available to customers. Some of the content available includes: · Product Support Data sheets and errata, application notes and sample programs, design resources, user's guides and hardware support documents, latest software releases and archived software · General Technical Support Frequently Asked Questions (FAQs), technical support requests, online discussion groups, Microchip design partner program member listing · Business of Microchip Product selector and ordering guides, latest Microchip press releases, listing of seminars and events, listings of Microchip sales offices, distributors and factory representatives Product Change Notification Service Microchip's product change notification service helps keep customers current on Microchip products. Subscribers will receive email notification whenever there are changes, updates, revisions or errata related to a specified product family or development tool of interest. To register, go to www.microchip.com/pcn and follow the registration instructions. Customer Support Users of Microchip products can receive assistance through several channels: · Distributor or Representative · Local Sales Office · Embedded Solutions Engineer (ESE) · Technical Support Customers should contact their distributor, representative or ESE for support. Local sales offices are also available to help customers. A listing of sales offices and locations is included in this document. Technical support is available through the website at: www.microchip.com/support Microchip Devices Code Protection Feature Note the following details of the code protection feature on Microchip products: · Microchip products meet the specifications contained in their particular Microchip Data Sheet. · Microchip believes that its family of products is secure when used in the intended manner, within operating specifications, and under normal conditions. · Microchip values and aggressively protects its intellectual property rights. Attempts to breach the code protection features of Microchip product is strictly prohibited and may violate the Digital Millennium Copyright Act. · Neither Microchip nor any other semiconductor manufacturer can guarantee the security of its code. Code protection does not mean that we are guaranteeing the product is "unbreakable". Code protection is constantly evolving. Microchip is committed to continuously improving the code protection features of our products. Legal Notice This publication and the information herein may be used only with Microchip products, including to design, test, and integrate Microchip products with your application. Use of this information in any other manner violates these terms. Information regarding device applications 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. Contact your local Microchip sales office for additional support or, obtain additional support at www.microchip.com/en-us/support/ design-help/client-support-services. © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 9 TB3308 THIS INFORMATION IS PROVIDED BY MICROCHIP "AS IS". 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 ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE, OR WARRANTIES RELATED TO ITS CONDITION, QUALITY, OR PERFORMANCE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL, OR CONSEQUENTIAL LOSS, DAMAGE, COST, OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE INFORMATION OR ITS USE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THE INFORMATION OR ITS USE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THE INFORMATION. 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 unless otherwise stated. Trademarks The Microchip name and logo, the Microchip logo, Adaptec, AnyRate, AVR, AVR logo, AVR Freaks, BesTime, BitCloud, CryptoMemory, CryptoRF, dsPIC, flexPWR, HELDO, IGLOO, JukeBlox, KeeLoq, Kleer, LANCheck, LinkMD, maXStylus, maXTouch, MediaLB, megaAVR, Microsemi, Microsemi logo, MOST, MOST logo, MPLAB, OptoLyzer, PIC, picoPower, PICSTART, PIC32 logo, PolarFire, Prochip Designer, QTouch, SAM-BA, SenGenuity, SpyNIC, SST, SST Logo, SuperFlash, Symmetricom, SyncServer, Tachyon, TimeSource, tinyAVR, UNI/O, Vectron, and XMEGA are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. AgileSwitch, APT, ClockWorks, The Embedded Control Solutions Company, EtherSynch, Flashtec, Hyper Speed Control, HyperLight Load, IntelliMOS, Libero, motorBench, mTouch, Powermite 3, Precision Edge, ProASIC, ProASIC Plus, ProASIC Plus logo, Quiet- Wire, SmartFusion, SyncWorld, Temux, TimeCesium, TimeHub, TimePictra, TimeProvider, TrueTime, WinPath, and ZL are registered trademarks of Microchip Technology Incorporated in the U.S.A. Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, Augmented Switching, BlueSky, BodyCom, CodeGuard, CryptoAuthentication, CryptoAutomotive, CryptoCompanion, CryptoController, dsPICDEM, dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, Espresso T1S, EtherGREEN, GridTime, IdealBridge, In-Circuit Serial Programming, ICSP, INICnet, Intelligent Paralleling, Inter-Chip Connectivity, JitterBlocker, Knob-on-Display, maxCrypto, maxView, memBrain, Mindi, MiWi, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, NVM Express, NVMe, Omniscient Code Generation, PICDEM, PICDEM.net, PICkit, PICtail, PowerSmart, PureSilicon, QMatrix, REAL ICE, Ripple Blocker, RTAX, RTG4, SAMICE, Serial Quad I/O, simpleMAP, SimpliPHY, SmartBuffer, SmartHLS, SMART-I.S., storClad, SQI, SuperSwitcher, SuperSwitcher II, Switchtec, SynchroPHY, Total Endurance, TSHARC, USBCheck, VariSense, VectorBlox, VeriPHY, ViewSpan, WiperLock, XpressConnect, and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. The Adaptec logo, Frequency on Demand, Silicon Storage Technology, Symmcom, and Trusted Time are registered trademarks of Microchip Technology Inc. in other countries. GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip Technology Inc., in other countries. All other trademarks mentioned herein are property of their respective companies. © 2021, Microchip Technology Incorporated and its subsidiaries. All Rights Reserved. ISBN: 978-1-5224-9447-8 © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 10 TB3308 Quality Management System For information regarding Microchip's Quality Management Systems, please visit www.microchip.com/quality. © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 11 AMERICAS Corporate Office 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: www.microchip.com/support Web Address: www.microchip.com Atlanta Duluth, GA Tel: 678-957-9614 Fax: 678-957-1455 Austin, TX Tel: 512-257-3370 Boston Westborough, MA Tel: 774-760-0087 Fax: 774-760-0088 Chicago Itasca, IL Tel: 630-285-0071 Fax: 630-285-0075 Dallas Addison, TX Tel: 972-818-7423 Fax: 972-818-2924 Detroit Novi, MI Tel: 248-848-4000 Houston, TX Tel: 281-894-5983 Indianapolis Noblesville, IN Tel: 317-773-8323 Fax: 317-773-5453 Tel: 317-536-2380 Los Angeles Mission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608 Tel: 951-273-7800 Raleigh, NC Tel: 919-844-7510 New York, NY Tel: 631-435-6000 San Jose, CA Tel: 408-735-9110 Tel: 408-436-4270 Canada - Toronto Tel: 905-695-1980 Fax: 905-695-2078 Worldwide Sales and Service ASIA/PACIFIC Australia - Sydney Tel: 61-2-9868-6733 China - Beijing Tel: 86-10-8569-7000 China - Chengdu Tel: 86-28-8665-5511 China - Chongqing Tel: 86-23-8980-9588 China - Dongguan Tel: 86-769-8702-9880 China - Guangzhou Tel: 86-20-8755-8029 China - Hangzhou Tel: 86-571-8792-8115 China - Hong Kong SAR Tel: 852-2943-5100 China - Nanjing Tel: 86-25-8473-2460 China - Qingdao Tel: 86-532-8502-7355 China - Shanghai Tel: 86-21-3326-8000 China - Shenyang Tel: 86-24-2334-2829 China - Shenzhen Tel: 86-755-8864-2200 China - Suzhou Tel: 86-186-6233-1526 China - Wuhan Tel: 86-27-5980-5300 China - Xian Tel: 86-29-8833-7252 China - Xiamen Tel: 86-592-2388138 China - Zhuhai Tel: 86-756-3210040 ASIA/PACIFIC India - Bangalore Tel: 91-80-3090-4444 India - New Delhi Tel: 91-11-4160-8631 India - Pune Tel: 91-20-4121-0141 Japan - Osaka Tel: 81-6-6152-7160 Japan - Tokyo Tel: 81-3-6880- 3770 Korea - Daegu Tel: 82-53-744-4301 Korea - Seoul Tel: 82-2-554-7200 Malaysia - Kuala Lumpur Tel: 60-3-7651-7906 Malaysia - Penang Tel: 60-4-227-8870 Philippines - Manila Tel: 63-2-634-9065 Singapore Tel: 65-6334-8870 Taiwan - Hsin Chu Tel: 886-3-577-8366 Taiwan - Kaohsiung Tel: 886-7-213-7830 Taiwan - Taipei Tel: 886-2-2508-8600 Thailand - Bangkok Tel: 66-2-694-1351 Vietnam - Ho Chi Minh Tel: 84-28-5448-2100 EUROPE Austria - Wels Tel: 43-7242-2244-39 Fax: 43-7242-2244-393 Denmark - Copenhagen Tel: 45-4485-5910 Fax: 45-4485-2829 Finland - Espoo Tel: 358-9-4520-820 France - Paris Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Germany - Garching Tel: 49-8931-9700 Germany - Haan Tel: 49-2129-3766400 Germany - Heilbronn Tel: 49-7131-72400 Germany - Karlsruhe Tel: 49-721-625370 Germany - Munich Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 Germany - Rosenheim Tel: 49-8031-354-560 Israel - Ra'anana Tel: 972-9-744-7705 Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781 Italy - Padova Tel: 39-049-7625286 Netherlands - Drunen Tel: 31-416-690399 Fax: 31-416-690340 Norway - Trondheim Tel: 47-72884388 Poland - Warsaw Tel: 48-22-3325737 Romania - Bucharest Tel: 40-21-407-87-50 Spain - Madrid Tel: 34-91-708-08-90 Fax: 34-91-708-08-91 Sweden - Gothenberg Tel: 46-31-704-60-40 Sweden - Stockholm Tel: 46-8-5090-4654 UK - Wokingham Tel: 44-118-921-5800 Fax: 44-118-921-5820 © 2021 Microchip Technology Inc. and its subsidiaries Technical Brief DS90003308A-page 12