www.pudn.com > SEEDVPM642_esam.rar > 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进行初始化					   */ 
/*      参数:  无													   */ 
/*      返回:  无													   */ 
/*																	   */ 
/***********************************************************************/ 
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卡复位										   */ 
/*      参数:  无													   */ 
/*      返回:  无													   */ 
/*																	   */ 
/***********************************************************************/ 
void seeddm642_esam_reset() 
{ 
	//将复位信号置低,ESAM处于复位状态 
	SEEDDM642_rset(SEEDDM642_ESAMW, 0x0); 
	//延时1ms 
	SEEDDM642_waitusec(5000); 
	//将复位信号置高,ESAM脱离复位状态 
	SEEDDM642_rset(SEEDDM642_ESAMW, 0x4); 
} 
/***********************************************************************/ 
/*																	   */ 
/*	seeddm642_esam_read 											   */ 
/*		描述:	读取ESAM的数据										   */ 
/*      参数:  src  :接收数据缓冲区								   */ 
/*              length :接收到的数据的个数                             */ 
/*      返回:  1:无新数据产生										   */ 
/*				2:有新数据产生                                         */ 
/*				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,读取数据的一个BIT,共读9次,拼成一个字节 
	//第九个BIT为偶较验位 
	while(1) 
	{ 
		if(esam_dnew == FALSE) 
		{ 
			//超时即数据接收完成,再无新数据产生 
			n++; 
			if(n == 1000) 
			{ 
				n = 0; 
				esamCnt++; 
			}		 
			if(esamCnt == 300) 
			{ 
				esamCnt = 0; 
				if(esamcount ==0) 
				{ 
					return 1;//无数据接收到 
				} 
				else 
				{ 
					*plength = esamcount; 
					return 2; 
				} 
			}  
		} 
		else 
		{ 
			//有新数据的产生,启动计数器 
			esam_dnew = FALSE;			 
			//记数重新开始 
			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)<>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);			 
	} 
	//停止计数器 
	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); 
	//设置有新的数据的标志 
	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; 
}