EETS4K模块使用笔记(1)
9S12系列单片机的一般包括4KB的EEPROM。Freescale将EEPROM模块称之为EETS4K。实际上,这儿所谓的EEPROM其实是FLASH,只不过Freescale特意将这儿Flash的sector做的很小(4Bytes),使得用户用起来像是在用EEPROM。
EEPROM是直接映射到9S12单片机的地址空间的,假如程序中仅仅读取EEPROM中的内容,而不涉及到对EEPROM中数据的修正。那就不需求特别的编程。就像读取RAM数据那样直接拜访就能够了。
只有当需求在程序中更新EEPROM中内容时,才需求学习下面的内容。
初始化EETS4K
在向EETS4K写入数据或擦除数据前要先装备EETS4K的时钟。EETS4K的时钟频率必须在150KHz——200KHz之间,为此需求装备ECLKDIV寄存器。
ECLKDIV寄存器(EEPROMClockDividerRegister)
图1ECLKDIV寄存器
PRDIV8是预分频位:当PRDIV8=1时输入时钟被预分频为1/8。
EDIV5——EDIV8为分频除数寄存器,最多能够发生1/64的分频比。简略的核算可知,当输入时钟大于12.8MHz时需求将PRDIV8置位。
通过PRDIV8和EDIV两级分频最多可将时钟频率分为1/512。
擦除和写入和读取
这儿不详细介绍每一个寄存器的用法。只对需求留意的当地加以阐明。
EETS4K模块的最小擦除单位是4Bytes,EETS4K模块供给了两条相关指令,一条是擦除一个sector,也便是4字节,而且要求是字节对其的双字。另一条指令擦除悉数EEPROM空间。
每次编程(写入)单位为两个字节。而且这两个字节要是对其字。
当EETS4K模块正在进行擦除或编程操作时是不能一起读取EEPROM中内容的。
有了这些介绍就够了。下面给出一个详细的比如。
- /*EETS4K.h*/
- #ifndefNVM_H
- #defineNVM_H
- /*
- *CONSTANTS
- */
- #defineNVM_NO_ERR(1)
- #defineNVM_ODD_ACCESS_ERR(-1)
- #defineNVM_ACCESS_ERR(-2)
- #defineNVM_PROTECTION_ERR(-3)
- /*
- *FUNCTIONPROTOTYPES
- */
- voidEEPROM_Init(unsignedlongsysclk);
- charEEPROM_Write_Word(unsignedintaddress,unsignedintdata);
- charEEPROM_Erase_Sector(unsignedintaddress);
- charEEPROM_Erase_All(void);
- unsignedintEEPROM_Read_Word(unsignedintaddress);
- #endif/*Endoffile*/
- /*EETS4K.C*/
- #include
/*commondefinesandmacros*/ - #include”derivative.h”/*derivative-specificdefinitions*/
- #include”eets4k.h”
- /**@briefThisfunctioninitializestheNonVolatileEEPROMcontrolregisters
- *andmustbecalledbeforeattemptingtowriteoreraseanEEPROMsector.
- *
- *@parasysclktheCPUclockfrequency(SYSCLK)drivenbytheonboardoscillatororthePLLifenabled.
- */
- voidEEPROM_Init(unsignedlongsysclk)
- {
- unsignedchareclk_val;
- if(sysclk>=12000){/*IftheSYSCLKis>12MHz,thensetFDIV8bit*/
- eclk_val=(sysclk/(8*200))-1;/*Computethecorrectdividervalue*/
- ECLKDIV|=ECLKDIV_PRDIV8_MASK|eclk_val;/*WritetheECLKDIVregisterwiththecorrectsettings*/
- }else{
- eclk_val=(sysclk/200)-1;/*Computethecorrectdividervalue*/
- ECLKDIV|=eclk_val;/*WritetheECLKDIVregisterwiththecorrectsettings*/
- }
- ESTAT|=(ESTAT_PVIOL_MASK|ESTAT_ACCERR_MASK);/*Clearanyerrorflags*/
- }
- /**@briefThisfunctionwritesa16-bitwordtoEEPROM
- *@paramaddress,thedestinationEEPROMaddresstowritethedata
- *@paramdata,thedatatowritetoargumentaddress.
- *@return
- *NVM_NO_ERR-EEPROMWriteSuccess
- *NVM_ODD_ACCESS_ERR-EEPROMWriteError,Addressnotonanevenaddressboundry
- *NVM_ACCESS_ERR-EEPROMWriteError,AccessViolation
- *NVM_PROTECTION_ERR-EEPROMWriteError,Attemptedtowriteaprotectedsector
- */
- charEEPROM_Write_Word(unsignedintaddress,unsignedintdata)
- {
- while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
- ;
- }
- ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
- if(address&0x0001){
- return(NVM_ODD_ACCESS_ERR);/*AddressisNOTalignedonanevenboundry?*/
- }
- (*(unsignedint*)address)=data;/*Writethedatatothespecifiedaddress*/
- ECMD=ECMD_CMDB5_MASK;/*StoreprogrammingcommandinFCMD*/
- ESTAT_CBEIF=1;/*Executethecommand*/
- if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
- return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
- }
- if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
- return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
- }
- return(NVM_NO_ERR);/*ReturnNoError*/
- }
- /**@briefThisfunctionerasesa4-bytesectorofEEPROM
- *@paramaddress,thestartofthe4-bytesectortoaddress
- *@return
- *NVM_NO_ERR-EEPROMWriteSuccess
- *NVM_ODD_ACCESS_ERR-EEPROMWriteError,Addressnotonanevenaddressboundry
- *NVM_ACCESS_ERR-EEPROMWriteError,AccessViolation
- *NVM_PROTECTION_ERR-EEPROMWriteError,Attemptedtowriteaprotectedsector
- */
- charEEPROM_Erase_Sector(unsignedintaddress)
- {
- while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
- ;
- }
- ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
- if(address&0x0001){
- return(NVM_ODD_ACCESS_ERR);/*AddressisNOTalignedonanevenboundry?*/
- }
- (*(unsignedint*)address)=0xFFFF;/*Writethedatatothespecifiedaddress*/
- ECMD=ECMD_CMDB6_MASK;/*StoreprogrammingcommandinFCMD*/
- ESTAT_CBEIF=1;/*Executethecommand*/
- if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
- return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
- }
- if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
- return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
- }
- return(NVM_NO_ERR);/*ReturnNoError*/
- }
- charEEPROM_Erase_All(void)
- {
- while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
- ;
- }
- ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
- (*(unsignedint*)0x0400)=0xFFFF;/*Writethedatatothespecifiedaddress*/
- ECMD=0x41;/*StoreprogrammingcommandinFCMD*/
- ESTAT_CBEIF=1;/*Executethecommand*/
- if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
- return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
- }
- if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
- return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
- }
- return(NVM_NO_ERR);/*ReturnNoError*/
- }
- /**@briefThisfunctionreadsa16-bitwordfromthespecifiedaddressinEEPROM
- *@paramaddress,thestartofthe16-bitdatatoread
- *@returnThe16-bitwordstoredinlocationaddress
- */
- unsignedintEEPROM_Read_Word(unsignedintaddress)
- {
- unsignedintdata;
- while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
- ;
- }
- data=(*(unsignedint*)address);/*Readthedataatlocationaddress*/
- return(data);/*Returnthedata*/
- }
- #include
/*commondefinesandmacros*/ - #include”derivative.h”/*derivative-specificdefinitions*/
- #include”sci.h”
- #include”eets4k.h”
- voidmain(void)
- {
- unsignedintdata;
- EEPROM_Init(16384);
- EnableInterrupts;
- EEPROM_Erase_All();
- EEPROM_Write_Word(0x400,1234);
- data=EEPROM_Read_Word(0x400);
- for(;;)
- {
- _FEED_COP();/*feedsthedog*/
- }/*loopforever*/
- }