remove alle unused code and fix all warnings
This commit is contained in:
parent
798048343e
commit
f5fa14bc21
561
Mfrc522.c
561
Mfrc522.c
@ -1,561 +0,0 @@
|
||||
/*
|
||||
File: Mfrc522.cpp
|
||||
Mfrc522 - Library for communicating with MFRC522 based NFC Reader/Writers
|
||||
Created by Eelco Rouw - Mainly based on code from Grant Gibson (www.grantgibson.co.uk) and Dr.Leong ( WWW.B2CQSHOP.COM )
|
||||
Lightly modified by Frank Milburn
|
||||
Lightly modified by Roy Bakker C++ --> C
|
||||
Released into the public domain
|
||||
Lightly modified by Mats van Reenen: use other SPI interface
|
||||
*/
|
||||
|
||||
#include <msp430.h>
|
||||
#include <Mfrc522.h>
|
||||
#include <SPI.h>
|
||||
//#include <SPI2.h>
|
||||
|
||||
void RC522_init(int chipSelectPin, int NRSTPD)
|
||||
{
|
||||
_chipSelectPin = chipSelectPin;
|
||||
P1DIR |= _chipSelectPin;
|
||||
P1OUT |= _chipSelectPin;
|
||||
|
||||
_NRSTPD = NRSTPD;
|
||||
P1DIR |= _NRSTPD;
|
||||
P1OUT |= _NRSTPD;
|
||||
|
||||
RC522_reset();
|
||||
|
||||
//Timer: TPrescaler*TreloadVal/6.78MHz = 24ms
|
||||
RC522_writeReg(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
|
||||
RC522_writeReg(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg
|
||||
RC522_writeReg(TReloadRegL, 30);
|
||||
RC522_writeReg(TReloadRegH, 0);
|
||||
|
||||
RC522_writeReg(TxAutoReg, 0x40); //100%ASK
|
||||
RC522_writeReg(ModeReg, 0x3D);
|
||||
|
||||
//RC522_clearBitMask(Status2Reg, 0x08); //MFCrypto1On=0
|
||||
//RC522_writeReg(RxSelReg, 0x86); //RxWait = RxSelReg[5..0]
|
||||
//RC522_writeReg(RFCfgReg, 0x7F); //RxGain = 48dB
|
||||
|
||||
RC522_antennaOn();
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: WriteReg
|
||||
* Description: write a byte data into one register of MR RC522
|
||||
* Input parameter: addr--register addressï¼›val--the value that need to write in
|
||||
* Return: Null
|
||||
*/
|
||||
void RC522_writeReg(unsigned char addr, unsigned char val)
|
||||
{
|
||||
SPIInitSend(_chipSelectPin, SPI_Mode0, SPI_Clock_4MHz);
|
||||
|
||||
SPI_packet[3] = (addr<<1)&0x7E;
|
||||
SPI_packet[4] = val;
|
||||
SPI_packetC = 3;
|
||||
|
||||
SPISend();
|
||||
|
||||
// P1OUT &= ~(_chipSelectPin);
|
||||
|
||||
// SPI_transfer((addr<<1)&0x7E);
|
||||
// SPI_transfer(val);
|
||||
|
||||
// P1OUT |= _chipSelectPin;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: ReadReg
|
||||
* Description: read a byte data into one register of MR RC522
|
||||
* Input parameter: addr--register address
|
||||
* Return: return the read value
|
||||
*/
|
||||
unsigned char RC522_readReg(unsigned char addr)
|
||||
{
|
||||
SPIInitSend(_chipSelectPin, SPI_Mode0, SPI_Clock_4MHz);
|
||||
|
||||
SPI_packet[3] = ((addr<<1)&0x7E) | 0x80;
|
||||
SPI_packet[4] = 0;
|
||||
SPI_packetC = 3;
|
||||
|
||||
SPISend();
|
||||
|
||||
return SPI_packet[4];
|
||||
|
||||
// unsigned char val;
|
||||
// P1OUT &= ~(_chipSelectPin);
|
||||
|
||||
// SPI_transfer(((addr<<1)&0x7E) | 0x80);
|
||||
// val = SPI_transfer(0x00);
|
||||
|
||||
// P1OUT |= _chipSelectPin;
|
||||
|
||||
// return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: SetBitMask
|
||||
* Description: set RC522 register bit
|
||||
* Input parameter: reg--register address;mask--value
|
||||
* Return: null
|
||||
*/
|
||||
void RC522_setBitMask(unsigned char reg, unsigned char mask)
|
||||
{
|
||||
unsigned char tmp;
|
||||
tmp = RC522_readReg(reg);
|
||||
RC522_writeReg(reg, tmp | mask); // set bit mask
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: ClearBitMask
|
||||
* Description: clear RC522 register bit
|
||||
* Input parameter: reg--register address;mask--value
|
||||
* Return: null
|
||||
*/
|
||||
void RC522_clearBitMask(unsigned char reg, unsigned char mask)
|
||||
{
|
||||
unsigned char tmp;
|
||||
tmp = RC522_readReg(reg);
|
||||
RC522_writeReg(reg, tmp & (~mask)); // clear bit mask
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Function: AntennaOn
|
||||
* Description: Turn on antenna, every time turn on or shut down antenna need at least 1ms delay
|
||||
* Input parameter: null
|
||||
* Return: null
|
||||
*/
|
||||
void RC522_antennaOn(void)
|
||||
{
|
||||
unsigned char temp;
|
||||
|
||||
temp = RC522_readReg(TxControlReg);
|
||||
if (!(temp & 0x03))
|
||||
{
|
||||
RC522_setBitMask(TxControlReg, 0x03);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Function: AntennaOff
|
||||
* Description: Turn off antenna, every time turn on or shut down antenna need at least 1ms delay
|
||||
* Input parameter: null
|
||||
* Return: null
|
||||
*/
|
||||
void RC522_antennaOff(void)
|
||||
{
|
||||
RC522_clearBitMask(TxControlReg, 0x03);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the MFRC522 Receiver Gain (RxGain) to value specified by given mask.
|
||||
* See 9.3.3.6 / table 98 in http://www.nxp.com/documents/data_sheet/MFRC522.pdf
|
||||
* NOTE: Given mask is scrubbed with (0x07<<4)=01110000b as RCFfgReg may use reserved bits.
|
||||
*/
|
||||
void RC522_setAntennaGain(unsigned char mask) {
|
||||
RC522_clearBitMask(RFCfgReg, (0x07<<4)); // clear needed to allow 000 pattern
|
||||
RC522_setBitMask(RFCfgReg, (mask & 0x07)<<4); // only set RxGain[2:0] bits
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: Reset
|
||||
* Description: reset RC522
|
||||
* Input parameter: null
|
||||
* Return: null
|
||||
*/
|
||||
void RC522_reset(void)
|
||||
{
|
||||
RC522_writeReg(CommandReg, PCD_RESETPHASE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: Request
|
||||
* Description: Searching card, read card type
|
||||
* Input parameter: reqMode--search methods,
|
||||
* TagType--return card types
|
||||
* 0x4400 = Mifare_UltraLight
|
||||
* 0x0400 = Mifare_One(S50)
|
||||
* 0x0200 = Mifare_One(S70)
|
||||
* 0x0800 = Mifare_Pro(X)
|
||||
* 0x4403 = Mifare_DESFire
|
||||
* Return: return MI_OK if successed
|
||||
*/
|
||||
unsigned char RC522_request(unsigned char reqMode, unsigned char *TagType)
|
||||
{
|
||||
unsigned char status;
|
||||
unsigned int backBits;
|
||||
|
||||
RC522_writeReg(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ???
|
||||
|
||||
TagType[0] = reqMode;
|
||||
status = RC522_toCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
|
||||
|
||||
if ((status != MI_OK) || (backBits != 0x10))
|
||||
{
|
||||
status = MI_ERR;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: ToCard
|
||||
* Description: communicate between RC522 and ISO14443
|
||||
* Input parameter: command--MF522 command bits
|
||||
* sendData--send data to card via rc522
|
||||
* sendLen--send data length
|
||||
* backData--the return data from card
|
||||
* backLen--the length of return data
|
||||
* Return: return MI_OK if successed
|
||||
*/
|
||||
unsigned char RC522_toCard(unsigned char command, unsigned char *sendData, unsigned char sendLen, unsigned char *backData, unsigned int *backLen)
|
||||
{
|
||||
unsigned char status = MI_ERR;
|
||||
unsigned char irqEn = 0x00;
|
||||
unsigned char waitIRq = 0x00;
|
||||
unsigned char lastBits;
|
||||
unsigned char n;
|
||||
unsigned int i;
|
||||
|
||||
switch (command)
|
||||
{
|
||||
case PCD_AUTHENT:
|
||||
{
|
||||
irqEn = 0x12;
|
||||
waitIRq = 0x10;
|
||||
break;
|
||||
}
|
||||
case PCD_TRANSCEIVE:
|
||||
{
|
||||
irqEn = 0x77;
|
||||
waitIRq = 0x30;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
RC522_writeReg(CommIEnReg, irqEn|0x80);
|
||||
RC522_clearBitMask(CommIrqReg, 0x80);
|
||||
RC522_setBitMask(FIFOLevelReg, 0x80);
|
||||
|
||||
RC522_writeReg(CommandReg, PCD_IDLE);
|
||||
|
||||
for (i=0; i<sendLen; i++)
|
||||
{
|
||||
RC522_writeReg(FIFODataReg, sendData[i]);
|
||||
}
|
||||
|
||||
RC522_writeReg(CommandReg, command);
|
||||
if (command == PCD_TRANSCEIVE)
|
||||
{
|
||||
RC522_setBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts
|
||||
}
|
||||
|
||||
i = 2000;
|
||||
do
|
||||
{
|
||||
n = RC522_readReg(CommIrqReg);
|
||||
i--;
|
||||
}
|
||||
while ((i!=0) && !(n&0x01) && !(n&waitIRq));
|
||||
|
||||
RC522_clearBitMask(BitFramingReg, 0x80); //StartSend=0
|
||||
|
||||
if (i != 0)
|
||||
{
|
||||
if(!(RC522_readReg(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
|
||||
{
|
||||
status = MI_OK;
|
||||
if (n & irqEn & 0x01)
|
||||
{
|
||||
status = MI_NOTAGERR; //??
|
||||
}
|
||||
|
||||
if (command == PCD_TRANSCEIVE)
|
||||
{
|
||||
n = RC522_readReg(FIFOLevelReg);
|
||||
lastBits = RC522_readReg(ControlReg) & 0x07;
|
||||
if (lastBits)
|
||||
{
|
||||
*backLen = (n-1)*8 + lastBits;
|
||||
}
|
||||
else
|
||||
{
|
||||
*backLen = n*8;
|
||||
}
|
||||
if (n == 0)
|
||||
{
|
||||
n = 1;
|
||||
}
|
||||
if (n > MAX_LEN)
|
||||
{
|
||||
n = MAX_LEN;
|
||||
}
|
||||
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
backData[i] = RC522_readReg(FIFODataReg);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
status = MI_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
//RC522_setBitMask(ControlReg,0x80); //timer stops
|
||||
//RC522_writeReg(CommandReg, PCD_IDLE);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Function: MFRC522_Anticoll
|
||||
* Description: Prevent conflict, read the card serial number
|
||||
* Input parameter: serNum--return the 4 bytes card serial number, the 5th byte is recheck byte
|
||||
* Return: return MI_OK if successed
|
||||
*/
|
||||
unsigned char RC522_anticoll(unsigned char *serNum)
|
||||
{
|
||||
unsigned char status;
|
||||
unsigned char i;
|
||||
unsigned char serNumCheck=0;
|
||||
unsigned int unLen;
|
||||
|
||||
|
||||
//RC522_clearBitMask(Status2Reg, 0x08); //TempSensclear
|
||||
//RC522_clearBitMask(CollReg,0x80); //ValuesAfterColl
|
||||
RC522_writeReg(BitFramingReg, 0x00); //TxLastBists = BitFramingReg[2..0]
|
||||
serNum[0] = PICC_ANTICOLL;
|
||||
serNum[1] = 0x20;
|
||||
status = RC522_toCard(PCD_TRANSCEIVE, serNum, 2, serNum, &unLen);
|
||||
|
||||
if (status == MI_OK)
|
||||
{
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
serNumCheck ^= serNum[i];
|
||||
}
|
||||
if (serNumCheck != serNum[i])
|
||||
{
|
||||
status = MI_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
//RC522_setBitMask(CollReg, 0x80); //ValuesAfterColl=1
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Function: CalulateCRC
|
||||
* Description: Use MF522 to calculate CRC
|
||||
* Input parameter: pIndata--the CRC data need to be read,len--data length,pOutData-- the caculated result of CRC
|
||||
* Return: Null
|
||||
*/
|
||||
void RC522_calculateCRC(unsigned char *pIndata, unsigned char len, unsigned char *pOutData)
|
||||
{
|
||||
unsigned char i, n;
|
||||
|
||||
RC522_clearBitMask(DivIrqReg, 0x04); //CRCIrq = 0
|
||||
RC522_setBitMask(FIFOLevelReg, 0x80);
|
||||
//RC522_writeReg(CommandReg, PCD_IDLE);
|
||||
|
||||
for (i=0; i<len; i++)
|
||||
{
|
||||
RC522_writeReg(FIFODataReg, *(pIndata+i));
|
||||
}
|
||||
RC522_writeReg(CommandReg, PCD_CALCCRC);
|
||||
|
||||
i = 0xFF;
|
||||
do
|
||||
{
|
||||
n = RC522_readReg(DivIrqReg);
|
||||
i--;
|
||||
}
|
||||
while ((i!=0) && !(n&0x04)); //CRCIrq = 1
|
||||
|
||||
pOutData[0] = RC522_readReg(CRCResultRegL);
|
||||
pOutData[1] = RC522_readReg(CRCResultRegM);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Function: SelectTag
|
||||
* Description: Select card, read card storage volume
|
||||
* Input parameter: serNum--Send card serial number
|
||||
* Return: return the card storage volume
|
||||
*/
|
||||
unsigned char RC522_selectTag(unsigned char *serNum)
|
||||
{
|
||||
unsigned char i;
|
||||
unsigned char status;
|
||||
unsigned char size;
|
||||
unsigned int recvBits;
|
||||
unsigned char buffer[9];
|
||||
|
||||
//RC522_clearBitMask(Status2Reg, 0x08); //MFCrypto1On=0
|
||||
|
||||
buffer[0] = PICC_SElECTTAG;
|
||||
buffer[1] = 0x70;
|
||||
for (i=0; i<5; i++)
|
||||
{
|
||||
buffer[i+2] = *(serNum+i);
|
||||
}
|
||||
RC522_calculateCRC(buffer, 7, &buffer[7]); //??
|
||||
status = RC522_toCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits);
|
||||
|
||||
if ((status == MI_OK) && (recvBits == 0x18))
|
||||
{
|
||||
size = buffer[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
size = 0;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Function: Auth
|
||||
* Description: verify card password
|
||||
* Input parameters:authMode--password verify mode
|
||||
0x60 = verify A password key
|
||||
0x61 = verify B password key
|
||||
BlockAddr--Block address
|
||||
Sectorkey--Block password
|
||||
serNum--Card serial number ,4 bytes
|
||||
* Return: return MI_OK if successed
|
||||
*/
|
||||
unsigned char RC522_auth(unsigned char authMode, unsigned char BlockAddr, unsigned char *Sectorkey, unsigned char *serNum)
|
||||
{
|
||||
unsigned char status;
|
||||
unsigned int recvBits;
|
||||
unsigned char i;
|
||||
unsigned char buff[12];
|
||||
|
||||
buff[0] = authMode;
|
||||
buff[1] = BlockAddr;
|
||||
for (i=0; i<6; i++)
|
||||
{
|
||||
buff[i+2] = *(Sectorkey+i);
|
||||
}
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
buff[i+8] = *(serNum+i);
|
||||
}
|
||||
status = RC522_toCard(PCD_AUTHENT, buff, 12, buff, &recvBits);
|
||||
|
||||
if ((status != MI_OK) || (!(RC522_readReg(Status2Reg) & 0x08)))
|
||||
{
|
||||
status = MI_ERR;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to exit card from authenticated state.
|
||||
* Remember to call this function after communicating with an authenticated card, otherwise no new communications can start.
|
||||
*/
|
||||
void RC522_stopCrypto() {
|
||||
// Clear MFCrypto1On bit
|
||||
RC522_clearBitMask(Status2Reg, 0x08); // Status2Reg[7..0] bits are: TempSensClear I2CForceHS reserved reserved MFCrypto1On ModemState[2:0]
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: ReadBlock
|
||||
* Description: Read data
|
||||
* Input parameters:blockAddr--block address;recvData--the block data which are read
|
||||
* Return: return MI_OK if successed
|
||||
*/
|
||||
unsigned char RC522_readBlock(unsigned char blockAddr, unsigned char *recvData)
|
||||
{
|
||||
unsigned char status;
|
||||
unsigned int unLen;
|
||||
|
||||
recvData[0] = PICC_READ;
|
||||
recvData[1] = blockAddr;
|
||||
RC522_calculateCRC(recvData,2, &recvData[2]);
|
||||
status = RC522_toCard(PCD_TRANSCEIVE, recvData, 4, recvData, &unLen);
|
||||
|
||||
if ((status != MI_OK) || (unLen != 0x90))
|
||||
{
|
||||
status = MI_ERR;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Function: WriteBlock
|
||||
* Description: write block data
|
||||
* Input parameters:blockAddr--block address;writeData--Write 16 bytes data into block
|
||||
* Return: return MI_OK if successed
|
||||
*/
|
||||
unsigned char RC522_writeBlock(unsigned char blockAddr, unsigned char *writeData)
|
||||
{
|
||||
unsigned char status;
|
||||
unsigned int recvBits;
|
||||
unsigned char i;
|
||||
unsigned char buff[18];
|
||||
|
||||
buff[0] = PICC_WRITE;
|
||||
buff[1] = blockAddr;
|
||||
RC522_calculateCRC(buff, 2, &buff[2]);
|
||||
status = RC522_toCard(PCD_TRANSCEIVE, buff, 4, buff, &recvBits);
|
||||
|
||||
if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A))
|
||||
{
|
||||
status = MI_ERR;
|
||||
}
|
||||
|
||||
if (status == MI_OK)
|
||||
{
|
||||
for (i=0; i<16; i++)
|
||||
{
|
||||
buff[i] = *(writeData+i);
|
||||
}
|
||||
RC522_calculateCRC(buff, 16, &buff[16]);
|
||||
status = RC522_toCard(PCD_TRANSCEIVE, buff, 18, buff, &recvBits);
|
||||
|
||||
if ((status != MI_OK) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A))
|
||||
{
|
||||
status = MI_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Function: Halt
|
||||
* Description: Command the cards into sleep mode
|
||||
* Input parameters: null
|
||||
* Return: status
|
||||
*/
|
||||
unsigned char RC522_halt(void)
|
||||
{
|
||||
unsigned char status;
|
||||
unsigned int unLen;
|
||||
unsigned char buff[4];
|
||||
|
||||
buff[0] = PICC_HALT;
|
||||
buff[1] = 0;
|
||||
RC522_calculateCRC(buff, 2, &buff[2]);
|
||||
|
||||
status = RC522_toCard(PCD_TRANSCEIVE, buff, 4, buff,&unLen);
|
||||
|
||||
return status;
|
||||
}
|
||||
145
Mfrc522.h
145
Mfrc522.h
@ -1,145 +0,0 @@
|
||||
/*
|
||||
File: Mfrc522.h
|
||||
Mfrc522 - Library for communicating with MFRC522 based NFC Reader/Writers
|
||||
Created by Eelco Rouw - Mainly based on code from Grant Gibson (www.grantgibson.co.uk) and Dr.Leong ( WWW.B2CQSHOP.COM )
|
||||
Lightly modified by Frank Milburn
|
||||
Lightly modified by Roy Bakker C++ --> C
|
||||
Released into the public domain
|
||||
*/
|
||||
#ifndef Mfrc522_h
|
||||
#define Mfrc522_h
|
||||
|
||||
/*
|
||||
MFRC522 and Card specific defines. For the role of each define, please consult the datasheet.
|
||||
*/
|
||||
|
||||
#define MAX_LEN 16
|
||||
|
||||
//MF522 - Commands
|
||||
#define PCD_IDLE 0x00
|
||||
#define PCD_AUTHENT 0x0E
|
||||
#define PCD_RECEIVE 0x08
|
||||
#define PCD_TRANSMIT 0x04
|
||||
#define PCD_TRANSCEIVE 0x0C
|
||||
#define PCD_RESETPHASE 0x0F
|
||||
#define PCD_CALCCRC 0x03
|
||||
|
||||
//Mifare_One - Commands
|
||||
#define PICC_REQIDL 0x26
|
||||
#define PICC_REQALL 0x52
|
||||
#define PICC_ANTICOLL 0x93
|
||||
#define PICC_SElECTTAG 0x93
|
||||
#define PICC_AUTHENT1A 0x60
|
||||
#define PICC_AUTHENT1B 0x61
|
||||
#define PICC_READ 0x30
|
||||
#define PICC_WRITE 0xA0
|
||||
#define PICC_DECREMENT 0xC0
|
||||
#define PICC_INCREMENT 0xC1
|
||||
#define PICC_RESTORE 0xC2
|
||||
#define PICC_TRANSFER 0xB0
|
||||
#define PICC_HALT 0x50
|
||||
|
||||
//MF522 - Status
|
||||
#define MI_OK 0
|
||||
#define MI_NOTAGERR 1
|
||||
#define MI_ERR 2
|
||||
|
||||
//MF522 - Registers
|
||||
//Page 0:Command and Status
|
||||
#define Reserved00 0x00
|
||||
#define CommandReg 0x01
|
||||
#define CommIEnReg 0x02
|
||||
#define DivlEnReg 0x03
|
||||
#define CommIrqReg 0x04
|
||||
#define DivIrqReg 0x05
|
||||
#define ErrorReg 0x06
|
||||
#define Status1Reg 0x07
|
||||
#define Status2Reg 0x08
|
||||
#define FIFODataReg 0x09
|
||||
#define FIFOLevelReg 0x0A
|
||||
#define WaterLevelReg 0x0B
|
||||
#define ControlReg 0x0C
|
||||
#define BitFramingReg 0x0D
|
||||
#define CollReg 0x0E
|
||||
#define Reserved01 0x0F
|
||||
|
||||
//Page 1:Command
|
||||
#define Reserved10 0x10
|
||||
#define ModeReg 0x11
|
||||
#define TxModeReg 0x12
|
||||
#define RxModeReg 0x13
|
||||
#define TxControlReg 0x14
|
||||
#define TxAutoReg 0x15
|
||||
#define TxSelReg 0x16
|
||||
#define RxSelReg 0x17
|
||||
#define RxThresholdReg 0x18
|
||||
#define DemodReg 0x19
|
||||
#define Reserved11 0x1A
|
||||
#define Reserved12 0x1B
|
||||
#define MifareReg 0x1C
|
||||
#define Reserved13 0x1D
|
||||
#define Reserved14 0x1E
|
||||
#define SerialSpeedReg 0x1F
|
||||
|
||||
//Page 2:CFG
|
||||
#define Reserved20 0x20
|
||||
#define CRCResultRegM 0x21
|
||||
#define CRCResultRegL 0x22
|
||||
#define Reserved21 0x23
|
||||
#define ModWidthReg 0x24
|
||||
#define Reserved22 0x25
|
||||
#define RFCfgReg 0x26
|
||||
#define GsNReg 0x27
|
||||
#define CWGsPReg 0x28
|
||||
#define ModGsPReg 0x29
|
||||
#define TModeReg 0x2A
|
||||
#define TPrescalerReg 0x2B
|
||||
#define TReloadRegH 0x2C
|
||||
#define TReloadRegL 0x2D
|
||||
#define TCounterValueRegH 0x2E
|
||||
#define TCounterValueRegL 0x2F
|
||||
|
||||
//Page 3:TestRegister
|
||||
#define Reserved30 0x30
|
||||
#define TestSel1Reg 0x31
|
||||
#define TestSel2Reg 0x32
|
||||
#define TestPinEnReg 0x33
|
||||
#define TestPinValueReg 0x34
|
||||
#define TestBusReg 0x35
|
||||
#define AutoTestReg 0x36
|
||||
#define VersionReg 0x37
|
||||
#define AnalogTestReg 0x38
|
||||
#define TestDAC1Reg 0x39
|
||||
#define TestDAC2Reg 0x3A
|
||||
#define TestADCReg 0x3B
|
||||
#define Reserved31 0x3C
|
||||
#define Reserved32 0x3D
|
||||
#define Reserved33 0x3E
|
||||
#define Reserved34 0x3F
|
||||
|
||||
void RC522_init(int chipSelectPin, int NRSTPD);
|
||||
void RC522_writeReg(unsigned char addr, unsigned char val);
|
||||
unsigned char RC522_readReg(unsigned char addr);
|
||||
void RC522_setBitMask(unsigned char reg, unsigned char mask);
|
||||
void RC522_clearBitMask(unsigned char reg, unsigned char mask);
|
||||
void RC522_antennaOn(void);
|
||||
void RC522_antennaOff(void);
|
||||
void RC522_setAntennaGain(unsigned char mask);
|
||||
void RC522_reset(void);
|
||||
|
||||
unsigned char RC522_request(unsigned char reqMode, unsigned char *TagType);
|
||||
unsigned char RC522_toCard(unsigned char command, unsigned char *sendData, unsigned char sendLen, unsigned char *backData, unsigned int *backLen);
|
||||
unsigned char RC522_anticoll(unsigned char *serNum);
|
||||
void RC522_calculateCRC(unsigned char *pIndata, unsigned char len, unsigned char *pOutData);
|
||||
unsigned char RC522_selectTag(unsigned char *serNum);
|
||||
unsigned char RC522_auth(unsigned char authMode, unsigned char BlockAddr, unsigned char *Sectorkey, unsigned char *serNum);
|
||||
void RC522_stopCrypto();
|
||||
unsigned char RC522_readBlock(unsigned char blockAddr, unsigned char *recvData);
|
||||
unsigned char RC522_writeBlock(unsigned char blockAddr, unsigned char *writeData);
|
||||
unsigned char RC522_halt(void);
|
||||
|
||||
int _chipSelectPin;
|
||||
int _NRSTPD;
|
||||
|
||||
|
||||
#endif
|
||||
85
NFC.c
85
NFC.c
@ -1,85 +0,0 @@
|
||||
#include <msp430.h>
|
||||
#include "NFC.h"
|
||||
#include "Mfrc522.h"
|
||||
void NFCInit(){
|
||||
P2DIR |= BIT4 | BIT5 | BIT6 ;
|
||||
P2OUT &= ~ (BIT4 | BIT5 | BIT6 );
|
||||
RC522_init(BIT6, BIT7);
|
||||
/*
|
||||
* Setting antenna gain to maximum (7) can read cards up to approximately 5cm.
|
||||
* Default can read cards up to approximately 1cm.
|
||||
* */
|
||||
RC522_setAntennaGain(7); // TODO fine tuning
|
||||
//TODO LED
|
||||
}
|
||||
|
||||
|
||||
//NFCNummer return serienummer van de NFC-tag
|
||||
uchar NFCAvailable(){ //is er een kaart en is het leesbaar? serienummer
|
||||
uchar str[5], Colour, Direction;
|
||||
if(RC522_request(PICC_REQALL, str) == MI_OK){
|
||||
if(RC522_anticoll(str) == MI_OK){ // controleert of de kaart reageert
|
||||
RC522_selectTag(str);
|
||||
Colour = NFCRead (0 , 1 , 0 , str);
|
||||
Direction = NFCRead (2 , 0 , 9 , str);
|
||||
RGBLed_update (Colour);
|
||||
|
||||
switch (Direction){
|
||||
case 'S':
|
||||
return 0b00; // stop = 00
|
||||
case 'D':
|
||||
return 0b11; // vooruit = 11
|
||||
case 'L':
|
||||
return 0b10; // 10= Links
|
||||
case 'R':
|
||||
return 0b01; // 01 = rechts
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//uchar* NFCTestConnection (uchar* TagType); //communiceren met de eerst gedetecteerde kaart
|
||||
//uchar* NFCActive(); //NFC tag communicatie controleren
|
||||
|
||||
// controleren of we de kaart kunnen "authenticaten" en De vervallen NFCActive
|
||||
//uchar NFCAuthenticate (uchar* NFCNummer);
|
||||
//sector*4 + block formule of (sector << 2) | block
|
||||
uchar NFCRead (uchar sector, uchar block, uchar byte, uchar* str){
|
||||
/* See if we can authenticate to read */
|
||||
uchar recvData[18];
|
||||
block = (sector << 2) | block;
|
||||
uchar keyA[6] = {0xBA, 0xBE, 0xBA, 0xBE, 0xBA, 0xBE};
|
||||
if(RC522_auth(PICC_AUTHENT1A, block, keyA, str) == MI_OK){
|
||||
|
||||
/* See if we can actually read data from the card. (block 1) */
|
||||
if(RC522_readBlock(block, recvData) == MI_OK){
|
||||
return recvData[byte];
|
||||
/* Color data is in block 1 of sector 0 */
|
||||
}
|
||||
|
||||
/* Stop authentication */
|
||||
RC522_stopCrypto();
|
||||
}
|
||||
|
||||
} // controle of de data kan worden gelezen
|
||||
|
||||
//stuur richting door pls
|
||||
|
||||
// byte van de NFCRead doorsturen naar RGBLed_update
|
||||
uchar RGBLed_update (uchar Colour){
|
||||
P2OUT &= ~ (BIT4 | BIT5 | BIT6 );
|
||||
switch (Colour){
|
||||
case 'R':
|
||||
P2OUT |= BIT4 ;
|
||||
break;
|
||||
case 'G':
|
||||
P2OUT |= BIT5 ;
|
||||
break;
|
||||
case 'B':
|
||||
P2OUT |= BIT6 ;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
17
NFC.h
17
NFC.h
@ -1,17 +0,0 @@
|
||||
/*
|
||||
* NFC.c
|
||||
*
|
||||
* Created on: 22 mei 2020
|
||||
* Author: Yeti
|
||||
*/
|
||||
|
||||
#ifndef NFC_C_
|
||||
#define NFC_C_
|
||||
|
||||
#include "typedefExtention.h"
|
||||
|
||||
void NFCInit();
|
||||
uchar NFCAvailable();
|
||||
|
||||
|
||||
#endif
|
||||
36
main.c
36
main.c
@ -1,9 +1,7 @@
|
||||
#include <msp430.h>
|
||||
//#include <math.h>
|
||||
#include "SPI.h"
|
||||
#include "motorDriver.h"
|
||||
#include "motionController.h"
|
||||
#include "NFC.h"
|
||||
//#include "motionController.h"
|
||||
|
||||
/* pinout
|
||||
*
|
||||
@ -34,10 +32,10 @@
|
||||
*/
|
||||
#define TEST 0
|
||||
|
||||
const uchar STEP_PINS = BIT4 | BIT5; // PORT 2
|
||||
const uchar DIRr_PIN = BIT6; // PORT 1
|
||||
const uchar DIRl_PIN = BIT7; // PORT 1
|
||||
typedef enum Direction {Forward=0, Backward=(DIRl_PIN | DIRr_PIN), RightTurn=DIRr_PIN, LeftTurn=DIRl_PIN};
|
||||
#define STEP_PINS BIT4 | BIT5 // PORT 2
|
||||
#define DIRr_PIN BIT6 // PORT 1
|
||||
#define DIRl_PIN BIT7 // PORT 1
|
||||
enum Direction {Forward=0, Backward=(DIRl_PIN | DIRr_PIN), RightTurn=DIRr_PIN, LeftTurn=DIRl_PIN};
|
||||
|
||||
const uchar v = 36;
|
||||
const ulong halfRound = 2800;
|
||||
@ -76,7 +74,6 @@ int main(void)
|
||||
int i, ii;
|
||||
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
|
||||
|
||||
|
||||
// set cpu op 16MHz
|
||||
BCSCTL1 = CALBC1_16MHZ;
|
||||
DCOCTL = CALDCO_16MHZ;
|
||||
@ -91,15 +88,6 @@ int main(void)
|
||||
|
||||
__delay_cycles(16000000);
|
||||
|
||||
#if TEST == 1
|
||||
|
||||
while(1){
|
||||
NFCAvailable();
|
||||
}
|
||||
|
||||
#endif
|
||||
#if TEST == 0
|
||||
|
||||
for(ii=2; ii>0; ii--){
|
||||
dir(Forward);
|
||||
stap(10000, v);
|
||||
@ -133,19 +121,5 @@ int main(void)
|
||||
stap(halfRound, v*2);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if TEST == 2
|
||||
|
||||
while(1){
|
||||
MCSetV(MC_MotorL, 1000);
|
||||
MCSetV(MC_MotorR, 1000);
|
||||
__delay_cycles(32000000);
|
||||
MCSetV(MC_MotorL, -1000);
|
||||
MCSetV(MC_MotorR, -1000);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#ifndef MOTIONCONTROLLER_H_
|
||||
#define MOTIONCONTROLLER_H_
|
||||
|
||||
typedef enum MC_motor {MC_MotorR=0x00, MC_MotorL=0x20};
|
||||
enum MC_motor {MC_MotorR=0x00, MC_MotorL=0x20};
|
||||
|
||||
void MCInit();
|
||||
void MCSetV(enum MC_motor m, int v);
|
||||
|
||||
@ -60,22 +60,6 @@ const uchar MD_CS = BIT3;
|
||||
#define MD_CC_hstrt 4 // 3 bits
|
||||
#define MD_CC_toff 0 // 4 bits
|
||||
|
||||
enum bool MDStatus(){
|
||||
ulong stat = MD_read(MD_GSTAT);
|
||||
if(stat != 0){
|
||||
if(stat == 0x1){ // only a recet has occert
|
||||
// restart the motor driver
|
||||
MDInit();
|
||||
}else{ // one of the motors stopt due to short or overheated or a undervoltage in chargepump
|
||||
// wait a while for checking again
|
||||
__delay_cycles(16000000);
|
||||
MDStatus();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void MD_write(uchar addr, ulong data) {
|
||||
char i;
|
||||
|
||||
@ -124,6 +108,22 @@ ulong MD_read(uchar addr) {
|
||||
return data;
|
||||
}
|
||||
|
||||
enum bool MDStatus(){
|
||||
ulong stat = MD_read(MD_GSTAT);
|
||||
if(stat != 0){
|
||||
if(stat == 0x1){ // only a recet has occert
|
||||
// restart the motor driver
|
||||
MDInit();
|
||||
}else{ // one of the motors stopt due to short or overheated or a undervoltage in chargepump
|
||||
// wait a while for checking again
|
||||
__delay_cycles(16000000);
|
||||
MDStatus();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void MDInit(){
|
||||
P1DIR |= MD_CS; // set MD_CS (pin 3) as output
|
||||
P1OUT |= MD_CS; // set MD_CS high
|
||||
|
||||
@ -13,7 +13,7 @@ typedef unsigned char uchar;
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned long ulong;
|
||||
|
||||
typedef enum bool {false=0, true=1};
|
||||
enum bool {false=0, true=1};
|
||||
|
||||
#define BIT00 1ul << 0
|
||||
#define BIT01 1ul << 1
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user