Home

M16C/Tiny Series Application Note Rewriting the User ROM Area in

image

Contents

1. 66 5 1 8 Mall MIOC C an 78 5 1 9 M16C EMO mk SEE 84 E E E E ie 86 1 Website and Support ettet a e innaka tease et ba ais a added 86 R01AN0440EJ0101 Rev 1 01 Page 2 of 86 134 NESAS Rewriting the User ROM Area in EWO Mode 3 Explanation of Example Usage 3 1 Overview The M16C Tiny series has a special mode known as CPU rewrite mode in which the user ROM area can be rewritten from the CPU by executing software commands This mode consists of two modes EWO and EWI EWO mode Advantages The CPU continues operating even while programming and erasing Interrupts generated while programming or erasing are responded quickly This applies when the interrupt handler routines are located in the RAM Disadvantages The flash memory program cannot be executed while programming or erasing Flash memory cannot be accessed for read Alarge amount of RAM is used The programming erasing routines must be located in the RAM EWI mode Advantages The amount of RAM used is small The programming erasing routines can be located in the flash memory Disadvantages The CPU remains idle in hold state while programming and erasing Interrupts generated while programming or erasing cannot be responded quickly The response time in the M16C 26 for example is 100 us typ during programming and 8 ms max during erasing In this application note a description is made of
2. 222222222222222 2 2 2 222 2 pe Write data to flash memory in the EWO mode param flashAddress in physical address on flash to begin write param buffer in address in buffer to write from param size in number of byte to write return Success or Error code retval F SUCCESS Success retval F_WRITE_ERROR Program error from the flash device retval F_CMD_SEQUENCE_ERROR Command sequence error from the Flash device retval F WRITE ADDRESS ERROR The address alignment error of the argument retval F WRITE SIZE ERROR The size error of the argument retval F DEVICE BUSY When a Read Programing Erasing requirement in Programing Erasing occurs FlashResult WriteFlashEWO F ADR flashAddress const void buffer unsigned short size gre y pe Write data to flash memory in the EWl mode param flashAddress in physical address on flash to begin write param buffer in address in buffer to write from param size in number of byte to write return Success or Error code retval F_SUCCESS Success retval F_WRITE_ERROR Program error from the flash device retval F_CMD_SEQUENCE_ERROR Command sequence error from the Flash device retval F WRITE ADDRESS ERROR The address alignment error of the argument retval F WRITE SIZE ERROR The size error of the argument retval F DEVICE BUSY When a Read Programing Erasing requirement in Programing Era
3. When the program command was executed on a write enabled Program error block but the block was not auto programmed correctly No error Successfully executed Note 1 Writing xxFFie in the second bus cycle of these commands places the flash memory into read array mode in which case the command code written in the first bus cycle is invalidated Read array command This command places the flash memory into read array mode In read array mode the content recorded in the flash memory can be read out The flash memory is placed into read array mode by writing xx FF16 in the first bus cycle Then when the address from which to read is entered in the next or the subsequent bus cycle the content of the specified address can be read out in 16 bit units Read array mode is retained intact until another command is written to the flash memory the contents of multiple addresses can be read out successively Disabling CPU rewrite mode Reset bit 1 FMRO1 in Flash Memory Control Register 0 FMRO to 0 Erase suspend To use the erase suspend function in EWO mode set the erase suspend request bit FMR41 bit in the FMRA register to 1 and then check the erase status bit FMR46 bit in the FMRA register to see that the flash memory is placed into erase suspend mode The FMR46 bit is 0 during auto erase operation and set to 1 when auto erase has stopped placed into erase suspend mode If the flash memory is pla
4. if a OxFF p7_0 0x01 amp a O Eraseing 1 Eraseed p7_1 0x01 a gt gt 1 0 Erase l suspend or erase complete p7_2 0x01 amp a gt gt 2 0 Suspend 1 not Suspend FAS telse define DEBUG OUT a tendif ifdef DEBUG M16C struct LockBitStatus unsigned char s_fmr16 1 unsigned char s fmr02 1 for SlowMCU RestoreMCU define USE_CLOCKGEAR typedef struct ProcessorMode unsigned char p_pml unsigned char p cm0 unsigned char p_cml ProcessorMode void SlowMCU ProcessorMode save void RestoreMCU ProcessorMode save endif ifndef FLASHM16C H R01AN0440EJ0101 Rev 1 01 Page 47 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode 5 1 4 flashdrvdev_ew0 c JAKKKKAKKKAKKKKKKAKKKAKKKAKKAKKKAK KKK kk k k k k k k kk k A AA A I ke eek x FILE NAME flashdrvdev_ew0 c E Ver 1 00 Ja CPU R8C M16C 26 26A 28 29 2 FUNCTION Flash low level erase read write driver by EWO mode operation cc cm Copyright C 2004 Renesas Technology Corp Copyright C 2004 Renesas Solutions Corp af All rights reserved JAKKKKAKKKAKKKKAKKAKKKAK A SId flashdrvdev_ew0 c v 1 11 2004 07 21 09 04 39 kato Exp f include lt string h gt include flashdevdrv h if defined M16C SERIES include flashml6c h elif defined R8C SERIES include flashr8c h endif ifdef FLASH MODE EWO
5. Check to see if auto erase has stopped placed in erase suspend mode FMR46 1 ARE Check to see if auto erase is completed Yes It is possible that erase operation will have finished when erase suspend mode is accepted In that case the read array command is needed in order to access FMROO 1 the flash memory No y Yes gatto du Issue the read array command because auto erase has been completed v Jump to flash memory flash memory accessible for read During erase suspend dti NOR Processing except issuing a flash command AN Jump to rewrite control program in RAM v FMR41 0 Issue an erase restart to resume erase Erase resumed Figure 3 6 Erase Suspend Processing Flowchart RO1AN0440EJ0101 Rev 1 01 Page 10 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode The flowcharts shown in the preceding pages are described in detail below 6 Setting clock and wait When the flash memory is placed in CPU rewrite mode by setting FMRO1 to 1 the following limitations apply A wait cycle must be inserted for access to any ROM and RAM area by setting PM17 to 1 The CPU clock must satisfy the conditions below Table 3 5 Clock Limitations for CPU Rewrite Mode Operating clocks Limitations Remarks Main clock 10 MHz or less Set by CMO and CM1 On chip oscillator fi ROC Set by ROCR M16C 26A 28 and 29 f2 ROC or f3 ROC and the ROCR register is set to divide by 4 or 8 PLL
6. Flash Memory Read during Interrupt Processing RO1AN0440EJ0101 Rev 1 01 2tENESAS Page 28 of 86 Rewriting the User ROM Area in EWO Mode The request from SuspendErase processing holds the currently erase suspended call too ResumeErase may be used to avoid it A sequence is shown below interrupt Driver Device 4 main Driver timer inter Explanation Suspend request i i Requested i No reques i dnterrupt generated Calling SuspendErase Wei interrupt cj LT during other than erase does SuspendErase Read array not cause any action to take return Accessible place Esso Return ul StartEraseFlash H i i Erase command input i 5 Wait for completion i of erase i Interrupt generated If SuspendErase request is e SuspendErase Erase under execution Not accessible BuspendErase request return Return Suspend request Return value F SUSPEND Erase suspend underway Interrupt generated Accessible Le lee return RestartEraseFlash i i i i Erase command input i Wait for completion i Erase under execution of erase i Not accessible Erase completed polling Error check Read status Read array Return value i F SUCCESS i issued in an interrupt wait for completion of erase is dr
7. LNLIST ECHO NS OUTDIR main_ml6c r30 gt gt ODINCMD LNLIST OUTDIR NS TARGET ODINCMD LNLIST X OUTDIR ncrt0_EW0 r30 X OUTDIR Ndepend ml c r30 X OUTDIR flashdrvdev_ew0 r30 OUTDIR main_ml6c r30 LN ODINCMD NS LNLIST RO1AN0440EJ0101 Rev 1 01 Page 84 of 86 134 NESAS Rewriting the User ROM Area in EWO Mode OUTDIR Ndepend ml c r30 N depend_ml6c c X flashdevdrv h flashdevconf h Vflashml6c h CC MRCFLAGS CFLAGS depend ml6c c S OUTDIR flashdrvdev_ew0 r30 N flashdrvdev_ew0 c X flashdevdrv h flashdevconf h Vflashml6c h CC MRCFLAGS CFLAGS flashdrvdev ew0 c OUTDIR main ml6c r30 X main_ml6c c flashdevdrv h flashdevconf h CC MRCFLAGS S CFLAGS main ml6c c OUTDIR ncrtO_EWO r30 X ncrt0_EW0 a30 X Nsect30 EWO inc AS MRAFLAGS AFLAGS ncrt0 ENO a30 FE EFE FE FE FE HE FE HE E FE FE FE E FE FE E FE E FE AE E FE FE FE E FE FE E FE FE FE E FE FE FE FE AEE HE HEE EEE REA HEE HEE HEHE HH End of makefile for TM V 3 20A COPYRIGHT C 1998 1998 2003 RENESAS TECHNOLOGY CORPORATION ALL RIGHTS RESERVED AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED ARA EEE HE HE RHEE EAHA HE RHE RHE REA HEE HE HEE SE dE de dE db d oc R01AN0440EJ0101 Rev 1 01 Page 85 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 6 Reference Hardware Manual M16C 26 Grou
8. SHE HEAP 1 HEAPSIZE equ OH else T HEAPSIZE HEAPSIZE equ 5 else HEAPSIZE equ HEAPSIZE endif H endif ES bu USTACKSIZE 0 STACKSIZE equ 300h else STACKSIZE equ USTACKSIZE endif SHE ISTACKSIZE 0 ISTACKSIZE equ 300h else ISTACKSIZE equ __ISTACKSIZE__ endif INTERRUPT VECTOR ADDRESS definition LE __R8C__ 1 VECTOR ADR equ Offd00h VECTOR ADR segu 0 700h else VECTOR ADR equ Ofedch endif list OFF include sect30 EWO inc list ON RO1AN0440EJ0101 Rev 1 01 Page 60 of 86 Rewriting the User ROM Area in EWO Mode SBDATA area definition Initialize Macro declaration N_BZERO macro TOP SECT_ mov b 00H ROL mov w TOP_ OFFFFH Al mov w sizeof SECT R3 sstr b endm N BCOPY macro FROM TO SECT mov w FROM_ amp OFFFFH AO mov b FROM_ gt gt 16 R1H mov w HTO Al mov w sizeof SECT R3 smovf b endm BZERO macro TOP SECT push w sizeof SECT gt gt 16 push w sizeof SECT amp Offffh pusha TOP gt gt 16 pusha TOP amp Offffh stk 8 glb _bzero Cali _bzero G jsr a bzero endm BCOPY macro FROM TO SECT_ push w sizeof SECT_ gt gt 16 push w sizeof SECT Offffh pusha TO gt gt 16 pusha TO Offffh pusha FROM gt gt 16 pusha FROM amp Offffh Stk 2 glb _bcopy call _bcopy G jsr a _bcopy endm Ef __R8C__ 1 r for M16C 60 30 20 10 series E
9. StartEraseFlash void F ADR SIZE OxF7FE else ResumErase err code RestartEraseFlash Check err code switch err_code case F_SUSPEND mode ERASE_TEST_RESTART break case F_SUCCESS mode ERASE_TEST_CHECK break default ErrorDisp EraseER1 break TimeOut Check Take an error when F SUSPEND occurs for one second after you start elimination if GetTotalTimer gt 1000 if mode ERASE TEST RESTART FlashReset Make condition revert with FlashReset ErrorDisp Err Tout break case ERASE_TEST_CHECK Check whether even an erase block 0xF000 0xF7FFF is being erased if 0 CheckErasedBlank void F ADR SIZE OxF000 BLOCK SIZE ErrorDisp EraseER2 mode PROGRAM TEST break case PROGRAM TEST Writing 32byte and error code check tj err code WriteFlash void F ADR SIZE 0xF000 TestData 32 if err code F SUCCESS ErrorDisp WriteERR The data being written check whether it begins to read it err code ReadFlash void F ADR SIZE 0xF000 buffer addr 32 if err code F SUCCESS ErrorDisp Read ERR Compare data of write and data of read if memcmp buffer addr TestData 32 ErrorDisp Comp ERR mode OTHER break default if GetTotalTimer gt 1000 mode ERASE_TEST_START Confirm whether a flash memory is in the blank f addr physical address on flash memory to confirm s
10. METUR iL g i Auto program under i execution Not accessible i SuspendFlash Wait for completion of auto program Auto program lt completed polling Read array Return value Read status F SUCCESS i Error check ReadFlash i return i Read array Return value i Return Accessible Estena pente en Figure 4 5 Flash Memory Read during Interrupt Processing Auto Program No problems may be incurred by executing SuspendFlash0 during other than auto program or auto erase R01AN0440EJ0101 Rev 1 01 Page 30 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 4 3 Software Interface The sample program interface is described below Table 4 3 StartEraseFlash Outline Erases the device Declaration FlashResult StartEraseFlash F_ADR flashAddress Include flash ewOdrv h Parameter Meaning Remark F ADR flashAddress Highest address of the flash memory to be erased even address Return value Meaning Value FlashResult Error code F_SUCCESSSuccessfully executed Other Error code See Table 4 13 Description This function erases the flash memory all 0xFF If SuspendErase or SuspendFlash is called in an interrupt process during erase and the interrupt processing is completed this function is terminated in the middle with the error code F SUSPEND returned In this case RestartEraseFlash described next should be called to res
11. PEC lt ANNNANDNANANANANANANNANANAHNANANANANANAAANNANANANANANANANANVNNAANANANANANANH NANANANNANANANANANANAHANNANANANANNANANANNANANN RO1AN0440EJ0101 Rev 1 01 D D DD D DD D D D D D D D D D D D gt D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D Li Li Li Li 20 20 20 20 KO NO NO NO nm NJ OO CO CO NO 0 KO KO NO to 0 0 O F NU Nu o N 00 00 BU oO o CO or B W J wd dl 3 oJ ai NU BUG O Jo wo ES O0 a O 4 ON a a so o W O1 O1 O1 O1 U 000 a oom ou SEN W B 10 Lo F NU KN U nm 1 vo W W U DB ds DB B A A WB A A 3 2 1 0 o poaq Z G OF N Rewriting the User ROM Area in EWO Mode 2tENESAS Page 71 of 86 PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC lt OU Un OU OU Uni Un Un Un Un Un Un Ua Un Un Un Un Un Ua Un OU Ua Un Un Un Un Uni Un Un Un Un Un Un Ua Un Un Un Un Ua NAN NANANANNANANANANANANAANNANANANANNANANANANAUNANN RO1AN0440EJ0101 Rev 1 01 pog DD D DD DD D D D D D D D D D gt DD D D D D DDD D D D D D D DD D D D D DD D D D D D D D D D D D D DD D D D D D D D D D D D
12. When the program is running in the flash memory read array mode is already entered into When the flash memory is being rewritten following the procedure described in this application note it is placed into read array mode when a programming process is completed Furthermore before data can be read from the data area block A or B the data area access enable bit PM10 in Processor Mode Register 1 PM1 must be set to 1 About the PM10 bit Set the PRCI bit in the PRCR register to 1 write enabled before rewriting the PM10 bit When CPU rewrite mode is enabled FMRO1 1 the PM10 bit is automatically set to 1 Table 3 12 PM10 Bit and the Status of Blocks A and B Status of blocks A and B Cannot be read Always PM10 1 during rewrite Can be read Can be rewritten In the sample program the PM10 bit is set to 1 in the device initialization process R01AN0440EJ0101 Rev 1 01 Page 21 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 3 6 Programs Running in the RAM Area The sample program includes some programs that run in the RAM In the example below the program data is stored in OFB000 6 and is executed in the RAM RAM program ram top program ram executed here program_ram section program_ram is transferred before being executed ROM FLASH 0FB00016 program_ram stored here Figure 3 13 Program Location Diagram 3 6 1 Alte
13. vector vector vector vector vector vector Rewriting the User ROM Area in EWO Mode for user vector 30 for user vector 31 vector vector vector W Ky Loc vector or user vector 4 5 for user vector 5 4 for user vector 6 3 for user vector 7 int5 for user vector 8 int4 for user vector 9 llision detection for user v10 or user vector 11 or user vector 12 put interrupt for user vect 13 r user vector 14 transmit for user vector 15 receive for user vector 16 transmit for user vector 17 receive for user vector 18 transmit for user vector 19 receive for user vector 20 A0 for user vector 21 for user vector 22 for user vector 23 for user vector 26 vector 24 for user Al A2 A3 for user A4 BO Bl for user vector 27 vector 25 B2 for user vector 28 for user vector 29 for user vector 30 for user vector 31 32 for user or MR30 33 for user or MR30 34 for user or MR30 35 for user or MR30 36 for user or MR30 37 for user or MR30 38 for user or MR30 39 for user or MR30 40 for user or MR30 41 for user or MR30 42 for user or MR30 43 for user or MR30 44 for user or MR30 45 for user or MR30 46 for user or MR30 47 for user or MR30 48 49 50 51 52 53 54 55 56 57 58 59 60 Page 69 of 86 2tENESAS Rewr
14. Flash status enum FlashStatus stat FLASH READY Flash status y struct unsigned char suspReq 1 unsigned char rstOccerred 1 f flags 0 0 define suspendReq f flags suspReq define resetOccerrd f flags rstOccerred Command target address DEPEND FSIZE F ADR SIZE com adr Prototype 74 FlashResult EraseCheckInternal void FlashResult FullStatusCheck void void SetUpLockBit void There functions are running on RAM void RestartFlashEraseRam void void FlashEraseCommandRam DEPEND FSIZE F ADR SIZE flashAddress void FlashWriteCommandRam DEPEND FSIZE F ADR SIZE flash ddress unsigned short data void WaitEraseCompleteRam DEPEND FSIZE F ADR SIZE flashAddress void FlashSuspendRam void JAKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKAK KKK KKAKKKAK KKK KKAKKKA KKK k ck kckckck ck kck ck ck ck ck ck ckck ck k Name FlashInitialize Purpose Initialization of a flash memory Returns Result Notice None KAKKKAKKAKKKAKKKAKKKAKKAKKKA KK Ck ON void FlashInitialize void _FLASH_DEVICE_INIT FlashReset RO1AN0440EJ0101 Rev 1 01 2tENESAS Page 48 of 86 Rewriting the User ROM Area in EWO Mode S EK K k ok k kok k oko ko kok k k kok k kok kok kok k k k kok kok kok kok k k k kok kok k k kok k kok kok kok k k k kok k k k k kk k k kk k k k k k k Name ReadFlashEW0 Purpose Read data from flash memory Arguments in flashAddress p
15. clock Unusable Change from the PLL clock to the main M16C 26A 28 and 29 clock using the system clock control bit CM11 in System Clock Control Register 1 CM1 There is no need to stop the operation of the PLL frequency synthesizer Furthermore if blocks A and B are rewritten 100 times or more blocks A and B must be accessed for read with one wait cycle by setting FMR17 to 1 even in other than CPU rewrite mode These are summarized below Table 3 6 CPU Rewrite Mode and Limitations State Clock limitations Use of PLL Wait CPU rewrite mode Read from any block Yes Unusable Necessary enabled Issuance of software command CPU rewrite mode Read from blocks A and B Usable Unnecessary disabled Rewritten less than 100 times Read from blocks A and B Usable Necessary Rewritten 100 times or more Read from any blocks other Usable Unnecessary than A and B RO1AN0440EJ0101 Rev 1 01 Page 11 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode Limitations on the CPU clock can be lifted during erase suspend To remove limitations on the CPU clock reset the CPU rewrite mode select bit FMROI to O disable during erase suspend The erase suspend state is retained intact even when CPU rewrite mode is disabled To resume auto erase set up registers etc to meet conditions for CPU rewrite mode enable CPU rewrite mode and then clear the erase suspend request bit FMR14 to 0 erase re
16. defined FLASH MODE ENO amp amp defined FLASH MODE ENI endif endif ifndef FLASHDEVCONF H RO1AN0440EJ0101 Rev 1 01 2tENESAS else error Please choose either R8C_SERIES or M16C SERIESN endif Check KR OK Ck ck kk kk k ck kk k kk k k ck ck ck k ck kck ck ck k ck ck kk k ck ke ke ke error Please choose FLASH MODE EWO FLASH MODE EWO or both Page 40 of 86 Rewriting the User ROM Area in EWO Mode 5 1 2 flashdevdrv h JAKKKKAKKKAKKKKAKKAKKKAKKKKAKKAKKKAK KKK kk kok k k KKK KKAKKKAKKKKKKKKKKAKKKKJ FILE NAME flashdevdrv h ja Ver 1 00 Es CPU M16C 26 j FUNCTION Flash erase read write driver El fe by ENO or ENI mode operation D Copyright C 2004 Renesas Technology Corp kj Copyright C 2004 Renesas Solutions Corp All rights reserved ef JAKKKKAKKKAKKKKKKAKKKAKKKKKKAKKKAK kk k kk k kok kok kk k kk k ck ck ck ckck ck ck ck ck ck kk kk Id flashdevdrv h v 1 5 2004 07 22 04 06 41 ikari Exp Bf tinclude flashdevconf h ifndef FLASHDEVDRV H define FLASHDEVDRV H Write Mode Define EWO Or EW1 RAR OK k ck kk ck k ck k ck k ck k ck ck ck ck ck kckck ck k ch if defined FLASH MODE EWO amp amp defined FLASH MODE EW1 define WriteFlash a b c WriteFlashEWO a b c define ReadFlash a b c ReadFlashEWO a b c define StartEraseFlash a StartEraseFlashEWO a define RestartEraseFlash RestartE
17. ee FILE NAME depend_ml6c c Pad Ver sol 00 EJ CPU M16C 26 26A 28 29 i FUNCTION Flash low level erase read write driver for M16C J orc Copyright C 2004 Renesas Technology Corp Copyright C 2004 Renesas Solutions Corp All rights reserved k kok k k kok k kok kok kok k k k kok kok kk kok kk k kk kok k k kk k k k AAA KKAKKKAKKKKKKKKKKAKKKKJ Id depend ml c c v 1 2 2004 06 29 00 40 23 nagayoshi Exp tinclude flashdevdrv h tinclude flashml6c h ifndef M16C_SERIES terror This file is only for M16C endif Protect flash status struct LockBitStatus fmr status 0 0 JAKKKKAKKKKKKAKKKAKKKAKKKKKKAKKKAK KKK KKAKKKAK KKK KKAKKKAK KKK k k k k k k k k k k k k k k k k k k k Name SlowMCU Purpose In order to rewrite a flash memory the clock of MCU operation is made late Flash Accsess time nomal gt slow Arguments address in area to save CPU clock setting Return None Notice It depends on your system You must modify this function for your system This function is the example of M16C 26 Xin 20MHz KAKKKAKKKA KKK kok kok kok k kok kok kk kok KKK KKK K KKK KKAKKKAKKKAKKKKKKAKKKAKKKKKKKKKKKKKKKKJ void SlowMCU ProcessorMode save asm FCLR I save gt p_cm0 cm0 Save current CPU clock setting save gt p_cml cml save gt p_pml pml Modify for your system This code is sample for M16C 26 Xin 20MHz prer 3 Unprotect
18. glb BankSelect _ BankSelect equ OBH macro define for special page Format SPECIAL number SPECIAL macro NUM org OFFFFEH NUM 2 glb SPECIAL QNUM word SPECIAL QNUM OFFFFH R01AN0440EJ0101 Rev 1 01 Page 61 of 86 134 NE SAS endm Rewriting the User ROM Area in EWO Mode Y Interrupt section start insf start S 0 glb start section interrupt ldc mov b mov b mov b mov b mov b mov b ldc ldc ldc ldintb istack top isp set 03h 0ah 00h 04h set 00100000B 07h set CMI 00000000B 06h set CMO 00000000B OCh set CM2 00h 0ah 0080h flg Stack top sp set data SE top sb set VECTOR ADR istack pointer processer mode stack pointer sb register Program RAM Initialize N BCOPY from addr program ram top program ram N BCOPY VECTOR ADR vector table vector ram ldintb vector table ldc vector table gt gt 16 INTBH ldc 4 vector table amp OFFFFh INTBL NEAR area initialize N BZERO bss SE top bss SE N_BZERO bss SO top bss SO N BZERO bss NE top bss NE N BZERO bss NO top bss NO N BCOPY data SEI top data SE top data SE N BCOPY data SOI top data SO top data SO N_BCOPY data NEI top data NE top data NE N BCOPY data NOI top data NO top data NO FAR area initialize BZERO BZERO Copy edata E O BCOPY BCOPY bss FE top bss FE bss FO top bss FO section from e
19. range specified by Renesas Electronics especially with respect to the maximum rating operating supply voltage range movement power voltage range heat radiation characteristics installation and other product characteristics Renesas Electronics shall have no liability for malfunctions or damages arising out of the use of Renesas Electronics products beyond such specified ranges 9 Although Renesas Electronics endeavors to improve the quality and reliability of its products semiconductor products have specific characteristics such as the occurrence of failure at a certain rate and malfunctions under certain use conditions Further Renesas Electronics products are not subject to radiation resistance design Please be sure to implement safety measures to guard them against the possibility of physical injury and injury or damage caused by fire in the event of the failure of a Renesas Electronics product such as safety design for hardware and software including but not limited to redundancy fire control and malfunction prevention appropriate treatment for aging degradation or any other appropriate measures Because the evaluation of microcomputer software alone is very difficult please evaluate the safety of the final products or system manufactured by you 10 Please contact a Renesas Electronics sales office for details as to environmental matters such as the environmental compatibility of each Renesas Electronics product Please use Renesas Electronics p
20. registers CMO and PMO cml 0x60 Use Xin Xin drive HIGH Xin 2 f2 eml 1 Use Xin Xin drive HIGH Xin 2 f2 cm17 0 Use Xin Xin drive HIGH Xin 2 f2 cm06 0 CM16 and CM17 are valid pml7 1 enable flash data block 1 wait access prer 0 Protection register back on asm FSET I AR RARA RA AA RR AA KKAKKKAK KKK k kok kok kok kk k kok kok kok k RR RR k ck ck ck ck ck kckck ck oko Name RestoreMCU Purpose Restore MCU clock Arguments address in area to load CPU clock setting Return None Notice It depends on your system You must modify this function for your system This function is the example of M16C 26 Xin 20MHz KAKKKAKKAKKKAKKKAKKKAKKAKKKA kkk kkk kok kok kk kok k k kk k ON void RestoreMCU ProcessorMode save asm FCLR I prer 3 Unprotect registers CMO and PMO R01AN0440EJ0101 Rev 1 01 Page 57 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode pml save gt p_pml cml save gt p_cml cm0 save gt p_cm0 prer 0 Protection register back on asm FSET I JAKKKKAKKKAKKAKKKAKKKAK KKK kk Ck ke kk Ck kk RAR oko kok kok kok ke kk kok kok kok k k A k k k k KKAKKKAKKKAK Name UnlockBlockFlash Purpose Disable write protect Arguments F BLOCK 0 F BLOCK 1 F ALLBLOCK All area writing is possible F BLOCK 2 F BLOCK 3 F BLOCK 4 F BLOCK 5 Block2 to 4 area writing is possible only M16C 26A 28 29 Return None Notice It depen
21. the programming procedure in EWO mode showing how to read write and erase the flash memory The procedure described in the following pages will help you understand how to rewrite the flash memory in EWO mode of the M16C Tiny series microcomputers Rewriting operation specifications for the M16C 26 Rewrite mode EWO Operating CPU frequency during 10 MHz Nete rewrite Programming time 2 bytes 75 us typ Erase time 2 Kbyte block 0 2 s typ Erase operation to erase suspend 8 ms max transition time Note For details about limitations on the on chip oscillator and PLL refer to Section 3 5 1 Auto Program and Auto Erase Procedure These limitations apply to flash memory rewrite operation in CPU rewrite mode and not to normal operation of the microcomputer RO1AN0440EJ0101 Rev 1 01 Page 3 of 86 134 NESAS Rewriting the User ROM Area in EWO Mode 3 2 About Flash Memory Flash memory is electrically programmable and erasable nonvolatile memory The following shows the manner in which the flash memory of the M16C Tiny series is accessed The flash memory can be programmed in units of one word Theflash memory is erased in block units Theflash memory cannot be accessed for read during programming and erasing 3 2 1 Operations on Flash Memory The following shows operations performed on flash memory Table 3 1 Operations and Limitations on Flash Memory Operation Description of operatio
22. 2004 06 June 25 Friday AM 10 42 Project M16C ENO HARE EARTH EEE DELETE del LNLIST PROJECT cmd FROM_ADDR 0FB000 LMC LMC30 CC NC30 AR LB30 UTL ut130 AS AS30 LIBFILE PROJECT lib OUTDIR M16CEWO MKDIR mkdir ABSFILE PROJECT x30 ODINCMD OUTDIR LN LN30 TARGET ABSFILE ECHO echo MKFILE PROJECT tmk PROJECT M16C ENO TYPE type LFLAGS MS L nc30lib G LOC program ram FROM ADDR O OUTDIR V TARGET UTLFLAGS CFLAGS c dir OUTDIR g gbool to char OR 04 OSA finfo fUD fNA fSA WNP WUP WNC Wall WUV WNUA LMCFLAGS L LIBFLAGS E G AFLAGS LM D HEAP 1 D STANDARD IO 4 1 D from addr FROM ADDR h USTACKSIZE 160h ISTACKSIZE 160h finfo O OUTDIR SUFFIXES a30 r30 c x30 lib PHONY all alle X OUTDIR TARGET PHONY clean clean DELETE OUTDIR NS TARGET DELETE ODINCMD NS LNLIST DELETE OUTDIR Yncrt0 EW0 r30 DELETE OUTDIR Ndepend ml c r30 DELETE OUTDIR flashdrvdev_ew0 r30 DELETE OUTDIR main_ml6c r30 ODINCMD NS LNLIST MKFILE ECHO MRLFLAGS S LFLAGS gt ODINCMD LNLIST ECHO OUTDIR ncrt0_EW0 r30 gt gt ODINCMD LNLIST ECHO OUTDIR Ndepend ml6c r30 gt gt ODINCMD NS LNLIST ECHO OUTDIR flashdrvdev_ew0 r30 gt gt ODINCMD
23. 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 FENU RU nm JJ o oo c Q CO oo EN WC ia U a JZ OO o Y JAY HQ JJ QY Q N O0 OO CO XO XO XO KO XO KO XO KO KO NO O kN Wu 010 10 OG or N WHE 010 Jo O O F ON BB 010 10 Lo o Rewriting the User ROM Area in EWO Mode 2tENESAS Page 72 of 86 ANNNANANNANNANNANAANANNANANANNANANAANHNNANANANANNANHANANHANNANANANANNANANANNANANANANMN NH PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC E aA vn a a aA X OO Er ES E ol on a UW BU E O1 O1 O1 O1 O1 U a a 5 O1 CO Z G PN E E O1 O1 a a C PB E DS to E E E W C C0 W ti y W CO CO CO ds dd bob do o PSB O PP N W NU O Lo HF N WHY o 4 EE N NN ow id JEA 7E No MN MN DN DY NU BUG Oy E N N D p pog DD D DD DD D dd DD D D D D D DD D D D D pg E HH ow Rewriting the User ROM Area in EWO Mode fixed vector section org Offfdch UDI lword dummy int OVER FLOW lword dummy int BRKI lword dummy int ADDRESS MATCH lword dummy int SINGLE STEP lword dummy int R01AN0440EJ0101 Rev 1 01 134 NE SAS Page 73 of 86 Rewriting the User ROM Area in EWO Mode WDT lword dum
24. 56789ABCDEF AR RAR AAA AA AA REA k kok ko e e e e e AAA AAC RA KKK KKAKKKAKKKKKKKKKK Main loop FAAC RA AAC kok kok k kok k k AAA A void main void unsigned char buffer addr 32 FlashResult err code F SUCCESS mcu init initialize MCU 5 FlashInitialize FlashMemory Initialize Ki StartTimerAl asm fset i Unlock gives a Flash block to write This example is unnecessary UnlockBlockFlash F_BLOCK_3 example When a main loop is done in 20mS and made to work The turn of the movement mode Address and contents of a test lt lt start gt gt ERASE_TEST_START 0xF000 0xF7FF Start Erasing ERASE_TEST_RESTART 0xF000 0xF7FF Restart Erasing ERASE_TEST_CHECK 0xF000 0xF7FF Erasing confirmation PROGRAM_TEST 0xF000 0xF01F Write and confirmation OTHER Wait until it passes from the erasing start for one second Judge it as the error and reset a flash memory when erasing isn t completed in one second lt lt Repetition gt gt 5 for Waiting 20ms passed while CheckTalPassed R01AN0440EJ0101 Rev 1 01 Page 79 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode switch mode case ERASE_TEST_START case ERASE_TEST_RESTART Start Restart erasing So that it may clear a suspend requirement to do ResumErase before resuming elimination E if mode ERASE_TEST_START ClearTotalTimer Make totalTimer 0 for the erasing time acquisition err code
25. 8 5FH U K Tel 44 1628 585 100 Fax 44 1628 585 900 Renesas Electronics Europe GmbH Arcadiastrasse 10 40472 D sseldorf Germany Tel 49 211 6503 0 Fax 49 211 6503 1327 Renesas Electronics China Co Ltd 7th Floor Quantum Plaza No 27 ZhiChunLu Haidian District Beijing 100083 P R China Tel 86 10 8235 1155 Fax 86 10 8235 7679 Renesas Electronics Shanghai Co Ltd Unit 204 205 AZIA Center No 1233 Lujiazui Ring Rd Pudong District Shanghai 200120 China Tel 86 21 5877 1818 Fax 86 21 6887 7858 7898 Renesas Electronics Hong Kong Limited Unit 1601 1613 16 F Tower 2 Grand Century Place 193 Prince Edward Road West Mongkok Kowloon Hong Kong Tel 852 2886 9318 Fax 852 2886 9022 9044 Renesas Electronics Taiwan Co Ltd 7F No 363 Fu Shing North Road Taipei Taiwan R O C Tel 886 2 8175 9600 Fax 886 2 8175 9670 Renesas Electronics Singapore Pte Ltd 1 harbourFront Avenue 06 10 keppel Bay Tower Singapore 098632 Tel 65 6213 0200 Fax 65 6278 8001 Renesas Electronics Malaysia Sdn Bhd Unit 906 Block B Menara Amcorp Amcorp Trade Centre No 18 JIn Persiaran Barat 46050 Petaling Jaya Selangor Darul Ehsan Malaysia Tel 60 3 7955 9390 Fax 60 3 7955 9510 Renesas Electronics Korea Co Ltd 11F Samik Lavied or Bldg 720 2 Yeoksam Dong Kangnam Ku Seoul 135 080 Korea Tel 82 2 558 3737 Fax 82 2 558 5141 2010 Renesas Electronics Corporation All rights r
26. 9 the FMRO2 bit retains its value only when CPU rewrite mode is enabled FMRO1 bit 1 whereas the FMR16 bit always retains its value regardless of whether CPU rewrite mode is enabled or not In the sample program therefore FMR02 and MFR16 are set in the processing described below Write protect by Write protect by During rewrite setting FMR16 setting FMR02 Clear set write protect Clear set write protect C During rewrite 2 Set limitations on CPU Store FMRO2 setting in rewrite mode RAM Set limitations on CPU rewrite mode Set FMRO 1 to 1 CPU rewrite mode enabled Set FMRO1 to 1 Write protect setting CPU rewrite mode enabled finished Get RAM settings PERS reflected in FMRO2 Reset FMRO1 to 0 CPU rewrite mode disabled i Rewrite processing i Lift limitations on CPU rewrite mode Clear FMRO1 to 0 CPU rewrite mode disabled Lift limitations on CPU rewrite mode Rewrite processing finished Write protect setting finished Figure 3 8 Write Protect Settings in the Sample Program In the sample program the write protect setting interface function and the rewrite interface function are separated The FMR16 bit has its value retained even when CPU rewrite mode is disabled FMRO1 bit 0 On the other hand the FMR02 bit has its value not retained and changed to O when CPU rewrite mode is disabled FMRO1 bit 0 The FMR16 bit is set in the wr
27. AR SlowMCU amp save dat Must change main clock speed to meet flash fendi f asm FCLR I FLASH E EW asm FSET I asm FCLR I SetUpLockBit asm FSET I DEBUG OUT DEBUG OUT ERASE START asm FCLR I stat FLASH ERASE RestartFlashEraseRam I flag is set in this function R01AN0440EJ0101 Rev 1 01 Page 50 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode ret EraseCheckInternal _FLASH_DIS_EW if defined USE_CLOCKGEAR RestoreMCU amp save dat Restore clock back to original speed tendif return ret JAKKKKAKKKAKKAKKKAKKKAKKKA k kok k kok kok oko k AA kok kok k k AA A k e A ck ck ck AAA AAA ke k Name EraseCheckInternal Purpose The eraseed result is judged Return Result Notice KAKKKAKKAKKKAKKKAKKKAKKAKKKAKKKKKKAKKKAKKKKKKAKKKA KKK kk RAR AAA AAA ke ko kk koe ke FlashResult EraseCheckInternal void FlashResult ret if FLASH BUSY DEBUG OUT DEBUG OUT SUSPEND stat FLASH SUSPEND ret F SUSPEND else DEBUG_OUT DEBUG_OUT_READY ret FullStatusCheck Erasing error asm FCLR I FLASH RESUME ERASE take the precaution of resumeing stat FLASH READY asm FSET I return ret JAKKKKAKKKAKKAKKKAKKKAKKKKA KKK e e e e e e e e e e e RE RR RR RE RR KKAKKKAKKAKKKAKKKAK Name WriteFlashEWO Purpose Write data to flash memory Arguments Address in flashAddress physical add on flash to begin
28. ASH DIS EW resetOccerrd 1 Wait 10us 1 10MHz 100ns DEC B lcycle JUNE 2 2cycle 5 cycle x 10us 100ns 5 20 ef cnt 20 while cnt asm FSET 1 RO1AN0440EJ0101 Rev 1 01 Page 53 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode if defined USE_CLOCKGEAR RestoreMCU amp save dat Restore clock back to original speed tendif JAKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKAK KKK KKAKKKAK kok kok k kok kok kok k k k k k kok kok k kok k kok k k k k k k Name FlashWriteCommandRam Purpose Operate write command on RAM and enable interrupt Return None Notice This functions on the RAM KKAAKKKAKKKAKKAKKKAKKKAKKAKKKAKKKAKKAKKKAKKKAKKAKKKAKKKAKKAKKKAKKKKKKAKKKAKKKKJ void FlashWriteCommandRam DEPEND_FSIZE F ADR SIZE flashAddress unsigned short data flashAddress FLASH_PRG_CMD Send write command flashAddress data Write next word of data asm ESET I while _FLASH_BUSY flashAddress FLASH_READARRAY_CMD Read allay command return ORK AAA kk Ck ko kk ke RAR Ck Kk Ck Ck kk e kk Ck kk Ck kk oko kk Ck kk Ck k kok kok kok AAC ck ck kc k ck ck ck ck k ck ck ck kc k k Name FlashEraseCommandRam Purpose Erase flash memory and enable interrupt Return None Notice This functions on the RAM KAKKKAKKAKKKAKKKAKKKAKKAKKKAKKKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKAKKKK kk ke A A ok ke xk void FlashEraseCommandRam DEPEND FSIZE F ADR SIZE flashAddress flashAddress fla
29. CCESS JAKKKKAKKKKAKKAKKKAKKKAKKKKKKAKKKAK KKK KKAKKKAK AC k kok kok kok k k k k k kok kok k k kk kok k k k k k k Read allay command Read allay command Name ResumFlashEWO void Purpose This function resumes the erasure processing suspended by SuspendFlashEWO Return None Notice Don t use under multiplex interruption KAKKKAKKKKKKAKKKAKKAKKKAKKKAK KKK KKAKKKAK KKK kk k ke kk ck ck kk kok ck kckck ck kck ck k k kk k k ko k ke FlashResult ResumFlashEWO void if stat FLASH_INT_SUSPEND DEBUG OUT DEBUG OUT ERASE START stat FLASH ERASE fmr4 0x03 amp susModeBackup RO1AN0440EJ0101 Rev 1 01 134 NE SAS Page 55 of 86 Rewriting the User ROM Area in EWO Mode suspendReq 0 return F SUCCESS return F_RESUM_ERROR tendif ifdef USE SUSPEND FLASH FUNCTION JAKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKAK KKK KKAK KKK KKAKKKAKKKAKKKKKKAKKKAKKAKKKAKKKKK Name GetFlashStatusEWO Purpose This function returns the condition of the flash memory Return None Notice This functions on the RAM KAKKKAKKAKKKAKKKAKKKAKKKAKKKAKKKAKKKAKKKA kk kok k kok k kk ck KKAKKKAKKKAKKKAKKKKKKKKKJ FlashStatus GetFlashStatusEW0 void return stat endif ifdef FLASH MODE EWO R01AN0440EJ0101 Rev 1 01 Page 56 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 5 1 5 depend_m16c c JAKKKKAKKKAKKKKAKKAKKKAKKKKAKKAKKKAK KKK kk k k k k kk k AAA AAA koe ke ke
30. EVICE BUSY When a Read Programing Erasing requirement in Programing Erasing occurs retval F SUSPEND Suspend requirement acceptance 7 FlashResult StartEraseFlashEW1 F ADR flashAddress pe Restart to erase a block of the flash memory in the EWO mode return Success or Error code retval F SUCCESS Success retval F ERASE ERROR Block erase error from the Flash device retval F CMD SEQUENCE ERROR Command sequence error from the Flash device retval F WRITE ADDRESS ERROR The address alignment error of the argument retval F DEVICE BUSY When a Read Programing Erasing requirement in Programing Erasing occurs retval F SUSPEND Suspend requirement acceptance ay FlashResult RestartEraseFlashEWO void Restart to erase a block of the flash memory in the ENI mode return Success or Error code Qretval F SUCCESS Success retval F ERASE ERROR Block erase error from the Flash device retval F CMD SEQUENCE ERROR Command sequence error from the Flash device retval F WRITE ADDRESS ERROR The address alignment error of the argument retval F DEVICE BUSY When a Read Programing Erasing requirement RO1AN0440EJ0101 Rev 1 01 ENESAS Page 42 of 86 retval A Rewriting the User ROM Area in EWO Mode in Programing Erasing occurs F_SUSPEND Suspend requirement acceptance FlashResult RestartEraseFlashEWl void
31. IONS CORPORATION ALL RIGHTS RESERVED SUO ERAS UU CORREO OR ORA KORR KORR OE RR ERRATA RETE oo ok RARE ORA OE ROG UC OS OS OO R01AN0440EJ0101 Rev 1 01 Page 65 of 86 134 NE SAS 5 1 7 sect30_EWO inc E RE C Compiler for R8C Tiny Rewriting the User ROM Area in EWO Mode M16C 60 30 20 10 COPYRIGHT C 1999 2000 2002 RENESAS TECHNOLOGY CORPORATION AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED Written by T Aoyama Sect30 inc section definition This program is applicable when using the basic I O library Id sect30 EWO inc v 1 1 2004 06 25 01 29 56 nagayoshi Exp CERERE AE Ritz le 1 A for M16C 60 30 20 10 SS SS Em O O EEE O PE EEE E E SS SSE SESE SS Arrangement of section i SBDATA area section data SE DATA org 400H data SE top Section bss SE DATA ALIGN bss SE top Section data SO DATA data SO top Section bss SO DATA bss SO top near RAM area Section data NE DATA ALIGN data NE top section bss_NE DATA ALIGN bss_NE_top section data_NO DATA data_NO_top section bss_NO DATA bss_NO_top section stack DATA blkb STACKSIZE stack top blkb ISTACKSIZE istack top R01AN0440EJ0101 Rev 1 01 Page 66 of 86 Rewriting the User ROM Area in EWO Mode heap section j O section heap DATA heap top blkb HEAPSIZE section program ram ALIGN program ram top glb program ram t
32. N BCOPY data NOI top data NO top data NO FAR area initialize BZERO BZERO bss FE top bss FE bss FO top bss FO Copy edata E O section from edata EI OI sec tion H BCOPY data FEI top data FE top data FE BCOPY data FOI top data FO top data FO ldc stack_top sp Stk 40 heap area initialize if HEAP 1 glb mbase gib mnext glb msize mov w Z4 heap top amp OFFFFH mbase mov w heap_top amp 0FFFFH mnext mov w HEAPSIZE amp OFFFFH msize endif Program RAM Initialize N BCOPY from addr program ram top program ram N BCOPY VECTOR ADR vector table vector ram ldc vector table gt gt 16 INTBH ldc amp vector table amp OFFFFh INTBL Initialize standard I O if STANDARD IO 1 glb init call _init G jsr a init endif Call main function p n n i ldc 0h fb for debuger RO1AN0440EJ0101 Rev 1 01 134 N SAS Page 64 of 86 Rewriting the User ROM Area in EWO Mode glb _main jsr a main endif P RBC exit function p NE glb exit glb Sexit exit End program Sexit jmp exit einsf dummy interrupt function dummy int reit vend zk ka OR RIAA ARA I RAR ASIA IAAI ICRA A dok ISIS AR RIA III RA kod RRR IS CK SOROR kok AA C COMPILER for R8C Tiny M16C 60 30 20 10 COPYRIGHT C 1999 2000 2002 RENESAS TECHNOLOGY CORPORATION AND RENESAS SOLUT
33. N0440EJ0101 Rev 1 01 Page 25 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode 4 1 File Configuration The sample program is comprised of the files listed below File Name Table 4 2 File Configuration Table Description flashdevconf h This file sets up the driver flashdevdrv h This header file is included when using the driver flashm16c h This is the include file in the flash memory driver for the M16C type dependent part flashdrvdev_ew0 c This is the EWO mode flash memory driver file depend m16c c This is the source of the flash memory driver for the M16C type dependent part nert0 EW0 a30 This is a C language initialization file It is an upgraded version from the standard file ncrt0 a30 with the RAM transfer processing at startup added sect80_EWO inc This is a C language section file It has had a new program section that runs in the RAM added from the earlier version sfr26 h sfr26a h sfr28 h sfr29 h These are include files for the M16C 26 26A 28 and 29 Please be sure to obtain the latest file main mlOc c This file contains a sample program usage example M16C EWO tmk This is the Makefile Specify make f M16C EWO tmk to compile it R01AN0440EJ0101 Rev 1 01 Page 26 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 4 2 Program Operation This section explains operation of the sample program as fla
34. Ora iword LWOra 1word LWOra LWOra LWOra LWOra 1word LWOra iwWord LWOra 1word dummy_int dummy_int vec dummy int timerAl int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int timerAl int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int vec dummy int e e D EL EL e UU DU oU 5 Gy UU O De DA de TIL iR e ih OB 1 ou ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int dummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int ummy int RO1AN0440EJ0101 Rev 1 01 INT1 INT2 BRK int3 f timerB timerB timerB si o4 si o3 Bus co DMAO DMA1 f Key in A D fo uart2 uart2 uarto uarto uarti uarti timer timer timer timer timer timer timer timer into intl int2 vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector
35. PU Clock Settings In CPU rewrite mode the CPU clock settings are subject to limitations To meet the conditions customize the processing in the functions listed below In the sample program the CPU clock settings are created assuming M16C 28 Xin 20 MHz Table 4 14 Functions That Need to Be Customized Function name Description void SlowMCU ProcessorMode save Corrects the CPU clock settings to overcome the limitations and saves the settings prior to correction in save void RestoreMCU ProcessorMode save For details about clock limitations refer to the contents described in Section 3 5 1 Restores the save data as CPU clock settings 4 4 2 Customizing Operation of the Driver Software Use flashdevconf h to customize the driver software Table 4 15 Definition of Sample Program Options Setup content Define name Default Contents to be set or description CPU series setting M16C_SERIES M16C_SERIES Add a M16C type dependent part to the source CPU group setting M16C 26 M16C 26A M16C 28 M16C 29 M16C 28 Set a CPU group Include the header provided for each type Add a type dependent part of lock bit settings Programming mode setting Watchdog timer clear FLASH MODE EWO ENABLE WATCHDOG RESET FLASH MODE EWO Unspecified Select the programming mode used Specify whether or not to clear the watchdog timer while waiting for completion of auto
36. REN ESAS APPLICATION NOTE M16C Tiny Series RO1ANO440EJO101 Rev 1 01 Rewriting the User ROM Area in EWO Mode Dec 28 2010 1 Abstract This application note presents a method for rewriting the flash memory in EWO mode of the M16C Tiny series microcomputers 2 Introduction The example application presented here is intended for use in the following type of microcomputer Applicable microcomputer Flash memory version of M16C Tiny series microcomputers This sample program may also be used in other M16C family microcomputers that have the SFR Special Function Registers similar to those in the M16C Tiny series However since it is possible that some functions of your microcomputer will have been altered for functional enhancements etc please consult the user s manual for confirmation Note also that careful evaluation is required before the sample program in this application note can be used R01AN0440EJ0101 Rev 1 01 Page 1 of 86 tENESAS Rewriting the User ROM Area in EWO Mode Table of Contents 3 Explanation of Example Usage sisi 3 Sol OVEMMIEW DE 3 3 2 About Flash Memo Y coronarias aa 4 3 2 1 Operations on Flash Memory is 4 3 3 Rewriting the Flash Memory in EW Mode 5 39 1 EWOMode acia dat 5 3 3 2 Flash Memory Mode Transition arrancar 6 3 3 3 Interrupts during Flash Memory Rewrite ss 7 3 4 EWO Auto Program and Auto Erase Proced
37. RO2 and FMR16 for the peBueuo Aiowew ysel ul amoexy3 y FSET enable interrupt y FCLR I disable interrupt M16C 26A 28 and 29 To set FMRO2 to 1 write O and then 1 in succession The same applies for FMR16 This will be detailed in Removing write protect Make sure no interrupts occur while writing 0 and then 1 to FMR40 Y FMR40 0 1 Also make sure no DMA transfers occur during that time Enable erase suspend FMR40 Y FSET enable interrupt FCLR disable interrupt Write 0 and then 1 to FMR40 in succession Make sure no DMA transfers occur during that time Interrupts are disabled in case a command is issued to the flash memory or erase suspend is activated during interrupt processing Read array command included Write xx5016 to the highest block address to be erased even address Clear status register Y Jump to rewrite control Therefore this step is unnecessary if no commands will be issued or erase suspend will not be activated in interrupt processing Clear the status If any error remains in the status no data can be written to the flash memory Execute processing in the RAM beginning with the first bus cycle of the block erase program in RAM Erase suspend y Write xx2016 to the highest block address to be erased even address y Write xxD016 to the highes
38. U rewrite mode FMRO 1 I Write and then 1 to FMRO1 in succession Make sure no DMA transfers occur during that time v FSET enable interrupt Make sure no interrupts occur when removing write protect FCLR I disable interrupt Also make sure no DMA transfers occur during that time v To remove write protect use FMRO2 for the M16C 26 or FMR02 and FMR16 for the M16C 26A 28 and 29 To set FMRO2 to 1 write O and then 1 in succession The same applies for FMR16 This will be detailed in Removing write protect Remove write protect v FSET enable interrupt Y Interrupts are disabled in case a command is issued to the flash memory during interrupt FCLR I disable interrupt processing Read array command included Y Therefore this step is unnecessary if no commands will be issued in interrupt processing Write xx5016 to programming address Clear the status Clear status register Y Write xx4016 to programming address Issue a program command y Write program data to Subsequent to the program command write program data to the programming address programming address Program data is handled in 2 byte units Programming commands h FSET I enable interrupt e Check to see if programming is completed FMROO 1 Yes y Check full status C
39. application for which it is not intended without the prior written consent of Renesas Electronics Renesas Electronics shall not be in any way liable for any damages or losses incurred by you or third parties arising from the use of any Renesas Electronics product for an application categorized as Specific or for which the product is not intended where you have failed to obtain the prior written consent of Renesas Electronics The quality grade of each Renesas Electronics product is Standard unless otherwise expressly specified in a Renesas Electronics data sheets or data books etc Standard Computers office equipment communications equipment test and measurement equipment audio and visual equipment home electronic appliances machine tools personal electronic equipment and industrial robots High Quality Transportation equipment automobiles trains ships etc traffic control systems anti disaster systems anti crime systems safety equipment and medical equipment not specifically designed for life support Specific Aircraft aerospace equipment submersible repeaters nuclear reactor control systems medical equipment or systems for life support e g artificial life support devices or systems surgical implantations or healthcare intervention e g excision etc and any other applications or purposes that pose a direct threat to human life 8 You should use the Renesas Electronics products described in this document within the
40. ase void Include flash ewOdrv h Parameter Meaning Remark None Return value Meaning Value None Description This function issues a request to suspend processing while completion of erase by StartEraseFlash or RestartEraseFlashO is being waited for The content of processing varies depending on the flash memory status as follows During programming No operation performed During erase A request to suspend wait for completion of erase is issued During normal No operation performed During erase suspend A request to suspend wait for completion of erase is issued If this function is called after calling the StartEraseFlash0 function StartEraseFlash suspends erase and returns with F SUSPEND Thereafter erase is restarted by RestartEraseFlash0 In this case erase may be suspended again by SuspendErase Error code None Remark RO1AN0440EJ0101 Rev 1 01 Page 35 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode Table 4 10 ResumErase Outline Cancels a request to suspend wait for completion of erase Declaration void ResumErase void Include flash_ew0drv h Parameter Meaning Remark None Return value Meaning Value None Description This function drops the request to suspend erase issued by SuspendErase If no requests occur no operation is performed Error code None Remark If this function is called after calling Suspe
41. cated in the RAM The CPU continues operating even during Auto Program and Auto Erase The interrupts whose vectors are accommodated in the variable vector table can be made usable by moving the vectors and the interrupt handler routines into the RAM area RAM Interrupt processing Rewrite routine Interrupt vector L Interrupt generated During programming alqisseooe JON Figure 3 1 Conceptual Diagram of Auto Program Operation Auto Erase has an erase suspend function The erase suspend function means suspending the auto erase processing during Auto Erase to read data from the flash memory In order for the erase suspend function to be controlled during EWO mode the registers associated with 1t must be set up in software For details refer to Section 3 5 EWO Auto Program and Auto Erase Procedure During erase suspend mode it is possible to call a process located in the flash memory or read data out of the flash memory RAM RAM RAM RAM Interrupt processing Interrupt processing Interrupt processing Interrupt processing Rewrite routine 3 Rewrite routine Rewrite routine Rewrite routine Interrupt vector AN Interrupt vector Interrupt vector Interrupt vector 2 Interrupt generated 1 Erase 4 Suspend request e 15 7 Erase restart Flash memory Blo
42. ced into erase suspend mode at the same time auto erase has finished issue the read array command before accessing the flash memory During erase suspend mode it is possible to call a process residing in the flash memory or read data out of the flash memory To resume auto erase clear the erase suspend request bit FMR41 bit in the FMR4 register to 0 from the program in the RAM R01AN0440EJ0101 Rev 1 01 Page 16 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode About differences between microcomputers in the M16C Tiny series The M16C 26 26A 28 and 29 differ in the following points with respect to the flash memory related features Table 3 11 Differences between the M16C 26 26A 28 and 29 with respect to the flash memory related features M16C 26 M16C 26A M16C 28 M16C 29 Write protect Write protected by FMR02 Write protected by FMR16 and FMRO2 FMR16 included or not Not included Included PLL included or not and PLL based rewriting PLL not included PLL based rewriting prohibited RO1AN0440EJ0101 Rev 1 01 134 NE SAS Page 17 of 86 Rewriting the User ROM Area in EWO Mode 3 4 2 Reducing the RAM Size To reduce the RAM size used by a program separate processes into those that can be and those that cannot be run in the flash memory area and then move the processes that do not need to be run in the RAM into the flash memory area It is only when the fla
43. ck A Block B E s sl E Blocks 0 3 E Interrupt processing Time N 1 Erase starts 4 A suspend request is 5 A program in the flash 7 Erase restarts to erase the 2 An interrupt occurs after issued to inspect the memory is executed or data flash memory erase started flash memory is inspected 3 Interrupt processing is 6 Control returns to the executed program in the RAM Figure 3 2 Conceptual Diagram of Auto Erase Operation In EWO mode unlike in EW1 mode the CPU continues operating even during Auto Erase or Auto Program so that interrupts generated during that time can be responded to as quickly as in normal operation However a finite amount of space must be provided in the RAM for the rewrite routine variable vector table and the relevant interrupt handler routines to be able to work In addition program transfer processing is needed RO1AN0440EJ0101 Rev 1 01 Page 5 of 86 134 NESAS Rewriting the User ROM Area in EWO Mode 3 3 2 Flash Memory Mode Transition The flash memory control registers and software commands are used to control the flash memory in the M16C Tiny series of microcomputers Software commands are generated by writing to the flash memory The diagram below shows operation modes of the flash memory during EWO mode Start from here after reset Read array mode All flash memory areas Adaptation to programming accessible for read limitations unnecessary FMRO1 0 Write O and th
44. d PMI o registers Restore the CPU clock to its original setting End of programming process Figure 3 11 Auto Program Flowchart for the RAM Reduced Version RO1AN0440EJ0101 Rev 1 01 Page 19 of 86 2tENESAS Program in the flash memory Program in the RAM At startup Transfer program for controlling rewrite in Rewriting the User ROM Area in EWO Mode Description CPU rewrite mode to other than flash memory Initialization finished Erase processing y Set clock and wait Execute processing for transfer to the RAM in the initialization process after reset Set the CPU clock to 10 MHz or less PLL unusable see page 9 for the on chip oscillator Set the PM17 bit in the PM1 register to 1 One wait cycle is inserted for access to any area Make sure no interrupts occur while writing 0 and then 1 to FMR01 Y FCLR I disable interrupt v FMRO fe de 0 FMRO1 1 v FSET enable interrupt Also make sure no DMA transfers occur during that time Enable CPU rewrite mode Write 0 and then 1 to FMRO1 in succession Make sure no DMA transfers occur during that time Make sure no interrupts occur when removing write protect y FCLR I disable interrupt Remove write protect Also make sure no DMA transfers occur during that time To remove write protect use FMRO2 for the M16C 26 or FM
45. d dummy int DMAO for user vector 11 lword dummy int DMA1 2 for user vector 12 lword dummy int input key for user vector 13 lword dummy int AD Convert for user vector 14 lword dummy int vector 15 lword dummy int vector 16 lword dummy int uart0 trance for user vector 17 lword dummy int uart0 receive for user vector 18 lword dummy int uartl trance for user vector 19 lword dummy int uartl receive for user vector 20 lword dummy int TIMER AO for user vector 21 lword dummy int TIMER A1 for user vector 22 lword dummy int TIMER A2 for user vector 23 lword dummy int TIMER A3 for user vector 24 lword dummy int TIMER A4 for user vector 25 lword dummy int TIMER BO for user vector 26 lword dummy int TIMER B1 for user vector 27 lword dummy int TIMER B2 for user vector 28 lword dummy int INTO for user vector 29 R01AN0440EJ0101 Rev 1 01 Page 68 of 86 134 NE SAS lword lword else glb glb LWOra LWOra LWOra 1word LWOra iwora LWOra 1word LWOra LWOra LWOra LWOra 1word LWOra iword LWOra 1word LWOra LWOra LWOra LWOra 1word LWOra 1vora LWOra 1word LWOra LWOra LWOra LWOra 1word endif LWOra LWOra 1word LWOra LWOra LWOra LWOra iwWord LWOra iword LWOra 1word LWOra LWOra LWOra LWOra 1word LW
46. data EI OI section data FEI top data FE top data FE data FOI top data FO top data FO RO1AN0440EJ0101 Rev 1 01 sR2ENESAS Page 62 of 86 ide stack_top sp Stk 40 Rewriting the User ROM Area in EWO Mode heap area initialize if HEAP 1 glb mbase glb mnext glb msize mov w mov w mov w mov w mov w endif heap top amp OFFFFH heap top gt gt 16 mbase 2 heap topsOFFFFH mov w Z4 heap top gt gt 16 mnext 2 HEAPSIZE amp OFFFFH HEAPSIZE gt gt 16 msizet2 mbase mnext msize Initialize standard I 0 if STANDARD IO 1 glb init call _init G jSr a Init endif Call main function p ldc 0h fb for debuger glb _main jsr a main else R8C insf start S 0 glb start section interrupt ldc istack top isp mov b 02h 0ah mov b 00h 04h mov b 00h 0ah ldc 0080h flg ldc Stack top sp ldc data_SE_top sb ldintb VECTOR ADR set set istack pointer processer mode stack pointer sb register NEAR area initialize N BZERO bss SE top bss SE R01AN0440EJ0101 Rev 1 01 134 N SAS Page 63 of 86 N_BZERO bss SO top bss SO N BZERO bss NE top bss NE N BZERO bss NO top bss NO Rewriting the User ROM Area in EWO Mode N BCOPY data SEI top data SE top data SE N BCOPY data SOI top data SO top data SO N_BCOPY data NEI top data NE top data NE
47. data is read out R01AN0440EJ0101 Rev 1 01 Page 39 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 5 1 Source Code 5 1 1 flashdevconf h AR RAR k kok k k ko AA A ga FILE NAME flashdevconf h fa Ver 1 00 E CPU M16C Tiny R8C Tiny FR FUNCTION Flash erase read write driver Vus by EWO or EWl mode operation ee pece cae Copyright C 2004 Renesas Technology Corp Copyright C 2004 Renesas Solutions Corp All rights reserved di 2 El E BA SS kok kok kok k k kok kok kk k kk k k kk k kk k kk k k AAA AAA koe ke e ke Id flashdevconf h v 1 9 2004 07 02 06 12 02 kato Exp ifndef _FLASHDEVCONE_H__ define FLASHDEVCONF H define DEBUG Select CPU TYPE KAKKKAKKAKKKAKKKA k Ck Ck k k ck kck kk k kk k k k ck kckck ck kck ck ck k k AAA ke define R8C SERIES define R8C 10 define R8C 11 define R8C 12 define R8C 13 define M16C SERIES define M16 26 define M16 26A define M16 28 define M16 29 Write Mode Define EWO Or EW1 KAKKKAKKAKKKAKKKAKKAKKKAKKKKKKKKKKKJ define FLASH MODE ENO define FLASH MODE ENI Watch Dog Reset Enable define ENABLE WATCHDOG RESET define ENABLE FLASH ERR CHECK USE SuspendFlash function define USE SUSPEND FLASH FUNCTION if defined M16C SERIES define F ADR SIZE far elif defined R8C SERIES define F ADR SIZE undef ENABLE FLASH ERR CHECK if
48. ds on your system KAKKKAKKKKKKAKKKAKKKAKKAKKKAKKKKAKKAKKKAKKKAKKKAKKKA AN void UnlockBlockFlash enum FlashBlock blockNumber if defined M16 26A defined M16_28 defined M16_29 ProcessorMode save_dat tendif asm FCLR I switch blockNumber case F_BLOCK_0 case F_BLOCK_1 case F_ALLBLOCK fmr_status s_fmr02 1 Not break fif defined M16 26A defined M16_28 defined M16_29 case F BLOCK 2 case F BLOCK 3 case F BLOCK 4 case F BLOCK 5 SlowMCU amp save dat Must change main clock speed to meet flash FLASH E EW fmrl6 0 fmrl6 1 FLASH DIS EW RestoreMCU amp save dat Restore clock back to original speed tendif break case F_ALLBLOCK case F_BLOCK_A case F_BLOCK_B default break asm FSET I JAKKAKKAKKKAKKAKKKAKKKAK KKK AAA AA I RAR RRA RAR k k kok ck ck kck ck k ck ck ck ck ck ck ck ck ck oko Name LockBlockFlash Purpose Enable write protect Arguments F BLOCK 0 F BLOCK 1 Block0 1 are locked F BLOCK 2 F BLOCK 3 F BLOCK 4 F BLOCK 5 all area is locked F BLOCK A F BLOCK B don t have bit for write to lock Return None Notice It depends on your system KAKKKAKKAKKKAKKKAKKKAKKAKKKAK KKK KKAKKKAKKKAKKKAKKKAKKKAKKKAKKKKAKKAKKKAKKKKKKKKKJ void LockBlockFlash enum FlashBlock blockNumber tif defined M16 26A defined M16_28 defined M16 29 ProcessorMode save dat fendi f asm FCLR I switch blockNumber tif defi
49. ducts and in the body of the manual differ from each other the description in the body of the manual takes precedence 1 Handling of Unused Pins Handle unused pins in accord with the directions given under Handling of Unused Pins in the manual The input pins of CMOS products are generally in the high impedance state In operation with an unused pin in the open circuit state extra electromagnetic noise is induced in the vicinity of LSI an associated shoot through current flows internally and malfunctions occur due to the false recognition of the pin state as an input signal become possible Unused pins should be handled as described under Handling of Unused Pins in the manual Processing at Power on The state of the product is undefined at the moment when power is supplied The states of internal circuits in the LSI are indeterminate and the states of register settings and pins are undefined at the moment when power is supplied In a finished product where the reset signal is applied to the external reset pin the states of pins are not guaranteed from the moment when power is supplied until the reset process is completed In a similar way the states of pins in a product that is reset by an on chip power on reset function are not guaranteed from the moment when power is supplied until the power reaches the level at which resetting has been specified Prohibition of Access to Reserved Addresses Access to reserved addre
50. ed in the ROM Do not use it while executing an erase program command Note 2 Since the fixed vectors are located in block 0 do not use nonmaskable interrupts while auto erasing block 0 R01AN0440EJ0101 Rev 1 01 Page 7 of 86 tENESAS Rewriting the User ROM Area in EWO Mode 3 4 EWO Auto Program and Auto Erase Procedure 3 4 1 Auto Program and Auto Erase Procedure The following shows an auto program flowchart following the procedure described in the manual This flowchart is shown for comparison with the auto program procedure for the RAM reduced version and not a flowchart for the sample program For details about the sample program flowchart refer to Figure 3 11 RAM Reduced Version Auto Program Flowchart Program in the flash memory Program in the RAM Description Programming process Set clock and wait y Transfer program for e to Make sure the program is transferred to the RAM before executing it other than flash memory Set the CPU clock to 10 MHz or less PLL unusable see page 9 for the on chip oscillator Set the PM17 bit in the PM1 register to 1 One wait cycle is inserted for access to any area Y Jump to rewrite control program transferred to other than flash memory y Make sure no interrupts occur while writing O and then 1 to FMRO1 FCLR disable interrupt Also make sure no DMA transfers occur during that time FMROLEG Enable CP
51. elow Driver Device Appl Driver Interrupt E pp p interr pt S Read array Accessible StartEraseFlash 19e Erase command input Wait for completion i of erase Interrupt generated Interrupt ee al SuspendEr se request Return i Suspend request i Response for stopped auto erase polling Return value F_SUSPEND D Suspend processing requires a maximum of 8 ms M16C 26 Interrupt requests can be responded to during that time too erase completed pol Read array Return value F_SUCCESS Figure 4 2 Erase Suspend Processing Sequence Flow 1 Accessible RetartEraseFlash Erase restart i gt Wait for completion i of erase i i Erase under i i execution Not accessible Error check i Read status Read array Accessible SuspendErase i Erase under execution i Not accessible Erase suspend underway Called in an interrupt SuspendErase only issues a request to the driver and does not perform any operation on the flash memory The flash memory cannot be accessed for read during an interrupt When a suspend request is issued a finite time of up to 8 ms is required for the M16C 26 until auto erase of the flash memory stops For details on how to read data from the flash memory during an interrupt refer to Section 4 2 3
52. en 1 to FMRO1 bit in succession Adaptation to programming limitations necessary xx5016 Clear status xxFF16 xx7016 register 270 Other XXDO e Write data xxFF 6 FMR41 0 y Read status mode No flash memory areas accessible for read Status can be read Program complete Read status FMR41 1 Block erase Block erase complete Figure 3 3 Flash Memory Operation Modes In Figure 3 3 Flash Memory Operation Modes transitions of xx4016 etc are software commands FMR41 0 means setting the indicated register bit to 0 During EWO mode the flash memory can be placed in either read array mode where the written content can be read or status read mode where the status can be read Table 3 3 Flash Memory Read Modes Read mode Feature Read array mode The written content can be read out Read status mode The status can be read from any area of the flash memory The status indicates whether the flash memory is operating or in error Written content cannot be read out While the flash memory is in read status mode no programs can be run in the flash memory This is because the program written to the flash memory cannot be read out during read status mode RO1AN0440EJ0101 Rev 1 01 Page 6 of 86 134 NESAS Rewriting the User ROM Area in EWO Mode 3 3 3 Interrupts during Flash Memory Re
53. erase or auto program Normally do this in the main processing of the user program rather than using this processing Address error check specification ENABLE FLASH ERR CHECK ENABLE FLAS H ERR CHECK Check address alignment and 2 byte unit programming Definition for use of flash memory suspend function USE SUSPEND FLAS H FUNCTION Unspecified Define this when the SuspendFlashO function is to be used Normally use the SuspendFlashO function Definition of flash memory address F ADR SIZE Specify the definition of the write pointer that indicates the flash memory location to which to write If only the data area needs to be written to this can be defined as near RO1AN0440EJ0101 Rev 1 01 2tENESAS Page 38 of 86 Rewriting the User ROM Area in EWO Mode 5 Using the Sample Program An example method for using the sample program is shown in the main of main ml c c For details on how to use refer to the main_m16c c file The following shows specifications of the main processing Operates with M16C 28 Xin 20 MHz The main loop executed every 20 ms is created by timer A interrupt processing Block B is erased every 1 s Erase is suspended every 20 ms with control returned to the main loop This processing is performed in timer A interrupt processing After completion of erase data is written to the flash memory every 32 bytes beginning with 0F00016 and the written
54. erase or auto program Use ResumFlash0 to restart auto erase after reading data from the flash memory The operation sequence is shown below Appl Driver Interrupt A vias Read array StartEraseFlash i i Accessibie Erase command input i 5 VVait for completion oferase Interrupt generate AA Erase under auto erase polling esse Interrupt cc i execution i Not accessible i SuspendFlash i Suspend request k SuspendErase notification 1 LRPsponse for stopped Beadfiash Erase suspend underway Accessible Suspend processing reguires a maximum of 8 ms M16C 26 Read return ResumFlash Erase restart Resume riotification i return Return Wait for completion of erase Erase under execution Not accessible Interrupt generate s Hm ies bem Suspend request Response for stopped auto erase polling SuspendFlash SuspendErage notification H return ReadFlash return Return Erase suspend underway Accessible Return value F_SUSPEND Figure 4 4 Flash Memory Read during Interrupt Processing Auto Erase Appl Driver Interrupt i i Read array WriteFlash Accessible Program command input i 5 Wait for completion of auto i Interrupt generatedi program a I
55. eserved Colophon 1 0
56. fmr01 0 asm N fmr01 1 asm X define _FLASH_DIS_EW fmr01 define FLASH E fmr40 0 asm fmr40 asm define 0 1 P E lo e MI SUSPEND FLASH SUSPEND ERASE fmr41 define _ 1 FLASH_RESUME_ERASE tile unsigned short DEPEND_FSIZE unsigned unsigned unsigned unsigned unsigned unsigned short Oxff short 0x70 short 0x50 short 0x40 short 0x20 short Oxdo I e A LA DI RO1AN0440EJ0101 Rev 1 01 2tENESAS Page 46 of 86 Rewriting the User ROM Area in EWO Mode fmr41 0 define FLASH RESUME ERASE ENI a fmr41 a define FLASH EW1 MODE fmr11 0 fmrll 1 define _CLEAR_WATCHDOG wdts Ox7fff define _FLASH_BUSY fmr00 0 define FLASH DEVICE INIT X fmr17 1 N prcr 0x3 Unlock CMO CM1 PMI pml0 1 enable flash data block 4KB Virtual EEPROM access prer 0 Lock the System Clock Control Register define ERASE_ERR 0x40 define PRGRAM_ERR 0x80 define FMR07_06 PRGRAM_ERR ERASE_ERR define _FLASH_GET_STAT_FLG fmr0 amp FMRO7 06 define DEBUG_OUT_ERASE_START 0x04 xxxx100 define DEBUG OUT SUSPEND 0x02 xxxx010 define DEBUG OUT READY 0x07 xxxx111 define DEBUG OUT WAIT ERASE OxFF not use define DEBUG M16C ifdef DEBUG M16C define DEBUG OUT a
57. heck to see if programming is terminated normally y Execute read array T i command The flash memory is thereby made accessible y FMRO1 0 Disable CPU rewrite mode v Jump to flash memory v Set CMO CM1 and PMI registers End of programming process Restore the CPU clock to its original setting Figure 3 4 Auto Program Flowchart Reference RO1AN0440EJ0101 Rev 1 01 Page 8 of 86 134 NESAS Rewriting the User ROM Area in EWO Mode The following shows an auto erase flowchart following the procedure described in the manual This flowchart is shown for comparison with the auto program procedure for the RAM reduced version and not a flowchart for the sample program For details about the sample program flowchart refer to Figure 3 12 RAM Reduced Version Auto Erase Flowchart Program in the flash memory Program in the RAM Description Erase process Set clock and wait Set the CPU clock to 10 MHz or less PLL unusable see page 9 for the on chip oscillator Y Transfer program for controlling rewrite in Set the PM17 bit in the PM1 register to 1 One wait cycle is inserted for access to any area CPU rewrite mode to other than flash memory y Jump to rewrite control program transferred to other than flash memory Block erase commands Erase suspend acceptable range FMRO 0 Make sure
58. hysical add on flash to begin read out buf address in buffer to read to in size number of byte to read Return Result Notice None KAKKKAKKAKKKAKKKAKKKA KKK KKKAKKKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKAKKKKKKKKKKKKKKKKJ FlashResult ReadFlashEW0 F ADR flashAddress void buf unsigned short size if stat FLASH READY amp amp stat FLASH SUSPEND return F DEVICE BUSY memcpy buf flashAddress size return F SUCCESS JAKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKAK KKK KKAKKKAK KKK KKAKKKAK KKK k ck ck ck kck ck kckck ck ck ck kckck ck oko ke Name StartEraseFlashEWO Purpose Start to erase 1 block of the flash memory Arguments iphysical address of the head of the block on flash Return Result Notice This function erase and return result only This function does not perform retry at the time of error KAKKKAKKKKKKAKKKAKKKKKKAKKKAK KKK KKAKKKAK KKK kk k ke kk k kk kok k k kk ck k A k kk k k AAA ke kk FlashResult StartEraseFlashEW0 F ADR flashAddress FlashResult ret tif defined USE CLOCKGEAR ProcessorMode save dat fendi f DEPEND_FSIZE F_ADR_SIZE adr flashAddress ifdef ENABLE FLASH ERR CHECK 1f 0x01 amp unsigned long adr return F WRITE ADDRESS ERROR endif disable interrupt asm FCLR I if stat FLASH READY asm FSET I return F DEVICE BUSY asm FSET I suspendReq 0 initialize request resetOccerrd 0 initialize reset flags tif defined USE CLOCKGEAR S
59. ing Value FlashStatus Flash memory status Description Used for interrupts only This function restores the driver from a suspend state and let it start erasing the flash memory This function is used in pairs with SuspendFlash If this function is called during other than suspend no operation is performed Error code None Remark Name Table 4 13 Error Code enum FlashResult Meaning F SUCCESS Successfully executed F WRITE ERROR Write error A write error was notified from the flash memory F ERASE ERROR Erase error F CMD SEQUENCE ERR OR Sequence error A sequence error was notified from the flash memory F WRITE ADDRESS ERR OR Write address alignment error 2 bytes aligned F WRITE SIZE ERROR Invalid write size error written in 2 byte units F DEVICE BUSY Device busy read during programming or erase F SUSPEND Suspended by a suspend request in the middle of programming F RESUM ERROR Called by ResumFlashQ when not suspended F RESET OCCURRED R01AN0440EJ0101 Rev 1 01 When the flash memory is reset Page 37 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 4 4 The sample program requires partial setup that needs to be made for each system Customization The following describes how to customize the sample program to make it suitable for each system 4 4 1 Customizing C
60. ing the I flag internally If interrupts need to be disabled in order to use this function disable interrupts in the IPL ReadFlash Reads from flash memory UnlockBlockFlash0 Unlocks flash memory from write protect LockBlockFlash0 SuspendErase0 Locks flash memory to write protect Issues an event requesting that flash memory erase be suspended If this function is called after calling the StartEraseFlashQ function StartEraseFlash0 suspends the erase operation and returns with F_SUSPEND Thereafter the erase operation is resumed by RestartEraseFlash At this time the erase operation will be suspended again by SuspendErase ResumErase Cancels an event requesting that flash memory erase be suspended If this function is called after calling the SuspendErase function the request to suspend erase operation issued by SuspendErase is canceled SuspendFlash Suspends erasing flash memory Used for interrupts only If this function is called while the flash memory is being erased the flash memory goes to an erase suspended state After return from this function the flash memory can be accessed for read ResumFlash0 Cancels suspended erase of flash memory Used for interrupts only This function is called only when the flash memory placed in an erase suspended state by SuspendFlash needs to be erased again from where it stopped from the beginning RO1A
61. ing was done during the Programing Erasing as to the call FlashResult RO1AN0440EJ0101 Rev 1 01 Page 41 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode yes Ey PES Initialize flash device void FlashInitialize void k et Device resetting Ey void FlashReset void pe tj yon Erase flash memory in the EWO mode param flashAddress in physical address of the head of i the block on flash return Success or Error code retval F SUCCESS Success retval F ERASE ERROR Block erase error from the Flash device retval F CMD SEQUENCE ERROR Command sequence error from the Flash device retval F WRITE ADDRESS ERROR The address alignment error of the argument retval F DEVICE BUSY When a Read Programing Erasing requirement in Programing Erasing occurs retval F SUSPEND Suspend requirement acceptance 7 FlashResult StartEraseFlashEWO F_ADR flashAddress n fe Erase flash memory in the ENI mode param flashAddress in physical address of the head of the block on flash return Success or Error code retval F SUCCESS Success retval F ERASE ERROR Block erase error from the Flash device retval F CMD SEQUENCE ERROR Command sequence error from the Flash device retval F WRITE ADDRESS ERROR The address alignment error of the argument retval F D
62. int timerx lword dummy int vector 0 lword dummy int vector 1 lword dummy int vector 2 lword dummy int vector 3 lword dummy int vector 4 lword dummy int vector 5 R01AN0440EJ0101 Rev 1 01 Page 75 of 86 134 NE SAS iword iwWord 1word 1word iwWord 1word 1word iword 1word 1word iwWord 1word 1word iword 1word LWOrq 1vora 1word iword iword 1word 1word iword 1Ivora 1vora 1vora 1word 1word iwWord LWOrq 1word iword 1word 1word iWord 1word 1word iword 1vora 1word iword LWOrq 1word iword 1word 1word iword Word 1word iword 1word 1word iwWord iwWord 1word lword 1word 1word dummy_int dummy_int a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a ummy int ummy int ummy int ummy int ummy int dummy int ummy int ummy int ummy int ummy int ummy int dummy int ummy int ummy int int timerx ummy int ummy int dummy int ummy int ummy int ummy int ummy int ummy int dummy int ummy int ummy int ummy int ummy int ummy int dummy int ummy int ummy int ummy int ummy int ummy int dummy int dummy int ummy int ummy int ummy int ummy int dummy int ummy int ummy int ummy int ummy int ummy int dummy int
63. ion data_FOI ROMDATA data FOI top SERRA IIA RR eS SOR KS BOR RAS RS OS ROS IAA AAA AOI AS ISAS ICA IU i C Compiler for R8C Tiny M16C 60 30 20 10 COPYRIGHT C 1999 2000 2002 RENESAS TECHNOLOGY CORPORATION AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED BURG ROS RARA REA EORR OR o a ooo RR SS EE RO1AN0440EJ0101 Rev 1 01 Page 77 of 86 2tENESAS 5 1 8 main_m16c c Rewriting the User ROM Area in EWO Mode ok k k k k kok k k ko kok kok k k ko kok kok kok k kok k kok kok kk kok kk k kk k kk k KKK KKKAKKKAKKKKKKKKKKKJ FILE NAME main ml c c 7 Ver 1 00 EJ CPU M16C 26 Ef e FUNCTION Data Flash rewrite Apllication Note sample program for EWO mode operation E D eos les E E Copyright C 2004 Renesas Technology Corp Copyright C 2004 Renesas Solutions Corp kj All rights reserved DI JAKKKKAKKKAKKAKKKAKKKAK KKK RR RR RR RR ck kc k ck k ck ck ckck ck k k ck ck k k sk kk SId main ml c c v 1 6 2004 06 30 02 28 41 nagayoshi Exp include lt string h gt include stdio h include sfr28 h include flashdevdrv h ifndef FALSE define FALSE 0 endif ifndef TRUE define TRUE 1 endif typedef unsigned char BOOL define BLOCK_SIZE pragma INTERRUPT timerAl_ void timerAl_int void Interrupt dummy running on ram int pragma INTERRUPT vec_dummy_int void far vec_dummy_int void void ErrorDi
64. is activated during interrupt processing Read array command included Therefore this step is unnecessary if no commands will be issued or erase suspend will not be activated in interrupt processing y Write xx2016 to the highest block address to be erased even address y Write xxD016 to the highest block address to be erased even address y FSET enable interrupt Yes y Check full status Clear the status Issue the first bus cycle of the block erase command issia the second bus cycle of the block erase command Check to see if erase operation is completed 1 No y Execute read array Check to see if erase Operation is terminated normally command v The flash memory is thereby made accessible FMRO 0 y Jump to flash memory Set CMO CM1 and PM1 Disable CPU rewrite mode registers End of erase process I Restore the CPU clock to its original setting Figure 3 5 Auto Erase Flowchart Reference RO1AN0440EJ0101 Rev 1 01 Page 9 of 86 134 NESAS Rewriting the User ROM Area in EWO Mode The following shows an erase suspend flowchart Program in the flash memory Program in the RAM Description During erase v FMR41 1 Issue a suspend request lg gt gt
65. ite protect setting interface function The FMRO2 bit is set up back again from its last set content during rewrite Before the rewrite interface function can be called the write protect setting interface function must be called in order to remove write protect The write protect information once set remains effective unless it is set again in the write protect setting interface function RO1AN0440EJ0101 Rev 1 01 Page 14 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode Program command This command writes data to the flash memory one word two bytes at a time When the program command is issued the CPU auto programs the flash memory by writing program data and verifying The auto program operation is depicted below First bus cycle of the Second bus cycle of Programming Read array program command the program command completed command Address Status changes when Read array command OF000 1216 Z Written in 2 byte units to 1216 AE Written in 2 byte units to 0016 ird flash mena ESE 8016 Kl vvritten o lis same 0F00116 3416 even address in blank 3416 same address as for the 2716 programming is 2716 address as programmed 0F00216 5616 5616 dcin 0016 completed 8016 address in 2 byte units pl A 7 c It Us a ae ee a a e M 16 16 ze Units in 16 c 16 16 lt 16 0016 Auto p
66. iting the User ROM Area in EWO Mode lword dummy int vector 61 lword dummy int vector 62 lword dummy int vector 63 fixed vector section section fvector ROMDATA fixed vector table special page defination H macro is defined in ncrt0 a30 Format SPECIAL number SPECIAL 255 SPECIAL 254 SPECIAL 253 SPECIAL 252 SPECIAL 251 SPECIAL 250 SPECIAL 249 SPECIAL 248 SPECIAL 247 SPECIAL 246 SPECIAL 245 SPECIAL 244 SPECIAL 243 SPECIAL 242 SPECIAL 241 SPECIAL 240 SPECIAL 239 SPECIAL 238 SPECIAL 237 SPECIAL 236 SPECIAL 235 SPECIAL 234 SPECIAL 233 SPECIAL 232 SPECIAL 231 SPECIAL 230 SPECIAL 229 SPECIAL 228 SPECIAL 227 SPECIAL 226 SPECIAL 225 SPECIAL 224 SPECIAL 223 SPECIAL 222 SPECIAL 221 SPECIAL 220 SPECIAL 219 SPECIAL 218 SPECIAL 217 SPECIAL 216 SPECIAL 215 SPECIAL 214 SPECIAL 213 SPECIAL 212 SPECIAL 211 SPECIAL 210 SPECIAL 209 SPECIAL 208 SPECIAL 207 SPECIAL 206 SPECIAL 205 SPECIAL 204 RO1AN0440EJ0101 Rev 1 01 Page 70 of 86 134 NE SAS PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC PEC
67. ize Number of bytes to confirm return l blank 0 not blank R01AN0440EJ0101 Rev 1 01 Page 80 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode int CheckErasedBlank void F_ADR_SIZE f_addr short size unsigned char F_ADR_SIZE faddr f_addr FlashResult err_code F_SUCCESS unsigned short r_buf 16 unsigned short r_size for size gt 0 r size size gt sizeof r buf sizeof r buf size err code ReadFlash faddr r buf r_size if CmpBlank unsigned char F ADR SIZE r buf r_size return 0 faddr r_size size r_size return 1 Compare the matter whether designated data are BLANK PATTERN f addr physical address on flash memory to confirm size Number of bytes to confirm return l blank 0 not blank KI define BLANK PATTERN Oxff int CmpBlank unsigned char F ADR SIZE buf short size while size if buf BLANK_PATTERN return 1 return 0 Error display and cancellation of a movement KI void ErrorDisp const char dt if DISPLAY 1 dt while 1 initialize MCU el void mcu init void Select full speed operation Switch port initialization pdi0 5 0 change switch ports to inputs pdl0 6 0 pdl0 7 0 LED initialization pd7 0 1 Change LED ports to outputs connected to LEDs pd7 1 1 pd7 2 1
68. locks flash memory blocks to lift write protect Declaration void UnlockBlockFlash enum FlashBlock blockNumber Include flash_ewOdrv h Parameter Meaning Remark enum FlashBlock Level of write protect to be lifted When F_ALLBLOCK all blocks blockNumber are removed of write protect Return value Meaning Value None Description This function unlocks flash memory blocks to lift write protect F_BLOCK_2 to F_BLOCK_5 Blocks 2 3 4 5 Aand B are write enabled M16C 26A 28 and 29 F BLOCK Oto F BLOCK 1 Blocks 0 1 2 3 4 5 A and B are write enabled Error code None Remark RO1AN0440EJ0101 Rev 1 01 Page 34 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode Table 4 8 LockBlockFlash Outline Locks flash memory blocks to write protect Declaration void LockBlockFlash enum FlashBlock blockNumber Include flash ewOdrv h Parameter Meaning Remark enum FlashBlock Level be write protected When F ALLBLOCK all blocks blockNumber are write protected Return value Meaning Value None Description This function locks flash memory blocks to write protect F BLOCK 2to F BLOCK 5 Blocks 0 1 2 3 4 and 5 are write enabled F BLOCK Oto F BLOCK 1 Blocks O and 1 are write protected Error code None Remark Table 4 9 SuspendErase Outline Issues a request to suspend wait for completion of erase Declaration void SuspendEr
69. lowMCU amp save dat Must change main clock speed to meet flash fendi f asm FCLR I FLASH E EW asm FSET I asm FCLR I SetUpLockBit asm FSET I asm FCLR I FLASH E SUSPEND R01AN0440EJ0101 Rev 1 01 Page 49 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode asm FSET I T Clear status register T asm FCLR I adr FLASH CLEAR STSREGS CMD Clear status register com adr adr stat FLASH ERASE T Flash command Erase entry T DEBUG OUT DEBUG OUT ERASE START FlashEraseCommandRam adr I flag is set in this function ret EraseCheckInternal FLASH DIS EW if defined USE CLOCKGEAR RestoreMCU amp save dat Restore clock back to original speed fendi f return ret JAKKKKAKKKAKKAKKKAKKKAKKKKAKKAKKKAK KKK KKK K KKK KKAKKKAKKKAKKKKKKAK RAR k k k RRA k k k k Name RestartEraseFlashEWO Purpose Restart to erase 1 block of the flash memory Return Result Notice This function erases flash memory with operating erase command It does not perform retry at the time of error KAKKKAKKAKKKAKKKAKKKAKKAKKKAK KKK KKAKKKA kk kok k k kk k k kkk kkk kkk kkk k kkk kkk kkk kkk k FlashResult RestartEraseFlashEWO void FlashResult ret if defined USE_CLOCKGEAR ProcessorMode save_dat fendi f asm FCLR I if stat FLASH SUSPEND asm FSET I return F DEVICE BUSY asm FSET I if defined USE CLOCKGE
70. my int DBC lword dummy int NMI lword dummy int org ffffcH RESET lword start else R8C for R8C Tiny SBDATA area Section data SE DATA org 400H data SE top section bss SE DATA ALIGN bss SE top section data SO DATA data SO top section bss SO DATA bss SO top near RAM area section data NE DATA ALIGN data NE top section bss NE DATA ALIGN bss NE top section data NO DATA data NO top section bss NO DATA bss NO top section stack DATA ALIGN blkb STACKSIZE stack_top blkb ISTACKSIZE istack top section heap DATA heap_top blkb HEAPSIZE RO1AN0440EJ0101 Rev 1 01 Page 74 of 86 Rewriting the User ROM Area in EWO Mode RAM program area Section program ram ALIGN program ram top glb program ram top section vector ram data ALIGN ALIGN vector table blkl 64 glb vector table section rom NE ROMDATA org 0e000H rom NE top section rom NO ROMDATA rom NO top Initial data of data section section data SEI ROMDATA ALIGN data SEI top section data SOI ROMDATA data SOI top section data NEI ROMDATA ALIGN data NEI top section data NOI ROMDATA data NOI top section switch table ROMDATA switch table top section program CODE ALIGN section interrupt CODE ALIGN section vector ROMDATA variable vector table org VECTOR ADR glb
71. n Limitation name Read Means reading out written data Any blocks cannot be accessed for read while programming and erasing Write Means changing bit value from 1 to 0 Only blank addresses can be written to Erase Means changing bit value in the entire Must be performed in block units block to 1 changed all to FF The following shows how to resolve the limitations imposed on data rewrite operation of flash memory Table 3 2 Flash Memory Limitations and How to Resolve Limitation Solution Flash memory cannot be Locate the programming erasing routines in other than the flash accessed for read while memory programming and erasing EWO mode Programs cannot be run in The CPU automatically halts while programming or erasing unable to flash memory read from flash memory EW1 mode Flash memory can only be Devise the data retention method so as to reduce the number of times erased in block units the flash memory needs to be erased Note Note This method is not discussed in this application note This application note uses EWO mode and the software in the RAM to rewrite the flash memory RO1AN0440EJ0101 Rev 1 01 Page 4 of 86 134 NESAS Rewriting the User ROM Area in EWO Mode 3 3 Rewriting the Flash Memory in EWO Mode 3 3 1 EWO Mode EWO mode allows the user ROM area to be rewritten by issuing the programming Auto Program and erasing Auto Erase commands from the program lo
72. n elimination is completed when a Suspend requirement is accepted if FLASH BUSY break return flashAddress FLASH READARRAY CMD return JAKKKKAKKKKAKKAKKKAKKKAKKKAKKAKKKAKKKAKKKAKKKAKKAKKKAKKKA KKK KKAKKKAKKAKKKAKKKAK Name WaitEraseCompleteRam Purpose Block erase command is suspe Wait until suspended Return None Notice This functions on the RAM KAKKKAKKKKAKKAKKKAKKKKAKKAKKKAK kok e e k e e k kok kok k kok k kk kok kck kc k ck kk ck ck ck ck ck k ok ko ke ke void FlashSuspendRam void FLASH_SUSPEND_ERASE while fmr46 1 JAKKKKAKKKAKKKKAKKAKKKAKKKKKKAKKKA kok kok k k k kok kok k k kok k k k k k k k k k k k k k k k k k k k k k k k k k k k k k Read allay command nded Name SuspendFlashEWO Purpose Suspend at interrupt Return Result Notice Don t use under multiplex interruption KAKKKAKKKKKKAKKKAKKAKKKAKKKAKKKA kk e e kk Ck Kk Ck Ck KKAKKKAK KKK kk k kok ck ckck ck ck ok ck RAR ck ck ok ke kk ifdef USE SUSPEND FLASH FUNCTION unsigned char susModeBackup 0 FlashResult SuspendFlashEWO void switch stat case FLASH ERASE DEBUG OUT DEBUG OUT SUSPEND suspendReg 1 stat FLASH INT SUSPEND susModeBackup fmr4 FlashSuspendRam if FLASH BUSY return F SUCCESS com adr FLASH READARRAY CMD return F SUSPEND case FLASH WRITE while FLASH BUSY com adr FLASH READARRAY CMD break default break return F SU
73. ndErase the request to suspend erase issued by SuspendErase is dropped However this does not apply if this function is called after SuspendErase has once been accepted Table 4 11 SuspendFlash Outline Checks whether the flash memory can be accessed for read during erase or program Declaration FlashResult SuspendFlash void Include flash ewOdrv h Parameter Meaning Remark None Return value Meaning Value FlashStatus Flash memory status Description Used for interrupts only This function checks whether the flash memory can be accessed for read while it is being erased It is used for the purpose of inspection when the flash memory needs to be accessed for read in an interrupt sequence The content of processing varies depending on the flash memory status as follows During programming Completion of programming is waited software loop During erase A suspend command is issued and completion of suspend or programming is waited software loop During normal No operation performed During erase suspend No operation performed Error code None Remark RO1AN0440EJ0101 Rev 1 01 Page 36 of 86 2tENESAS Outline Rewriting the User ROM Area in EWO Mode Table 4 12 ResumFlash Restores from suspend state Declaration FlashResult ResumFlash void Include flash ewOdrv h Parameter Meaning Remark None Return value Mean
74. ned M16 26A defined M16 28 defined M16_29 case F_BLOCK_2 case F_BLOCK_3 case F_BLOCK_4 case F_BLOCK_5 case F_ALLBLOCK SlowMCU amp save dat Must change main clock speed to meet flash FLASH E EW fmrl6 0 FLASH DIS EW R01AN0440EJ0101 Rev 1 01 Page 58 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode RestoreMCU amp save dat Restore clock back to original speed not break fendi f case F_BLOCK_0 case F_BLOCK_1 fmr_status s_fmr02 0 break case F_ALLBLOCK case F_BLOCK_A case F_BLOCK_B default break asm FSET I JAKKKKAKKKAKKAKKKAKKKAKKKKAKKAKKKAK KKK KKAKKKAK kok kok k kok kok kok k k ko kok kok kok k kok k kok k k A k k Name SetUpLockBit Purpose set up lock bit Notice It depends on your system FRA k kok kok kok AAC kok AAC AO void SetUpLockBit void if fmr status s fmr02 fmr02 0 fmr02 1 R01AN0440EJ0101 Rev 1 01 Page 59 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 5 1 6 ncrto EW0 a30 RR RR AR A ARR A REA RAR AR ACR lc EA C COMPILER for R8C Tiny M16C 60 30 20 10 COPYRIGHT C 1999 2000 2002 RENESAS TECHNOLOGY CORPORATION AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED ncrt0 a30 NC30 startup program This program is applicable when using the basic 1 0 library Id ncrt0_EW0 a30 v 1 4 2004 06 29 00 40 22 nagayoshi Exp RI ARRIR ERRE RARE OR RUE OE OR AUR ARR UK KR KR
75. ode to other than flash memory Initialization finished Execute processing for transfer to the RAM in the initialization process after reset Set the CPU clock to 10 MHz or less PLL unusable see page 9 for the on chip oscillator MARET CC Set the PM17 bit in the PM1 register to 1 One wait cycle is inserted for access Set clock and wait 7 to any area y Make sure no interrupts occur while writing O and then 1 to FMRO1 FCLR disable interrupt Also make sure no DMA transfers occur during that time 0 Enable CPU rewrite mode 5 Write O and then 1 to FMRO1 in succession Make sure no DMA transfers occur during that time y FSET enable interrupt Make sure no interrupts occur when removing write protect FCLRI disable interrupt Also make sure no DMA transfers occur during that time y To remove write protect use FMRO2 for the M16C 26 or FMR02 and FMR16 for the Remove write protect M16C 26A 28 and 29 To set FMRO2 to 1 write O and then 1 in succession The same applies for FMR16 v This will be detailed in Removing write protect FSET I enable interrupt Interrupts are disabled in case a command is issued to the flash memory during v peBueuo Aiowaw usej ur e1n28x3 FCLR disable interrupt interrupt processing Read array command included Therefore this step is unnecessary if no commands will be i
76. ok k k k k k k k k k k Name SuspendEraseEWO Purpose Send the suspension request of a flash memory Return None F A AE kok kok kok k k ke e e kok KKK KKAKKKAK KKK KKAKKKAK KKK k k k ke k k k k k k k k k k k k k k kkk k kk k k kkk kkk void SuspendEraseEW0 void send event to wait roop suspendReg 1 JEKKKKAKKKAKKAKKKAKKKAKKKA IR kk Ck kk Ck KKAKKKAK KKK KKAKKKA KKK k ck kck kc k ck ck ck ck ck ck ck ck ck ck ck k Name ResumEraseEWO Purpose This function clears suspension request Return None KAKKKAKKAKKKAKKKAKKKAKKAKKKAK kk kok kok kk kok kk kok k kok k kk k kk k kkk kkk k k kk ke ko kk ok ke void ResumEraseEW0 void send event to wait roop suspendReq 0 JAKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKAK KKK KKAK kk Ck K KKK KKAKKKA KK Ck Ck KKAKKKAKKAKKKAKKKAK Name FlashReset Purpose Reset flash memory Return None Notice If you use KD30 debugger it is necessary to set a setup of KD30 debugger to FreeRunMode KAKKKAKKKKKKAKKKAKKKKKKAKKKAK KKK KKAKKKA CK kk Ck kk Ck A kk k ck kk ck ck ck k kok ck ckck ck ck ok ck ck k k kk void FlashReset void unsigned char fmr01 reg unsigned char cnt if defined USE CLOCKGEAR ProcessorMode save dat SlowMCU amp save dat Must change main clock speed to meet flash endif disable interrupt asm FCLR I fmr01 reg fmr01 FLASH E EW fmstp 1 cnt 1 while cnt fmstp 0 if fmr01 reg 0 FL
77. on is specified for the program_ram section so that both sections will start on an even address This option only specifies the registered address of a set section and does not have a function to transfer the program to its run time address area To call a process in the set section be sure to transfer the desired process to the address set by the program before calling it RO1AN0440EJ0101 Rev 1 01 Page 23 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode 3 6 4 Setting Vector Addresses in the RAM Area Locate the interrupt vectors in the RAM Here explanation is made of the following operations Reserve storage for the variable vector table in the RAM Copy the necessary variable vectors from vector_table which is included as a standard component Copy them to the variable vectors in the RAM at startup First in sect30 inc reserve 4 bytes x 64 256 bytes for the vector area in the RAM RAM vector area Fr section vector_ram data ALIGN ALIGN vector table blkl 64 glb vector table At startup copy the default interrupt vectors to the interrupt vectors in the RAM to alter the vector addresses Add this setting to the startup routine ncrt0 a30 L vector area f N BCOPY VECTOR ADR vector table vector ram Ide vector table gt gt 16 INTBH ldc vector table amp OFFFFh INTBL Note that since the contents described in Section 3 7 Abo
78. op section vector ram data ALIGN ALIGN vector table blkl 64 glb vector table section rom NE ROMDATA org Of000H rom NE top section rom NO ROMDATA rom NO top section data FE DATA ODG 10000H data_FE_top section bss_FE DATA ALIGN bss_FE_top section data FO DATA data FO top Section bss FO DATA bss FO top section rom_FE ROMDATA org 0F8000H rom_FE_top section rom FO ROMDATA rom FO top Initial data of data section Section data SEI ROMDATA data SEI top section data_SOI ROMDATA data_SOI_top R01AN0440EJ0101 Rev 1 01 Page 67 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode section data_NEI ROMDATA data NEI top section data_NOI ROMDATA data_NOI_top section data_FEI ROMDATA data FEI top section data_FOI ROMDATA data FOI top section switch_table ROMDATA switch_table_top section program section interrupt E DES must be set internal ROM area section program_S section vector ROMDATA variable vector table org VECTOR ADR LE M60TYPE 1 lword dummy int vector 0 BRK lword dummy int vector 1 lword dummy int vector 2 lword dummy int vector 3 lword dummy int vector 4 lword dummy int vector 5 lword dummy int vector 6 lword dummy int vector 7 lword dummy int vector 8 lword dummy int vector 9 lword dummy int vector 10 lwor
79. opped i Response for stopped auto eras polling The SuspendErase request bod i i issued during suspend is rod i MESTRE generate stored in memory However i Bo JEST io stored only once no matter i i SuspendErase i how many times a request is Return value y already exists i F SUSPEND i 1 uspendrequest i A Response for stopped auto eras polling issued because no count functions are available Erase is suspended __ immediately after it started because a suspend request BuspendErase request SuspendErase i return ___ The suspend request is i i cleared temporarily before ResumErase i e RN restarting Read array When erase is completed m Accessible process returns with A EF SUCCESS Figure 4 3 Erase Suspend Processing Sequence Flow 2 Although the erase interface consists of synchronous functions use of the SuspendErase function allows for return from the erase interface As in Figure 4 3 Erase Suspend Processing Sequence Flow 2 SuspendErase processing may be periodically invoked using a timer in order to prevent the CPU from being occupied by StartFlashErase or RestartFlashErase RO1AN0440EJ0101 Rev 1 01 2tENESAS Page 29 of 86 Rewriting the User ROM Area in EWO Mode 4 2 3 Flash Memory Read during Interrupt Processing Use the SuspendFlash function to read data from the flash memory in an interrupt process during auto
80. p Hardware Manual Rev 0 90 M16C 28 Group Hardware Manual Rev 0 60 M16C 29 Group Hardware Manual Rev 1 00 The latest version can be downloaded from the Renesas Electronics website TECHNICAL UPDATE TECHNICAL NEWS The latest information can be downloaded from the Renesas Electronics website 7 Website and Support Renesas Electronics Website http www renesas com Inquiries http www renesas com inquiry AII trademarks and registered trademarks are the property of their respective owners R01AN0440EJ0101 Rev 1 01 134 NE ESAS Page 86 of 86 Revision Record Description Rev Date Page Summary 1 00 Mar 16 05 First edition issued 1 01 Dec 28 10 9 Figure 3 5 Auto Erase Flowchart Reference partially modified 15 Figure 3 10 Auto Erase Procedure partially modified 20 Figure 3 12 Auto Erase Flowchart for the RAM Reduced Version partially modified 31 Table 4 3 StartEraseFlash parameter meaning Beginning address of the flash memory to be erased Highest address of the flash memory to be erased even address 80 9th line 0xF000 gt OxF7FE 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 Pro
81. processing suspended by SuspendFlashEWO EJ FlashResult ResumFlashEWO void tendif tendif ifndef FLASHDEVDRV H R01AN0440EJ0101 Rev 1 01 Page 45 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 5 1 3 flashm16c h A RAR k oko kk kok kok k k kok k kok kok kok k k k kk AA FILE NAME flashml6c h Ver 1 00 CPU M16C Es FUNCTION p Copyright C 2004 Copyright C 2004 Flash erase read write driver by ENO or ENI mode operation Renesas Technology Corp Renesas Solutions Corp All rights reserved JAKKKKAKKKAKKAKKKAKKKAK KKK KKAKKKAK KKK kk k kok k k kk ck ck kc kckck ck ck ckck ck ck ck ck ck k k sk la include LE include elif include elif include elif include else endif flashml6c h v 1 4 2004 06 30 02 28 41 nagayoshi Exp flashdevconf h defined M16 26 sfr26 h defined M16_26A sfr26a h defined M16 28 str28 h defined M16 29 sfr29 h error This cpu type not support ifndef FLASHM16C H define _ FLASHM16C H typedef volatile unsigned short F ADR SIZE DEPEND F ADR typedef vola define FLASH READARRAY CMD define FLASH STSREGS CMD define FLASH CLEAR STSREGS CMD define FLASH PRG CMD define FLASH BLOCK ERASE 1 CMD define FLASH BLOCK ERASE 2 CMD define FLASH E EW
82. protect it void UnlockBlockFlash enum FlashBlock blockNumber 7 PES Enable write protect param blockNumber in The block number to unprotect it void LockBlockFlash enum FlashBlock blockNumber 15 e pel Send the suspension request of a flash memory in the EWO mode f void SuspendEraseEW0 void Es j Send the suspension request of a flash memory in the ENI mode EJ void SuspendEraseEW1 void el Clear the suspension request of a flash memory in the EWO mode sy void ResumEraseEW0 void ui yos Clear the suspension request of a flash memory in the EWO mode KY void ResumEraseEWl void el Status of flash memory or this driver typedef enum FlashStatus FLASH READY ready su FLASH_WRITE programing EJ FLASH_ERASE erasing FLASH_SUSPEND suspend erasing FLASH INT SUSPEND suspend erasing during the interruption JFlashStatus pre Z R01AN0440EJ0101 Rev 1 01 Page 44 of 86 sR2ENESAS Rewriting the User ROM Area in EWO Mode Get flash memory status for the EWO mode El FlashStatus GetFlashStatusEWO void JE s Ja Get flash memory status for the ENI mode FlashStatus GetFlashStatusEWl void ifdef FLASH MODE EWO y el Suspend at interrupt FlashResult SuspendFlashEW0 void j Ki pel This function resumes the erasure
83. raseFlashEWO define SuspendErase SuspendEraseEWO define ResumErase ResumEraseEW0 define GetFlashStatus GetFlashStatusEWO endif if defined FLASH MODE ENO amp amp defined FLASH MODE EW1 if defined FLASH MODE EWO amp amp defined FLASH MODE EW1 define WriteFlash a b c NriteFlasheni a b c define ReadFlash a b c ReadFlashEWl a b c define StartEraseFlash a StartEraseFlashEWl a define RestartEraseFlash RestartEraseFlashEWl define SuspendErase SuspendEraseEWl define ResumErase ResumEraseEWl define GetFlashStatus GetFlashStatusEWl endif if idefined FLASH MODE ENO amp amp defined FLASH MODE EW1 The address definition of the flash memory typedef void F_ADR_SIZE F_ADR Error code typedef enum FlashResult F_SUCCESS Success F_WRITE_ERROR Program error from the flash device ti F ERASE ERROR Block erase error from the Flash device ef F_CMD_SEQUENCE_ERROR Command sequence error from the Flash device F WRITE ADDRESS ERROR The address alignment error of the argument F WRITE SIZE ERROR The size error of the argument F DEVICE BUSY When a Read Programing Erasing requirement in Programing Erasing occurs x F SUSPEND Suspend requirement acceptance 7 F_RESUM_ERROR When Suspend isn t being done with ResumFlash call x F_RESET_OCCURRED Resett
84. return ret Write Pass Rewriting the User ROM Area in EWO Mode initialize reset_flags Must change main clock speed to meet flash Clear status register I flag is set in this function Write error x subract 2 from byte counter increase to next data index Ef increase to next flash index E Restore clock back to original speed AR K K K K k k RARA oko k kok k kok kok kok k k k k k kok k k kok k k k kok kok kok kok k kok kok kok k k k kok kok kk k kk k kok AAA k k k Name FullStatusCheck Purpose Check the status of flash memory Arguments None Return Result KAKKKAKKAKKKAKKKAKKKAKKKAKKKAK KKK KKAKKKAK KKK kk k ko kk kok KKK kk k ck kck ck ckck ck ck ok ck ck k I FlashResult FullStatusCheck void unsigned char reg reg FLASH GET STAT FLG if resetOccerrd return F RESET OCCURRED if reg 0 return F SUCCESS if reg PRGRAM ERR if reg ERASE ERR return F CMD SEQUENCE ERROR return F WRITE ERROR return F ERASE ERROR RO1AN0440EJ0101 Rev 1 01 Page 52 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode JAKKKKAKKKAKKKKKKAKKKAKKKKAKKAKKKAKKAKKKAKKKAK e e e IR IA IR e e KKK KKAKKKAKKKAK running on RAM KAKKKAKKAKKKAKKKAK KKK KKAKKKAK KKK KKAKKKAK KKK KKAKKKAK KKK KKAKKKAKKKAKKKAKKKKKKKKKJ pragma SECTION program program ram S EK K k K k k k k kok k k k k k k kok k kok kok oko k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k
85. ring the Section Name The following explains a method for adding another section named program ram and locating in it a program that is to be run in the RAM To relocate the program from the program section into the program ram section write a program statement as follows void ThisProgramOnROM void This program is located in the program section Defined after the program SECTION declaration are located in program_ram pragma SECTION program program_ram void ThisProgramOnRAM void This program is located in the program ram section 1 Next add to sect30 inc the program_ram section and a label description _program_ram_top at the beginning The position at which the program will be run is determined by sect30 inc In the example here it is located after heap section Note that program ram top is used to transfer the program heap section section heap top blkb H heap DATA A section to be inserted EAPSIZE RAM program area i Insert here section program_ram ALIGN program ram top glb o program ram top RO1AN0440EJ0101 Rev 1 01 Page 22 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode 3 6 2 Transferring the Program Next write a program statement to transfer the program from the ROM to the RAM In the example explained below the program is transferred at startup Use the N_BCOPY macro to transfer
86. rite protect removed before they can be rewritten To remove write protect the FMRO2 and FMR16 bits M16C 26A 28 and 29 must be set The tables below show how write protect is set for each type of microcomputer Table 3 7 Write Protect Set for the M16C 26 Register settings Blocks A and B Blocks 0 and 1 Other blocks O x O O O O O Rewritable X Not rewritable write protected Table 3 8 Write Protect Set for the M16C 26A 28 and 29 O O O O O Rewritable X Not rewritable write protected The following shows how to set FMR02 and FMR16 Table 3 9 How to Set FMR02 and FMR16 Bit name If FMRO1 bit 0 If FMRO1 bit 1 FMRO2 Always 0 Can be set To set the bit to 1 write 0 and then 1 in succession Note FMR16 Cannot be set Can be set Value retained To set the bit to 1 write 0 and then 1 in succession Note Note Make sure no interrupts or DMA transfers occur before writing 1 after writing 0 The data areas blocks A and B have an access enable bit PM10 Set the PM10 bit to 1 when accessing the data area for read When CPU rewrite mode is enabled FMRO1 1 the PM10 bit is automatically set to 1 For this reason blocks A and B do not have write protect For details about the PM10 bit refer to Section 3 6 Readout Procedure RO1AN0440EJ0101 Rev 1 01 Page 13 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode As shown in Table 3
87. roducts in compliance with all applicable laws and regulations that regulate the inclusion or use of controlled substances including without limitation the EU RoHS Directive Renesas Electronics assumes no liability for damages or losses occurring as a result of your noncompliance with applicable laws and regulations 11 This document may not be reproduced or duplicated in any form in whole or in part without prior written consent of Renesas Electronics 12 Please contact a Renesas Electronics sales office if you have any questions regarding the information contained in this document or Renesas Electronics products or if you have any other inquiries Note 1 Renesas Electronics as used in this document means Renesas Electronics Corporation and also includes its majority owned subsidiaries Note 2 Renesas Electronics product s means any product developed or manufactured by or for Renesas Electronics 434 NE SAS SALES OFFICES Renesas Electronics Corporation http www renesas com Refer to http www renesas com for the latest and detailed information Renesas Electronics America Inc 2880 Scott Boulevard Santa Clara CA 95050 2554 U S A Tel 1 408 588 6000 Fax 1 408 588 6130 Renesas Electronics Canada Limited 1101 Nicholson Road Newmarket Ontario L3Y 9C3 Canada Tel 1 905 898 5441 Fax 1 905 898 3220 Renesas Electronics Europe Limited Dukes Meadow Millboard Road Bourne End Buckinghamshire SL
88. rogram writing 8016 e 16 which pan FFt Write XX16 FF16 Write BC16 2216 program data and 2216 Write XX16 FF16 FFie 0016 verifying 8016 FF16 FF16 2216 2216 dica Frio o Program Il VR Data to be ao LI NES e Read array FFie command FFi6 written 2716 2716 command Flash memory in Flash memory in Flash memory in Li Flash memory in Flash memory in read array mode read array mode read status mode read status mode read array mode Figure 3 9 Auto Program Procedure Make sure the command is written in 16 bit units to the even addresses in the user ROM area For details about the change of flash memory modes after the CPU started programming the flash memory refer to Figure 3 3 Flash Memory Operation Modes Erase command This command erases data from the flash memory in block units When the erase command is issued the CPU auto erases the flash memory by erasing data and verifying The auto erase operation is depicted below First bus cycle of the Second bus cycle of program command the program command Erase completed Read array command Address OF7FE e Status changes when flash memory erase is Written in 2 byte units to even address OF7FF e completed 0F80016 FFi6 1 Gare XX16 Highest block address Highest block address Units in which e even address even address n Auto erase erasing erased data and verifying erased in blocks Written in 2 byte units Written in 2 byte units Read arra
89. rol laws and regulations and follow the procedures required by such laws and regulations You should not use Renesas Electronics products or the technology described in this document for any purpose relating to military applications or use by the military including but not limited to the development of weapons of mass destruction Renesas Electronics products and technology may not be used for or incorporated into any products or systems whose manufacture use or sale is prohibited under any applicable domestic or foreign laws or regulations 6 Renesas Electronics has used reasonable care in preparing the information included in this document but Renesas Electronics does not warrant that such information is error free Renesas Electronics assumes no liability whatsoever for any damages incurred by you resulting from errors in or omissions from the information included herein 7 Renesas Electronics products are classified according to the following three quality grades Standard High Quality and Specific The recommended applications for each Renesas Electronics product depends on the product s quality grade as indicated below You must check the quality grade of each Renesas Electronics product before using it in a particular application You may not use any Renesas Electronics product for any application categorized as Specific without the prior written consent of Renesas Electronics Further you may not use any Renesas Electronics product for any
90. sh memory driver The driver is always used when reading or writing to the flash memory as well as when erasing the flash memory During read or write the flash memory cannot be accessed for data readout fetch or read The driver is assumed to be run in the RAM A sequence flow of the driver during auto erase is shown below 4 2 1 Auto Program and Auto Erase Operations The following shows an example driver operation using auto program and auto erase APIs Appl Driver Interrupt Driver Gaulle H i 1 StartEraseFlash i Li or WriteFlash Erase command input or program command input 5 Wait for completion of auto erase i or wait for completion of auto i i Auto erase under execution program d i or auto program under d d execution Not accessible Auto erase or auto program completed polling Error check i i Read status Read array i Return value F SUCCESS Read array Accessible The auto program and the auto erase APIs return F SUCCESS when the respective operations are Figure 4 1 Operation of Auto Program and Auto Erase APIs 1 successfully completed RO1AN0440EJ0101 Rev 1 01 Page 27 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode 4 2 2 Erase Suspend Processing The SuspendErase function suspends the erase processing being executed by StartEraseFlashO or RestartEraseFlash The operation sequence is shown b
91. sh memory is in read status mode including cases where auto program or auto erase is underway that processes need to be run in the RAM Alter the following contents from the procedure described in Section 3 5 1 Auto Program and Auto Erase Procedure to reduce the RAM size When programming the flash memory Run all processing up to immediately preceding the first bus cycle of the program command in the flash memory Perform operations from the first bus cycle of the program command to check for completion of programming in the RAM No changes needed To perform full status check place the flash memory back into read array mode prior to the full status check Perform full status check in the flash memory When erasing the flash memory Run all processing up to immediately preceding the first bus cycle of the erase command in the flash memory Perform operations from the first bus cycle of the erase command to check for completion of auto erase in the RAM No changes needed To perform full status check place the flash memory back into read array mode prior to the full status check Perform full status check in the flash memory Next a flowchart with the above contents incorporated will be shown R01AN0440EJ0101 Rev 1 01 Page 18 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode Program in the flash memory Program in the RAM Description At startup Transfer program for controlling rewrite in CPU rewrite m
92. shAddress asm FSET I WaitEraseCompleteRam flashAddress FLASH BLOCK ERASE 1 CMD Send erase command FLASH BLOCK ERASE 2 CMD Send erase confirm command JAKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKAK KKK KKAKKKA KKK KA k k k k k k k k k k k k k k k k k k k k k k k k k k k k k k Name RestartFlashEraseRam Purpose Restart Erasing Return None Notice This functions on the RAM KAKKKAKKAKKKAKKKAKKKA KKK KKKA KK Ck kok k kok kok kok kok kok kok k ck kk ck kc k ck ck kck k kck ck kok ck ck kk k kc k sk kk void RestartFlashEraseRam void _FLASH_RESUME_ERASE 4 asm FSET I WaitEraseCompleteRam com adr return JAKKKKAKKKAKKAKKKAKKKAKKKKKKAKKKA kk kok k kok kok kok kok ko kok kok kok Ck ck kk ck kk ck ck kck ck k ck ck ck ck OK k Name WaitEraseCompleteRam Purpose Wait until complete or suspended Flash memory will be set Read Array mode you can read data on flash memory if this function return Return None Notice This functions on the RAM FER A A kok kok k kok KCkCk kok k kok kok kk k ON void WaitEraseCompleteRam DEPEND FSIZE F ADR SIZE flashAddress DEBUG OUT DEBUG OUT WAIT ERASE while FLASH BUSY tifdef ENABLE WATCHDOG RESET CLEAR WATCHDOG fendi f if suspendReq RO1AN0440EJ0101 Rev 1 01 Page 54 of 86 2tENESAS suspendReq 0 FlashSuspendRam Data on the flash memory can t be read if Read Array Mode isn t Rewriting the User ROM Area in EWO Mode taken whe
93. sing occurs Ey FlashResult WriteFlashEW1 F ADR flashAddress const void buffer unsigned short size gt gt t Read data from flash memory for the ENI mode param flash ddress in physical address on flash to begin read param buffer out address in buffer to read to param size in number of byte to read return Success or Error code retval F SUCCESS Success retval F_DEVICE_BUSY When a Read Programing Erasing requirement in Programing Erasing occurs f FlashResult ReadFlashEW0 F_ADR flashAddress void buffer unsigned short size P mm cu In pel Read data from flash memory for the ENI mode param flashAddress in physical address on flash to begin read param buffer out address in buffer to read to param size in number of byte to read return Success or Error code retval F_SUCCESS Success retval F_DEVICE_BUSY When a Read Programing Erasing requirement R01AN0440EJ0101 Rev 1 01 Page 43 of 86 sR2ENESAS Rewriting the User ROM Area in EWO Mode in Programing Erasing occurs af FlashResult ReadFlashEW1 F_ADR flashAddress void buffer unsigned short size s Block number enum FlashBlock F BLOCK 0 F BLOCK 1 F BLOCK 2 F BLOCK 3 F BLOCK 4 F BLOCK 5 F ALLBLOCK 1 F BLOCK A 2 F BLOCK B 3 Z Disable write protect param blockNumber in The block number to
94. sp const char void mcu init void dt int CheckErasedBlank void F ADR SIZE f addr short size int CmpBlank unsigned char F ADR SIZE buf short size Ja Timer KAKKKKKKJ Ure aif enum TimerSource TIMER_DEV_1 0x00 TIMER_DEV_8 0x40 TIMER_DEV_32 0x80 TIMER SUB 32 0xC0 y define TIMER CLOCK Hz 20000000 define TIMER SUBCLOCK Hz 32768 define TA1 DEV32 MS a TIMER CLOCK Hz 32000 a 1 define TA1 DEV8 MS a TIMER CLOCK Hz 8000 a 1 define TA1 DEV1 MS a TIMER CLOCK Hz 1000 a 1 BOOL Che void Set void Tim inline v R01AN044 ckTalPassed void TickTimer unsigned char tm erAlInitTimerMode enum TimerSource source unsigned short timer oid StartTimerAl void 0EJ0101 Rev 1 01 134 NESAS Page 78 of 86 Rewriting the User ROM Area in EWO Mode inline void StartTimerAl void tals 1 TimerAl start void ClearTotalTimer void void SystemTimerInc void RAM for Timer BOOL tm ps FALSE unsigned char tm ms 0 unsigned long totalTimer 0 ms inline unsigned long GetTotalTimer void inline unsigned long GetTotalTimer void return totalTimer us RT Timer End J el enum MainMode ERASE_TEST_START ERASE_TEST_RESTART ERASE_TEST_CHECK PROGRAM_TEST OTHER mode ERASE_TEST_START const char TestData 33 0123456789ABCDEF01234
95. sses is prohibited The reserved addresses are provided for the possible future expansion of functions Do not access these addresses the correct operation of LSI is not guaranteed if they are accessed Glock Signals After applying a reset only release the reset line after the operating clock signal has become stable When switching the clock signal during program execution wait until the target clock signal has stabilized When the clock signal is generated with an external resonator or from an external oscillator during a reset ensure that the reset line is only released after full stabilization of the clock signal Moreover when switching to a clock signal produced with an external resonator or by an external oscillator while program execution is in progress wait until the target clock signal is stable Differences between Products Before changing from one product to another i e to one with a different type number confirm that the change will not lead to problems The characteristics of MPU MCU in the same group but having different type numbers may differ because of the differences in internal memory capacity and layout pattern When changing to products of different type numbers implement a system evaluation test for each of the products Notice 1 All information included in this document is current as of the date this document is issued Such information however is subject to change without an
96. ssued in interrupt v Write xx5016 to processing programming address Clear status register Clear the status If any error remains in the status no data can be written to the flash memory v Jump to rewrite control Execute processing in the RAM beginning with the first bus cycle of the program program in RAM command Issue a program command Wir DIE Although the flash memory enters read status mode following the second bus cycle rite xx O i n programming address LT write of the program command execute processing in the RAM beginning with this processing Y Bs Write program data to Subsequent to the program command write program data to the programming programming address address Program data is handled in 2 byte units y FSET enable interrupt Li Check to see if programming is completed Go to read array mode first b The flash memory is thereby made accessible Execute read array LA The value of the status register remains unchanged even when this processing is command performed x y FR 8 Jump to flash memory Now that the flash memory is accessible execute processing in the flash memory H L 5i Y 5 Full status check Check to see if programming is terminated normally 3 3 Y o lt FMRO1 0 Disable CPU rewrite mode al ES 3 Y 2 Set CMO CM1 an
97. start An example operation is shown below Clock limitations Flash memory status A pog Auto erase start None y Y None Set limitations on CPU l Set limitations on CPU rewrite mode Read array rewrite mode H PPETI y i v i i Enable CPU rewrite i Enable CPU rewrite I mode i mode v v i Y Erase restart es Auto er v Wes E v Auto erase H U Wait for completion of i Erase suspend i Tr EDEN Yes Y Y Erase completed Read status Disable CPU rewri H i sabio CPU reiia i I Read array command mode i v v Lift limitations on CPU j Frase suspend Disable CPU rewrite rewrite mode mode v v i Read array Normal processing None Lift limitations on CPU except flash rewrite rewrite mode I I I I I y v Normal processing None Erase completed Figure 3 7 Clock Control during Erase Suspend Transferring a program to the RAM area Refer to Section 3 7 About Programs Running in the RAM Area Entering CPU rewrite mode Set bit 1 FMRO1 in Flash Memory Control Register 0 FMRO To set the FMRO1 bit to 1 write 0 and then 1 in succession Make sure no interrupts or DMA transfers occur before writing 1 after writing 0 RO1AN0440EJ0101 Rev 1 01 Page 12 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode Removing write protect Some areas must have their w
98. t block address to be erased even address Y FSET I enable interrupt command Issue the first bus cycle of the block erase command Although the flash memory enters read status mode following the second bus cycle write of the block erase command execute processing in the RAM beginning with this processing k Issue the second bus cycle of the block erase command Check to see if erase is completed acceptable range Go to read array mode first Yes y Execute read array command The flash memory is thereby made accessible y Jump to flash memory m x D o E y a Full status check Check to see if erase is terminated normally 3 LS E E lt FMRO1 0 Disable CPU rewrite mode 5 5 Y r n CUR 2 ar is Restore the CPU clock to its original setting End of ease processing Figure 3 12 Auto Erase Flowchart for the RAM Reduced Version In the sample program the auto erase procedure consists of the processing shown above plus erase suspend processing based on the erase suspend function RO1AN0440EJ0101 Rev 1 01 Page 20 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 3 5 Readout Procedure The flash memory can be accessed for data readout during read array mode Writing xxFF16 in the first bus cycle places the flash memory into read array mode
99. ted Other Error code See Table 4 13 Remark If an error occurs responded with error from the flash memory device no retry operation is attempted If an error occurs therefore the erase operation should be retried a number of times as specified in the manual This function cannot be used in an interrupt Interrupts are controlled using the I flag internally If interrupts need to be disabled in order to use this function disable interrupts in the IPL R01AN0440EJ0101 Rev 1 01 Page 32 of 86 134 NE SAS Outline Rewriting the User ROM Area in EWO Mode Table 4 5 WriteFlash Writes data to the flash memory Declaration FlashResult WriteFlash F_ADR flashAddress const void buffer unsigned short size Include flash ewOdrv h Parameter Meaning Remark F ADR flashAddress unsigned short size Beginning address of the area to which data is to be written Beginning address of the data to be written The data size to be written Because data is written in 2 byte units specify an even address Be sure to specify in 2 byte units Return value Meaning Value FlashResult Error code F_SUCCESSSuccessfully executed Other Error code See Table 4 13 Description This function writes data to the flash memory Since data is written to the flash memory in 2 byte units an even address must be specified for flashAddress and an multiple of 2 bytes m
100. the program from the far area to the near area or the BCOPY macro to transfer to the far area The N BCOPY and BCOPY macros are defined in ncrt0 a30 Add the transfer processing to the C language startup routine nert0 a30 by using this macro The following shows an example in which N_BCOPY is used crt0 a30 Program RAM Initialize from addr is specified in the option D from addr 0FB000h N BCOPY from addr program ram top program ram 3 6 3 Specifying Locations where the Program is Stored and Executed The compiler must be directed to allocate separate addresses for the program at which it is to be stored and executed This can be accomplished by specifying an option in 1n30 as shown below 1n30 LOC program ram 0FB000 The above linker option LOC specifies the address at which the data in the specified section should be stored by specifying the location of the program ram section program to be stored at the address OFB00016 Note This option LOC and the related option ORDER cannot be specified in 1n30 for NC30 Make sure the option is set in In30 The programs relocated by ALIGN specification may not always run normally Therefore if the beginning address of a relocated section is an even address set an even address for the location to which the program is to be transferred The same applies to odd addresses In the example here the address specified by LOC is set to an even address and the ALIGN opti
101. the program is transferred to the RAM before executing it Make sure no interrupts occur while writing O and then 1 to FMRO1 Also make sure no Y FCLR I disable interrupt DMA transfers occur during that time Enable CPU rewrite mode Write O and then 1 to FMRO1 in succession FMRO1 1 FSET enable interrupt FCLR I disable interrupt Make sure no DMA transfers occur during that time Make sure no interrupts occur when removing write protect Also make sure no DMA transfers occur during that time Remove write protect y FSET enable interrupt y FCLR disable interrupt To remove write protect use FMRO2 for the M16C 26 or FMR02 and FMR16 for the M16C 26A 28 and 29 To set FMRO2 to 1 write 0 and then 1 in succession The same applies for FMR16 This will be detailed in Removing write protect Make sure no interrupts occur while writing 0 and then 1 to FMR40 Also make sure no DMA transfers occur during that time FMR40 FMR40 0 y FSET enable interrupt Enable erase suspend processing N Write 0 and then 1 to FMR40 in succession Also make sure no DMA transfers occur during that time highest block address to be erased even address clear status register FCLR I disable interrupt Write xx5016 to the Interrupts are disabled in case a command is issued to the flash memory or erase suspend
102. topped talic 0x07 Set Timer Al Interrupt Priolity Level talmr source Set Timer Al mode regster Mode Timer mode Count src f1 RO1AN0440EJ0101 Rev 1 01 Page 82 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode tal timer Set Timer Al timer value 50ms Clear totaltimer el void ClearTotalTimer void totalTimer 0 JAKKKKAKKKAKKAKKKAK KKK KKAKKKAKKKAKKAKKKAK KKK KKAKKKAKKAKKKAKKKAKKAKKKAKKKAKKKKKK The folloving is a program to vork by the RAM ENO only KAKKKAKKAKKKAKKKAKKKKKKAKKKAK KKK kk kok kk Ck kok k kok k kok k kk k kok k kok k KKK KKKAKKKKKKKKKKJ ifdef FLASH MODE EWO pragma SECTION program program ram fendif Timer Al Interrupt function void timerAl_int void Suspend erasing and advance timer SuspendErase SystemTimerInc This is function for Timer Al interrupt s void SystemTimerInc void ims tm ps TRUE totalTimer tm ms dummy Interrupt a void vec dummy int void R01AN0440EJ0101 Rev 1 01 Page 83 of 86 134 NE SAS Rewriting the User ROM Area in EWO Mode 5 1 9 M16C EWO tmk HEHEHE HE HEHE HE HE HE HE HE HE FE FE FE FE FE FE FE EEE E E HE t Makefile for TM V 3 20A COPYRIGHT C 1998 1998 2003 RENESAS TECHNOLOGY CORPORATION ALL RIGHTS RESERVED AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED Notice Don t edit Date
103. ume suspended erase and complete it Error code F_SUCCESS Successfully executed Other Error code See Table 4 13 Remark If an error occurs responded with error from the flash memory device no retry operation is attempted If an error occurs therefore the erase operation should be retried a number of times as specified in the manual This function cannot be used in an interrupt Interrupts are controlled using the I flag internally If interrupts need to be disabled in order to use this function disable interrupts in the IPL RO1AN0440EJ0101 Rev 1 01 Page 31 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode Table 4 4 RestartEraseFlash Outline Resumes device erase Declaration FlashResult RestartEraseFlash void Include flash_ew0drv h Parameter Meaning Remark None Return value Meaning Value FlashResult Error code F_SUCCESSSuccessfully executed Other Error code See Table 4 13 Description This function resumes erase of the flash memory all 0xFF If StartEraseFlash or RestartEraseFlash returns with the error code F SUSPEND erase suspended this function is called in order to resume suspended erase When erase is completed after being resumed F SUCCESS is flagged As for StartEraseFlash the erase operation resumed by this function can be suspended again by SuspendErase or SuspendFlash0 Error code F_SUCCESS Successfully execu
104. ummy int ummy int ummy int ummy int ummy int dummy int ummy int ummy int vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector o NA 0 JU Oo U A UN F o 9 20 21 Rewriting the User ROM Area in EWO Mode timerx vector 22 vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector vector 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ay 38 39 4 o OG WAND U A WIN r od 52 53 54 55 56 57 58 59 60 61 62 63 fixed vec tor section section fvector ROMDATA SOG OffdcH ZUDI lword dummy int OVER FLOW RO1AN0440EJ0101 Rev 1 01 fixed vector table 134 NE SAS Page 76 of 86 Rewriting the User ROM Area in EWO Mode lword dummy int BRKI E lword dummy int ADDRESS MATCH lword dummy int SINGLE STEP lword dummy int WDT P lword dummy int DBC H lword dummy int NMI lword dummy int org OfffcH RESET lword start OFF000000H endif F R8C section rom_FE ROMDATA E org 10000H F H section rom FO ROMDATA section data FEI ROMDATA ALIGN data FEI top sect
105. unused pins configure as outputs to decrease power consumption pd6 0x90 pd8 0 1 pd8 1 1 pd8 2 1 pd8 3 1 RO1AN0440EJ0101 Rev 1 01 Page 81 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode prc2 1 P9 is write protected disable protection before writing to P9 pd9 0 pd9 1 pd9 2 pd9 3 1 pre2 0 Write protect P9 e I Pupo lt pd10 0 pdio 1 pdio 2 pdl0 3 pdl0 4 Il se Il Pere lt Set up a Timer Al TimerAlInitTimerMode TIMER DEV 32 TA1 DEV32 MS 20 SetTickTimer 20 talic 0x07 Set Timer Al Interrupt Priolity Level Port Initialize p6 0x00 Port 6 clear pd6 Oxff fi Port 6 is output port p7 0x07 77 p7_0 p7_2 LED off pd7 0x07 p7_0 to p7_2 output select pu25 1 p10_4 to P10 7 pull up p10 0x00 Port 10 clear pdl0 Ox1f p10_5 to pl0 7 is Key in port JAKKKKAKKKAKKKAKKKKKKAKKKAKKKKKKKKKKKKKKKJ Timer S EK K k k k kok k k k kok k kok kok k k k k k kk k ke ke ke ke ke ke e ke Check tick time Xy BOOL CheckTalPassed void if tm_ps tm_ps FALSE return TRUE return FALSE Set up value of tick time 7 void SetTickTimer unsigned char tm tm ms tm Initialize Timer Al el void TimerAlInitTimerMode enum TimerSource source unsigned short timer Set up a Timer Al tals 0 Timer Al S
106. ure 8 3 4 1 Auto Program and Auto Erase Procedure eee eee eeeee eee eee nene nenene en 8 3 4 2 ReducingtheRAM SIZG ur e a a 18 3 5 Readout Procedure cin iniciuar edet ca daniel a e iaia 21 3 6 Programs Running in the RAM Area 22 3 6 1 Altering the Section Name iii 22 3 6 2 Transferring the Program siennes 23 3 6 3 Specifying Locations where the Program is Stored and Executed 23 3 6 4 Setting Vector Addresses in the RAM Area 24 4 Sample Programs erotik enean kia x dita dunas ki Ens cer dha dd dha d sh 25 AA Eile Configurationi i EL 26 4 2 Program Operation iii 27 4 2 1 Auto Program and Auto Erase Operations enne 27 4 2 2 Erase Suspend Processing eee elena 28 4 2 3 Flash Memory Read during Interrupt Processing 30 4 9 Software Interface error faca entere hui abas ia 31 dA EG nre E M dean dad 38 4 4 1 Customizing CPU Clock Settings enne nnne 38 4 4 2 Customizing Operation of the Driver Software nennen 38 5 Using the Sample Program sues ciet tete et Rer aa 39 51 Source Code rsca P L atenda diaas 40 HALT ashdevcon zzz mini den 40 D 1 2 Iles 41 5 1 3 TMashAmtoc E E M 46 5 1 4 flashdrvd v ENO Gaci cine a pes SU ada aa 48 5 1 5 deperid MOO c bz scr M8 teer lia aaa 57 UE MEUM o 60 5 1 7 Sect30 AU IEPTE
107. ust be specified for size An error is assumed unless the conditions are met Error code F_SUCCESS Successfully executed Other See Table 4 13 Error code Remark A write alignment error or write size error can be deleted by commenting out define ENABLE_FLASH_ERR_CHECK In this case if a parameter error 2 byte data size check or alignment occurs the program behavior is undefined This function cannot be used in an interrupt Interrupts are controlled using the I flag internally If interrupts need to be disabled in order to use this function disable interrupts in the IPL RO1AN0440EJ0101 Rev 1 01 2tENESAS Page 33 of 86 Rewriting the User ROM Area in EWO Mode Table 4 6 ReadFlash Outline Reads data from the flash memory Declaration FlashResult ReadFlash F_ADR flashAddress void buffer unsigned short size Include flash ewOdrv h Parameter Meaning Remark F ADR flashAddress Beginning address of the read data vad puer le Address into which data is read unsigned short size The data size to be written Return value Meaning Value FlashResult Error code F_SUCCESSSuccessfully executed Other Error code See Table 4 13 Description This function reads data from the flash memory Error code F_SUCCESS Successfully executed Other Error code See Table 4 13 Remark Table 4 7 UnlockBlockFlash Outline Un
108. ut Programs Running in the RAM Area all are written in the sample program please refer to the sample program for details about the entire description RO1AN0440EJ0101 Rev 1 01 Page 24 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode 4 Sample Program The sample program explained here includes the content described in Section 3 5 2 Reducing the RAM Size The sample program is written as a flash memory device driver Following are defined as the driver interfaces If an error occurs during flash memory auto program or auto erase error code is returned In such a case please execute the processing described in the manual After executing the clear status command reexecute auto program for a programming error or auto erase for an erase error up to three times repeatedly Function Name Table 4 1 Function Table Description Remark StartEraseFlash0 Starts erasing flash memory Unusable in an interrupt Interrupts are controlled using the I flag internally If interrupts need to be disabled in order to use this function disable interrupts in the IPL RestartEraseFlash Resumes suspended erase Unusable in an interrupt Interrupts are controlled using the I flag internally If interrupts need to be disabled in order to use this function disable interrupts in the IPL WriteFlash Writes to flash memory Unusable in an interrupt Interrupts are controlled us
109. write If the erase suspend function is used in EW1 mode be aware that the erase operation performed differs depending on the type of interrupt used maskable interrupt or nonmaskable interrupt During programming too if interrupts are used in other than byte write the programming operation performed differs depending on the type of interrupt used as for the erase operation Operational differences due to the type of interrupt are outlined below Table 3 4 Auto Erase Auto Program and Interrupt Operation When a maskable When a nonmaskable interrupt NMI interrupt interrupt is accepted watchdog timer oscillation stoppage detection or voltage detection interrupt is accepted During auto Maskable interrupts can When an interrupt request 1s accepted auto erase or erase be used and serviced by auto program is forcibly stopped and the flash locating their vectors in memory is reset When the flash memory restarts a the RAM certain time later the CPU starts processing the interrupt During auto Since auto erase or auto program is forcibly stopped program correct values may not be read from the block that was being erased or the address that was being programmed Therefore execute auto erase auto program again after the flash memory has restarted and confirm that the operation is terminated normally The watchdog timer remains idle during command execution Note 1 The vector for the address match interrupt is locat
110. write in buf address in buffer to write from in size number of byte to write Return Result Notice This function writes flash memory with operating write command It does not perform retry at the time of error KAKKKAKKKAKKAKKKAKKKAKKAKKKA KKK AN FlashResult WriteFlashEW0 F ADR flashAddress const void buffer unsigned short size tif defined USE CLOCKGEAR ProcessorMode save dat tendif FlashResult ret DEPEND_FSIZE F_ADR_SIZE adr flashAddress const DEPEND_FSIZE buf buffer ifdef ENABLE_FLASH_ERR_CHECK 1f 0x01 amp unsigned long adr return F WRITE ADDRESS ERROR if 0x01 amp size return F WRITE SIZE ERROR RO1AN0440EJ0101 Rev 1 01 134 NE SAS Page 51 of 86 fendi f disable interrupt asm FCLR I if stat FLASH READY asm FSET I return F DEVICE BUSY asm FSET I resetOccerrd 0 if defined USE_CLOCKGEAR SlowMCU amp save dat fendi f asm FCLR I FLASH E EW asm FSET I asm FCLR I SetUpLockBit asm FSET I ret F SUCCESS while size gt 0 asm FCLR I stat FLASH WRITE adr FLASH CLEAR STSREGS CMD FlashWriteCommandRam adr buf ret FullStatusCheck LE stat FLASH READY if ret F_SUCCESS break size sizeof DEPEND_FSIZE buf adr disable interrupt asm FCLR I FLASH DIS EW if defined USE CLOCKGEAR RestoreMCU amp save dat tendif
111. y v command 2016 D016 ORFE XX16 a Erase command 1 Erase command 2 Flash memory in Flash memory in Flash memory in Flash memory in Flash memory in read array mode read array mode read status mode read status mode read array mode Figure 3 10 Auto Erase Procedure Make sure the command is written in 16 bit units to the even addresses in the user ROM area For details about the change of flash memory modes after the CPU started erasing the flash memory refer to Figure 3 3 Flash Memory Operation Modes RO1AN0440EJ0101 Rev 1 01 Page 15 of 86 2tENESAS Rewriting the User ROM Area in EWO Mode Fullstatus check To confirm whether auto program or auto erase has been executed normally inspect the FMRO6 and FMRO 7 bits The table below shows the relationship between the status register status and errors Table 3 10 Status of the FMRO Register and Errors Status of the FMROO register status register Error occurrence conditions FMR07 FMRO06 When the command was not written correctly When invalid data not xxD016 or xxFF16 was written in the second bus cycle of the block erase command Note 1 When the block erase command was executed on a write protected block When the program command was executed on a write protected block When the block erase command was executed on a write Command sequence error Erase error i enabled block but the block was not auto erased correctly
112. y prior notice Before purchasing or using any Renesas Electronics products listed herein please confirm the latest product information with a Renesas Electronics sales office Also please pay regular and careful attention to additional and different information to be disclosed by Renesas Electronics such as that disclosed through our website 2 Renesas Electronics does not assume any liability for infringement of patents copyrights or other intellectual property rights of third parties by or arising from the use of Renesas Electronics products or technical information described in this document No license express implied or otherwise is granted hereby under any patents copyrights or other intellectual property rights of Renesas Electronics or others You should not alter modify copy or otherwise misappropriate any Renesas Electronics product whether in whole or in part Descriptions of circuits software and other related information in this document are provided only to illustrate the operation of semiconductor products and application examples You are fully responsible for the incorporation of these circuits software and information in the design of your equipment Renesas Electronics assumes no responsibility for any losses incurred by you or third parties arising from the use of these circuits software or information 5 When exporting the products or technology described in this document you should comply with the applicable export cont

Download Pdf Manuals

image

Related Search

Related Contents

MATRIGON*  Home Decorators Collection 0552700450 Instructions / Assembly  25-044WiFi Camera User Manual  取扱説明害 保管用一  ClosetMaid 1598 Installation Guide  The Design of a Chemical Virtual Instrument Based on LabVIEW for  DETECCIÓN DE PRESIONES ANORMALES PARA  Pinzas amperimétricas y comprobadores eléctricos  

Copyright © All rights reserved.
Failed to retrieve file