Application Note M16C/63, 64, 64A, 64C, 65, 65C, 6C, 5LD, 56D, 5L, 56, 5M, and 57 Groups
Application Note M16C/63, 64, 64A, 64C, 65, 65C, 6C, 5LD, 56D, 5L, 56, 5M, and 57 Groups
1. Abstract
This document describes the method for rewriting the MCU internal flash memory (data flash, program
ROM 1) using the user boot function in M16C/63, 64, 64A, 64C, 65 (products with 512 KB or less of
program ROM 1 only), 65C, 6C, 5LD, 56D, 5L, 56, 5M, and 57 Group products.
The user boot function is for rewriting the MCU internal flash memory with a user-selected communication
method.
(The user boot function is not the start up function of the user application program.)
This application note describes the user boot function using the M16C/64 Group.
2. Introduction
The application example described in this document applies to the following MCUs:
MCUs: M16/63, 64, 64A, 64C, 65 (products with 512 KB or less of program ROM 1 only)(1),
65C, 6C, 5LD, 56D, 5L, 56, 5M, 57 Groups
Oscillation frequency: 8 MHz
Operation frequency: 24 MHz (8 MHz in CPU rewrite mode)
Note:
1. MCUs with more than 512 KB of program ROM 1 have different software commands. Refer to
the hardware manual for details.
Careful evaluation is recommended before using the program described in this application note.
3. Application
3.1 Outline
In this application note, control rewriting the MCU flash memory using serial communication from the host
PC. Xmodem is used to transmit the MOTOROLA-S format. This application note introduces the following
three methods regarding rewrite operation: normal programming, erase ignoring the lock bit status, and
erase not ignoring the lock bit status.
The features of flash memory rewrite operation in user boot mode are described below.
• Ports (pins) for entry are freely selectable
UART1 is used in standard serial I/O mode.
• Serial programmer(1) is freely selectable
Protocols including communication method, contents of transmit/receive data (command, program
code), and communication timing can be designed.
Note:
1. In standard serial I/O mode, a serial programmer supporting M16C/63, 64, 64A, 64C, 65, 65C, 6C,
5LD, 56D, 5L, 56, 5M, and 57 Groups is necessary.
Program ROM 2
010000h
User boot program area
013FF0h
Boot code
(8-byte)
013FF8h
Address (1)
(2-byte) Port information for entry
Notes:
1. Port address for entry (Example: For P0, the address is 03E0h; for P1, the address is 03F4h)
2. Bit number of port for entry (0 to 7)
3. Port level for entry (high: 01h, low: 00h)
Figure 3.1 The User Boot Program Area and The User Boot Code Area
3.4 Set Values in the User Boot Code Area and Start Mode
Set values in the user boot code area and the corresponding start mode are described below where port
Pi_j (i = 0 to 10, j = 0 to 7) is selected for entry. Table 3.1 lists Set Values in the User Boot Code Area and
Start Mode. Only use the values listed in Table 3.1.
Table 3.1 Set Values in the User Boot Code Area and Start Mode
Port Information for Entry Port
Boot Code Address Start level Pi_j
Bit Start Mode
(13FF0h to 13FF7h) (13FF8h to select Input
(13FFAh) Level
13FF9h) (13FFBh)
00000000h — User boot mode
Standard serial I/O
High
00h mode
"UserBoot"(1) Pi register 00h to 07h Low User boot mode
address(2) (value of j) High User boot mode
01h Standard serial I/O
Low
mode
Standard serial I/O
Other than "UserBoot" — — — —
mode
Notes:
1. See Table 3.2 "UserBoot" in ASCII Code.
2. See Table 3.3 Addresses of Selectable Ports for Entry.
Configuration example for user boot code area on the following conditions:
Section name is "ubtcd_sec".
Port P10 (P10) is selected as a port for entry.
Bit 0 (P10_0) is selected as a bit for entry.
High is selected as start level.
<Section definition>
- When section address is specified in assembly language
;----------------------------------------------------------------
; User boot code area section
;----------------------------------------------------------------
.section ubtcd_sec, ROMDATA
.org 013FF0H
.section ubtcd_sec_FE, ROMDATA, ALIGN
Internal ROM
Internal RAM (Program ROM 2)
000400h 010000h
User boot program
(Rewrite operation to the flash
(Note 2) memory is not performed
in this area.) User boot
program
code area
Flash memory rewrite program Flash memory rewrite program
(RAM) (ROM)
(Note 1)
Interrupt handling program Interrupt handling program
(RAM) (ROM)
(Note 2)
00XXFFh
Notes:
2. Work RAM or stack area which is used in user boot program. 013FFFh
Internal RAM
000400h
013FF0h
User boot code area
013FFFh
Internal ROM
0X0000h
Block N
Program ROM 1
0XFFFFh
0D0000h
Block 2
0DFFFFh
0E0000h Block 1
0EFFFFh
0F0000h Block 0
Table 3.5 SFR which Changes Is Status after CPU Reset or when User Boot Mode is Selected
Value when User Boot
Address Register Symbol Reset Value
Mode is Selected
0220h Flash Memory Control Register 0 FMR0 0000 0001b 0010 0001b(1)
Note:
1. Set bit 5 to 1 to operate the FMR0 resister.
b15 b0
0000h Data register (R0)
0000h Data register (R1)
0000h Data register (R2)
0000h Data register (R3)
0000h Address register (A0)
0000h Address register (A1)
0000h Frame base register (FB)
b19 b0
00000h Interrupt table register (INTB)
Address 10000h Program counter (PC)
b15 b0
0000h User stack pointer (USP)
0000h Interrupt stack pointer (ISP)
0000h Static base register (SB)
b15 b0
Flag register (FLG)
b15 b8 b7 b0
X 0 0 0 X X X X 0 0 X 0 X X X X X: Undefined
IPL U I O B S Z D C
4.1 Overview
Program the internal flash memory using CPU rewrite mode (EW0 mode) and the user boot function in
M16C/63, 64, 64A, 64C, 65 (products with 512 KB or less of program ROM 1 only), 65C, 6C, 5LD, 56D,
5L, 56, 5M, and 57 Group products.
Programming targets
• Data flash (blocks A and B)
• Program ROM 1
Processing for the flash memory is transferred using a terminal software(1) running on a host PC.
Data written to the internal flash memory is MOTOROLA-S formatted text data and is transferred using
XMODEM protocol(2).
The received MOTOROLA-S formatted data is converted to binary data and then written to the internal
flash memory.
Notes:
1. The communication settings in the terminal software are as follows:
Bits per second: 115200
Data length: 8 bits
Parity: None
Stop bit: 1
Flow control: None
2. Only the S1 record (16-bit address length) and S2 record (24-bit address length) are processed
as data to program.
(1) Menu
Select a number from 1 to 8 which corresponds to the number shown in Table 4.2 Processing.
The menu is displayed again if a number other than 1 to 8 is entered.
1...Blank Check
2...Erase
3...Erase of selection block
4...Program Flash via XModem Download
5...Checksum calculation and CRC calculation
6...Lock of lock bit
7...Display of lock bit status
8...Unlock of lock bit and erase of selection block
>
>1
Blank checking user area...
When blocks are blank (no written data present)
Block-A is a blank.
Block-B is a blank.
Block-0 is a blank.
Block-1 is not a blank. When a block is not blank (written data present)
Block-2 is a blank.
Block-3 is a blank.
Blank check is done.
(3) Erase
When “2” is entered, the erase command is executed for blocks A, B, and 0 to 3 in the internal flash
memory. The results of the erase operations are displayed for each block. The erase operation for
blocks locked with the lock bit fails. For blocks that are not locked and are blank, the erase command
is not executed and “Succeeded.” is displayed.
>2
Really erase ALL user blocks (Y/N)?y
Erasing of Block-A ... Succeeded.
When the erase operation succeeds
Erasing of Block-B ... Succeeded.
Erasing of Block-0 ... Succeeded.
Erasing of Block-1 ... Failed. When the erase operation fails
Erasing of Block-2 ... Failed.
Erasing of Block-3 ... Succeeded.
Erasing all blocks ended.
1...Blank Check
2...Erase
3...Erase of selection block
4...Program Flash via XModem Download
5...Checksum calculation and CRC calculation
6...Lock of lock bit
7...Display of lock bit status
8...Unlock of lock bit and erase of selection block Block selected is block 3.
>3
Please select the block. Choices are 0,1,2,3,A, and B.?3
Erase Block-3 (Y/N)?y
When the erase operation succeeds
Erasing of Block-3 Succeeded.
>3
Please select the block. Choices are 0,1,2,3,A, and B.?1 Block selected is block 1.
Erase Block-1 (Y/N)?y
Erasing of Block-1 Failed. When the erase operation fails
>4
Program Flash (Y/N)y?
Start XModem download…
>4
Program Flash (Y/N)y?
Start XModem download…
>6
The lock bit is put into the state of the lock.
Lock of Block-A Failed.
When the lock bit program fails
Lock of Block-B Failed.
Lock of Block-0 Succeeded.
Lock of Block-1 Succeeded.
When the lock bit program succeeds
Lock of Block-2 Succeeded.
Lock of Block-3 Succeeded.
Block lock bit program is done.
>7
The state of the lock bit is displayed.
Block-A is unlocked.
When blocks are unlocked
Block-B is unlocked.
Block-0 is locked.
Block-1 is locked.
When blocks are locked
Block-2 is locked.
Block-3 is locked.
Read Block lock bit status is done.
1...Blank Check
2...Erase
3...Erase of selection block
4...Program Flash via XModem Download
5...Checksum calculation and CRC calculation
6...Lock of lock bit
7...Display of lock bit status
8...Unlock of lock bit and erase of selection block
>8
Please select the block. Choices are 0,1,2,3,A, and B.?0
Unlock and erase Block-0 (Y/N)?y
When the unlock and erase
Unlock and erasing of Block-0 Succeeded. operations succeed
START
Select Menu 1
(Blank check)
Not blank
Is block blank?
Blank
Select Menu 2
(Erase)
Select Menu 4
(Program)
Select Menu 5
(Checksum and CRC
calculation)
Calculation
Checksum
failed
and CRC calculated
successfully?
Calculation
successful
Select Menu 6
(Lock of lock bit)
END
4.4 Program
Functions of flash memory rewriting module (flash_drv) used in the sample user boot program are
described below.
Function InitFlashDrv
Outline Initialization of the internal flash memory rewriting module
Definition
Interface Name Content
Header
PRCR SFR (Protect register) sfr64.h
PM10 SFR (Data flash enable bit) sfr64.h
Standard function (Copy of memory
memcpy string.h
area)
WDT_INITIALIZE Watchdog timer refresh wdtRefresh.h
RAM_DATA_ADDRESS RAM address to which data copies UserBoot.h
External
Reference RAM_BASED_ROM_DATA_ADDRESS ROM address from which data UserBoot.h
copies
SIZE_OF_RAM_BASED_ROM_DATA Number of bytes of copied data UserBoot.h
RAM address to which program
RAM_PROG_ADDRESS UserBoot.h
code is copied
ROM address from which program
RAM_BASED_ROM_PROG_ADDRESS UserBoot.h
code is copied
Number of bytes of copied program
SIZE_OF_RAM_BASED_ROM_PROG UserBoot.h
code
Declaration void InitFlashDrv (void);
Type Meaning
Argument
void None
Returned Type Meaning
Value void None
Function
• Enable data flash (addresses 0E000h to 0FFFFh).
• Copy the program code for handling the internal flash memory and data to the RAM area.
• Execute watchdog timer refresh processing (refreshing watchdog timer).
Remark
This function must be executed (once) when the function of the internal flash memory rewriting module
(flash_drv) is used.
Function ClearStatusRegister
Outline Request to clear the status register of internal flash memory
Definition
Interface Name Content
Header
External
FMR00 SFR (RY/BY status flag) sfr64.h
Reference
FMR06 SFR (Program status flag) sfr64.h
FMR07 SFR (Erase status flag) sfr64.h
Declaration void ClearStatusRegister (void);
Type Meaning
Argument
void None
Returned Type Meaning
Value void None
Function
• Enter CPU rewrite mode (EW0 mode) and execute the software command (clear status register) in the
internal flash memory.
• Exit CPU rewrite mode (EW0 mode) after verifying the status register is cleared.
Remark
Exit this function when the internal flash memory is ready (FMR00 is 1), program status is normal (FMR06
is 0), and the erase status is normal (FMR07 is 0). Interrupts are disabled (I flag is cleared) while in CPU
rewrite mode (EW0 mode).
Function BlankReadCheck
Outline Blank check of a specified block (value read in flash memory is 0xFFFF)
Definition
External Interface Name Content
Header
Reference
None None None
Declaration FLASH_STATUS BlankReadCheck (FLASH_BLOCK_NUM BlockNum, UWORD *Data);
Type Meaning
The block index number for blank check in the internal
Argument FLASH_BLOCK_NUM
flash memory (value read in flash memory is 0xFFFF)
UWORD * Value read in flash memory when the block is not blank
Type Meaning
Returned
FDS_OK: Blank
Value FLASH_STATUS
FDS_NOT_BLANK: Not blank
Function
• Return FDS_OK (blank) when the data of all specified block areas in the internal flash memory is
0xFFFF.
• Store the read value in the argument *Data when the data of the specified block areas is not 0xFFFF, and
return FDS_NOT_BLANK (not blank).
Remark
None
Function BlockBlankCheck
Outline Request to blank check (state after erase) the specified block
Definition
Interface Name Content
External Header
Reference FMR00 SFR (RY/BY status flag) sfr64.h
FMR07 SFR (Erase status flag) sfr64.h
Declaration FLASH_STATUS BlockBlankCheck (FLASH_BLOCK_NUM BlockNum);
Type Meaning
Argument The block index number of the specified block for blank
FLASH_BLOCK_NUM
check (state after erase) in the internal flash memory
Type Meaning
Returned
FDS_OK: Blank
Value FLASH_STATUS
FDS_NOT_BLANK: Not blank
Function
• Enter CPU rewrite mode (EW0 mode) and execute the software command (block blank check) in the
internal flash memory.
• Determine the returned value depending on the erase status flag (FMR07) and exit CPU rewrite mode
(EW0 mode).
a) Return FDS_OK (blank) when the erase status flag is normal (FMR07 is 0).
b) Return FDS_NOT_BLANK (not blank) when the erase status flag is in error (FMR07 is 1).
Remark
The software command (clear status register) is executed when the returned value is FDS_NOT_BLANK.
Exit this function when the erase status becomes normal (FMR07 is 0) in the internal flash memory.
Interrupts are disabled (I flag is cleared) while in CPU rewrite mode (EW0 mode).
Function BlockErase
Outline Request for the auto-erase operation for the specified block
Definition
Interface Name Content
Header
External FMR00 sfr64.h
SFR (RY/BY status flag)
Reference
FMR07 SFR (Erase status flag) sfr64.h
WDT_INITIALIZE Refreshing watchdog timer wdtRefresh.h
Declaration FLASH_STATUS BlockErase (FLASH_BLOCK_NUM BlockNum, UWORD LBPEnableFlag);
Type Meaning
The block index number for the auto-erase operation in
FLASH_BLOCK_NUM
Argument the internal flash memory
LBP_ENABLE: Lock bit enabled (FMR02 is 0)
UWORD LBPEnableFlag
LBP_DISABLE: Lock bit disabled (FMR02 is 1)
Type Meaning
Returned
FDS_OK: Erase succeeded
Value FLASH_STATUS
FDS_ERASE_FAIL: Erase failed
Function
When the specified block is not blank or is locked while LBPEnableFlag is LBP_ENABLE (lock bit enabled),
the erase command is executed for the block.
When the block is blank and not locked, the erase command is not executed and FDS_OK (erase
succeeded) is returned.
When the specified block is not blank or is locked while LBPEnableFlag is LBP_DISABLE (lock bit
disabled), the lock bit is disabled and the erase command is executed for the block.
When the block is blank and not locked, the erase command is not executed and FDS_OK (erase
succeeded) is returned.
When an erase error or an illegal command error occurs, FDS_ERASE_FAIL (erase failed) is returned as
the returned value.
When the erase operation succeeds, FDS_OK (erase succeeded) is returned.
Remark
The watchdog timer is refreshed during the auto-erase operation (FMR00 is 0) in internal flash memory.
Interrupts are disabled (I flag is cleared) in CPU rewrite mode (EW0 mode).
Function Program128bytes
Outline Request for 128-byte continuous auto-program operation
Definition
Interface Name Content
External Header
Reference FMR00 SFR (RY/BY status flag) sfr64.h
FMR06 SFR (Program status flag) sfr64.h
Declaration FLASH_STATUS Program128bytes (UDWORD Address, UWORD *Data);
Type Meaning
UDWORD Start address to program in the internal flash memory
Argument
Buffer pointer for storing data to be programmed (128
UWORD *
bytes)
Type Meaning
Returned FDS_OK: Program succeeded
Value FLASH_STATUS FDS_PROG_FAIL: Program failed
FDS_ADDR_ERROR: Write address error
Function
• When the start address for programming (UDWORD Address) is not 4-byte aligned, set FDS_ADDR_ERROR
(write address error) as the returned value and terminate the operation.
• When the start address for programming (UDWORD Address) is 4-byte aligned, enter CPU rewrite mode
(EW0 mode) and execute the software command (program) for 128-byte data in the internal flash
memory.
• Determine the returned value depending on the program status flag (FMR06) and exit CPU rewrite mode
(EW0 mode).
a) Return FDS_OK (program succeeded) when the program status flag is normal (FMR06 is 0).
b) Return FDS_PROG_FAIL (program failed) when program status flag is in error (FMR06 is 1).
Remark
Interrupts are disabled (I flag is cleared) while in CPU rewrite mode (EW0 mode).
Function LockBitProgram
Outline Request to enable the lock bit of the specified block
Definition
Interface Name Content
External Header
Reference FMR00 SFR (RY/BY status flag) sfr64.h
FMR06 SFR (Program status flag) sfr64.h
Declaration FLASH_STATUS LockBitProgram (FLASH_BLOCK_NUM BlockNum);
Type Meaning
Argument The block index number of the specified block for the
FLASH_BLOCK_NUM
lock bit program in the internal flash memory
Type Meaning
Returned
Value FDS_OK: Lock bit program succeeded
FLASH_STATUS
FDS_LBP_FAIL: Lock bit program failed
Function
• Enter CPU rewrite mode (EW0 mode) and execute the software command (lock bit program) in the
internal flash memory.
• Determine the returned value depending on the program status flag (FMR06) and exit CPU rewrite mode
(EW0 mode).
a) Return FDS_OK (lock bit program succeeded) when program status flag is normal (FMR06 is 0).
b) Return FDS_LBP_FAIL (lock bit program failed) when program status flag is in error (FMR06 is 1).
Remark
Interrupts are disabled (I flag is cleared) in CPU rewrite mode (EW0 mode).
Name ReadLockBitStatus
Outline Request to obtain the lock bit status of the specified block
Definition
Interface Name Content
External Header
Reference FMR00 SFR (RY/BY status flag) sfr64.h
FMR16 SFR (Lock bit status flag) sfr64.h
Declaration FLASH_STATUS ReadLockBitStatus (FLASH_BLOCK_NUM BlockNum);
Type Meaning
Argument The block index number for obtaining the lock bit status
FLASH_BLOCK_NUM
in the internal flash memory
Type Meaning
Returned
FDS_BLOCK_LOCK: The lock bit status is 0 (locked)
Value FLASH_STATUS
FDS_BLOCK_UNLOCK: The lock bit status is 1 (unlocked)
Function
• Enter CPU rewrite mode (EW0 mode) and execute the software command (read lock bit status) in the
internal flash memory.
• Determine the returned value depending on the lock bit status (FMR16) and exit CPU rewrite mode (EW0
mode).
a) Return FDS_BLOCK_LOCK (locked) when the lock bit status is normal (FMR16 is 0).
b) Return FDS_BLOCK_UNLOCK (unlocked) when the lock bit status is in error (FMR16 is 1).
Remark
Interrupts are disabled (I flag is cleared) while in CPU rewrite mode (EW0 mode).
Name FlashReadSumCalc
Outline Calculation of checksum value and CRC-CCITT value for the specified block
Definition
Interface Name Content
External Header
Reference CRCD SFR (CRC data register) sfr64.h
CRCIN SFR (CRC input register) sfr64.h
void FlashReadSumCalc (FLASH_BLOCK_NUM BlockNum, UWORD *SumValue, UWORD
Declaration
*CrcValue);
Type Meaning
The block index number for checksum and CRC
FLASH_BLOCK_NUM
Argument calculation in the internal flash memory
UWORD * Buffer pointer for the checksum value
UWORD * Buffer pointer for the CRC value
Returned Type Meaning
Value void None
Function
Read data of all specified block areas in the internal flash memory and calculate their checksum values
and CRC values.
Remark
The checksum value (UWORD *SumValue) and the CRC value (UWORD *CrcValue) should be initialized
before this function is called.
5. Sample Code
Sample code can be downloaded from the Renesas Electronics website.
6. Reference Documents
M16C/63 Group User’s Manual: Hardware Rev.2.00
M16C/64 Group User’s Manual: Hardware Rev.1.05
M16C/64A Group User’s Manual: Hardware Rev.2.00
M16C/64C Group User’s Manual: Hardware Rev.1.00
M16C/65 Group User’s Manual: Hardware Rev.2.00
M16C/65C Group User’s Manual: Hardware Rev.1.00
M16C/6C Group User’s Manual: Hardware Rev.2.00
M16C/5LD Group, M16C/56D Group User’s Manual: Hardware Rev.1.20
M16C/5L Group, M16C/56 Group User’s Manual: Hardware Rev.1.10
M16C/5M Group, M16C/57 Group User’s Manual: Hardware Rev.1.10
The latest versions can be downloaded from the Renesas Electronics website.
C Compiler Manual
M16C Series, R8C Family C Compiler Package V.5.45
C Compiler User’s Manual Rev.2.00
The latest version can be downloaded from the Renesas Electronics website.
Inquiries
http://www.renesas.com/inquiry
Description
Rev. Date
Page Summary
1.00 Jan. 22, 2010 — First edition issued
Add: M16C/63, M16C/64C, M16C/65C, M16C/6C, M16C/5LD,
1.01 Feb. 28. 2011 —
M16C/56D, M16C/5L, M16C/56, M16C/5M, and M16C/57
1. Abstract: Added the sentence “This application note describes the
1.02 Dec. 28. 2011 1
user boot function using the M16C/64 Group”.
2 3.1 Outline: Added.
2 3.2 Introduction of User Boot Function: Rewrote the second bullet.
Figure 3.5 Processing of User Boot Mode Selection: Changed “Low-
9
voltage detect reset” to “Voltage monitor reset”.
12 4.3 Processing Outline: Revised.
Table 4.4 Files Used in the Sample Program: Modified the Outlines for
20
xmodem.c and xmodem.h.
Table 4.5 Module Interfaces:
• Modified the Remarks from FDS_OK to FDS_ERASE_FAIL.
21
• Added interfaces LBP_DISABLE and LBP_ENABLE.
• Modified the Function for FlashReadSumCalc.
BlockErase function table:
25 • Added arguments LBP_ENABLE and LBP_DISABLE.
• Modified the Function.
27 FlashReadSumCalc function table: Modified the Outline.
All trademarks and registered trademarks are the property of their respective owners.
A-1
General Precautions in the Handling of MPU/MCU Products
The following usage notes are applicable to all MPU/MCU products from Renesas. For detailed usage notes
on the products covered by this manual, refer to the relevant sections of the manual. If the descriptions under
General Precautions in the Handling of MPU/MCU Products and in the body of the manual differ from each
other, the description in the body of the manual takes precedence.