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

📄 spi.c

📁 this the firmware provided freely for the trf7960
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "spi.h"
#include "globals.h"

#define DBG 0

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void PARset(void)
{
	EnableSet;

	TRFDirOUT;			/* P4 output */
	TRFFunc;
	TRFWrite = 0x00;	/* P4 set to 0 - choose parallel inteface for the TRF796x */

	clkOFF;
	clkPINset;			/* CLK on P2.7 */

	/*
	 * P2IE |= BIT3;
	 * //IRQ on P2.3
	 */
	irqPINset;
	irqEDGEset;			/* rising edge interrupt */

	LEDallOFF;
	LEDportSET;
}						/* PARset */

/*
 =======================================================================================================================
 =======================================================================================================================
 */

void STOPcondition(void)
{
	TRFWrite |= 0x80;	/* stop condition */
	clkON;
	TRFWrite = 0x00;
	clkOFF;
}						/* STOPcondition */

/*
 =======================================================================================================================
 =======================================================================================================================
 */

void STOPcont(void)
{	/* stop condition for continous mode */
	TRFWrite = 0x00;
	TRFDirOUT;
	TRFWrite = 0x80;
	__no_operation();
	TRFWrite = 0x00;
}	/* STOPcond */

/*
 =======================================================================================================================
 =======================================================================================================================
 */

void STARTcondition(void)
{
	TRFWrite = 0x00;
	clkON;
	TRFWrite = 0xff;
	clkOFF;
}	/* STARTcondition */

/*
 =======================================================================================================================
    Function writes only one register or a multiple number ;
    of registers with specified addresses ;
 =======================================================================================================================
 */
void WriteSingle(unsigned char *pbuf, unsigned char lenght)
{
	/*~~~~~~~~~~~~~~*/
	unsigned char	i;
	/*~~~~~~~~~~~~~~*/

	STARTcondition();
	while(lenght > 0)
	{
		*pbuf = (0x1f &*pbuf);	/* register address */

		/* address, write, single */
		for(i = 0; i < 2; i++)
		{
			TRFWrite = *pbuf;	/* send command and data */
			clkON;
			clkOFF;
			pbuf++;
			lenght--;
		}
	}	/* while */

	STOPcondition();
}		/* WriteSingle */

/*
 =======================================================================================================================
    Function writes a specified number of registers from ;
    a specified address upwards ;
 =======================================================================================================================
 */
void WriteCont(unsigned char *pbuf, unsigned char lenght)
{
	STARTcondition();
	*pbuf = (0x20 | *pbuf); /* address, write, continous */
	*pbuf = (0x3f &*pbuf);	/* register address */
	while(lenght > 0)
	{
		TRFWrite = *pbuf;	/* send command */
		clkON;
		clkOFF;
		pbuf++;
		lenght--;
	}						/* while */

	STOPcont();
}	/* WriteCont */

/*
 =======================================================================================================================
    Function reads only one register ;
 =======================================================================================================================
 */
void ReadSingle(unsigned char *pbuf, unsigned char lenght)
{
	STARTcondition();
	while(lenght > 0)
	{
		*pbuf = (0x40 | *pbuf); /* address, read, single */
		*pbuf = (0x5f &*pbuf);	/* register address */

		TRFWrite = *pbuf;		/* send command */
		clkON;
		clkOFF;

		TRFDirIN;				/* read register */
		clkON;
		__no_operation();
		*pbuf = TRFRead;
		clkOFF;

		TRFWrite = 0x00;
		TRFDirOUT;

		pbuf++;
		lenght--;
	}	/* while */

	STOPcondition();
}		/* ReadSingle */

/*
 =======================================================================================================================
    Function reads specified number of registers from a ;
    specified address upwards. ;
 =======================================================================================================================
 */
void ReadCont(unsigned char *pbuf, unsigned char lenght)
{
	STARTcondition();
	*pbuf = (0x60 | *pbuf); /* address, read, continous */
	*pbuf = (0x7f &*pbuf);	/* register address */
	TRFWrite = *pbuf;		/* send command */
	clkON;
	clkOFF;
	TRFDirIN;				/* read register */

	/*
	 * TRFWrite = 0x00;
	 */
	while(lenght > 0)
	{
		clkON;

		/*
		 * TRFDirIN;
		 */
		__no_operation();
		*pbuf = TRFRead;

		/*
		 * TRFDirOUT;
		 */
		clkOFF;
		pbuf++;
		lenght--;
	}						/* while */

	STOPcont();
}	/* ReadCont */

/*
 =======================================================================================================================
    Function DirectCommand transmits a command to the reader chip
 =======================================================================================================================
 */
void DirectCommand(unsigned char *pbuf)
{
	STARTcondition();
	*pbuf = (0x80 | *pbuf); /* command */
	*pbuf = (0x9f &*pbuf);	/* command code */
	TRFWrite = *pbuf;		/* send command */
	clkON;
	clkOFF;
	STOPcondition();
}	/* DirectCommand */

/*
 =======================================================================================================================
    Function used for direct writing to reader chip ;
 =======================================================================================================================
 */
void RAWwrite(unsigned char *pbuf, unsigned char lenght)
{
	STARTcondition();
	while(lenght > 0)
	{
		TRFWrite = *pbuf;	/* send command */
		clkON;
		clkOFF;
		pbuf++;
		lenght--;
	}						/* while */

	STOPcont();
}	/* RAWwrite */

/*
 =======================================================================================================================
    Direct mode (no stop condition) ;
 =======================================================================================================================
 */
void DirectMode(void)
{
	OOKdirOUT;
	STARTcondition();
	TRFWrite = ChipStateControl;
	clkON;
	clkOFF;
	TRFWrite = 0x61;	/* write a 1 to BIT6 in register
						 * 0x00;
						 * */
	clkON;
	clkOFF;

	TRFDirIN;			/* put the PORT1 to tristate */
}						/* DirectMode */

/*
 =======================================================================================================================
    Send a specified number of bytes from buffer to host ;
 =======================================================================================================================
 */
void Response(unsigned char *pbuf, unsigned char lenght)
{
	/*
	 * char msg[40];
	 */
	while(lenght > 0)
	{
		/*
		 * sprintf(msg, "[%x]", *pbuf++);
		 * *send_cstring(msg);
		 */
		kputchar('[');
		Put_byte(*pbuf);
		kputchar(']');
		pbuf++;
		lenght--;
	}

	put_crlf();
}	/* Response */

/*
 =======================================================================================================================

⌨️ 快捷键说明

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