|
| 1 | +// NAME: PN5180ISO14443.h |
| 2 | +// |
| 3 | +// DESC: ISO14443 protocol on NXP Semiconductors PN5180 module for Arduino. |
| 4 | +// |
| 5 | +// Copyright (c) 2019 by Dirk Carstensen. All rights reserved. |
| 6 | +// |
| 7 | +// This file is part of the PN5180 library for the Arduino environment. |
| 8 | +// |
| 9 | +// This library is free software; you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU Lesser General Public |
| 11 | +// License as published by the Free Software Foundation; either |
| 12 | +// version 2.1 of the License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// This library 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 GNU |
| 17 | +// Lesser General Public License for more details. |
| 18 | +// |
| 19 | +// #define DEBUG 1 |
| 20 | + |
| 21 | +#include <Arduino.h> |
| 22 | +#include "PN5180ISO14443.h" |
| 23 | +#include <PN5180.h> |
| 24 | +#include "Debug.h" |
| 25 | + |
| 26 | +PN5180ISO14443::PN5180ISO14443(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin) |
| 27 | + : PN5180(SSpin, BUSYpin, RSTpin) { |
| 28 | +} |
| 29 | + |
| 30 | +bool PN5180ISO14443::setupRF() { |
| 31 | + PN5180DEBUG(F("Loading RF-Configuration...\n")); |
| 32 | + if (loadRFConfig(0x00, 0x80)) { // ISO14443 parameters |
| 33 | + PN5180DEBUG(F("done.\n")); |
| 34 | + } |
| 35 | + else return false; |
| 36 | + |
| 37 | + PN5180DEBUG(F("Turning ON RF field...\n")); |
| 38 | + if (setRF_on()) { |
| 39 | + PN5180DEBUG(F("done.\n")); |
| 40 | + } |
| 41 | + else return false; |
| 42 | + |
| 43 | + return true; |
| 44 | +} |
| 45 | + |
| 46 | +uint16_t PN5180ISO14443::rxBytesReceived() { |
| 47 | + uint32_t rxStatus; |
| 48 | + uint16_t len = 0; |
| 49 | + readRegister(RX_STATUS, &rxStatus); |
| 50 | + // Lower 9 bits has length |
| 51 | + len = (uint16_t)(rxStatus & 0x000001ff); |
| 52 | + return len; |
| 53 | +} |
| 54 | +/* |
| 55 | +* buffer : must be 10 byte array |
| 56 | +* buffer[0-1] is ATQA |
| 57 | +* buffer[2] is sak |
| 58 | +* buffer[3..6] is 4 byte UID |
| 59 | +* buffer[7..9] is remaining 3 bytes of UID for 7 Byte UID tags |
| 60 | +* kind : 0 we send REQA, 1 we send WUPA |
| 61 | +* |
| 62 | +* return value: the uid length: |
| 63 | +* - zero if no tag was recognized |
| 64 | +* - single Size UID (4 byte) |
| 65 | +* - double Size UID (7 byte) |
| 66 | +* - triple Size UID (10 byte) - not yet supported |
| 67 | +*/ |
| 68 | +uint8_t PN5180ISO14443::activateTypeA(uint8_t *buffer, uint8_t kind) { |
| 69 | + uint8_t cmd[7]; |
| 70 | + uint8_t uidLength = 0; |
| 71 | + // Load standard TypeA protocol |
| 72 | + if (!loadRFConfig(0x0, 0x80)) |
| 73 | + return 0; |
| 74 | + |
| 75 | + // OFF Crypto |
| 76 | + if (!writeRegisterWithAndMask(SYSTEM_CONFIG, 0xFFFFFFBF)) |
| 77 | + return 0; |
| 78 | + // Clear RX CRC |
| 79 | + if (!writeRegisterWithAndMask(CRC_RX_CONFIG, 0xFFFFFFFE)) |
| 80 | + return 0; |
| 81 | + // Clear TX CRC |
| 82 | + if (!writeRegisterWithAndMask(CRC_TX_CONFIG, 0xFFFFFFFE)) |
| 83 | + return 0; |
| 84 | + //Send REQA/WUPA, 7 bits in last byte |
| 85 | + cmd[0] = (kind == 0) ? 0x26 : 0x52; |
| 86 | + if (!sendData(cmd, 1, 0x07)) |
| 87 | + return 0; |
| 88 | + // READ 2 bytes ATQA into buffer |
| 89 | + if (!readData(2, buffer)) |
| 90 | + return 0; |
| 91 | + //Send Anti collision 1, 8 bits in last byte |
| 92 | + cmd[0] = 0x93; |
| 93 | + cmd[1] = 0x20; |
| 94 | + if (!sendData(cmd, 2, 0x00)) |
| 95 | + return 0; |
| 96 | + //Read 5 bytes, we will store at offset 2 for later usage |
| 97 | + if (!readData(5, cmd+2)) |
| 98 | + return 0; |
| 99 | + //Enable RX CRC calculation |
| 100 | + if (!writeRegisterWithOrMask(CRC_RX_CONFIG, 0x01)) |
| 101 | + return 0; |
| 102 | + //Enable TX CRC calculation |
| 103 | + if (!writeRegisterWithOrMask(CRC_TX_CONFIG, 0x01)) |
| 104 | + return 0; |
| 105 | + //Send Select anti collision 1, the remaining bytes are already in offset 2 onwards |
| 106 | + cmd[0] = 0x93; |
| 107 | + cmd[1] = 0x70; |
| 108 | + if (!sendData(cmd, 7, 0x00)) |
| 109 | + return 0; |
| 110 | + //Read 1 byte SAK into buffer[2] |
| 111 | + if (!readData(1, buffer+2)) |
| 112 | + return 0; |
| 113 | + // Check if the tag is 4 Byte UID or 7 byte UID and requires anti collision 2 |
| 114 | + // If Bit 3 is 0 it is 4 Byte UID |
| 115 | + if ((buffer[2] & 0x04) == 0) { |
| 116 | + // Take first 4 bytes of anti collision as UID store at offset 3 onwards. job done |
| 117 | + for (int i = 0; i < 4; i++) buffer[3+i] = cmd[2 + i]; |
| 118 | + uidLength = 4; |
| 119 | + } |
| 120 | + else { |
| 121 | + // Take First 3 bytes of UID, Ignore first byte 88(CT) |
| 122 | + if (cmd[2] != 0x88) |
| 123 | + return 0; |
| 124 | + for (int i = 0; i < 3; i++) buffer[3+i] = cmd[3 + i]; |
| 125 | + // Clear RX CRC |
| 126 | + if (!writeRegisterWithAndMask(CRC_RX_CONFIG, 0xFFFFFFFE)) |
| 127 | + return 0; |
| 128 | + // Clear TX CRC |
| 129 | + if (!writeRegisterWithAndMask(CRC_TX_CONFIG, 0xFFFFFFFE)) |
| 130 | + return 0; |
| 131 | + // Do anti collision 2 |
| 132 | + cmd[0] = 0x95; |
| 133 | + cmd[1] = 0x20; |
| 134 | + if (!sendData(cmd, 2, 0x00)) |
| 135 | + return 0; |
| 136 | + //Read 5 bytes. we will store at offset 2 for later use |
| 137 | + if (!readData(5, cmd+2)) |
| 138 | + return 0; |
| 139 | + // first 4 bytes belongs to last 4 UID bytes, we keep it. |
| 140 | + for (int i = 0; i < 4; i++) { |
| 141 | + buffer[6 + i] = cmd[2+i]; |
| 142 | + } |
| 143 | + //Enable RX CRC calculation |
| 144 | + if (!writeRegisterWithOrMask(CRC_RX_CONFIG, 0x01)) |
| 145 | + return 0; |
| 146 | + //Enable TX CRC calculation |
| 147 | + if (!writeRegisterWithOrMask(CRC_TX_CONFIG, 0x01)) |
| 148 | + return 0; |
| 149 | + //Send Select anti collision 2 |
| 150 | + cmd[0] = 0x95; |
| 151 | + cmd[1] = 0x70; |
| 152 | + if (!sendData(cmd, 7, 0x00)) |
| 153 | + return 0; |
| 154 | + //Read 1 byte SAK into buffer[2] |
| 155 | + if (!readData(1, buffer + 2)) |
| 156 | + return 0; |
| 157 | + uidLength = 7; |
| 158 | + } |
| 159 | + return uidLength; |
| 160 | +} |
| 161 | + |
| 162 | +bool PN5180ISO14443::mifareBlockRead(uint8_t blockno, uint8_t *buffer) { |
| 163 | + bool success = false; |
| 164 | + uint16_t len; |
| 165 | + uint8_t cmd[2]; |
| 166 | + // Send mifare command 30,blockno |
| 167 | + cmd[0] = 0x30; |
| 168 | + cmd[1] = blockno; |
| 169 | + if (!sendData(cmd, 2, 0x00)) |
| 170 | + return false; |
| 171 | + //Check if we have received any data from the tag |
| 172 | + delay(5); |
| 173 | + len = rxBytesReceived(); |
| 174 | + if (len == 16) { |
| 175 | + // READ 16 bytes into buffer |
| 176 | + if (readData(16, buffer)) |
| 177 | + success = true; |
| 178 | + } |
| 179 | + return success; |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | +uint8_t PN5180ISO14443::mifareBlockWrite16(uint8_t blockno, uint8_t *buffer) { |
| 184 | + uint8_t cmd[1]; |
| 185 | + // Clear RX CRC |
| 186 | + writeRegisterWithAndMask(CRC_RX_CONFIG, 0xFFFFFFFE); |
| 187 | + |
| 188 | + // Mifare write part 1 |
| 189 | + cmd[0] = 0xA0; |
| 190 | + cmd[1] = blockno; |
| 191 | + sendData(cmd, 2, 0x00); |
| 192 | + readData(1, cmd); |
| 193 | + |
| 194 | + // Mifare write part 2 |
| 195 | + sendData(buffer,16, 0x00); |
| 196 | + delay(10); |
| 197 | + |
| 198 | + // Read ACK/NAK |
| 199 | + readData(1, cmd); |
| 200 | + |
| 201 | + //Enable RX CRC calculation |
| 202 | + writeRegisterWithOrMask(CRC_RX_CONFIG, 0x1); |
| 203 | + return cmd[0]; |
| 204 | +} |
| 205 | + |
| 206 | +bool PN5180ISO14443::mifareHalt() { |
| 207 | + uint8_t cmd[1]; |
| 208 | + //mifare Halt |
| 209 | + cmd[0] = 0x50; |
| 210 | + cmd[1] = 0x00; |
| 211 | + sendData(cmd, 2, 0x00); |
| 212 | + return true; |
| 213 | +} |
| 214 | + |
| 215 | +uint8_t PN5180ISO14443::readCardSerial(uint8_t *buffer) { |
| 216 | + |
| 217 | + uint8_t response[10]; |
| 218 | + uint8_t uidLength; |
| 219 | + // Always return 10 bytes |
| 220 | + // Offset 0..1 is ATQA |
| 221 | + // Offset 2 is SAK. |
| 222 | + // UID 4 bytes : offset 3 to 6 is UID, offset 7 to 9 to Zero |
| 223 | + // UID 7 bytes : offset 3 to 9 is UID |
| 224 | + for (int i = 0; i < 10; i++) response[i] = 0; |
| 225 | + uidLength = activateTypeA(response, 1); |
| 226 | + if ((response[0] == 0xFF) && (response[1] == 0xFF)) |
| 227 | + return 0; |
| 228 | + // check for valid uid |
| 229 | + if ((response[3] == 0x00) && (response[4] == 0x00) && (response[5] == 0x00) && (response[6] == 0x00)) |
| 230 | + return 0; |
| 231 | + if ((response[3] == 0xFF) && (response[4] == 0xFF) && (response[5] == 0xFF) && (response[6] == 0xFF)) |
| 232 | + return 0; |
| 233 | + for (int i = 0; i < 7; i++) buffer[i] = response[i+3]; |
| 234 | + mifareHalt(); |
| 235 | + return uidLength; |
| 236 | +} |
| 237 | + |
| 238 | +bool PN5180ISO14443::isCardPresent() { |
| 239 | + uint8_t buffer[10]; |
| 240 | + return (readCardSerial(buffer) >=4); |
| 241 | +} |
| 242 | + |
0 commit comments