Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
rc522.h
Go to the documentation of this file.
1 #ifndef RC522_H
2 #define RC522_H
3 /*
4  * This file is slightly modified version of the same file from https://github.com/paulvha/rfid-rc522.git
5  *
6  *
7  * Header file to support routines to access the RC522
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by the
11  * Free Software Foundation, either version 3 of the License, or (at your
12  * option) any later version.
13  *
14  * The program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * Created on: 14.08.2013
23  * authorizer: alexs
24  * modified: trestikp
25  *
26  *
27  * version 1.51 / paulvh / July 2017
28  * Fixed number of bugs and code clean-up.
29  */
30 
31 #include <stdint.h> //for uintx_t
32 //#include "spidev_lib++.h"
33 #include "app_workspace.h" // app_workspace includes spidev_lib++. This is to prevent redefinition, which occurs in rfid_reader.cpp
34 
35 // the registers that are RFU (reserved for future use) have been left out.
36 
37 //MF522 commands for the CommandReg
38 #define PCD_IDLE 0x00 // no action, cancels current command execution
39 #define PCD_AUTHENT 0x0E // perform the MIFARE standard authentication as a reader
40 #define PCD_RECEIVE 0x08 // activate the receiver circuits
41 #define PCD_TRANSMIT 0x04 // transmits data from the FIFO buffer
42 #define PCD_TRANSCEIVE 0x0C // transmits data from the FIFO buffer & activate receive after transmission
43 #define PCD_RESETPHASE 0x0F // softreset command
44 #define PCD_CALCCRC 0x03 // activates the CRC co-processor or perform a selftest
45 
46 //Mifare_One basic function library
47 #define PICC_REQIDL 0x26 // REQA (idle card will respond)
48 #define PICC_REQALL 0x52 // WUPA (All cards will respond IDLE and Halt)
49 #define PICC_ANTICOLL1 0x93 // cascade level 1
50 #define PICC_ANTICOLL2 0x95 // cascade level 2
51 #define PICC_ANTICOLL3 0x97 // cascade level 3
52 #define PICC_AUTHENT1A 0x60 // authentication with KeyA
53 #define PICC_AUTHENT1B 0x61 // authentication with keyb
54 #define PICC_READ 0x30 // read 16 byte datablock
55 #define PICC_WRITE 0xA0 // write 16 byte datablock
56 #define PICC_DECREMENT 0xC0 // decrement value block
57 #define PICC_INCREMENT 0xC1 // increment value block
58 #define PICC_RESTORE 0xC2 // move value block to internal data register
59 #define PICC_TRANSFER 0xB0 // move internal data-reg to value block
60 #define PICC_HALT 0x50 // + 00
61 
62 //MF522 FIFO
63 #define DEF_FIFO_LENGTH 64 //FIFO size=64byte
64 #define MAXRLEN 18
65 
67 // command and status
68 #define CommandReg 0x01 // starts and stops command execution
69 #define ComIEnReg 0x02 // enable and disable interrupt request control bits
70 #define DivlEnReg 0x03 // enable and disable interrupt request control bits
71 #define ComIrqReg 0x04 // interrupt request bits
72 #define DivIrqReg 0x05 // interrupt request bits
73 #define ErrorReg 0x06 // error bits showing the error status of last command
74 #define Status1Reg 0x07 // communication status bits
75 #define Status2Reg 0x08 // receiver and transmitter status bits
76 #define FIFODataReg 0x09 // input and output of 64 byte FIFO buffer
77 #define FIFOLevelReg 0x0A // number of bytes stored in the FIFO buffer
78 #define WaterLevelReg 0x0B // level for FIFO underflow and overflow warning
79 #define ControlReg 0x0C // miscellaneus control reigsters
80 #define BitFramingReg 0x0D // adjustments for bit-oriented frames
81 #define CollReg 0x0E // bit position of the first bit-collision detected on RF int.
82 // command
83 #define ModeReg 0x11 // defines general modes for transmitting and receiving
84 #define TxModeReg 0x12 // defines transmission data rate and framing
85 #define RxModeReg 0x13 // defines reception data rate and framing
86 #define TxControlReg 0x14 // controls the logical behavior of the antenna driver TX1/ TX2
87 #define TxASKReg 0x15 // controls the setting of the transmission modulation
88 #define TxSelReg 0x16 // selects the internal sources for the antenna driver
89 #define RxSelReg 0x17 // selects the internal receiver settings
90 #define RxThresholdReg 0x18 // selects thresholds for the bit decoder
91 #define DemodReg 0x19 // defines demodulator settings
92 #define MiTXReg 0x1C // controls some MIFARE communication transmit parameters
93 #define MiRXReg 0x1D // controls some MIFARE communication receive parameters
94 #define SerialSpeedReg 0x1F // selects the speed for the serial UART interface
95 // configuration
96 #define CRCResultRegM 0x21 // shows the MSB value for the CRC calculation
97 #define CRCResultRegL 0x22 // shows the LSB value for the CRC calculation
98 #define ModWidthReg 0x24 // controls the ModWidth settings
99 #define RFCfgReg 0x26 // configures the receiver gain
100 #define GsNReg 0x27 // selects the conductance of the antenna on TX1/TX2 modulation
101 #define CWGsCfgReg 0x28 // defines the conductance of the p-driver output
102 #define ModGsCfgReg 0x29 // defines the conductance of the p-driver output
103 #define TModeReg 0x2A // defines settings for the internal timer
104 #define TPrescalerReg 0x2B // defines settings for the internal timer
105 #define TReloadRegH 0x2C // defines the 16 bit timer reload value
106 #define TReloadRegL 0x2D // defines the 16 bit timer reload value
107 #define TCounterValueRegH 0x2E // shows the 16 bit timer reload value
108 #define TCounterValueRegL 0x2F // shows the 16 bit timer reload value
109 // test register
110 #define TestSel1Reg 0x31 // general test signal configuration
111 #define TestSel2Reg 0x32 // general test signal configuration and PRBS control
112 #define TestPinEnReg 0x33 // enables pin output driver on pins D1 to D7
113 #define TestPinValueReg 0x34 // defines values for D1 to D7 when used as I/O bus
114 #define TestBusReg 0x35 // shows the status of the internal test bus
115 #define AutoTestReg 0x36 // controls the digital self test
116 #define VersionReg 0x37 // shows the software version
117 #define AnalogTestReg 0x38 // controls the pins AUX1 and AUX2
118 #define TestDAC1Reg 0x39 // defines the test value for testDAC1
119 #define TestDAC2Reg 0x3A // defines the test value for testDAC2
120 #define TestADCReg 0x3B // shows the value of ADC I and Q channels
121 
123 //MF522 status
125 #define TAG_OK (0)
126 #define TAG_NOTAG (1)
127 #define TAG_ERR (2)
128 #define TAG_ERRCRC (3)
129 #define TAG_COLLISION (4)
130 typedef char tag_stat;
131 
133 //MF522 command routines
135 
136 // perform init
137 void InitRc522(SPI *spi);
138 char PcdReset(void);
139 
140 // set and clear bits in registers
141 void SetBitMask(uint8_t reg,uint8_t mask);
142 void ClearBitMask(uint8_t reg,uint8_t mask);
143 
144 // read and write a register
145 uint8_t WriteRawRC(uint8_t Address, uint8_t value);
146 uint8_t ReadRawRC(uint8_t Address);
147 
148 // switch antenna on or off
149 void PcdAntennaOn(void);
150 void PcdAntennaOff(void);
151 
152 // stop RC522 from sending in Crypto-1 encrypted communication
153 void Pcd_stopcrypto1();
154 
155 /* check whether already authorized */
156 int PcdCheckAuth();
157 
159 //card supporting routines
161 
162 // enable extra delay between card access calls
163 extern int extra_delay;
164 
165 /* Decrement, Increment or restore a value block */
166 char PcdValue(uint8_t act, uint8_t addr, double value);
167 
168 // perform request to start card
169 char PcdRequest(unsigned char req_code,unsigned char *pTagType);
170 
171 // communicate with card
172 char PcdComMF522(uint8_t Command,
173  uint8_t *pIn ,
174  uint8_t InLenByte,
175  uint8_t *pOut ,
176  uint8_t *pOutLenBit,
177  int CheckTimeout);
178 
179 void CalulateCRC(uint8_t *pIn ,uint8_t len,uint8_t *pOut );
180 
181 // get the UID and select the right card
182 char PcdAnticoll(uint8_t , uint8_t *);
183 char PcdSelect(uint8_t , uint8_t *, uint8_t *);
184 
185 // authorise access to a block and set Crypto-1 encrypted communication
186 char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr);
187 
188 // read and write card block
189 char PcdWrite(unsigned char addr,unsigned char *pData);
190 char PcdRead(unsigned char addr,unsigned char *pData);
191 
192 // put current card on hold
193 char PcdHalt(void);
194 
196 //Global variables
198 
199 extern int extra_delay; // set extra delay on card access calls
200 
201 #endif
SPI * spi
Definition: rc522.cpp:36
char tag_stat
Definition: rc522.h:130
char PcdRead(unsigned char addr, unsigned char *pData)
void InitRc522(SPI *spi)
Definition: rc522.cpp:39
char PcdValue(uint8_t act, uint8_t addr, double value)
Definition: rc522.cpp:385
int PcdCheckAuth()
Definition: rc522.cpp:445
char PcdHalt(void)
Definition: rc522.cpp:363
char PcdSelect(uint8_t, uint8_t *, uint8_t *)
Definition: rc522.cpp:187
char PcdReset(void)
Definition: rc522.cpp:498
char PcdComMF522(uint8_t Command, uint8_t *pIn, uint8_t InLenByte, uint8_t *pOut, uint8_t *pOutLenBit, int CheckTimeout)
Definition: rc522.cpp:626
char PcdAnticoll(uint8_t, uint8_t *)
Definition: rc522.cpp:89
char PcdWrite(unsigned char addr, unsigned char *pData)
void ClearBitMask(uint8_t reg, uint8_t mask)
Definition: rc522.cpp:618
int extra_delay
Definition: rc522.h:199
void PcdAntennaOn(void)
Definition: rc522.cpp:790
void PcdAntennaOff(void)
Definition: rc522.cpp:798
uint8_t WriteRawRC(uint8_t Address, uint8_t value)
Definition: rc522.cpp:593
void Pcd_stopcrypto1()
Definition: rc522.cpp:433
void SetBitMask(uint8_t reg, uint8_t mask)
Definition: rc522.cpp:609
char PcdRequest(unsigned char req_code, unsigned char *pTagType)
uint8_t ReadRawRC(uint8_t Address)
Definition: rc522.cpp:573
void CalulateCRC(uint8_t *pIn, uint8_t len, uint8_t *pOut)
Definition: rc522.cpp:461
char PcdAuthState(unsigned char auth_mode, unsigned char addr, unsigned char *pKey, unsigned char *pSnr)