📄 spi.c~
字号:
#include "spi.h"
void PARset(void)
{
EnableSet;
TRFDirOUT; //P4 je izhod
TRFFunc;
TRFWrite = 0x00; //P4 postavimo na 0 - paralelna komunikacija
clkOFF;
clkPINset; //CLK on P2.7
// P2IE |= BIT3; //IRQ on P2.3
irqPINset;
irqEDGEset; //rising edge interrupt
LEDportDATA &= ~(BIT0 + BIT1);
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)
{
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);
lenght--;
}
put_crlf();
}//Response
void InitialSettings(void)
{
unsigned char command[2];
command[0] = ModulatorControl;
command[1] = 0x21;
WriteSingle(command, 2);
}
void InterruptHandlerReader(unsigned char *Register)
{
char phello[20]; //for testing
unsigned char len;
// Register = Register & 0xF7; //set the parity flag to 0
//parity is not used in 15693 and Tag-It
Put_byte(*Register);
//-------------------------------------------------------------
if(*Register == 0xA0){ //TX active and only 3 bytes left in FIFO
i_reg = 0x00;
kputchar('.'); //FIFO filling is done in the transmit function
}
//-------------------------------------------------------------
else if((*Register == BIT7) || (*Register == 0x00)){ //TX complete
i_reg = 0x00;
*Register = Reset; //reset the FIFO after TX
DirectCommand(Register);
kputchar('T');
}
//-------------------------------------------------------------
else if((*Register & BIT1) == BIT1){ //collision error
if((*Register & BIT6) == BIT6){
i_reg = 0x01; //RX still active
RXErrorFlag = 0x02;
}//if
else i_reg = 0x02; //RX complete
kputchar('{');
CollPoss = CollisionPosition;
ReadSingle(&CollPoss, 1);
// CollPoss &= 0xF7;
len = CollPoss - 0x20; //number of valid bytes if FIFO
Put_byte(CollPoss);
kputchar('}');
if((len & 0x0f) != 0x00)
len = len + 0x10; //add 1 byte if broken byte recieved
len = len >> 4;
if(len != 0x00){
buf[RXTXstate] = FIFO; //write the recieved bytes to the correct place of the buffer;
ReadCont(&buf[RXTXstate], len);
RXTXstate = RXTXstate + len;
}//if
if(!(P2IFG & BIT3)) return;
*Register=IRQStatus; //IRQ status register address
ReadSingle(Register, 1); //function call for single address read
if((*Register == 0x40) || (*Register == 0x00)){ //end of recieve
i_reg = 0x02; //signal to the recieve funnction that EOF is recieved
*Register = Reset; //reset the FIFO after last byte has been read out
DirectCommand(Register);
}//if
}
//-------------------------------------------------------------
else if(*Register == BIT6){ //RX flag means that EOF has been recieved
//and the number of unread bytes is in FIFOstatus regiter
if(RXErrorFlag == 0x02){
i_reg = 0x02;
return;
}
*Register = FIFOStatus;
ReadSingle(Register, 1); //determine the number of bytes left in FIFO
*Register = (0x0F & *Register) + 0x01;
buf[RXTXstate] = FIFO; //write the recieved bytes to the correct place of the buffer;
ReadCont(&buf[RXTXstate], *Register);
RXTXstate = RXTXstate + *Register;
*Register = TXLenghtByte2; //determine if there are broken bytes
ReadSingle(Register, 1); //determine the number of bits
if((*Register & BIT0) == BIT0){
*Register = (*Register >> 1) & 0x07; //mask the first 5 bits
*Register = 8 - *Register;
buf[RXTXstate - 1] &= 0xFF << *Register;
}//if
kputchar('E');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -