Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux port #216

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MFRC522.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,6 @@ MFRC522::StatusCode MFRC522::PCD_NTAG216_AUTH(byte* passWord, byte pACK[]) //Aut

// Transceive the data, store the reply in cmdBuffer[]
byte waitIRq = 0x30; // RxIRq and IdleIRq
byte cmdBufferSize = sizeof(cmdBuffer);
byte validBits = 0;
byte rxlength = 5;
result = PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, cmdBuffer, 7, cmdBuffer, &rxlength, &validBits);
Expand Down
21 changes: 20 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Makefile for MFRC522 library
#

all: package
CXXFLAGS=-g -Wall -std=gnu++11
CPPFLAGS=-Ilinux -I.
LDFLAGS=-lstdc++

BROKEN=examples/MinimalInterrupt/MinimalInterrupt.ino examples/servo_motor/servo_motor.ino

SOURCES=$(filter-out $(BROKEN), $(wildcard examples/*/*.ino))

all: package examples

help:
@echo "Please use \`make <target>' where <target> is one of"
Expand All @@ -15,3 +23,14 @@ clean:

package:
zip -o ./MFRC522.zip ./MFRC522.h ./MFRC522.cpp

$(patsubst %.ino,%.o,$(SOURCES)) MFRC522.o linux/lib.o:linux/Arduino.h linux/SPI.h

%.o: %.ino
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -x c++ $< -o $@

$(patsubst %.ino,%,$(SOURCES)): linux/lib.o MFRC522.o


examples: $(patsubst %.ino,%,$(SOURCES))

40 changes: 21 additions & 19 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ Pin Layout

The following table shows the typical pin layout used:

+-----------+----------+---------------------------------------------------------------+--------------------------+
| | PCD | Arduino | Teensy |
| +----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+
| | MFRC522 | Uno | Mega | Nano v3 |Leonardo / Micro | Pro Micro | 2.0 | ++ 2.0 | 3.1 |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+
| Signal | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin |
+===========+==========+=============+=========+=========+=================+===========+========+========+========+
| RST/Reset | RST | 9 [1]_ | 5 [1]_ | D9 | RESET / ICSP-5 | RST | 7 | 4 | 9 |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+
| SPI SS | SDA [3]_ | 10 [2]_ | 53 [2]_ | D10 | 10 | 10 | 0 | 20 | 10 |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+
| SPI MOSI | MOSI | 11 / ICSP-4 | 51 | D11 | ICSP-4 | 16 | 2 | 22 | 11 |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+
| SPI MISO | MISO | 12 / ICSP-1 | 50 | D12 | ICSP-1 | 14 | 3 | 23 | 12 |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+
| SPI SCK | SCK | 13 / ICSP-3 | 52 | D13 | ICSP-3 | 15 | 1 | 21 | 13 |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+
+-----------+----------+---------------------------------------------------------------+--------------------------+-----------------------+
| | PCD | Arduino | Teensy | UEXT (OlinuXino A10s) |
| +----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+-----------------------+
| | MFRC522 | Uno | Mega | Nano v3 |Leonardo / Micro | Pro Micro | 2.0 | ++ 2.0 | 3.1 | UEXT | A10s | Linux |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+------+------+---------+
| Signal | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | Pin | GPIO # |
+===========+==========+=============+=========+=========+=================+===========+========+========+========+======+======+=========+
| RST/Reset | RST | 9 [1]_ | 5 [1]_ | D9 | RESET / ICSP-5 | RST | 7 | 4 | 9 | 6 | PB18 | 50 |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+------+------+---------+
| SPI SS | SDA [3]_ | 10 [2]_ | 53 [2]_ | D10 | 10 | 10 | 0 | 20 | 10 | 10 | PB11 | 43 |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+------+------+---------+
| SPI MOSI | MOSI | 11 / ICSP-4 | 51 | D11 | ICSP-4 | 16 | 2 | 22 | 11 | 8 | | |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+------+------+---------+
| SPI MISO | MISO | 12 / ICSP-1 | 50 | D12 | ICSP-1 | 14 | 3 | 23 | 12 | 7 | | |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+------+------+---------+
| SPI SCK | SCK | 13 / ICSP-3 | 52 | D13 | ICSP-3 | 15 | 1 | 21 | 13 | 9 | | |
+-----------+----------+-------------+---------+---------+-----------------+-----------+--------+--------+--------+------+------+---------+

.. [1] Configurable, typically defined as RST_PIN in sketch/program.
.. [2] Configurable, typically defined as SS_PIN in sketch/program.
Expand All @@ -55,14 +55,16 @@ Hardware

There are three hardware components involved:

1. **Micro Controller**:
1. **Micro Controller or Linux board with SPI**:

* An `Arduino`_ or compatible executing the Sketch using this library.

* Prices vary from USD 7 for clones, to USD 75 for "starter kits" (which
* Prices vary from USD 7 for clones, to USD 75 for "starter kits" (which
might be a good choice if this is your first exposure to Arduino;
check if such kit already includes the Arduino, Reader, and some Tags).

* A Linux devboard - prices vary from around $10

2. **Proximity Coupling Device (PCD)**:

* The PCD is the actual RFID **Reader** based on `NXP MFRC522`_ Contactless
Expand Down
3 changes: 3 additions & 0 deletions examples/MifareClassicValueBlock/MifareClassicValueBlock.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

MFRC522::MIFARE_Key key;

void formatValueBlock(byte blockAddr);
void dump_byte_array(byte *buffer, byte bufferSize);

/**
* Initialize.
*/
Expand Down
5 changes: 1 addition & 4 deletions examples/Ntag216_AUTH/Ntag216_AUTH.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,10 @@ void loop() {
Serial.print(pACK[0], HEX);
Serial.println(pACK[1], HEX);

byte WBuff[] = {0x00, 0x00, 0x00, 0x04};
byte RBuff[18];

//Serial.print("CHG BLK: ");
//Serial.println(mfrc522.MIFARE_Ultralight_Write(0xE3, WBuff, 4)); //How to write to a page

mfrc522.PICC_DumpMifareUltralightToSerial(); //This is a modifier dunp just cghange the for cicle to < 232 instead of < 16 in order to see all the pages on NTAG216

delay(3000);
}
}
6 changes: 5 additions & 1 deletion examples/RFID-Cloner/RFID-Cloner.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void setup() {
}
}


void start();

//Via seriele monitor de bytes uitlezen in hexadecimaal

Expand Down Expand Up @@ -154,6 +154,10 @@ void loop() {

}

void keuze1();
void keuze2();
void keuze3();

void start(){
choice = Serial.read();

Expand Down
2 changes: 2 additions & 0 deletions examples/ReadAndWrite/ReadAndWrite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

MFRC522::MIFARE_Key key;

void dump_byte_array(byte *buffer, byte bufferSize);

/**
* Initialize.
*/
Expand Down
3 changes: 3 additions & 0 deletions examples/ReadNUID/ReadNUID.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[3];

void printHex(byte *buffer, byte bufferSize);
void printDec(byte *buffer, byte bufferSize);

void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
Expand Down
2 changes: 2 additions & 0 deletions examples/ReadUidMultiReader/ReadUidMultiReader.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ byte ssPins[] = {SS_1_PIN, SS_2_PIN};

MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.

void dump_byte_array(byte *buffer, byte bufferSize);

/**
* Initialize.
*/
Expand Down
59 changes: 59 additions & 0 deletions linux/Arduino.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef ARDUINO_H
#define ARDUINO_H

#include <string.h>
#include <unistd.h>
#include <inttypes.h>

#define LOW 0
#define HIGH 1
#define OUTPUT 0
#define INPUT 2

#define HEX 16
#define OCT 8
#define DEC 10

#define byte uint8_t
#define word uint32_t
#define boolean bool

#define delay(ms) usleep((ms)*1000)

#define F(x) (x)
#define PROGMEM
#define pgm_read_byte(x) (*(x))
#define __FlashStringHelper char

class Serial {
static int printc(unsigned char c, int radix);

public:
static inline void setTimeout(long){ /*FIXME*/}
static void begin(int speed);
static void print(unsigned char c, int radix = DEC);
static void print(const char * str);
static void println(unsigned char c, int radix = DEC);
static void println(const char * str = "");
static int readBytesUntil(char stop, char * buffer, int length);
static byte read();
static void write(byte);
};
inline bool operator !(class Serial arg) { return 0; }

extern Serial Serial;
static inline void printData(const char * data, int length, int base)
{
for(int i=0; i<length; i++){
if(i) Serial.print(" ");
if(data[i]<base) Serial.print("0");
Serial.print(data[i],base);
}
Serial.println();
}

void digitalWrite(int gpio, int state);
int digitalRead(int gpio);
void pinMode(int gpio, int mode);

#endif /*ARDUINO_H*/
40 changes: 40 additions & 0 deletions linux/SPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef SPI_H
#define SPI_H

#include <Arduino.h>

#include <linux/spi/spidev.h>

#define SPI_CLOCK_DIV4 1000000 /*some arbitrary value*/
#define MSBFIRST 0
#define LSBFIRST SPI_LSB_FIRST
#define SPI_MODE0 SPI_MODE_0
#define SPI_MODE1 SPI_MODE_1
#define SPI_MODE2 SPI_MODE_2
#define SPI_MODE3 SPI_MODE_3

class SPISettings {
public:
int speed;
int byte_order;
int mode;
SPISettings(int speed, int byte_order, int mode);
};

class SPI {
static void printbuf(const char * str, const byte * buffer, size_t size);
static int spi_transfern(const byte * in, byte * out, size_t size);
static void deselect_cs();
static void select_cs();
static void set_params();

public:
static void begin();
static void endTransaction();
static void beginTransaction();
static void beginTransaction(SPISettings settings);
static char transfer(byte c);

} extern SPI;

#endif /*SPI_H*/
Loading