Freescale Semiconductor Application Note Document Number: AN3521 Rev. 0, 09/2007 Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller by: Paolo Alcantara RTAC Americas 1 Introduction This document is a quick reference to get the FLASH module configured for the MCF521x microcontroller. It includes basic functional description and configuration options to give the user a better understanding of how the flash module works. This application note provides an example that demonstrates how to configure the flash module for the MCF521x microcontroller. The examples mentioned may be modified to suit the specific needs for any application. 2 MCF521x Flash Features The ColdFire FLASH module (CFM) is a nonvolatile memory (NVM) module for integration with a CPU. The CFM provides 256 Kbytes of 32-bit flash memory serving as electrically erasable and programmable, nonvolatile memory. The flash memory is ideal for programming and data storage for single-chip applications, allowing field reprogramming without requiring external programming voltage sources. © Freescale Semiconductor, Inc., 2007. All rights reserved. Contents 1 2 3 4 5 6 7 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . MCF521x Flash Features . . . . . . . . . . . . . . . . . . . . . . . . 2.1 Description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . FLASH Module Explanation . . . . . . . . . . . . . . . . . . . . . . 3.1 Example: Case Study for the CFM Module . . . . . . . 3.2 Example: CFM Clock Configuration . . . . . . . . . . . . 3.3 Example: Configuring CFM Module. . . . . . . . . . . . . Register Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1 CFM Clock Divider Register . . . . . . . . . . . . . . . . . . 4.2 CFM Module Configuration Register . . . . . . . . . . . . 4.3 CFM Clock Select Register . . . . . . . . . . . . . . . . . . . Configuration Summary. . . . . . . . . . . . . . . . . . . . . . . . . . Configuration Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1 Considerations and References . . . . . . . . . . . . . . . 1 1 2 2 3 3 4 4 4 5 5 7 7 8 8 FLASH Module Explanation 2.1 Description Main features include the following: • 256 Kbytes of 32-bit flash memory (the MCF5211 has only 128 Kbytes). • Automated program, erase, and verify operations • Single power supply for program and erase operations • Software programmable interrupts on command completion, access violations, or protection violations • Fast page erase operation • Fast word program operation • Protection scheme to prevent the flash memory of accidental programming or erasing • Access restriction control for supervisor/user and data/instruction operations • Security feature to prevent unauthorized access to the flash memory 3 FLASH Module Explanation This document demonstrates how to configure and use the FLASH module in an easy way. It allows programmers to include it quickly in their projects. The example code shows how to write a longword to the flash memory using the M5213EVB board. The code also includes a routine that copies a function from ROM to RAM and executes it in the volatile memory. The ROM cannot be modified while there is code reading from there. Internally, the flash is divided in the following ways: • Logical block — there is one block, (256 KBytes in 5212 and 5213, 128 KBytes in 5211). It is not possible to read from any block while the same block is being erased, programmed, or verified • Physical block — there are two physical blocks inside each logical block • Sector — portion of a logical block. There are 32 sectors in an entire logical block, each one 8 Kbytes, also known as a logical sector) • Logical Page — each page is 2 Kbytes The entire logical block is divided in sectors according to Figure 1. Each one can be programmed to: • Protect against program and erase • Declare as supervisor address space only • Place as data or data and instruction address space Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 Freescale Semiconductor 2 FLASH Module Explanation (PROGRAM_ARRAY_BASE + $0001_FFFF) SECTOR 31 PROTECT[31] (PROGRAM_ARRAY_BASE + $0001_F000) (PROGRAM_ARRAY_BASE + $0001_2000) 4kBytes • • • SECTOR 17 4kBytes SECTOR 16 4kBytes SECTOR 15 4kBytes SECTOR 14 4kBytes Flash Logical Sectors protection defined by CFMPROT (PROGRAM_ARRAY_BASE + $0001_1000) (PROGRAM_ARRAY_BASE + $0001_0000) (PROGRAM_ARRAY_BASE + $0000_F000) (PROGRAM_ARRAY_BASE + $0000_E000) (PROGRAM_ARRAY_BASE + $0000_1000) PROTECT[0] • • • SECTOR 0 4kBytes (PROGRAM_ARRAY_BASE + $0000_0000) Figure 1. Sector Division of FLASH The following actions can be used to modify flash during runtime: • Word Program: program a 32-bit word • Page Erase: erase a flash logical page • Mass Erase: erase the entire flash memory. All flash memory protection must be disabled • Blank Check: verify that the entire flash memory is erased • Page Erase Verify: verifies that a flash logical page is erased 3.1 Example: Case Study for the CFM Module After programming code in ROM, it shows messages through a serial port (UART0), and a menu of some longwords for testing purposes to write into flash: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 0xdeadbeef 0xcafebabe 0xbabadada 0xdecafbad 0xfeedface 0xdeadc0de 0xabadbabe 0xfeedbabe 0x00000000 0xFFFFFFFF After this, the serial terminal waits for pressing associated number with longword (see longwords above), and the program does the following: Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 3 Freescale Semiconductor FLASH Module Explanation • • • Read flash address 0x6000: to check whether the last write was successful Execute an erase page (2 Kbytes) starting in flash address 0x6000: it writes in flash during runtime and only clears bits but does not set them. An erase has to be executed to allow a new longword in that space by clearing bits only Write flash address 0x6000. The word is verified in next reading space After writing the word in flash, if code is downloaded to the flash, the M5213EVB board can receive a power-on-reset to check if longword write selection is executed successfully. In another case, the starting menu appears again. 3.2 Example: CFM Clock Configuration Equation 1 shows the formula applied to maintain flash frequency within 150kHz ≤ fCFM ≤ 200kHz , as internal flash specifications states. f CORE f CFM = (2 * DIV [5 : 0] * (1 + (7 * PRODIV 8))) Clock Frequency • • 3.3 Eqn. 1 CFMCLKD [DIV[5:0]]: chosen such Equation 1 is valid CFMCLKD[PRODIV8]: set in case fCORE is greater than 12.8 MHz Example: Configuring CFM Module After a longword is selected Section 3.1, “Example: Case Study for the CFM Module,” the program asks for another option. The most important part of the code is related to copy a function from ROM to RAM and then executed in RAM. In any case longword selected is deleted. There are two ways to copy a function from ROM to RAM: 1. Compiler independent code (code named CFMSoftwareDEMO): a) Declares a variable that stores the entire function from the ROM. This is reserved space in the RAM to be modified during runtime b) Declares a function pointer in the RAM memory to point to the variable that stores the function in the RAM. A cast from char to function pointer is necessary. c) Copies entire function space from the ROM to the variable in the RAM using memcopy from stdio.h library. This has to be stored only one time. d) Executes a function that points to a variable that stores a function in the RAM. This way it executes the function in the RAM, and after returns to the flash to continue executing code there. e) If only one function is copied from ROM to RAM, the lcf file does not need to be modified in this case. 2. CodeWarrior stile (code named CFMSofwareDEMO-LCF): a) Declares a memory segment inside the ROM.lcf file from CodeWarrior b) After that it declares a segment that holds the function in the RAM memory Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 Freescale Semiconductor 4 Register Configuration c) From the code, it declares a section with the same name as the segment d) Writes your function inside this section 4 Register Configuration This section describes the registers configuration that affects the CFM module and the code lines used for this purpose. 4.1 CFM Clock Divider Register The following code lines are used to configure the CFMCLKD, Figure 2, to assign the CFM frequency. /* Select FLASH frequency to 200 kHz. Section 15.4.3.1 MCF5213RM */ MCF_CFM_CFMCLKD = MCF_CFM_CFMCLKD_DIV(25) | MCF_CFM_CFMCLKD_PRDIV8;. IPSBAR Offset: 0x1D_0002 (CFMCLKD) R 7 6 DIVLD PRDIV8 0 0 Access: User read/write 5 4 3 2 1 0 0 0 0 DIV W Reset: 0 0 0 Figure 2. CFM Clock Divider Register Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 5 Freescale Semiconductor Register Configuration 4.2 CFM Module Configuration Register To configure the CFMMCR, the next code lines are used, the changes performed in the register are shown in Figure 3. /* clear the lock bit */ MCF_CFM_CFMMCR &= ~MCF_CFM_CFMMCR_LOCK; IPSBAR Offset: 0x1D_0000 (CFMMCR) R Access: User read/write 15 14 13 12 11 0 0 0 0 0 0 0 0 0 0 W Reset 10 9 8 7 6 5 LOCK PVIE AEIE CBEI CCIE KEYA E CC 0 0 0 0 0 0 4 3 2 1 0 0 0 0 0 0 0 0 0 0 0 Figure 3. CFM Module Configuration Register 4.3 CFM Clock Select Register To write the values shown in Figure 4, Figure 5, and Figure 6, in registers, the program uses these lines: /* clear sector protection registers if set from reset */ MCF_CFM_CFMPROT = 0x0; MCF_CFM_CFMSACC = 0x0; MCF_CFM_CFMDACC = 0x0; Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 Freescale Semiconductor 6 Register Configuration IPSBAR Offset: 0x1D_0010 (CFMPROT) 31 30 29 Access: User read/write 28 27 26 25 R 21 20 19 18 17 16 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 F1 F1 F1 F1 F1 F1 F1 F1 R 1 22 F1 PROTECT W Reset 23 PROTECT W Reset 24 F1 F1 F1 F1 F1 F1 F1 F1 Reset state loaded from flash configuration field during reset. Figure 4. CFM Protection Register IPSBAR Offset: 0x1D_0014 (CFMSACC) 31 30 29 Access: User read/write 28 27 26 25 R 21 20 19 18 17 16 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 F1 F1 F1 F1 F1 F1 F1 F1 SUPV W 1 22 F1 R Reset 23 SUPV W Reset 24 F1 F1 F1 F1 F1 F1 F1 F1 Reset state loaded from flash configuration field during reset. Figure 5. CFM Supervisor Access Register IPSBAR Offset: 0x1D_0018 (CFMDACC) 31 30 29 Access: User read/write 28 27 26 25 R 21 20 19 18 17 16 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 F1 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 F1 F1 F1 F1 F1 F1 F1 F1 DACC W 1 22 F1 R Reset 23 DACC W Reset 24 F1 F1 F1 F1 F1 F1 F1 F1 Reset state loaded from flash configuration field during reset. Figure 6. CFM Data Access Register Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 7 Freescale Semiconductor Configuration Summary 5 Configuration Summary The following steps are needed to modify the flash content. Reading flash memory can be performed directly without any CFM register configuration. 1. Clear the CFMMCR[LOCK] bit 2. Set the CFMCLKD with the calculated value 3. Clear registers: CMFPROT, CFMSACC, CFMDACC 4. Run the following steps from the RAM 5. Wait until CFMUSTAT[CBEIF] flag is set to continue 6. Write a longword from 0x0400 0000 plus IPSBAR in addresses divisible by 4 and within flash boundaries. The memory space to use is 64 k @ 32 bits in MCF5212 and MCF5213. It is 32 k @ 32 bits in MCF5211. This longword value is useless, but mandatory if you want to execute an erase operation 7. Write in the MCFCMD register the command code desired 8. Set the CFMUSTAT[CBEIF] bit to clear flag 9. Wait until the CFMUSTAT[CCIF] flag is set to continue 10. If you want to write more longwords, then repeat step 4 down 6 Configuration Notes The following details are considered important when configuring and using the CFM: • Only 32-bit write operations are allowed to the flash memory space. Byte and halfword write operations to the flash memory space result in a cycle termination transfer error. • Avoid writing longword or erasing page to areas used by code. • It is not possible to read from any block while the same logical block is being erased, programmed, or verified. • Always maintain flash write frequency around 150kHz ≤ fCFM ≤ 200kHz • Program and erase command execution time increases proportionally with the period of FCLK. • As active commands are immediately aborted when the MCU enters stop mode, it is strongly recommended that the user does not execute the stop instruction during program and erase operations. • After erasing a longword, using page or mass erase, the value is 0xFFFFFFFF. The programmer can clear bits in the same longword, but cannot set bits, until an erase command is executed previously. • Security information that allows the MCU to prevent intrusive access to the flash memory is stored in the flash configuration field. The flash configuration field is composed of 24 bytes of reserved memory space within the flash memory, which contains information that determines the CFM protection and access restriction scheme out of reset. This area is between FLASHBAR+0x400 to FLASHBAR+0x417. This restriction is included at the bottom of the file vectors.s in the software example. Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 Freescale Semiconductor 8 Conclusion 7 Conclusion The CFM module is a non-volatile-memory that is not only used as code storage. It can also be used as external event storage, keeping ADC measures, or an external event to be used later, even if the microcontroller is turned off for a while. All this information can be stored or changed during runtime. 7.1 Considerations and References Find the newest software updates and configuration files for the MCF521x on the Freescale Semiconductor home site www.freescale.com. This application note considers MCF5211, MCF5212 and MCF5213 devices. The M5213EVB development board employed CFM software demo. For more information on FLASH module refer to MCF5213 ColdFire Integrated Microcontroller Reference Manual. Rev 2.0 at www.freescale.com The CFMSoftwareDemo software was developed in CodeWarrior for ColdFire v6.4. Download the source files for CFMSoftwareDemo software (CFMSoftwareDemo.zip) from www.freescale.com. Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 9 Freescale Semiconductor THIS PAGE IS INTENTIONALLY BLANK Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 Freescale Semiconductor 10 How to Reach Us: Home Page: www.freescale.com Web Support: http://www.freescale.com/support USA/Europe or Locations Not Listed: Freescale Semiconductor, Inc. Technical Information Center, EL516 2100 East Elliot Road Tempe, Arizona 85284 +1-800-521-6274 or +1-480-768-2130 www.freescale.com/support Europe, Middle East, and Africa: Freescale Halbleiter Deutschland GmbH Technical Information Center Schatzbogen 7 81829 Muenchen, Germany +44 1296 380 456 (English) +46 8 52200080 (English) +49 89 92103 559 (German) +33 1 69 35 48 48 (French) www.freescale.com/support Japan: Freescale Semiconductor Japan Ltd. Headquarters ARCO Tower 15F 1-8-1, Shimo-Meguro, Meguro-ku, Tokyo 153-0064 Japan 0120 191014 or +81 3 5437 9125 [email protected] Asia/Pacific: Freescale Semiconductor Hong Kong Ltd. Technical Information Center 2 Dai King Street Tai Po Industrial Estate Tai Po, N.T., Hong Kong +800 2666 8080 [email protected] For Literature Requests Only: Freescale Semiconductor Literature Distribution Center P.O. Box 5405 Denver, Colorado 80217 1-800-441-2447 or 303-675-2140 Fax: 303-675-2150 [email protected] Document Number: AN3521 Rev. 0 09/2007 Information in this document is provided solely to enable system and software implementers to use Freescale Semiconductor products. There are no express or implied copyright licenses granted hereunder to design or fabricate any integrated circuits or integrated circuits based on the information in this document. Freescale Semiconductor reserves the right to make changes without further notice to any products herein. Freescale Semiconductor makes no warranty, representation or guarantee regarding the suitability of its products for any particular purpose, nor does Freescale Semiconductor assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability, including without limitation consequential or incidental damages. “Typical” parameters that may be provided in Freescale Semiconductor data sheets and/or specifications can and do vary in different applications and actual performance may vary over time. All operating parameters, including “Typicals”, must be validated for each customer application by customer’s technical experts. Freescale Semiconductor does not convey any license under its patent rights nor the rights of others. Freescale Semiconductor products are not designed, intended, or authorized for use as components in systems intended for surgical implant into the body, or other applications intended to support or sustain life, or for any other application in which the failure of the Freescale Semiconductor product could create a situation where personal injury or death may occur. Should Buyer purchase or use Freescale Semiconductor products for any such unintended or unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claim alleges that Freescale Semiconductor was negligent regarding the design or manufacture of the part. RoHS-compliant and/or Pb-free versions of Freescale products have the functionality and electrical characteristics as their non-RoHS-compliant and/or non-Pb-free counterparts. For further information, see http://www.freescale.com or contact your Freescale sales representative. For information on Freescale’s Environmental Products program, go to http://www.freescale.com/epp. Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2007. All rights reserved. Using the ColdFire Flash Module with the MCF521x ColdFire Microcontroller, Rev. 0 Freescale Semiconductor 12