⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 esam.c

📁 DEC——6713开发板ESAM加密源代码
💻 C
字号:
/*********************************************************************************
* ESAM.C	v1.00	     							                            *
* Copyright	2003 by SEED Electronic Technology Ltd.
* All rights reserved. Property of SEED Electronic Technology Ltd.			                *
* Designed by:	Hongshuai.Li								                                *
*********************************************************************************/
/********************************************************************************\
\*	The routine include all applied function in DEC6713_ESAM project. *\
\********************************************************************************/

#include <csl.h>
#include <csl_irq.h>
#include <csl_timer.h>
#include <csl_gpio.h>
#include <stdio.h>
#include <std.h>
#include <DEC6713.h>
#include <ESAM.h>

//#define TIMER_CNT  100  		/* Maximum count value */
static TIMER_Handle hTimer1;
static Uint32 TimerEventId;
static GPIO_Handle hGpio;
static int cnt = 0; 
Uint32 EsamCnt = 0; 
Bool DataReady = FALSE;
Bool NewData = FALSE;
Bool FirstBit = FALSE;
//void TimerEventHandler(void);
Uint8 INTCNT = 0;
/********************************************************************************/
static Uint32 TimerControl = TIMER_CTL_RMK( /* Timer control register (CTL)*/
  	TIMER_CTL_INVINP_NO, /* TINP inverter control(INVINP). Only affects operation
					      if CLKSRC =0.
                           TIMER_CTL_INVINP_NO  - Uninverted TINP drives timer
                           TIMER_CTL_INVINP_YES - inverted TINP drives timer */
  
  	TIMER_CTL_CLKSRC_CPUOVR4,/* Timer input clock source (CLKSRC)
						   TIMER_CTL_CLKSRC_CPUOVR4 - CPU clock /4 = 112.5MHz.         */
						   	
  	TIMER_CTL_CP_CLOCK, /* Clock/pulse mode(CP)
					       TIMER_CTL_CP_PULSE - Pulse mode.TSTAT is active one 
						        CPU clock after the timer reaches the timer
								period.PWID determines when it goes inactive.*/
					    
  	TIMER_CTL_HLD_YES, /* Hold(HLD). Counter may be read or written regardless of 
					    HLD value.
						   TIMER_CTL_HLD_YES - Counter is disabled and held in
						        current value.
						   TIMER_CTL_HLD_NO - COunter is allowed to count.   */
						   
  	TIMER_CTL_GO_NO, /* Go bit(GO). Resets and starts the timer counter.
				           TIMER_CTL_GO_NO - No effects on the timer.
						   TIMER_CTL_GO_YES - if HLD =1, the counter register
						        is zeroed and begins counting on next clock. */
  	TIMER_CTL_PWID_TWO, /* Pulse width(PWID). Only used in pulse mode.
					       TIMER_CTL_PWID_ONE - TSTAT goes inactive one timer 
						        input clock cycle after the timer counter value
								equals the timer period value.
                           TIMER_CTL_PWID_TWO -  TSTAT goes inactive one timer 
						        input clock cycle after the timer counter value
								equals the timer period value.               */

  	TIMER_CTL_DATOUT_0, /* Data output (DATOUT).
					       TIMER_CTL_DATOUT_0 - If FUNC  =0,the DATOUT is
						        driven on TOUT. 
                           TIMER_CTL_DATOUT_1 - If FUNC =1,The DATOUT is driven
						        on TOUT after inversion by INVOUT.           */

  	TIMER_CTL_INVOUT_NO, /* TOUT inverter control (INVOUT) 
					       TIMER_CTL_INVOUT_NO - Uninverted TSTAT drives TOUT
						   TIMER_CTL_INVOUT_YES - Inverted TSTAT drives TOUT.*/
  	TIMER_CTL_FUNC_TOUT /* Function of TOUT pin(FUNC).
					       TIMER_CTL_FUNC_GPIO - TOU is a general purpose 
						        output pin
                           TIMER_CTL_FUNC_TOUT - TOUT is a timer output pin  */
					        
); 

GPIO_Config MyGPIOConfig = {
	0x00000030,			// gpgc  -logic mode
	0x00008000,			// gpen  - Enable GP15 as interrupt input.
	0x00007fff,			// gpdir - Set gp15 as input. 	
	0x0000ffff,			// gpval - Default values are 0.
	0x00000000,			// gphm  - gp15hm=0.
	0x00008000,			// gplm	 - gp15lm = 1.
	0x00008000			// gppol - gp15pol = 1.
}; 

/********************************************************************************/
/********************************************************************************\
\* DEC6713_EsamOpen()		-Open esam model. *\
\* Parameters:									*\
\* Return:										*\
\********************************************************************************/
void DEC6713_EsamOpen()
{ 
	//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);
	
	//Reset the timer events
	IRQ_reset(TimerEventId);
    
	//---------Configure the timer devices---------	
	//Start count value at zero
	TIMER_configArgs(hTimer1,
    TimerControl, // use predefined control value  
    0x00000B72,   // set period = (1/9600)/2                 
    0x00000000    // start count value at zero     
  	);

	
	//Enable the timer events(events are disabled while resetting)
	IRQ_enable(TimerEventId);
	
	
	// Enable GPIO interrupt
	DEC6713_EsamPortDirection(Output);
	
	hGpio = GPIO_open(GPIO_DEV0,GPIO_OPEN_RESET);
	GPIO_reset(hGpio);
	GPIO_pinEnable(hGpio,GPIO_PIN15);
	GPIO_pinDirection(hGpio,GPIO_PIN15,GPIO_OUTPUT);
	
	GPIO_pinWrite(hGpio,GPIO_PIN15,1);
	
	GPIO_config(hGpio,&MyGPIOConfig);
	IRQ_map(IRQ_EVT_GPINT0,13);
	IRQ_reset(IRQ_EVT_GPINT0);
	IRQ_enable(IRQ_EVT_GPINT0);
	IRQ_globalEnable();

}

