AVR102: Block Copy Routines Features • Program Memory (Flash) to SRAM Copy Routine • SRAM to SRAM Copy Routine • Extremely Code Efficient Routines Flash → SRAM: 6 Words, SRAM → SRAM: 5 Words • Runable Test/Example Program Introduction 8-bit Microcontroller Application Note This application note contains routines for transfer of data blocks. • Flash to SRAM Copy. Copies data blocks of sizes up to 256 bytes from any Flash address to any SRAM address. • SRAM to SRAM copy. Copies data blocks of sizes up to 256 bytes from any SRAM source address to any SRAM destination address. The routines are described in detail in the following sections. Flash to SRAM Copy – Subroutine “flash2ram” Before calling this routine, the following input parameters must be set up by the user: • “flashsize” – Register variable holding the size of the data block. • Z-pointer – 2 x source address of first Flash block byte. • Y-pointer – Destination address of first SRAM block byte. The routine uses the “LPM”-instruction which is the one to use for data transfer from Flash to Register File. The instruction selects the Flash word (16-bit) pointed to by the 15 most significant bytes of the Z-pointer. If LSB of the Z-pointer is 0, the lower byte of the Flash word is returned, otherwise the higher byte is returned. The returned byte is always found in R0. To obtain the 15 MSBs of Z with the correct word value, the user must be careful to load Z with two times the address of the first double byte in the table. The program listing shows how to handle this precaution. During data transfer, the auto-increment facility of the pointers is utilized. Writing to SRAM is done using the “ST Y+,Rs”-instruction. The “LPM”-instruction does not support auto-increment or decrement. The “ADIW” instruction is used to increment the 16bit pointer. The register variable “flashsize” is used as a loop counter in the routine. Rev. 0933B–AVR–05/02 1 Figure 1. “flash2ram” Flow Chart FLASH2RAM R0 ← FLASH@Z (LPM) SRAM@Y ← R0, Y←Y+1 Z←Z+1 FLASHSIZE ← FLASHSIZE - 1 N FLASHSIZE = 0? Y RETURN Table 1. “flash2ram” Register Usage Register Input Internal R0 Output Temporary data storage R16 “flashsize” – Size of data block to copy R28 “YL” – Low address of SRAM data start R29 “YH” – High address of SRAM data start R30 “ZL” – Low address of Flash data start R31 “ZH” – High address of Flash data start Table 2. “flash2ram” Performance Figures Parameter Value Code Size (Words) 5 + return Execution Time 10 x (block size) + return Register Usage 2 Low Registers High Registers Pointers Interrupts Usage None Peripherals Usage None :1 :1 :Y, Z AVR102 0933B–AVR–05/02 AVR102 SRAM to SRAM Copy – Subroutine “ram2ram” Before calling this routine, the following input parameters must be set up by the user: • “ramsize” – Register variable holding the size of the data block. • Z-pointer – source address of first byte in block. • Y-pointer – destination address of first byte in block. By utilizing the pointer auto-increment facility, this routine is extremely compact. Moving one byte of data and incrementing both pointers requires two instructions only. Two more instructions are needed to decrement an check the register variable “ramsize” which is used as a loop counter in the routine. The data are temporarily stored in the register variable “ramtemp” during transfer. Figure 2. “ram2ram” Flow Chart RAM2RAM RAMTEMP ← SRAM@Z, Z←Z+1 SRAM@Y ← RAMTEMP, Y←Y+1 RAMSIZE ← RAMSIZE - 1 RAMSIZE = 0? N Y RETURN Table 3. “ram2ram” Register Usage Register Input R1 Internal Output “ramtemp” – Temporary data storage R16 “ramsize” – Size of data block to copy R28 “YL” – Destination address low byte R29 “YH” – Destination address high byte R30 “ZL” – Source address low byte R31 “ZH” – Source address high byte 3 0933B–AVR–05/02 Table 4. “flash2ram” Performance Figures Parameter Value Code Size (Words) 4 + return Execution Time 6 x (block size) + return Register Usage Test/Example Program 4 Low Registers High Registers Pointers Interrupts Usage None Peripheral Usage None :1 :1 :Y, Z The application note program file contains a runable test program which uses the copy application routines to copy 20 bytes of data from Flash to SRAM, then make a second copy of the data at another SRAM location. The test program is made for use with an AT90S8515. By changing including another “*def.inc” file, the program can be used with any other AVR MCU with SRAM. AVR102 0933B–AVR–05/02 AVR102 5 0933B–AVR–05/02 Atmel Headquarters Atmel Operations Corporate Headquarters Memory 2325 Orchard Parkway San Jose, CA 95131 TEL 1(408) 441-0311 FAX 1(408) 487-2600 Europe Atmel SarL Route des Arsenaux 41 Casa Postale 80 CH-1705 Fribourg Switzerland TEL (41) 26-426-5555 FAX (41) 26-426-5500 Asia Atmel Asia, Ltd. Room 1219 Chinachem Golden Plaza 77 Mody Road Tsimhatsui East Kowloon Hong Kong TEL (852) 2721-9778 FAX (852) 2722-1369 Japan Atmel Japan K.K. 9F, Tonetsu Shinkawa Bldg. 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan TEL (81) 3-3523-3551 FAX (81) 3-3523-7581 Atmel Corporate 2325 Orchard Parkway San Jose, CA 95131 TEL 1(408) 436-4270 FAX 1(408) 436-4314 Microcontrollers Atmel Corporate 2325 Orchard Parkway San Jose, CA 95131 TEL 1(408) 436-4270 FAX 1(408) 436-4314 Atmel Nantes La Chantrerie BP 70602 44306 Nantes Cedex 3, France TEL (33) 2-40-18-18-18 FAX (33) 2-40-18-19-60 ASIC/ASSP/Smart Cards Atmel Rousset Zone Industrielle 13106 Rousset Cedex, France TEL (33) 4-42-53-60-00 FAX (33) 4-42-53-60-01 RF/Automotive Atmel Heilbronn Theresienstrasse 2 Postfach 3535 74025 Heilbronn, Germany TEL (49) 71-31-67-0 FAX (49) 71-31-67-2340 Atmel Colorado Springs 1150 East Cheyenne Mtn. Blvd. Colorado Springs, CO 80906 TEL 1(719) 576-3300 FAX 1(719) 540-1759 Biometrics/Imaging/Hi-Rel MPU/ High Speed Converters/RF Datacom Atmel Grenoble Avenue de Rochepleine BP 123 38521 Saint-Egreve Cedex, France TEL (33) 4-76-58-30-00 FAX (33) 4-76-58-34-80 Atmel Colorado Springs 1150 East Cheyenne Mtn. Blvd. Colorado Springs, CO 80906 TEL 1(719) 576-3300 FAX 1(719) 540-1759 Atmel Smart Card ICs Scottish Enterprise Technology Park Maxwell Building East Kilbride G75 0QR, Scotland TEL (44) 1355-803-000 FAX (44) 1355-242-743 e-mail [email protected] Web Site http://www.atmel.com © Atmel Corporation 2002. Atmel Corporation makes no warranty for the use of its products, other than those expressly contained in the Company’s standard warranty which is detailed in Atmel’s Terms and Conditions located on the Company’s web site. The Company assumes no responsibility for any errors which may appear in this document, reserves the right to change devices or specifications detailed herein at any time without notice, and does not make any commitment to update the information contained herein. No licenses to patents or other intellectual property of Atmel are granted by the Company in connection with the sale of Atmel products, expressly or by implication. Atmel’s products are not authorized for use as critical components in life support devices or systems. ATMEL ® and AVR® are the registered trademarks of Atmel. Other terms and product names may be the trademarks of others. Printed on recycled paper. 0933B–AVR–05/02 0M