?? seeddm642_esam.c
字號:
/********************************************************************/
/* Copyright 2004 by SEED Incorporated. */
/* All rights reserved. Property of SEED Incorporated. */
/* Restricted rights to use, duplicate or disclose this code are */
/* granted through contract. */
/* */
/********************************************************************/
#include "seeddm642.h"
#include "seeddm642_esam.h"
//---------Global constants---------
//Maximum count value
#define TIMER_CNT 20
//Timer control register (CTL)
Uint32 TimerControl =
TIMER_CTL_RMK
(
TIMER_CTL_SPND_EMUSTOP,
TIMER_CTL_INVINP_NO, // TINP inverter control(INVINP)
TIMER_CTL_CLKSRC_CPUOVR8, // Timer input clock source (CLKSRC)
TIMER_CTL_CP_PULSE, // Clock/pulse mode(CP)
TIMER_CTL_HLD_YES, // Hold(HLD)
TIMER_CTL_GO_NO, // Go bit(GO)-
// resets & starts timer counter
TIMER_CTL_PWID_ONE, // Pulse width(PWID)-
// used only in pulse mode
TIMER_CTL_DATOUT_0, // Data output (DATOUT)
TIMER_CTL_INVOUT_NO, // TOUT inverter control (INVOUT)
TIMER_CTL_FUNC_GPIO // Function of TOUT pin(FUNC)
);
//---------Function prototypes---------
void TimerEventHandler(void);
//---------Global data definition---------
TIMER_Handle hTimer1;
TIMER_Config myTimConfig;
Uint32 TimerEventId;
int cnt = 0;
Uint32 esamCnt = 0;
Bool cntfirst = TRUE;
Bool esam_rrdy = FALSE;
Bool esam_xrdy = FALSE;
Bool esam_wen = FALSE;
Bool esam_dnew = FALSE;
/***********************************************************************/
/* */
/* seeddm642_esam_open */
/* 描述: 初始化定時器,為讀寫ESAM進行初始化 */
/* 參數(shù): 無 */
/* 返回: 無 */
/* */
/***********************************************************************/
void seeddm642_esam_open()
{
//Open TIMER1 device, and reset it to power-on default state
hTimer1 = TIMER_open(TIMER_DEV1, TIMER_OPEN_RESET);
//Obtain the event ID for the timer device
TimerEventId = TIMER_getEventId(hTimer1);
//Map TIMER events to physical interrupt number
IRQ_map(TimerEventId, 14);
//Map External int4 to physical interrupt number
IRQ_map(IRQ_EVT_EXTINT4,4);
//Reset the timer events
IRQ_reset(TimerEventId);
IRQ_reset(IRQ_EVT_EXTINT4);
//---------Configure the timer devices---------
//Start count value at zero
myTimConfig.cnt = 0x0;
//Use predefined control value */
myTimConfig.ctl = TimerControl;
//Set period,周期為(1/9600)/2
myTimConfig.prd = 0xF42;
IRQ_globalEnable();
//Enable the timer events(events are disabled while resetting)
IRQ_enable(TimerEventId);
IRQ_enable(IRQ_EVT_EXTINT4);
}
/***********************************************************************/
/* */
/* seeddm642_esam_reset */
/* 描述: 使ESAM卡復位 */
/* 參數(shù): 無 */
/* 返回: 無 */
/* */
/***********************************************************************/
void seeddm642_esam_reset()
{
//將復位信號置低,ESAM處于復位狀態(tài)
SEEDDM642_rset(SEEDDM642_ESAMW, 0x0);
//延時1ms
SEEDDM642_waitusec(5000);
//將復位信號置高,ESAM脫離復位狀態(tài)
SEEDDM642_rset(SEEDDM642_ESAMW, 0x4);
}
/***********************************************************************/
/* */
/* seeddm642_esam_read */
/* 描述: 讀取ESAM的數(shù)據(jù) */
/* 參數(shù): src :接收數(shù)據(jù)緩沖區(qū) */
/* length :接收到的數(shù)據(jù)的個數(shù) */
/* 返回: 1:無新數(shù)據(jù)產(chǎn)生 */
/* 2:有新數(shù)據(jù)產(chǎn)生 */
/* 3:接收錯誤 */
/* */
/***********************************************************************/
int seeddm642_esam_read(Uint32 src, Uint32 length )
{
Uint8 *pdst;
Uint8 *plength;
Uint8 esamdata = 0;
Uint8 esambit = 0;
Uint8 esamcheck = 0;
Uint8 esamcount =0;
Uint32 i;
Uint32 n= 0;
/* Establish ricieve pointer */
pdst = (Uint8 *)src;
plength = (Uint8 *)length;
*plength = 0;
//打開中斷
IRQ_enable(IRQ_EVT_EXTINT4);
//將IO口配置成為輸入模式
SEEDDM642_rset(SEEDDM642_ESAMW, 0x4);
//如果讀取標志置1,讀取數(shù)據(jù)的一個BIT,共讀9次,拼成一個字節(jié)
//第九個BIT為偶較驗位
while(1)
{
if(esam_dnew == FALSE)
{
//超時即數(shù)據(jù)接收完成,再無新數(shù)據(jù)產(chǎn)生
n++;
if(n == 1000)
{
n = 0;
esamCnt++;
}
if(esamCnt == 300)
{
esamCnt = 0;
if(esamcount ==0)
{
return 1;//無數(shù)據(jù)接收到
}
else
{
*plength = esamcount;
return 2;
}
}
}
else
{
//有新數(shù)據(jù)的產(chǎn)生,啟動計數(shù)器
esam_dnew = FALSE;
//記數(shù)重新開始
esamCnt = 0;
for(i = 0;i<8;i++)
{
while(esam_rrdy == FALSE){}
esam_rrdy = FALSE;
esambit = SEEDDM642_rget(SEEDDM642_ESAMR);
esamcheck = esamcheck + (esambit & 0x1);
esamdata = esamdata + ((esambit & 0x1)<<i);
}
/*讀較驗位*/
while(esam_rrdy == FALSE){}
esam_rrdy = FALSE;
esambit = SEEDDM642_rget(SEEDDM642_ESAMR);
esamcheck = esamcheck + (esambit & 0x1);
if((esamcheck & 0x1) == 1)
{
return 0;
}
*pdst++ = esamdata;
esamcount++;
esamdata = 0;
esamcheck = 0;
esambit = 0;
//停止計數(shù)器
TIMER_pause(hTimer1);
//打開中斷
IRQ_reset(IRQ_EVT_EXTINT4);
IRQ_enable(IRQ_EVT_EXTINT4);
}
}
}
/***********************************************************************/
/* */
/* seeddm642_esam_write */
/* 描述: 寫入ESAM的數(shù)據(jù) */
/* 參數(shù): src :接收數(shù)據(jù)緩沖區(qū) */
/* length :發(fā)送的數(shù)據(jù)的個數(shù) */
/* 返回: 無 */
/***********************************************************************/
void seeddm642_esam_write(Uint32 src, Uint32 length)
{
Uint32 i,j,k;
Uint8 esam_xd;
Uint8 esam_xbit;
Uint8 esam_xcheck = 0;
Uint8 *pdst;
//使能寫操作
esam_wen = TRUE;
/* Establish ricieve pointer */
pdst = (Uint8 *)src;
//禁止ESAM中斷
IRQ_disable(IRQ_EVT_EXTINT4);
//啟動計數(shù)器
TIMER_config(hTimer1, &myTimConfig);
//Start the timers
TIMER_start(hTimer1);
/*發(fā)送數(shù)據(jù)*/
for(i = 0;i<length;i++)
{
esam_xd = *pdst++;
/*將ESAMIO置為高電平*/
SEEDDM642_rset(SEEDDM642_ESAMW, 0x7);
/*延時一段時間,做為兩個字節(jié)間的間隔*/
for(k= 0;k<4;k++)
{
while(esam_xrdy ==FALSE){}
esam_xrdy = FALSE;
}
/*發(fā)出起始位*/
while(esam_xrdy ==FALSE){}
esam_xrdy = FALSE;
SEEDDM642_rset(SEEDDM642_ESAMW, 0x6);
for(j = 0; j<8;j++)
{
while(esam_xrdy ==FALSE){}
esam_xrdy = FALSE;
esam_xbit = (esam_xd >>j) & 0x1;
esam_xcheck = esam_xcheck + esam_xbit;
SEEDDM642_rset(SEEDDM642_ESAMW, (0x6 + esam_xbit));
}
//設置偶較驗位
while(esam_xrdy ==FALSE){}
esam_xrdy = FALSE;
if((esam_xcheck & 0x1)==0)
{
SEEDDM642_rset(SEEDDM642_ESAMW, 0x6);
}
else
{
SEEDDM642_rset(SEEDDM642_ESAMW, 0x7);
}
while(esam_xrdy ==FALSE){}
esam_xrdy = FALSE;
SEEDDM642_rset(SEEDDM642_ESAMW, 0x7);
}
//停止計數(shù)器
TIMER_pause(hTimer1);
//禁止寫
esam_wen = FALSE;
}
//---------Subroutine definition---------
//Function called from TIMER1 ISR. Just increments the count by
// one each time it enters this function. Exit from the program
// after certain count value is reached.
void TimerEventHandler(void)
{
//Process timer event here
if(esam_wen ==TRUE)
{
cnt++;
if(cnt == 2)
{
cnt = 0;
esam_xrdy = TRUE;
}
}
else
{
if(cntfirst == TRUE)
{
cnt++;
if(cnt ==3)
{
esam_rrdy = TRUE;
cntfirst = FALSE;
cnt= 0;
}
}
else
{
cnt++;
if(cnt == 2)
{
cnt = 0;
esam_rrdy = TRUE;
}
}
}
}
//ISR to service TIMERINT1.
// vecs_dm642.asm must be modified to include c_int14 entry.
interrupt void c_int14(void)
{
TimerEventHandler();
return;
}
//ISR to service EXTERN INT1.
// ves_dm642.asm must be modified to include c_int04 entry.
interrupt void c_int04(void)
{
//禁止ESAM中斷
IRQ_disable(IRQ_EVT_EXTINT4);
//設置有新的數(shù)據(jù)的標志
esam_dnew = TRUE;
cntfirst = TRUE;
//Reset the timer events
IRQ_reset(TimerEventId);
//Enable the timer events(events are disabled while resetting)
IRQ_enable(TimerEventId);
TIMER_config(hTimer1, &myTimConfig);
//Start the timers
TIMER_start(hTimer1);
cnt = 0;
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -