From 48891d71ebccf827ef129f848cb42bab4682a32e Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Mon, 18 Jun 2018 00:55:49 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 + Makefile | 10 ++++ src/main.cpp | 19 +++++++ src/mfrc522.cpp | 13 +++++ src/mfrc522.h | 14 +++++ src/spi.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ src/spi.h | 33 ++++++++++++ 7 files changed, 225 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 src/main.cpp create mode 100644 src/mfrc522.cpp create mode 100644 src/mfrc522.h create mode 100644 src/spi.cpp create mode 100644 src/spi.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e98ec9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*/*.o +mrfc522 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ca484c4 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ + +CC_FLAGS=-I. -Os -g +LD_FLAGS= +LIBS=-lstdc++ + +mrfc522: src/main.o src/mfrc522.o src/spi.o + gcc ${LD_FLAGS} -o $@ $^ ${LIBS} + +%.o: %.cpp + gcc -c ${CC_FLAGS} -o $@ $< diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..e926478 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include +#include +#include "spi.h" +#include "mfrc522.h" + + +int main(int argc, char** argv) +{ + + SPI spi(0); + MFRC522 reader(&spi); + + + +} \ No newline at end of file diff --git a/src/mfrc522.cpp b/src/mfrc522.cpp new file mode 100644 index 0000000..7196fa7 --- /dev/null +++ b/src/mfrc522.cpp @@ -0,0 +1,13 @@ +#include "mfrc522.h" + + +MFRC522::MFRC522(SPI *spi) +{ + this->spi = spi; + spi->SpiOpenPort(); +} + +MFRC522::~MFRC522() +{ + spi->SpiClosePort(); +} \ No newline at end of file diff --git a/src/mfrc522.h b/src/mfrc522.h new file mode 100644 index 0000000..59fbaf1 --- /dev/null +++ b/src/mfrc522.h @@ -0,0 +1,14 @@ +#pragma once + +#include "spi.h" + +class MFRC522 +{ + public: + MFRC522(SPI *spi); + ~MFRC522(); + + private: + SPI *spi; + +}; diff --git a/src/spi.cpp b/src/spi.cpp new file mode 100644 index 0000000..59fbb77 --- /dev/null +++ b/src/spi.cpp @@ -0,0 +1,134 @@ +#include "spi.h" + +SPI::SPI(int device) +{ + spi_device = device; +} + +SPI::~SPI() +{ + // +} + +//spi_device 0=CS0, 1=CS1 +int SPI::SpiOpenPort() +{ + int status_value = -1; + + + //----- SET SPI MODE ----- + //SPI_MODE_0 (0,0) CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge + //SPI_MODE_1 (0,1) CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge + //SPI_MODE_2 (1,0) CPOL = 1, CPHA = 0, Clock idle high, data is clocked in on falling edge, output data (change) on rising edge + //SPI_MODE_3 (1,1) CPOL = 1, CPHA = 1, Clock idle high, data is clocked in on rising, edge output data (change) on falling edge + spi_mode = SPI_MODE_3; + + //----- SET BITS PER WORD ----- + spi_bitsPerWord = 8; + + //----- SET SPI BUS SPEED ----- + spi_speed = 1000000; //1000000 = 1MHz (1uS per bit) + + + if (spi_device) + this->spi_cs_fd = open(std::string("/dev/spidev0.1").c_str(), O_RDWR); + else + this->spi_cs_fd = open(std::string("/dev/spidev0.0").c_str(), O_RDWR); + + if (this->spi_cs_fd < 0) + { + perror("Error - Could not open SPI device"); + exit(1); + } + + status_value = ioctl(this->spi_cs_fd, SPI_IOC_WR_MODE, &spi_mode); + if(status_value < 0) + { + perror("Could not set SPIMode (WR)...ioctl fail"); + exit(1); + } + + status_value = ioctl(this->spi_cs_fd, SPI_IOC_RD_MODE, &spi_mode); + if(status_value < 0) + { + perror("Could not set SPIMode (RD)...ioctl fail"); + exit(1); + } + + status_value = ioctl(this->spi_cs_fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bitsPerWord); + if(status_value < 0) + { + perror("Could not set SPI bitsPerWord (WR)...ioctl fail"); + exit(1); + } + + status_value = ioctl(this->spi_cs_fd, SPI_IOC_RD_BITS_PER_WORD, &spi_bitsPerWord); + if(status_value < 0) + { + perror("Could not set SPI bitsPerWord(RD)...ioctl fail"); + exit(1); + } + + status_value = ioctl(this->spi_cs_fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed); + if(status_value < 0) + { + perror("Could not set SPI speed (WR)...ioctl fail"); + exit(1); + } + + status_value = ioctl(this->spi_cs_fd, SPI_IOC_RD_MAX_SPEED_HZ, &spi_speed); + if(status_value < 0) + { + perror("Could not set SPI speed (RD)...ioctl fail"); + exit(1); + } + return(status_value); +} + + +int SPI::SpiClosePort() +{ + int status_value = -1; + + + status_value = close(this->spi_cs_fd); + if(status_value < 0) + { + perror("Error - Could not close SPI device"); + exit(1); + } + return(status_value); +} + + + +int SPI::SpiWriteAndRead(unsigned char *data, int length) +{ + struct spi_ioc_transfer spi[length]; + int i = 0; + int retVal = -1; + + //one spi transfer for each byte + + for (i = 0 ; i < length ; i++) + { + memset(&spi[i], 0, sizeof (spi[i])); + spi[i].tx_buf = (unsigned long)(data + i); // transmit from "data" + spi[i].rx_buf = (unsigned long)(data + i) ; // receive into "data" + spi[i].len = sizeof(*(data + i)) ; + spi[i].delay_usecs = 0 ; + spi[i].speed_hz = spi_speed ; + spi[i].bits_per_word = spi_bitsPerWord ; + spi[i].cs_change = 0; + } + + retVal = ioctl(this->spi_cs_fd, SPI_IOC_MESSAGE(length), &spi) ; + + if(retVal < 0) + { + perror("Error - Problem transmitting spi data..ioctl"); + exit(1); + } + + return retVal; +} diff --git a/src/spi.h b/src/spi.h new file mode 100644 index 0000000..8b1651a --- /dev/null +++ b/src/spi.h @@ -0,0 +1,33 @@ +#pragma once + +#include //Needed for SPI port +#include //Needed for SPI port +#include //Needed for SPI port +#include //Needed for SPI port +#include +#include +#include +#include +#include +#include + + +class SPI +{ + public: + SPI(int port); + ~SPI(); + + int SpiOpenPort(); + int SpiClosePort(); + int SpiWriteAndRead(unsigned char *data, int length); + + private: + int spi_device; + int spi_cs_fd; + //int spi_cs0_fd; //file descriptor for the SPI device + //int spi_cs1_fd; //file descriptor for the SPI device + unsigned char spi_mode; + unsigned char spi_bitsPerWord; + unsigned int spi_speed; +};