mfrc522

package
v3.3.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2018 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package mfrc522 controls a Mifare RFID card reader.

Datasheet

https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf

Example
package main

import (
	"encoding/hex"
	"log"
	"reflect"
	"time"

	"periph.io/x/periph/conn/spi/spireg"
	"periph.io/x/periph/experimental/devices/mfrc522"
	"periph.io/x/periph/experimental/devices/mfrc522/commands"
	"periph.io/x/periph/host"
	"periph.io/x/periph/host/rpi"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Using SPI as an example. See package ./spi/spireg for more details.
	p, err := spireg.Open("")
	if err != nil {
		log.Fatal(err)
	}
	defer p.Close()

	rfid, err := mfrc522.NewSPI(p, rpi.P1_13, rpi.P1_11)
	if err != nil {
		log.Fatal(err)
	}

	// Idling device on exit.
	defer rfid.Halt()

	// Setting the antenna signal strength.
	rfid.SetAntennaGain(5)

	// Converting access key.
	// This value corresponds to first pi "numbers": 3 14 15 92 65 35.
	hexKey, _ := hex.DecodeString("030e0f5c4123")
	var key [6]byte
	copy(key[:], hexKey)

	// Converting expected data.
	// This value corresponds to string "@~>f=Um[X{LRwA3}".
	expected, _ := hex.DecodeString("407e3e663d556d5b587b4c527741337d")

	timedOut := false
	cb := make(chan []byte)
	timer := time.NewTimer(10 * time.Second)

	// Stopping timer, flagging reader thread as timed out
	defer func() {
		timer.Stop()
		timedOut = true
		close(cb)
	}()

	go func() {
		log.Printf("Started %s", rfid.String())

		for {
			// Trying to read data from sector 1 block 0
			data, err := rfid.ReadCard(byte(commands.PICC_AUTHENT1B), 1, 0, key)

			// If main thread timed out just exiting.
			if timedOut {
				return
			}

			// Some devices tend to send wrong data while RFID chip is already detected
			// but still "too far" from a receiver.
			// Especially some cheap CN clones which you can find on GearBest, AliExpress, etc.
			// This will suppress such errors.
			if err != nil {
				continue
			}

			cb <- data
		}
	}()

	for {
		select {
		case <-timer.C:
			log.Fatal("Didn't receive device data")
			return
		case data := <-cb:
			if !reflect.DeepEqual(data, expected) {
				log.Fatal("Received data is incorrect")
			} else {
				log.Println("Received data is correct")
			}

			return
		}
	}
}
Output:

Index

Examples

Constants

View Source
const (
	AnyKeyRWID    BlockAccess = iota
	RAB_WN_IN_DN              = 0x02 // Read (A|B), Write (None), Increment (None), Decrement(None)
	RAB_WB_IN_DN              = 0x04
	RAB_WB_IB_DAB             = 0x06
	RAB_WN_IN_DAB             = 0x01
	RB_WB_IN_DN               = 0x03
	RB_WN_IN_DN               = 0x05
	RN_WN_IN_DN               = 0x07

	KeyA_RN_WA_BITS_RA_WN_KeyB_RA_WA        SectorTrailerAccess = iota
	KeyA_RN_WN_BITS_RA_WN_KeyB_RA_WN                            = 0x02
	KeyA_RN_WB_BITS_RAB_WN_KeyB_RN_WB                           = 0x04
	KeyA_RN_WN_BITS_RAB_WN_KeyB_RN_WN                           = 0x06
	KeyA_RN_WA_BITS_RA_WA_KeyB_RA_WA                            = 0x01
	KeyA_RN_WB_BITS_RAB_WB_KeyB_RN_WB                           = 0x03
	KeyA_RN_WN_BITS_RAB_WB_KeyB_RN_WN                           = 0x05
	KeyA_RN_WN_BITS_RAB_WN_KeyB_RN_WN_EXTRA                     = 0x07
)

Access bits.

Variables

View Source
var DefaultKey = [...]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}

DefaultKey provides the default bytes for card authentication for method B.

Functions

func CalculateBlockAccess

func CalculateBlockAccess(ba *BlocksAccess) []byte

CalculateBlockAccess calculates the block access.

Types

type BlockAccess

type BlockAccess byte

BlockAccess defines the block access bits.

type BlocksAccess

type BlocksAccess struct {
	B0, B1, B2 BlockAccess
	B3         SectorTrailerAccess
}

BlocksAccess defines the access structure for first 3 blocks of the sector and the access bits for the sector trail.

func ParseBlockAccess

func ParseBlockAccess(ad []byte) *BlocksAccess

ParseBlockAccess parses the given byte array into the block access structure.

func (*BlocksAccess) String

func (ba *BlocksAccess) String() string

type Dev

type Dev struct {
	LowLevel *commands.LowLevel
	// contains filtered or unexported fields
}

Dev is an handle to an MFRC522 RFID reader.

func NewSPI

func NewSPI(spiPort spi.Port, resetPin gpio.PinOut, irqPin gpio.PinIn) (*Dev, error)

NewSPI creates and initializes the RFID card reader attached to SPI.

spiPort - the SPI device to use.
resetPin - reset GPIO pin.
irqPin - irq GPIO pin.

func (*Dev) Halt

func (r *Dev) Halt() error

Halt implements conn.Resource.

It soft-stops the chip - PowerDown bit set, command IDLE

func (*Dev) ReadAuth

func (r *Dev) ReadAuth(auth byte, sector int, key [6]byte) (data []byte, err error)

ReadAuth - read the card authentication data.

sector - the sector to authenticate on.
key - the key to be used for accessing the sector data.

func (*Dev) ReadCard

func (r *Dev) ReadCard(auth byte, sector int, block int, key [6]byte) (data []byte, err error)

ReadCard reads the card sector/block.

auth - the authentication mode.
sector - the sector to authenticate on.
block - the block within sector to authenticate.
key - the key to be used for accessing the sector data.

func (*Dev) SetAntennaGain

func (r *Dev) SetAntennaGain(gain int) error

SetAntennaGain configures antenna signal strength.

gain - signal strength from 0 to 7.

func (*Dev) SetOperationTimeout

func (r *Dev) SetOperationTimeout(timeout time.Duration)

SetOperationTimeout updates the device timeout for card operations.

Effectively that sets the maximum time the RFID device will wait for IRQ from the proximity card detection.

timeout the duration to wait for IRQ strobe.

func (*Dev) String

func (r *Dev) String() string

String implements conn.Resource.

func (*Dev) WriteCard

func (r *Dev) WriteCard(auth byte, sector int, block int, data [16]byte, key [6]byte) (err error)

WriteCard writes the data into the card block.

auth - the authentiction mode.
sector - the sector on the card to write to.
block - the block within the sector to write into.
data - 16 bytes if data to write
key - the key used to authenticate the card - depends on the used auth method.

func (*Dev) WriteSectorTrail

func (r *Dev) WriteSectorTrail(auth byte, sector int, keyA [6]byte, keyB [6]byte, access *BlocksAccess, key [6]byte) (err error)

WriteSectorTrail writes the sector trail with sector access bits.

auth - authentication mode.
sector - sector to set authentication.
keyA - the key used for AuthA authentication scheme.
keyB - the key used for AuthB authentication scheme.
access - the block access structure.
key - the current key used to authenticate the provided sector.

type SectorTrailerAccess

type SectorTrailerAccess byte

SectorTrailerAccess defines the sector trailing block access bits.

Directories

Path Synopsis
Package commands contains the command that a MFRC522 supports.
Package commands contains the command that a MFRC522 supports.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL