SM59R16 EEPROM 功能使用方法 Application Note SM59R16 EEPROM 功能使用方法 1 適用產品:SM59R16A2 / SM59R08A2 2 EEPROM 使用概述: 燒錄次數限制: SM59R16 使用 program flash 模擬為 Internal EEPROM,寫入保證次數可逹 100K。 Page Erase 及 Program Byte 的動作說明: Page Erase 是將 flash 的電位全部提升至”1”,Program Byte 是將 flash 的電位”high”拉為”low”。當程序執行一次 Program Byte,IC 內部硬體完整的步驟為:(1)read page(2)modify Byte(3)erase page(4)write page;寫 N 個 byte,就必須經過 N 次(1~4)的步驟。 若少量次數的資料變動,可直接使用 EEPROM Program Byte 的方式。若大量的資料變動,為加快 Program Byte 完成的速度,可先將 (1)資料 page read 至 SRAM 中做修改 (2)將該 program flash 做 page erase (3)將 SRAM 資 料寫回至 program flash。 A. 為什麼第一次會較快? 因為當第一次使用時,flash 在 program 前已先被 erase 為 0xFF,當 IC 內部硬體判斷該位址資料為 0xFF,在 program 時就會省略(3)Erase 的步驟。 B. 為什麼第二次以後會變慢? 因為使用第二次以後,當 IC 內部硬體判斷不為 0xFF,在 program 時就會做(3)Erase 的步驟。 Erase/Program/Read/Modify 測試速度: System clock (MHz) 25 24 22.1184 12 11.1592 Page Erase (ms) 13.22 13.77 14.94 27.54 Program Byte (us) 112.2 116.8 126.8 233.8 Read Byte (us) 3.12 3.25 3.53 6.5 Modify Byte (ms) 68.44 71.3 77.36 142.6 29.88 253.6 7.05 156.6 Note: (1)Program Byte 即該位置原資料等於 0xFF (2)Modify Byte 即該位置原資料非 0xFF Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 1 Ver A 2008/08 SM59R16 EEPROM 功能使用方法 Application Note 應用說明: 如果使用 External ISP,燒錄後 code flash 中的 data 如何保留?例如 Product ID、Key word 或 Menu setting … 應用方式如下: 1. 執行程序:read flash data,再將 data write to Xdata(SRAM) 2. 執行燒錄:External ISP program (使用 OCD or MSM9055) 3. 執行重置:Reset MCU (不可斷電) 4. 執行程序:read Xdata,再將 data write to flash,即可保存 flash 中的資料 256 512 (Byte) SRAM-->code flash 3.4 6.8 ms code flash-->SRAM 3 6 ms test with external crystal 22.1184MHz * 為保持 SRAM 中的資料正確,執行過程請勿中斷電源。 * 此方法限制於內建 SRAM size,最大為 2KB。 3 EEPROM 相關的暫存器: Mnemonic PCON PES Description Direct Bit 7 Power Control Program Memory Page Erase Control Register 87h SMOD A1h EPE Bit 6 Bit 5 Bit 4 EEPROM Function MDUF PMW - Mnemonic: PCON 7 6 5 4 3 SMOD MDUF PMW PMW = 1: R / W Flash Memory enable - - 2 - Bit 3 Bit 2 Bit 1 Bit 0 RESET - - STOP IDLE 00h - - - - 00h 1 STOP Address: 87h 0 Reset IDLE 00h PMW = 0:disable Mnemonic: PES 7 6 5 4 3 2 1 EPE EPE=1:執行抺除Page Erase (=512 bytes),完成後由硬體自動清 Address: A1h 0 Reset 00h Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 2 Ver A 2008/08 SM59R16 EEPROM 功能使用方法 Application Note 4 執行 EEPROM page erase 抺除程序,請參考如下: //==================================================================== //This function will erase one page(512-Byte). //SM59R16A2 64KB = 0~127 pages. //SM59R08A2 32KB = 0~ 63 pages. //==================================================================== void PMEE_PageErase(unsigned int Address) { unsigned char xdata *pAddr; PCON |=0x10; // R / W Flash Memory enable pAddr =Address; PES =0x80; // Page erase enable, after finish auto clear by hard *pAddr=0xff; // Page erase command PCON = PCON & 0xEF; // R / W Flash Memory disable } * EEPROM Erase 重要說明: 1. page erase為一硬體設定動作,執行程序順序請依照以上範例 2. page erase enable與command之間,絶不可插入其它程序 3. page erase enable後,必須在要抺除的flash page的第一個位置寫入”0xFFh”,當作page erase command,即可抺除,command address如下例表: SM59R16/08 page Erase Page Memory address 0 0x0000~0x01FF First byte address of page (Command address) 0x0000 1 0x0200~0x03FF 0x0200 2 0x0400~0x05FF 0x0400 . . . . . . . . . . . . 126 0xFC00~0xFDFF 0xFC00 127 0xFE00~0xFFFF 0xFE00 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 3 Ver A 2008/08 SM59R16 EEPROM 功能使用方法 Application Note 5 範例程式流程圖(一):EEPROM 的應用流程圖: Entry SyncMOS EEPROM function <Step1> Execute: EEPROM Erase 3rd. page (0x0400~0x05FF) <Step2> Execute: EEPROM Byte Write (PMEE_start~PMEE_END =0x11) < Step3. > Execute: EEPROM Byte Read (PMEE_start~PMEE_END) ture If = 0x11 fail < Pass!! > P2 give 0x55 P2 give 0x66 < Fail > P2 give 0x77 P2 give 0x88 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 4 Ver A 2008/08 SM59R16 EEPROM 功能使用方法 Application Note 6 範例程式(一):EEPROM 範例程式 Description Main program //==================================================================== // // S Y N C M O S T E C H N O L O G Y // //==================================================================== #include "..\h\SM59R16A2.h" #include "..\MISC\delay.h" #define PMEE_start #define PMEE_end 0X0400 0x0600 //==================================================================== //PES:Program Memory Page Erase Control Register //When enable the EPE bit,the Page Erase(Each page include 512 bytes //function can be executed by below Instructions? //==================================================================== sfr PES = 0xA1; //sbit EPE = PES^7; //==================================================================== //This function will erase one page(512-Byte). //SM59R16A2 64KB = 0~127 pages. //SM59R08A2 32KB = 0~ 63 pages. //==================================================================== void PMEE_PageErase(unsigned int Address) { unsigned char xdata *pAddr; PCON |=0x10; // R / W Flash Memory enable pAddr =Address; PES =0x80; // Page erase enable, after finish auto clear by hard *pAddr=0xff; // Page erase command PCON = PCON & 0xEF; // R / W Flash Memory disable } //==================================================================== //This function will write one byte to EEPROM. //Adderss is from 0x0000~ 0xFFFF. //==================================================================== void PMEE_ByteWrite(unsigned int Address, unsigned char Data) { unsigned char xdata *pAddr; PCON |= 0x10; // R / W Flash Memory enable pAddr = Address; // Set address *pAddr = Data; // Write data PCON = PCON & 0xEF; // R / W Flash Memory disable } //==================================================================== //This function will read one byte from EEPROM. //Adderss is from 0x0000~ 0xFFFF. //==================================================================== unsigned char PMEE_ByteRead(unsigned int Address) { Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 5 Ver A 2008/08 SM59R16 EEPROM 功能使用方法 Application Note unsigned char xdata *pAddr; unsigned char temp; PCON |= 0x10; // R / W Flash Memory enable pAddr = Address; // Set address temp = (*pAddr); // Read data PCON = PCON & 0xEF; // R / W Flash Memory disable return(temp); } void main(void) { unsigned int i; PMEE_PageErase(0x0400); //Erase 3rd. page (0x0400~0x05FF) for(i=PMEE_start; i<PMEE_end; i++) //EEPROM Write { PMEE_ByteWrite(i, 0x11); } while(1) { for(i=PMEE_start;i<PMEE_end;i++) //EEPROM Read { if(PMEE_ByteRead(i) == 0x11) //EEPROM Verify { P2 = 0x55; Delay1mSec(100);//Pass!! P2 = 0x66; Delay1mSec(100);//Pass!! } else { P2 = 0x77; Delay1mSec(200);//Fail Warning!! P2 = 0x88; Delay1mSec(200);//Fail Warning!! } } } } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 6 Ver A 2008/08 SM59R16 EEPROM 功能使用方法 Application Note 7 範例程式流程圖(二):EEPROM + SRAM 的應用流程圖: Entry SyncMOS EEPROM function < Step1. > Triger_Int1() 1. Read EEPROM 2. Write to Xdata < Step2. > Execute External ISP program < Step3. > Reset MCU (don't power down!!) < Step4. > Entry main() 1. Read Xdata 2. Write to EEPROM < Step5. > User code.... Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 7 Ver A 2008/08 SM59R16 EEPROM 功能使用方法 Application Note 8 範例程式(二):EEPROM + SRAM 使用的範例程式 Description Main program //==================================================================== // // S Y N C M O S T E C H N O L O G Y // //==================================================================== #include "..\h\SM59R16A2.h" #include "..\h\SM59R16A2_extradef.h" //#include "..\InternalEE\InternalEE.h" #include "..\Interrupt\Interrupt.h" #include "..\MISC\delay.h" #define Expanded_start 0X0600 #define Expanded_end 0x0700 unsigned char xdata *Expanded_RAM; unsigned char xdata *PMEE_RWpt; //==================================================================== //This function will write one byte to eeprom. //Adderss is from 0x0000~ 0xFFFF. //==================================================================== void PMEE_ByteWrite(unsigned int Address, unsigned char Data) { //IEN0_EA = 0; // Disable all interrupt PCON |= PCON_PROGMEM_WR; // Enable eeprom write PMEE_RWpt = Address; // Set address *PMEE_RWpt = Data; // Write data PCON &= (~PCON_PROGMEM_WR); // Disable eeprom write } //==================================================================== //This function will read one byte from eeprom. //Adderss is from 0x0000~ 0xFFFF. //==================================================================== unsigned char PMEE_ByteRead(unsigned int Address) { unsigned char temp; //IEN0_EA = 0; // Disable all interrupt PCON |= PCON_PROGMEM_WR; // Enable eeprom write PMEE_RWpt = Address; // Set address temp = (*PMEE_RWpt); // Read data PCON &= (~PCON_PROGMEM_WR); // Disable eeprom write return(temp); } void Expanded_RAM_Byte_Write(unsigned int addr, unsigned char dat) { Expanded_RAM = addr; //set addr *Expanded_RAM = dat; //write Data } unsigned char Expanded_RAM_Byte_Read(unsigned int addr) { Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 8 Ver A 2008/08 SM59R16 EEPROM 功能使用方法 Application Note unsigned char dat; Expanded_RAM = addr; dat = *Expanded_RAM; return dat; //set addr //read Data at } //==================================================================== // Read from eeprom, Write to Xdata. We want to keep Xdata // so don't power down!! //==================================================================== void Int1ISR(void) interrupt 2 using 0 { unsigned int i; unsigned char buff=0; for(i=Expanded_start; i<Expanded_end; i++) { buff = PMEE_ByteRead(i); //Read from eeprom Expanded_RAM_Byte_Write( i , buff );//Write to Xdata } while(INT0IF)// Clear again to debounce. { INT1IF = false; Delay10mSec(1); } } void mcu_init(void) { //Initialize External INT //Int0Init( EXINT0_EN, INT0_FALLEDGE ); Int1Init( EXINT1_EN, INT1_FALLEDGE ); } void main(void) { unsigned int i; unsigned char buff=0; mcu_init(); // Read from Xdata, Write to eeprom for(i=Expanded_start; i<Expanded_end; i++) { buff = Expanded_RAM_Byte_Read(i); //Read from Xdata PMEE_ByteWrite( i , buff ); //Write to eeprom } while(1) { //user code... } } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A091 9 Ver A 2008/08