Renesas Technology America, Inc. Flash!
CPU Rewrite CPU-rewrite is a term that refers to an Renesas MCU’s ability to erase/program its own internal Flash in circuit. R8C is BYTE programmable, where current M16C and M32C devices are WORD programmable. Old M16C/M32C devices were 256byte programmable. In order to erase any data, the ENTIRE block must be erase.
Renesas Technology America, Inc. EW0 vs. EW1 Mode HNDc Flash devices can perform Flash operations using either EW0 or EW1 Mode. EW0 Mode: CPU-rewrite Code must be executed out of RAM and the Flash register polled for operation completion. EW1 Mode: CPU-rewrite Code is run out of Flash (just not in the same block as target block) and the CPU is kept in HOLD state until operation completion.
Renesas Technology America, Inc. Block LOCK bit Hardware mechanism to protect Flash blocks from accidentally be erased/programmed. Only on older DINOR, HNDa,b Flash devices (M16C/62P,/6N, M32C/83,/85) Once a block is locked, it remains that way (even through resets) until it is erased. To erase a locked block requires and extra step in software which disables ALL lock bit settings. There are 2 Flash erase commands for this type of Flash: –Erase Block (can be used to override lock bit settings) –Erase All unlocked blocks. (can never erase a locked block)
Renesas Technology America, Inc. Block PROTECT bits Hardware mechanism to protect Flash blocks from accidentally be erased/programmed. Will be used for all new devices (R8C, M16C, M32C) Typically, there are only 2 bits (4 settings) for all the Flash Blocks in the device. Therefore, blocks are grouped (ie, Block 0&1, All other user blocks) Data Flash is never protected. Since the protect bits are SET every time you enter CPU-rewrite mode, an extra software step is required to remove protection to certain blocks. The Protect bits reside in the Flash registers.
Renesas Technology America, Inc. ROM Code Protect Disables Flash memory from being read out by a parallel programmer. The ROMCP register always resides in the last byte of Flash (0xFFFFF or 0xFFFFFF) Once set, user can only access flash via serial boot mode (which has a 7-byte ID code protection – next page)
Renesas Technology America, Inc. ROM Code Protect *Taken from the M16C/26 Hardware Manual! ROM Code Protect Register ROMCP
Renesas Technology America, Inc..
Flash Suspend Flash writes are fast, erases are slow. The bigger the Flash block, the longer the operation will take. Some newer devices have an Erase Suspend option that will allow you to postpone an erase operation and leave CPU-rewrite mode before it has completed (for example in order to service an interrupt). You can then go back into CPU-rewrite mode later and continue the erase. The HNDe Flash type improves the flash suspend response time (from 8ms to 90us). The first device to have HNDe will be R8C/20 Series.
Renesas Technology America, Inc. R8C Flash R8C/ has a Watchdog register at address 0xFFFF. If that address programmed to 00 (by default of NC30 compiler startup file), then the watchdog will start firing automatically after reset and interfere with CPU-rewrite operations that don’t account for this. The FlashAPI source code does not “pet” the watchdog. Therefore, you must set address FFFF to FF in your R8C startup file.
Renesas Technology America, Inc. Flash Lookup Table MCUTYPEWriteData FlashLock/ProtectSuspendNotes R8C/10,11,14,16HNDcBYTE-ProtectYesW-Dog INTR R8C/12,13HNDcBYTEA,B (2k each)ProtectYesW-Dog INTR R8C/15,17HNDcBYTEA,B (1k each)ProtectYesW-Dog INTR M16C/26HNDcWORDA,B (2k each)ProtectYesonly 1 protect bit M16C/26A,28,29HNDcWORDA,B (2k each)ProtectYes M16C/62P,6NHNDcWORDA,1 (4k each)Lock- M32C/83DINOR256 BYTE-Lock-EW0 only M32C/85,/87HNDcWORDA,1 (4k each)Lock- EW0 and EW1 Mode 7 Byte Serial ID code ALL: ROM Code Protection FoUSB Programming
Renesas Technology America, Inc. Simple Flash API R8C, M16C and M32C MCUs with HNDc type Flash are similar enough to create one API source file for all of them. Current supported devices are listed to the right. R8CM16CM32C R8C/10M16C/1N2M32C/83 R8C/11M16C/26M32C/84 R8C/12M16C/26AM32C/85 R8C/13M16C/28 R8C/14M16C/29 R8C/15M16C/62P R8C/16M16C/6N4 R8C/17M16C/6N5
Renesas Technology America, Inc. Flash API Files There are only 2 files for the API: Flash_API.c Flash_API.h There is a very simple sample program that works with all the supported MCU: FlashAPI_Sample.c There is an appnote for using this API: REU05B0058-2_Simple_Flash_API.pdf A Hew project using these files comes with the SKP’s.
Renesas Technology America, Inc. Flash API Functions There are only 2 functions in the API unsigned char FlashErase( unsigned char block ); unsigned char FlashWrite( FLASH_PTR_TYPE flash_addr, BUF_PTR_TYPE buffer_addr, unsigned int bytes) The acceptable values for “block” in FlashErase are listed in FlashAPI.h (BLOCK_0, BLOCK_1, etc…) The types FLASH_PTR_TYPE and BUF_PTR_TYPE in FlashWrite are memory pointers. The have to be defined this way in order to deal with the different memory models between the R8C, M16C and M32C. success = FlashErase( BLOCK_A ); Success = FlashWrite((FLASH_PTR_TYPE)0xF000, (BUF_PTR_TYPE) “hello”, 6);
Renesas Technology America, Inc. Flash API Setup You must define the MCU you are using. The list of valid devices are at the top of the file Flash_API.h. #define M16C_62P 1 #define R8C_13 0 You must define the Flash Mode you want to use. Mode 0 sends flash command while running from RAM. Mode 1 sends command while running in Flash. #define EW_MODE 0 #define EW_MODE 1 You must define the Bus Clock Frequncy (BCLK) you are running at. This is used to determine how much to slow the MCU down when sending commands to the flash controller. #define BCLK_FREQUENCY
Renesas Technology America, Inc. Flash API Setup You can also set up the #defines within HEW.
Renesas Technology America, Inc. Flash API Setup There are 3 things that must be #defined before using API. They are all listed in the file Flash_API.h. If something is not defined, the compiler will stop and print out a message. C:\FlashAPI\Flash_API.h 179 Error "You must specify your device in Flash_API.h first!!" C:\FlashAPI\Flash_API.c Phase M16C C Compiler finished
Renesas Technology America, Inc. New Virtual EEPROM APIs The Virtual EEPROM software was created to: –Fix design bugs in the initial release –Use the new Flash API code –Account for writing into a Flash space that wasn’t blank. –Improve reliability in loss of power situations –Allow for multiple and uniquely sized data to be stored
Renesas Technology America, Inc. Virtual EEPROM The concept is not to erase a flash block every time stored data needs to be changed. Instead, write the new data to another un-programmed location within that flash block until the entire block has been filled. Reducing the number of erase cycles of a block better utilizes and extends the life of the Flash. The Renesas Virtual EEPROM solution is a software implementation utilizing the two high Erase-Write cycle Flash blocks.
Renesas Technology America, Inc. Virtual EEPROM The virtual EEPROM API uses the Flash API in order to erase/program the Data Flash areas of the MCU Data Flash Flash API Virtual EEPROM API User Code
Renesas Technology America, Inc. Virtual EEPROM M16C/62PBLOCKS A & 14KB each M16C/Tiny Series (2x)BLOCKS A & B2KB each R8C 12,13BLOCKS A & B2KB each R8C 15,17BLOCKS A & B1KB each M32C/85BLOCKS A & 14KB each All new R8C, M16C and M32C MCUs come with two Flash blocks with 10,000 erase/write cycle. 10,000 E/W versions have special Option codes (#D7,#D9)
Renesas Technology America, Inc. Virtual EEPROM Virtual EEPROM (Fixed Method) The user has a single fixed amount of data that can be stored referred to a a SEGMENT. All the bytes in the SEGMENT must be modified at the same time. SEGMENT SIZE = 5 5 WORD array
Renesas Technology America, Inc. Virtual EEPROM (Fixed Method) To change one or more bytes, user application must read the SEGMENT array into a RAM buffer, modify, then write back VirEE Read VirEE Write Buffer in RAM Virtual EEPROM 5 WORD array
Renesas Technology America, Inc. Virtual EEPROM (Multiple/Variable Method) The user may have multiple records (segments) of variable length that may be modified individually. Virtual EEPROM User assigned record label (0-254) 24 data 17 data 50 data 10 data Size in WORDs -1 (1-255) User data for that record
Renesas Technology America, Inc. Virtual EEPROM Example #include “virtEE_Variable.h” typedef struct { unsigned char record_label; /* 0x00 to 0xFE */ unsigned char data_size; /* number of WORDS - 1*/ unsigned int NV_FAR *pData; /* pointer to data */ } NV_type; void main(void) { NV_type EE_data; /* Read Record 1 */ EE_data.record_label = 1; NV_Read( &EE_data ); value = EE_data.pData[0]; /* Write Record 5 */ EE_data.record_label = 5; EE_data.pData[0] = 55; EE_data.pData[1] = 22; EE_data.data_size = 2; NV_Write( &EE_data );
Renesas Technology America, Inc. Continue in Lab 5