PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MifareClassicValueBlock.ino 13KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * ----------------------------------------------------------------------------
  3. * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
  4. * for further details and other examples.
  5. *
  6. * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
  7. *
  8. * Released into the public domain.
  9. * ----------------------------------------------------------------------------
  10. * This sample shows how to setup blocks on a MIFARE Classic PICC (= card/tag)
  11. * to be in "Value Block" mode: in this mode the operations Increment/Decrement,
  12. * Restore and Transfer can be used.
  13. *
  14. * BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7).
  15. *
  16. *
  17. * Typical pin layout used:
  18. * -----------------------------------------------------------------------------------------
  19. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  20. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  21. * Signal Pin Pin Pin Pin Pin Pin
  22. * -----------------------------------------------------------------------------------------
  23. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  24. * SPI SS SDA(SS) 10 53 D10 10 10
  25. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  26. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  27. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  28. *
  29. */
  30. #include <SPI.h>
  31. #include <MFRC522.h>
  32. constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
  33. constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
  34. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  35. MFRC522::MIFARE_Key key;
  36. /**
  37. * Initialize.
  38. */
  39. void setup() {
  40. Serial.begin(9600); // Initialize serial communications with the PC
  41. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  42. SPI.begin(); // Init SPI bus
  43. mfrc522.PCD_Init(); // Init MFRC522 card
  44. // Prepare the key (used both as key A and as key B)
  45. // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
  46. for (byte i = 0; i < 6; i++) {
  47. key.keyByte[i] = 0xFF;
  48. }
  49. Serial.println(F("Scan a MIFARE Classic PICC to demonstrate Value Block mode."));
  50. Serial.print(F("Using key (for A and B):"));
  51. dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
  52. Serial.println();
  53. Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
  54. }
  55. /**
  56. * Main loop.
  57. */
  58. void loop() {
  59. // Look for new cards
  60. if ( ! mfrc522.PICC_IsNewCardPresent())
  61. return;
  62. // Select one of the cards
  63. if ( ! mfrc522.PICC_ReadCardSerial())
  64. return;
  65. // Show some details of the PICC (that is: the tag/card)
  66. Serial.print(F("Card UID:"));
  67. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  68. Serial.println();
  69. Serial.print(F("PICC type: "));
  70. MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  71. Serial.println(mfrc522.PICC_GetTypeName(piccType));
  72. // Check for compatibility
  73. if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
  74. && piccType != MFRC522::PICC_TYPE_MIFARE_1K
  75. && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  76. Serial.println(F("This sample only works with MIFARE Classic cards."));
  77. return;
  78. }
  79. // In this sample we use the second sector,
  80. // that is: sector #1, covering block #4 up to and including block #7
  81. byte sector = 1;
  82. byte valueBlockA = 5;
  83. byte valueBlockB = 6;
  84. byte trailerBlock = 7;
  85. MFRC522::StatusCode status;
  86. byte buffer[18];
  87. byte size = sizeof(buffer);
  88. int32_t value;
  89. // Authenticate using key A
  90. Serial.println(F("Authenticating using key A..."));
  91. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  92. if (status != MFRC522::STATUS_OK) {
  93. Serial.print(F("PCD_Authenticate() failed: "));
  94. Serial.println(mfrc522.GetStatusCodeName(status));
  95. return;
  96. }
  97. // Show the whole sector as it currently is
  98. Serial.println(F("Current data in sector:"));
  99. mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
  100. Serial.println();
  101. // We need a sector trailer that defines blocks 5 and 6 as Value Blocks and enables key B
  102. // The last block in a sector (block #3 for Mifare Classic 1K) is the Sector Trailer.
  103. // See http://www.nxp.com/documents/data_sheet/MF1S503x.pdf sections 8.6 and 8.7:
  104. // Bytes 0-5: Key A
  105. // Bytes 6-8: Access Bits
  106. // Bytes 9: User data
  107. // Bytes 10-15: Key B (or user data)
  108. byte trailerBuffer[] = {
  109. 255, 255, 255, 255, 255, 255, // Keep default key A
  110. 0, 0, 0,
  111. 0,
  112. 255, 255, 255, 255, 255, 255}; // Keep default key B
  113. // The access bits are stored in a peculiar fashion.
  114. // There are four groups:
  115. // g[0] Access bits for block 0 (for sectors 0-31)
  116. // or blocks 0-4 (for sectors 32-39)
  117. // g[1] Access bits for block 1 (for sectors 0-31)
  118. // or blocks 5-9 (for sectors 32-39)
  119. // g[2] Access bits for block 2 (for sectors 0-31)
  120. // or blocks 10-14 (for sectors 32-39)
  121. // g[3] Access bits for the Sector Trailer: block 3 (for sectors 0-31)
  122. // or block 15 (for sectors 32-39)
  123. // Each group has access bits [C1 C2 C3], in this code C1 is MSB and C3 is LSB.
  124. // Determine the bit pattern needed using MIFARE_SetAccessBits:
  125. // g0=0 access bits for block 0 (of this sector) using [0 0 0] = 000b = 0
  126. // which means key A|B have r/w for block 0 of this sector
  127. // which (in this example) translates to block #4 within sector #1;
  128. // this is the transport configuration (at factory delivery).
  129. // g1=6 access bits for block 1 (of this sector) using [1 1 0] = 110b = 6
  130. // which means block 1 (of this sector) is used as a value block,
  131. // which (in this example) translates to block #5 within sector #1;
  132. // where key A|B have r, key B has w, key B can increment,
  133. // and key A|B can decrement, transfer, and restore.
  134. // g2=6 same thing for block 2 (of this sector): set it to a value block;
  135. // which (in this example) translates to block #6 within sector #1;
  136. // g3=3 access bits for block 3 (of this sector): the Sector Trailer here;
  137. // using [0 1 1] = 011b = 3 which means only key B has r/w access
  138. // to the Sector Trailer (block 3 of this sector) from now on
  139. // which (in this example) translates to block #7 within sector #1;
  140. mfrc522.MIFARE_SetAccessBits(&trailerBuffer[6], 0, 6, 6, 3);
  141. // Read the sector trailer as it is currently stored on the PICC
  142. Serial.println(F("Reading sector trailer..."));
  143. status = mfrc522.MIFARE_Read(trailerBlock, buffer, &size);
  144. if (status != MFRC522::STATUS_OK) {
  145. Serial.print(F("MIFARE_Read() failed: "));
  146. Serial.println(mfrc522.GetStatusCodeName(status));
  147. return;
  148. }
  149. // Check if it matches the desired access pattern already;
  150. // because if it does, we don't need to write it again...
  151. if ( buffer[6] != trailerBuffer[6]
  152. || buffer[7] != trailerBuffer[7]
  153. || buffer[8] != trailerBuffer[8]) {
  154. // They don't match (yet), so write it to the PICC
  155. Serial.println(F("Writing new sector trailer..."));
  156. status = mfrc522.MIFARE_Write(trailerBlock, trailerBuffer, 16);
  157. if (status != MFRC522::STATUS_OK) {
  158. Serial.print(F("MIFARE_Write() failed: "));
  159. Serial.println(mfrc522.GetStatusCodeName(status));
  160. return;
  161. }
  162. }
  163. // Authenticate using key B
  164. Serial.println(F("Authenticating again using key B..."));
  165. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
  166. if (status != MFRC522::STATUS_OK) {
  167. Serial.print(F("PCD_Authenticate() failed: "));
  168. Serial.println(mfrc522.GetStatusCodeName(status));
  169. return;
  170. }
  171. // A value block has a 32 bit signed value stored three times
  172. // and an 8 bit address stored 4 times. Make sure that valueBlockA
  173. // and valueBlockB have that format (note that it will only format
  174. // the block when it doesn't comply to the expected format already).
  175. formatValueBlock(valueBlockA);
  176. formatValueBlock(valueBlockB);
  177. // Add 1 to the value of valueBlockA and store the result in valueBlockA.
  178. Serial.print("Adding 1 to value of block "); Serial.println(valueBlockA);
  179. status = mfrc522.MIFARE_Increment(valueBlockA, 1);
  180. if (status != MFRC522::STATUS_OK) {
  181. Serial.print(F("MIFARE_Increment() failed: "));
  182. Serial.println(mfrc522.GetStatusCodeName(status));
  183. return;
  184. }
  185. status = mfrc522.MIFARE_Transfer(valueBlockA);
  186. if (status != MFRC522::STATUS_OK) {
  187. Serial.print(F("MIFARE_Transfer() failed: "));
  188. Serial.println(mfrc522.GetStatusCodeName(status));
  189. return;
  190. }
  191. // Show the new value of valueBlockA
  192. status = mfrc522.MIFARE_GetValue(valueBlockA, &value);
  193. if (status != MFRC522::STATUS_OK) {
  194. Serial.print(F("mifare_GetValue() failed: "));
  195. Serial.println(mfrc522.GetStatusCodeName(status));
  196. return;
  197. }
  198. Serial.print("New value of value block "); Serial.print(valueBlockA);
  199. Serial.print(" = "); Serial.println(value);
  200. // Decrement 10 from the value of valueBlockB and store the result in valueBlockB.
  201. Serial.print("Subtracting 10 from value of block "); Serial.println(valueBlockB);
  202. status = mfrc522.MIFARE_Decrement(valueBlockB, 10);
  203. if (status != MFRC522::STATUS_OK) {
  204. Serial.print(F("MIFARE_Decrement() failed: "));
  205. Serial.println(mfrc522.GetStatusCodeName(status));
  206. return;
  207. }
  208. status = mfrc522.MIFARE_Transfer(valueBlockB);
  209. if (status != MFRC522::STATUS_OK) {
  210. Serial.print(F("MIFARE_Transfer() failed: "));
  211. Serial.println(mfrc522.GetStatusCodeName(status));
  212. return;
  213. }
  214. // Show the new value of valueBlockB
  215. status = mfrc522.MIFARE_GetValue(valueBlockB, &value);
  216. if (status != MFRC522::STATUS_OK) {
  217. Serial.print(F("mifare_GetValue() failed: "));
  218. Serial.println(mfrc522.GetStatusCodeName(status));
  219. return;
  220. }
  221. Serial.print(F("New value of value block ")); Serial.print(valueBlockB);
  222. Serial.print(F(" = ")); Serial.println(value);
  223. // Check some boundary...
  224. if (value <= -100) {
  225. Serial.println(F("Below -100, so resetting it to 255 = 0xFF just for fun..."));
  226. status = mfrc522.MIFARE_SetValue(valueBlockB, 255);
  227. if (status != MFRC522::STATUS_OK) {
  228. Serial.print(F("mifare_SetValue() failed: "));
  229. Serial.println(mfrc522.GetStatusCodeName(status));
  230. return;
  231. }
  232. }
  233. // Dump the sector data
  234. mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
  235. Serial.println();
  236. // Halt PICC
  237. mfrc522.PICC_HaltA();
  238. // Stop encryption on PCD
  239. mfrc522.PCD_StopCrypto1();
  240. }
  241. /**
  242. * Helper routine to dump a byte array as hex values to Serial.
  243. */
  244. void dump_byte_array(byte *buffer, byte bufferSize) {
  245. for (byte i = 0; i < bufferSize; i++) {
  246. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  247. Serial.print(buffer[i], HEX);
  248. }
  249. }
  250. /**
  251. * Ensure that a given block is formatted as a Value Block.
  252. */
  253. void formatValueBlock(byte blockAddr) {
  254. byte buffer[18];
  255. byte size = sizeof(buffer);
  256. MFRC522::StatusCode status;
  257. Serial.print(F("Reading block ")); Serial.println(blockAddr);
  258. status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
  259. if (status != MFRC522::STATUS_OK) {
  260. Serial.print(F("MIFARE_Read() failed: "));
  261. Serial.println(mfrc522.GetStatusCodeName(status));
  262. return;
  263. }
  264. if ( (buffer[0] == (byte)~buffer[4])
  265. && (buffer[1] == (byte)~buffer[5])
  266. && (buffer[2] == (byte)~buffer[6])
  267. && (buffer[3] == (byte)~buffer[7])
  268. && (buffer[0] == buffer[8])
  269. && (buffer[1] == buffer[9])
  270. && (buffer[2] == buffer[10])
  271. && (buffer[3] == buffer[11])
  272. && (buffer[12] == (byte)~buffer[13])
  273. && (buffer[12] == buffer[14])
  274. && (buffer[12] == (byte)~buffer[15])) {
  275. Serial.println(F("Block has correct Value Block format."));
  276. }
  277. else {
  278. Serial.println(F("Formatting as Value Block..."));
  279. byte valueBlock[] = {
  280. 0, 0, 0, 0,
  281. 255, 255, 255, 255,
  282. 0, 0, 0, 0,
  283. blockAddr, ~blockAddr, blockAddr, ~blockAddr };
  284. status = mfrc522.MIFARE_Write(blockAddr, valueBlock, 16);
  285. if (status != MFRC522::STATUS_OK) {
  286. Serial.print(F("MIFARE_Write() failed: "));
  287. Serial.println(mfrc522.GetStatusCodeName(status));
  288. }
  289. }
  290. }