/********************************************************************************\
\* DEC6713_EsamPortDirection()				*\
\* Parameters:	In. Set data port as input.	*\
\*				Out.Set data Port as input. *\
\* Return:									*\
\********************************************************************************/
void DEC6713_EsamPortDirection(DataDirection Direction)
{
	Uint8 RegNum;
	// Set ESAM data port direction.
	RegNum = DEC6713_CTL_REG; 
	if(Direction == 0)
	{
		DEC6713_cpld_rset(RegNum,0x90);	
	}
	else
	{
		DEC6713_cpld_rset(RegNum,0x80);	
	}
}

/********************************************************************************\
\* DEC6713_EsamReset()		-Esam reset function. 		*\
\* Parameters:											*\
\* Return:												*\
\********************************************************************************/
void DEC6713_EsamReset()
{
	Uint8 RegNum;
	RegNum = DEC6713_CTL_REG;
//	DEC6713_cpld_rset(RegNum,0x98);
//	DEC6713_wait(0x377c8);
	DEC6713_cpld_rset(RegNum,0x90);
	// Delay about 1ms.
	DEC6713_wait(0x377c8);
	DEC6713_cpld_rset(RegNum,0x98);
}

/********************************************************************************\
\* DEC6713_EsamRead()						*\
\* Parameters:	Dst. Receive data buffer.
				Length. Read data length. 	*\
\* Return:									*\
\********************************************************************************/
Uint32 DEC6713_EsamRead(Uint32 Dst,Uint32 Length)
{
	Uint32 i;
	Uint32 n=0;
	Uint8 EsamDataBit = 0;
	Uint8 EsamData = 0;
	Uint8 EsamCheck = 0;
	Uint8 EsamCounter = 0;
	
	Uint8 *pdst;
	Uint8 *plength;
	
	pdst = (Uint8 *)Dst;
	plength = (Uint8 *)Length;
	*plength = 0;
	// Open interrupt GPIO15.

	IRQ_enable(IRQ_EVT_GPINT0);



	while(1)
	{	
		if(NewData==FALSE)
		{
			n++;
			if(n ==1000)
			{
				n = 0;
				EsamCnt++;
			}
			if(EsamCnt == 300)
			{
				EsamCnt = 0;
				if(EsamCounter == 0)
				{
					return 1;
				}
				else
				{
					*plength = EsamCounter;
					return 2;			
				}
			}
		}
		// ESAM output data consist of 9 bits. The first 8 bits are data, the last
		// is even parity bit.
		else
		{
			NewData = FALSE;
			EsamCnt = 0;
			// Configure GPIO15 as data receiving port.
			IRQ_disable(IRQ_EVT_GPINT0);
			GPIO_pinDirection(hGpio,GPIO_PIN15,GPIO_INPUT);
			for(i=0;i<8;i++)
			{
				while(!DataReady);
				DataReady = FALSE;
				EsamDataBit = GPIO_pinRead(hGpio,GPIO_PIN15);
				EsamCheck = EsamCheck + (EsamDataBit & 0x1);
				EsamData = EsamData + ((EsamDataBit&0x01)<<i);
			}	
			// Read parity bit.
			while(!DataReady);
			DataReady = FALSE;
			EsamDataBit = GPIO_pinRead(hGpio,GPIO_PIN15);
			EsamCheck = EsamCheck + (EsamDataBit&0x01);

			if(EsamCheck & 0x01)
			{
				return 0;
			}
		
			*pdst++ = EsamData;
			EsamCounter++;
			EsamDataBit = 0;
	 		EsamData = 0;
	 		EsamCheck = 0;
	 		TIMER_pause(hTimer1);
	
		GPIO_close(hGpio);		
		hGpio = GPIO_open(GPIO_DEV0,GPIO_OPEN_RESET);
		GPIO_reset(hGpio);
		GPIO_pinEnable(hGpio,GPIO_PIN15);
		GPIO_config(hGpio,&MyGPIOConfig);
		IRQ_map(IRQ_EVT_GPINT0,13);
		IRQ_reset(IRQ_EVT_GPINT0);
		IRQ_enable(IRQ_EVT_GPINT0);
		IRQ_globalEnable();	
		}	
	}
}
/********************************************************************************\
\* Timer interrupt handle routine.*\
\********************************************************************************/
interrupt void c_int14(void)
{
	if(FirstBit == TRUE)
	{
		cnt++;
		if(cnt ==3)
		{
			DataReady = TRUE;
			FirstBit = FALSE;
			cnt= 0;
		}
		
	}
	else
	{
		cnt++;
		if(cnt == 2)
		{
			cnt = 0;
			DataReady = TRUE;
		}  
	}
	return;
} 

/********************************************************************************\
\* GPIO15 interrupt handle routine.*\
\********************************************************************************/
interrupt void c_int13(void)
{
	IRQ_disable(IRQ_EVT_GPINT0);
	NewData = TRUE;
	FirstBit = TRUE;
	IRQ_reset(TimerEventId);
	//Enable the timer events(events are disabled while resetting)
	IRQ_enable(TimerEventId);
	
	TIMER_configArgs(hTimer1,
    TimerControl, // use predefined control value  
    0x000005B9,   // set period = (1/9600)/2                 
    0x00000000    // start count value at zero     
  	);
	//Start the timers
	cnt = 0;
	TIMER_start(hTimer1);
	INTCNT++;
	return;
}    
   
/********************************************************************************\
\* End of ESAM.C *\
\********************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -