Skip to content
Snippets Groups Projects
Select Git revision
  • bf9e68e4545e3ea2bad2337985c857500374f551
  • master default protected
2 results

MFRC522.cpp

Blame
  • MFRC522.cpp 73.49 KiB
    /*
    * MFRC522.cpp - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
    * NOTE: Please also check the comments in MFRC522.h - they provide useful hints and background information.
    * Released into the public domain.
    */
    
    #include "MFRC522.h"
    #include "SPI.h"
    #include "Serial.h"
    
    /////////////////////////////////////////////////////////////////////////////////////
    // Functions for setting up the Arduino
    /////////////////////////////////////////////////////////////////////////////////////
    /**
     * Constructor.
     */
    MFRC522::MFRC522() {
    	bcm2835_init();
    } // End constructor
    
    /**
     * Constructor.
     * Prepares the output pins.
     */
    MFRC522::MFRC522(	byte chipSelectPin,		///< Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low)
    					byte resetPowerDownPin	///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low). If there is no connection from the CPU to NRSTPD, set this to UINT8_MAX. In this case, only soft reset will be used in PCD_Init().
    				) {
    	bcm2835_init();
    	_chipSelectPin = chipSelectPin;
    	_resetPowerDownPin = resetPowerDownPin;
    } // End constructor
    
    /////////////////////////////////////////////////////////////////////////////////////
    // Basic interface functions for communicating with the MFRC522
    /////////////////////////////////////////////////////////////////////////////////////
    
    /**
     * Writes a byte to the specified register in the MFRC522 chip.
     * The interface is described in the datasheet section 8.1.2.
     */
    void MFRC522::PCD_WriteRegister(	PCD_Register reg,	///< The register to write to. One of the PCD_Register enums.
    									byte value			///< The value to write.
    								) {
    	SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0));	// Set the settings to work with SPI bus
    	digitalWrite(_chipSelectPin, LOW);		// Select slave
    	SPI.transfer(reg);						// MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
    	SPI.transfer(value);
    	digitalWrite(_chipSelectPin, HIGH);		// Release slave again
    	SPI.endTransaction(); // Stop using the SPI bus
    } // End PCD_WriteRegister()
    
    /**
     * Writes a number of bytes to the specified register in the MFRC522 chip.
     * The interface is described in the datasheet section 8.1.2.
     */
    void MFRC522::PCD_WriteRegister(	PCD_Register reg,	///< The register to write to. One of the PCD_Register enums.
    									byte count,			///< The number of bytes to write to the register
    									byte *values		///< The values to write. Byte array.
    								) {
    	SPI.beginTransaction(SPISettings(MFRC522_SPICLOCK, MSBFIRST, SPI_MODE0));	// Set the settings to work with SPI bus
    	digitalWrite(_chipSelectPin, LOW);		// Select slave
    	SPI.transfer(reg);						// MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
    	for (byte index = 0; index < count; index++) {
    		SPI.transfer(values[index]);
    	}
    	digitalWrite(_chipSelectPin, HIGH);		// Release slave again
    	SPI.endTransaction(); // Stop using the SPI bus
    } // End PCD_WriteRegister()
    
    /**