Initial commit

This commit is contained in:
Chris 2018-06-18 00:55:49 +02:00
commit 48891d71eb
7 changed files with 225 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*/*.o
mrfc522

10
Makefile Normal file
View File

@ -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 $@ $<

19
src/main.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <cstring>
#include "spi.h"
#include "mfrc522.h"
int main(int argc, char** argv)
{
SPI spi(0);
MFRC522 reader(&spi);
}

13
src/mfrc522.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "mfrc522.h"
MFRC522::MFRC522(SPI *spi)
{
this->spi = spi;
spi->SpiOpenPort();
}
MFRC522::~MFRC522()
{
spi->SpiClosePort();
}

14
src/mfrc522.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include "spi.h"
class MFRC522
{
public:
MFRC522(SPI *spi);
~MFRC522();
private:
SPI *spi;
};

134
src/spi.cpp Normal file
View File

@ -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;
}

33
src/spi.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <fcntl.h> //Needed for SPI port
#include <sys/ioctl.h> //Needed for SPI port
#include <linux/spi/spidev.h> //Needed for SPI port
#include <unistd.h> //Needed for SPI port
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <cstring>
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;
};