alt rfid driver

This commit is contained in:
2021-10-27 22:07:48 +02:00
parent 2022eec0f1
commit f70b64b793
6 changed files with 113 additions and 76 deletions

View File

@@ -2,19 +2,22 @@
#include "SPI.h"
// Write command to the CR95HF
void CR95HF::writeCmd(unsigned short cmd, unsigned short dataLen) {
void CR95HF::writeCmd(unsigned short cmd, unsigned short dataLen)
{
unsigned short i = 0;
digitalWrite(CS, LOW);
SPI.beginTransaction(SETTINGS);
SPI.transfer(0x00); // Send cmd to CR95HF
SPI.transfer(0x00); // Send cmd to CR95HF
SPI.transfer(cmd);
SPI.transfer(dataLen);
while (dataLen == 0) {
while (dataLen == 0)
{
digitalWrite(CS, HIGH);
break;
}
for (i = 0; i < dataLen; i++) {
for (i = 0; i < dataLen; i++)
{
SPI.transfer(sdata[i]);
}
SPI.endTransaction();
@@ -22,10 +25,12 @@ void CR95HF::writeCmd(unsigned short cmd, unsigned short dataLen) {
}
// Poll the CR95HF
void CR95HF::readCmd() {
void CR95HF::readCmd()
{
unsigned short i = 0;
while (1) {
while (1)
{
digitalWrite(CS, LOW);
SPI.beginTransaction(SETTINGS);
SPI.transfer(0x03);
@@ -33,7 +38,8 @@ void CR95HF::readCmd() {
SPI.endTransaction();
digitalWrite(CS, HIGH);
if ((res & 0x08) >> 3) {
if ((res & 0x08) >> 3)
{
digitalWrite(CS, LOW);
SPI.beginTransaction(SETTINGS);
SPI.transfer(0x02);
@@ -52,7 +58,8 @@ void CR95HF::readCmd() {
}
// Initialize MCU and peripherals
void CR95HF::begin() {
void CR95HF::begin()
{
// Configure GPIO pins
// if (SSI_0 != -1)
// pinMode(SSI_0, OUTPUT);
@@ -80,10 +87,11 @@ void CR95HF::begin() {
delay(1);
Serial.println("RFID init: init SPI");
SPI.begin(SCK,MISO, MOSI);
SPI.begin(SCK, MISO, MOSI);
Serial.print("RFID init: Wait for response");
while (!EchoResponse()) { // Until CR95HF is detected
while (!EchoResponse())
{ // Until CR95HF is detected
if (IRQ_IN != -1)
digitalWrite(IRQ_IN, HIGH);
delay(10);
@@ -93,7 +101,7 @@ void CR95HF::begin() {
Serial.print(".");
}
Serial.println("ok");
Serial.printf("RFID init: Serial=%s\n", readSerial());
Calibration();
IndexMod_Gain();
@@ -102,40 +110,49 @@ void CR95HF::begin() {
}
// Get Echo reponse from CR95HF
char CR95HF::EchoResponse() {
char CR95HF::EchoResponse()
{
digitalWrite(CS, LOW);
SPI.beginTransaction(SETTINGS);
SPI.transfer(0x00); // Send cmd to CR95HF
SPI.transfer(0x00); // Send cmd to CR95HF
SPI.transfer(ECHO);
SPI.endTransaction();
digitalWrite(CS, HIGH);
Serial.println("send echo");
while (1) {
while (1)
{
digitalWrite(CS, LOW);
SPI.beginTransaction(SETTINGS);
SPI.transfer(0x03);
tmp = SPI.transfer(0);
SPI.endTransaction();
digitalWrite(CS, HIGH);
Serial.printf("rfid: received %x\n",tmp);
if ((tmp & 0x08) >> 3) {
if ((tmp & 0x08) >> 3)
{
digitalWrite(CS, LOW);
SPI.beginTransaction(SETTINGS);
SPI.transfer(0x02);
tmp = SPI.transfer(0);
SPI.endTransaction();
digitalWrite(CS, HIGH);
if (tmp == ECHO) {
if (tmp == ECHO)
{
Serial.println("receive echo");
return 1;
}
Serial.printf("no echo, received %x\n",tmp);
return 0;
}
}
}
// Calibrate CR95HF device
void CR95HF::Calibration() {
void CR95HF::Calibration()
{
sdata[0] = 0x03;
sdata[1] = 0xA1;
sdata[2] = 0x00;
@@ -274,45 +291,52 @@ void CR95HF::Calibration() {
}
// Get CR95HF chip ID
char* CR95HF::readSerial() {
char *CR95HF::readSerial()
{
writeCmd(IDN, 0);
readCmd();
for (j = 0; j < dataNum; j++) {
for (j = 0; j < dataNum; j++)
{
CR95HF_ID[j] = rdata[j];
}
return CR95HF_ID;
}
// Select the RF communication protocol (ISO/IEC 14443-A)
void CR95HF::Select_ISO_IEC_14443_A_Protocol() {
void CR95HF::Select_ISO_IEC_14443_A_Protocol()
{
sdata[0] = 0x02;
sdata[1] = 0x00;
writeCmd(ProtocolSelect, 2);
readCmd();
// Clear read and write buffers
for (j = 0; j < 18; j++ ) {
for (j = 0; j < 18; j++)
{
rdata[j] = 0;
sdata[j] = 0;
}
}
// Select the RF communication protocol (ISO/IEC 18092)
void CR95HF::Select_ISO_IEC_18092_Protocol() {
void CR95HF::Select_ISO_IEC_18092_Protocol()
{
sdata[0] = 0x04;
sdata[1] = 0x51;
writeCmd(ProtocolSelect, 2);
readCmd();
// Clear read and write buffers
for (j = 0; j < 18; j++ ) {
for (j = 0; j < 18; j++)
{
rdata[j] = 0;
sdata[j] = 0;
}
}
// Configure IndexMod & Gain
void CR95HF::IndexMod_Gain() {
void CR95HF::IndexMod_Gain()
{
sdata[0] = 0x09;
sdata[1] = 0x04;
sdata[2] = 0x68;
@@ -324,7 +348,8 @@ void CR95HF::IndexMod_Gain() {
}
// Configure Auto FDet
void CR95HF::AutoFDet() {
void CR95HF::AutoFDet()
{
sdata[0] = 0x09;
sdata[1] = 0x04;
sdata[2] = 0x0A;
@@ -336,32 +361,37 @@ void CR95HF::AutoFDet() {
}
// Read the tag ID
void CR95HF::GetTagID() {
void CR95HF::GetTagID()
{
sdata[0] = 0x26;
sdata[1] = 0x07;
writeCmd(SendRecv , 2);
writeCmd(SendRecv, 2);
readCmd();
sdata[0] = 0x93;
sdata[1] = 0x20;
sdata[2] = 0x08;
writeCmd(SendRecv , 3);
writeCmd(SendRecv, 3);
readCmd();
if (res == 0x80) {
for (j = 1; j < dataNum - 3; j++) {
if (res == 0x80)
{
for (j = 1; j < dataNum - 3; j++)
{
ID += String(rdata[j], HEX);
}
TAG_flag = 1;
}
else {
else
{
TAG_flag = 0;
Select_ISO_IEC_18092_Protocol();
}
}
// Read the NFC Forum tags
void CR95HF::GetNFCTag() {
void CR95HF::GetNFCTag()
{
sdata[0] = 0x00;
sdata[1] = 0xFF;
sdata[2] = 0xFF;
@@ -370,39 +400,46 @@ void CR95HF::GetNFCTag() {
writeCmd(SendRecv, 5);
readCmd();
if (res == 0x80) {
for (j = 0; j < dataNum; j++) {
if (res == 0x80)
{
for (j = 0; j < dataNum; j++)
{
ID += String(rdata[j], HEX);
}
NFC_flag = 1;
}
else {
else
{
NFC_flag = 0;
Select_ISO_IEC_14443_A_Protocol();
}
}
String CR95HF::getID() {
String CR95HF::getID()
{
String id = "";
if (!TAG_flag)
GetNFCTag(); // Get NFC ID
GetNFCTag(); // Get NFC ID
if (!NFC_flag)
GetTagID(); // Get Tag ID
GetTagID(); // Get Tag ID
if (ID.c_str()[0] == 0) { // If there is no tag present
flag++; // Increment counter flag
if (ID.c_str()[0] == 0)
{ // If there is no tag present
flag++; // Increment counter flag
}
else { // If tag is present
flag = 0; // Reset counter flag
id = ID; // Set current ID as previous ID
else
{ // If tag is present
flag = 0; // Reset counter flag
id = ID; // Set current ID as previous ID
}
ID = ""; // Terminate the ID string
ID = ""; // Terminate the ID string
// Clear read and write buffers
for (j = 0; j < 18; j++) {
for (j = 0; j < 18; j++)
{
rdata[j] = 0;
sdata[j] = 0;
}

View File

@@ -11,7 +11,7 @@
#define BaudRate 0x0A
#define ECHO 0x55
class CR95HF
class CR95HF
{
public:

View File

@@ -79,22 +79,22 @@ void initAudio()
void handleAudio()
{
if (hallIsIdle())
{
if (mp3->isRunning())
{
Serial.println("Audio: stop playback");
mp3->stop();
audio_start = false;
}
}
else
{
if(!audio_start)
{
playSong(audio_current_Song);
audio_start = true;
}
// if (hallIsIdle())
// {
// if (mp3->isRunning())
// {
// Serial.println("Audio: stop playback");
// mp3->stop();
// audio_start = false;
// }
// }
// else
// {
// if(!audio_start)
// {
// playSong(audio_current_Song);
// audio_start = true;
// }
if (mp3->isRunning())
{
@@ -104,5 +104,5 @@ void handleAudio()
playSong(audio_current_Song);
}
}
}
//}
}

View File

@@ -1,17 +1,17 @@
#pragma once
#define I2S_BCLK 21
#define I2S_WCLK 13
#define I2S_DATA 14
#define DAC_SDMODE 27
#define I2S_BCLK 19
#define I2S_WCLK 21
#define I2S_DATA 18
#define DAC_SDMODE 23
#define NFC_SS 25
#define NFC_SCK 18
#define NFC_MOSI 23
#define NFC_MISO 19
#define NFC_RST 22 //not connectedx
#define NFC_IRQ 26
#define NFC_SS 27
#define NFC_SCK 25
#define NFC_MOSI 26
#define NFC_MISO 14
//#define NFC_RST 22 //not connectedx
#define NFC_IRQ 13
#define PWR_HOLD 4
#define PWR_BTN 9

View File

@@ -13,7 +13,7 @@
void setup()
{
initPower();
//initPower();
Serial.begin(115200);
delay(2000);
@@ -34,6 +34,6 @@ void loop()
handleAudio();
handleRfid();
//handleGame();
handlePower();
//handlePower();
//handleOta();
}