📄 main.c
字号:
#include <hidef.h> /* common defines and macros */
#include <mc9s12xdt512.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xdt512"
#include "main.h"
//****************************************************
// EEPROM
//****************************************************
/****************************************************
Function Name : EEpromInit
Arguments : none
Returns : none
Notes : This function initialize EEPROM module.
*****************************************************/
void EEPROMInit(u32_t OSCCLK)
{
u32_t clk;
/*set ECLKDIV register according to the Oscillator */
if(OSCCLK>1280000)
{
ECLKDIV_PRDIV8=1;
clk = OSCCLK / 8;
}
else
{
ECLKDIV_PRDIV8=0;
clk=OSCCLK;
}
ECLKDIV_EDIV=(u8_t)(clk/200000);
ESTAT_PVIOL=1; /*clear the PVIOL flag bit*/
ESTAT_ACCERR=1; /*clear the ACCERR flag bit*/
ECNFG_CCIE=0; /*command complete interrupt disable*/
ECNFG_CBEIE=0; /*command buffer empty interrput disable*/
EPROT_EPOPEN=1; /*sectors not protected and are enabled for program or erase*/
EPROT_EPDIS=1; /*protection disable*/
}
/***************************************************
Function Name : EEPROMEraseVerify
Arguments : none
Returns : FAIL:0
OK:1
Notes :
****************************************************/
u8_t EEPROMEraseVerify(void)
{
while(!ESTAT_CBEIF){} //if there are still some commands in the buffer, wait
ECMD=0x05;
ESTAT_CBEIF=1; //clear interrupt flag
if((ESTAT_PVIOL==1) || (ESTAT_ACCERR==1))//if there errors
{
ESTAT_PVIOL=1;
ESTAT_ACCERR=1; //clear error flags
return(0);
}
while(!ESTAT_CBEIF){} //wait for the command complete
if(ESTAT_BLANK==1)
return(1); //if eeprom is blank
else
return(0);
}
/*************************************************
Function Name : EEPROMWordProgram
Arguments : addr
data
data_length -------how many words
Returns FAIL:0
OK:1
Notes :
**************************************************/
u8_t EEPROMWordProgram(u16_t addr,u16_t *data,u8_t data_length)
{
u16_t *paddr;
u8_t count;
if((addr<0xC00) || (addr>0x1000)) //if address is out of EEPROM range. according to datasheet Local Memory Map
return (0);
if(addr%2!=0) //if it is not an aligned word.
return(0);
if((ESTAT_PVIOL==1) || (ESTAT_ACCERR==1))
{
ESTAT_PVIOL=1;
ESTAT_ACCERR=1; //clear error flags
//return(0);
}
for(count = 0;count < data_length;count++)
{
paddr= (u16_t *)addr;
while(!ESTAT_CBEIF){} //if there are still some commands in the buffer, wait
*paddr=*data; //write the data into the address
ECMD=0x20;
ESTAT_CBEIF=1; //clear the interrupt flag.
if(ESTAT_PVIOL==1 || ESTAT_ACCERR==1) //there are errors.
{
ESTAT_PVIOL=1;
ESTAT_ACCERR=1; //clear the error flags.
return(0);
}
data = data+1;
addr=addr+2;
}
while(ESTAT_CCIF!=1)
{
}
return(1);
}
/*************************************************
Function Name : EEPROMSectorErase
Arguments : addr
data_length -----how many words
Returns : FAIL:0
OK:1
Notes :
**************************************************/
u8_t EEPROMSectorErase(u16_t addr,u8_t data_length)
{
u16_t *paddr;
u8_t count;
if((addr<0xC00) || (addr>0x1000)) //if address is out of EEPROM range. according to datasheet Local Memory Map
return(0);
if(addr%4!=0) //if address is not aligned
return(0);
if((ESTAT_PVIOL==1) || (ESTAT_ACCERR==1))
{
ESTAT_PVIOL=1;
ESTAT_ACCERR=1; //clear error flags
return(0);
}
for(count = 0;count < data_length;count++)
{
paddr=(u16_t *)addr;
while(!ESTAT_CBEIF){} //if there are still some commands in the buffer, wait
*paddr=0xFF;
ECMD=0x40;
ESTAT_CBEIF=1; //clear the interrupt flag
if((ESTAT_PVIOL==1) || (ESTAT_ACCERR==1))//if there are erros
{
ESTAT_PVIOL=1;
ESTAT_ACCERR=1; //clear error flags
return(0);
}
addr=addr+2;
}
while(ESTAT_CCIF!=1)
{
}
return(1);
}
/******************************************************
Function Name : EEPROMWordRead
Arguments : addr
data
Returns FAIL:0
OK:1
Notes :
******************************************************/
u8_t EEPROMWordRead(u16_t addr,u16_t *data)
{
if((addr<0xC00) || (addr>0x1000)) //if address is out of EEPROM range.
{
return (0);
}
if(addr%2!=0) //if it is not an aligned word.
{
return(0);
}
else
{
*data=*(u16_t *)addr;
return(1);
}
}
/******************************************************
Function Name : EEPROMU32Read
Arguments : addr
data
Returns FAIL:0
OK:1
Notes : read 32 bits data
******************************************************/
u8_t EEPROMU32Read(u16_t addr,u32_t *data)
{
if((addr<0xC00) || (addr>0x1000)) //if address is out of EEPROM range.
{
return (0);
}
if(addr%2!=0) //if it is not an aligned word.
{
return(0);
}
else
{
*data=*(u32_t *)addr;
return(1);
}
}
/*****************************************/
void main(void) {
/* put your own code here */
u8_t test_flag = 0;
u32_t write_data,read_data;
EEPROMInit(16000000);
write_data = 0x12345678;
while(test_flag == 0)
{
test_flag = EEPROMSectorErase(0xC00,2);
}
test_flag = 0;
while(test_flag == 0)
{
test_flag = EEPROMWordProgram(0xC00,&write_data,2);
}
test_flag = 0;
while(test_flag == 0)
{
test_flag = EEPROMU32Read(0xC00,&read_data);
}
EnableInterrupts;
while(1)
{
}
/* please make sure that you never leave this function */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -