Guest User

pn352.cpp

a guest
Mar 17th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.54 KB | None | 0 0
  1. // PN532 library by adafruit/ladyada
  2. // MIT license
  3.  
  4. /* authenticateBlock, readMemoryBlock, writeMemoryBlock contributed  */
  5. /* by Seeed Technology Inc (www.seeedstudio.com)                                         */
  6. /* Modified history:                                                 */
  7. /* Nov 5,2012 modified by Frankie.Chu at Seeed.                      */
  8. /*   Add some program comments of some key functions;                */
  9. /*   Modified the basic interface functions spiwrite() and spiread() */
  10. /*     to be write() and read(),and they will call the functions of  */
  11. /*     class SPIClass but not to simulate timing of SPI;             */
  12. /*   Rename all of the functions to be like the abAbAb;              */
  13. /*   Modified the constructor of PN532 class;                        */
  14.  
  15. #include <Arduino.h>
  16. #include <SPI.h>
  17. #include "PN532.h"
  18.  
  19.  
  20. byte pn532ack[] = {
  21.   0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
  22. byte pn532response_firmwarevers[] = {
  23.   0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
  24.  
  25. #define PN532_PACK_BUFF_SIZE 64
  26. byte pn532_packetbuffer[PN532_PACK_BUFF_SIZE];
  27.  
  28. /*Construct PN532 object and construct PN532's own SPI interface object */
  29. /* at the same time.*/
  30. PN532::PN532(uint8_t cs):
  31. pn532_SPI()
  32. {
  33.   _cs = cs;
  34.   pinMode(_cs,OUTPUT);
  35.   digitalWrite(_cs,HIGH);
  36. }
  37.  
  38. void PN532::begin()
  39. {
  40.   pn532_SPI.begin();
  41.   /*The mode of the SPI interface should be set at mode0
  42.       according to the datasheet of PN532 */
  43.   pn532_SPI.setDataMode(SPI_MODE0);
  44.   pn532_SPI.setBitOrder(LSBFIRST);
  45.   /*Set the SPI frequency to be one sixteenth of the
  46.       frequency of the system clock*/
  47.   pn532_SPI.setClockDivider(SPI_CLOCK_DIV64);
  48.   digitalWrite(_cs, LOW);
  49.   delay(1000);
  50.   /*Not exactly sure why but we have to send a dummy command to get synced up*/
  51.   pn532_packetbuffer[0] = PN532_FIRMWAREVERSION;
  52.   /*Ignore response!*/
  53.   sendCommandCheckAck(pn532_packetbuffer, 1);
  54. }
  55.  
  56. uint32_t PN532::getFirmwareVersion(void)
  57. {
  58.   uint32_t response;
  59.  
  60.   pn532_packetbuffer[0] = PN532_FIRMWAREVERSION;
  61.  
  62.   if (!sendCommandCheckAck(pn532_packetbuffer, 1))
  63.     return 0;
  64.  
  65.   //Read data packet
  66.   read(pn532_packetbuffer, 12);
  67.   //Check some basic stuff
  68.   if (0 != strncmp((char *)pn532_packetbuffer, (char *)pn532response_firmwarevers, 6))
  69.   {
  70.     return 0;
  71.   }
  72.  
  73.   response = pn532_packetbuffer[6];
  74.   response <<= 8;
  75.   response |= pn532_packetbuffer[7];
  76.   response <<= 8;
  77.   response |= pn532_packetbuffer[8];
  78.   response <<= 8;
  79.   response |= pn532_packetbuffer[9];
  80.  
  81.   return response;
  82. }
  83.  
  84. /**********************************************************************/
  85. /*Function: Send command to PN532 with SPI and check the ACK.          */
  86. /*Parameter:-uint8_t* cmd,The pointer that saves the command code to be sent;*/
  87. /*          -uint8_t cmd_len,The number of bytes of the command;        */
  88. /*      -uint16_t timeout,default timeout is one second            */
  89. /*Return:   boolean,ture = send command successfully                   */
  90. boolean PN532::sendCommandCheckAck(uint8_t* cmd, uint8_t cmd_len, uint16_t timeout)
  91. {
  92.   uint16_t timer = 0;
  93.  
  94.   // write the command
  95.   writeCommand(cmd, cmd_len);
  96.  
  97.   // Wait for chip to say it's ready!
  98.   while (readSpiStatus() != PN532_SPI_READY) {
  99.     if (timeout != 0) {
  100.       timer+=10;
  101.       if (timer > timeout)
  102.         return false;
  103.     }
  104.     delay(10);
  105.   }
  106.  
  107.   // read acknowledgement
  108.   if (!checkSpiAck()) {
  109.     return false;
  110.   }
  111.  
  112.   timer = 0;
  113.   // Wait for chip to say its ready!
  114.   while (readSpiStatus() != PN532_SPI_READY) {
  115.     if (timeout != 0) {
  116.       timer+=10;
  117.       if (timer > timeout)
  118.         return false;
  119.     }
  120.     delay(10);
  121.   }
  122.  
  123.   return true; // ack'd command
  124. }
  125.  
  126. boolean PN532::SAMConfig(void)
  127. {
  128.   pn532_packetbuffer[0] = PN532_SAMCONFIGURATION;
  129.   pn532_packetbuffer[1] = 0x01; // normal mode;
  130.   pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second
  131.   pn532_packetbuffer[3] = 0x01; // use IRQ pin!
  132.  
  133.   if (! sendCommandCheckAck(pn532_packetbuffer, 4))
  134.     return false;
  135.  
  136.   // read data packet
  137.   read(pn532_packetbuffer, 8);
  138.  
  139.   return  (pn532_packetbuffer[5] == 0x15);
  140. }
  141.  
  142. /**********************************************************************/
  143. /*Function: Configure the NFC shield as initiator in the peer to peer */
  144. /*        commnunication and only the initiator set the baud rate.*/
  145. /*Parameter:-uint8_t baudrate,Any number from 0-2. 0 for 106kbps or
  146.         1 for 201kbps or 2 for 424kbps but 106kps is not supported yet;*/
  147. /*Return: boolean,ture = the shield finds the target and is configured */
  148. /*  as initiator successfully.                                 */
  149. uint32_t PN532::configurePeerAsInitiator(uint8_t baudrate)
  150. {
  151.  
  152.   pn532_packetbuffer[0] = PN532_INJUMPFORDEP;
  153.   pn532_packetbuffer[1] = 0x01; //Active Mode
  154.   pn532_packetbuffer[2] = baudrate;// Use 1 or 2. //0 i.e 106kps is not supported yet
  155.   pn532_packetbuffer[3] = 0x01; //Indicates Optional Payload is present
  156.  
  157.   //Polling request payload
  158.   pn532_packetbuffer[4] = 0x00;
  159.   pn532_packetbuffer[5] = 0xFF;
  160.   pn532_packetbuffer[6] = 0xFF;
  161.   pn532_packetbuffer[7] = 0x00;
  162.   pn532_packetbuffer[8] = 0x00;
  163.  
  164.   if (!sendCommandCheckAck(pn532_packetbuffer, 9))
  165.     return false;
  166.  
  167.   // read data packet
  168.   read(pn532_packetbuffer, 19+6);
  169.  
  170. #ifdef PN532DEBUG
  171.   Serial.println();
  172.   // check the response
  173.   Serial.println("PEER_INITIATOR");
  174.   for(uint8_t i=0;i<19+6;i++)
  175.   {
  176.     Serial.print(pn532_packetbuffer[i], HEX);
  177.     Serial.print(" ");
  178.   }
  179. #endif
  180.  
  181.   return (pn532_packetbuffer[7] == 0x00); //No error
  182.  
  183. }
  184. /**********************************************************************/
  185. /*Function: Transmit to the target and receive from the target.       */
  186. /*Parameter:-char* dataOut,data buffer to send;                       */
  187. /*      -char* dataIn,data buffer to save the data receive.       */
  188. /*Return:   boolean,ture = No error                                   */
  189. boolean PN532::initiatorTxRx(char* dataOut,char* dataIn)
  190. {
  191.   pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
  192.   pn532_packetbuffer[1] = 0x01; //Target 01
  193.  
  194.     for(uint8_t iter=(2+0);iter<(2+16);iter++)
  195.   {
  196.     pn532_packetbuffer[iter] = dataOut[iter-2]; //pack the data to send to target
  197.   }
  198.  
  199.   if (! sendCommandCheckAck(pn532_packetbuffer, 18))
  200.     return false;
  201.  
  202.   // read data packet
  203.   read(pn532_packetbuffer, 18+6);
  204.  
  205. #ifdef PN532_P2P_DEBUG
  206.   Serial.println();
  207.   // check the response
  208.   Serial.println("INITIATOR receive:");
  209.   for(uint8_t i=0;i<18+6;i++)
  210.   {
  211.     Serial.print(pn532_packetbuffer[i], HEX);
  212.     Serial.print(" ");
  213.   }
  214. #endif
  215.  
  216.   for(uint8_t iter=8;iter<(8+16);iter++)
  217.   {
  218.     dataIn[iter-8] = pn532_packetbuffer[iter]; //data received from target
  219.   }
  220.  
  221.   return (pn532_packetbuffer[7] == 0x00); //No error
  222. }
  223.  
  224. uint32_t PN532::configurePeerAsTarget()
  225. {
  226.   byte pbuffer[38] =      {
  227.     PN532_TGINITASTARGET,
  228.     0x00,
  229.     0x08, 0x00, //SENS_RES
  230.     0x12, 0x34, 0x56, //NFCID1
  231.     0x40, //SEL_RES
  232.     0x01, 0xFE, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, // POL_RES
  233.     0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  234.     0xFF, 0xFF,
  235.     0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, //NFCID3t: Change this to desired value
  236.     0x00, 0x00 //Length of general and historical bytes
  237.   };
  238.   for(uint8_t i = 0;i < 38;i ++)
  239.   {
  240.     pn532_packetbuffer[i] = pbuffer[i];
  241.   }
  242.   if (! sendCommandCheckAck(pn532_packetbuffer, 38))
  243.     return false;
  244.  
  245.   // read data packet
  246.   read(pn532_packetbuffer, 18+6);
  247.  
  248. #ifdef PN532DEBUG
  249.   Serial.println();
  250.   // check some basic stuff
  251.  
  252.   Serial.println("PEER_TARGET");
  253.   for(uint8_t i=0;i<18+6;i++)
  254.   {
  255.     Serial.print(pn532_packetbuffer[i], HEX);
  256.     Serial.println(" ");
  257.   }
  258. #endif
  259.  
  260.   return (pn532_packetbuffer[23] == 0x00); //No error as it received all response
  261. }
  262. /*Function: Recieve data first and then transmit data to the initiator*/
  263. uint32_t PN532::targetTxRx(char* dataOut,char* dataIn)
  264. {
  265.   /* Receiving from Initiator */
  266.   pn532_packetbuffer[0] = PN532_TGGETDATA;
  267.   if (! sendCommandCheckAck(pn532_packetbuffer, 1))
  268.     return false;
  269.  
  270.   // read data packet
  271.   read(pn532_packetbuffer, 18+6);
  272.  
  273. #ifdef PN532_P2P_DEBUG
  274.   Serial.println();
  275.   // check the response
  276.   Serial.println("TARGET RX:");
  277.   for(uint8_t i=0;i<18+6;i++)
  278.   {
  279.     Serial.print(pn532_packetbuffer[i], HEX);
  280.     Serial.print(" ");
  281.   }
  282. #endif
  283.  
  284.   for(uint8_t iter=8;iter<(8+16);iter++)
  285.   {
  286.     dataIn[iter-8] = pn532_packetbuffer[iter]; //data received from initiator
  287.   }
  288.  
  289.   /* Sending to Initiator */
  290.   if(pn532_packetbuffer[7] == 0x00) //If no errors in receiving, send data.
  291.   {
  292.     pn532_packetbuffer[0] = PN532_TGSETDATA;
  293.     for(uint8_t iter=(1+0);iter<(1+16);iter++)
  294.     {
  295.       pn532_packetbuffer[iter] = dataOut[iter-1]; //pack the data to send to target
  296.     }
  297.  
  298.     if (! sendCommandCheckAck(pn532_packetbuffer, 17))
  299.       return false;
  300.  
  301.     // read data packet
  302.     read(pn532_packetbuffer, 2+6);
  303.  
  304. #ifdef PN532_P2P_DEBUG
  305.     Serial.println();
  306.     // check the response
  307.     Serial.println("TARGET get response after transmiting: ");
  308.     for(uint8_t i=0;i<2+6;i++)
  309.     {
  310.       Serial.print(pn532_packetbuffer[i], HEX);
  311.       Serial.print(" ");
  312.     }
  313. #endif
  314.  
  315.     return (pn532_packetbuffer[7] == 0x00); //No error
  316.   }
  317.  
  318. }
  319.  
  320. uint32_t PN532::authenticateBlock(uint8_t cardnumber /*1 or 2*/,uint32_t cid /*Card NUID*/, uint8_t blockaddress /*0 to 63*/,uint8_t authtype/*Either KEY_A or KEY_B */, uint8_t * keys)
  321. {
  322.   pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
  323.   pn532_packetbuffer[1] = cardnumber;  // either card 1 or 2 (tested for card 1)
  324.   if(authtype == KEY_A)
  325.   {
  326.     pn532_packetbuffer[2] = PN532_AUTH_WITH_KEYA;
  327.   }
  328.   else
  329.   {
  330.     pn532_packetbuffer[2] = PN532_AUTH_WITH_KEYB;
  331.   }
  332.   pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card
  333.  
  334.   pn532_packetbuffer[4] = keys[0];
  335.   pn532_packetbuffer[5] = keys[1];
  336.   pn532_packetbuffer[6] = keys[2];
  337.   pn532_packetbuffer[7] = keys[3];
  338.   pn532_packetbuffer[8] = keys[4];
  339.   pn532_packetbuffer[9] = keys[5];
  340.  
  341.   pn532_packetbuffer[10] = ((cid >> 24) & 0xFF);
  342.   pn532_packetbuffer[11] = ((cid >> 16) & 0xFF);
  343.   pn532_packetbuffer[12] = ((cid >> 8) & 0xFF);
  344.   pn532_packetbuffer[13] = ((cid >> 0) & 0xFF);
  345.  
  346.   if (! sendCommandCheckAck(pn532_packetbuffer, 14))
  347.     return false;
  348.  
  349.   // read data packet
  350.   read(pn532_packetbuffer, 2+6);
  351.  
  352. #ifdef PN532DEBUG
  353.   for(int iter=0;iter<14;iter++)
  354.   {
  355.     Serial.print(pn532_packetbuffer[iter], HEX);
  356.     Serial.print(" ");
  357.   }
  358.   Serial.println();
  359.   // check some basic stuff
  360.  
  361.   Serial.println("AUTH");
  362.   for(uint8_t i=0;i<2+6;i++)
  363.   {
  364.     Serial.print(pn532_packetbuffer[i], HEX);
  365.     Serial.println(" ");
  366.   }
  367. #endif
  368.  
  369.   if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
  370.   {
  371.     return true;
  372.   }
  373.   else
  374.   {
  375.     return false;
  376.   }
  377.  
  378. }
  379. /****************************************************************************/
  380. /*Function: Read a block(16 bytes) from the tag and stores in the parameter.*/
  381. /*Parameter:-uint8_t cardnumber,can be 1 or 2;                              */
  382. /*          -blockaddress,range from 0 to 63;                               */
  383. /*      -uint8_t* block,will save 16bytes that read from tag.           */
  384. /*Return:   boolean                                         */
  385. boolean PN532::readMemoryBlock(uint8_t cardnumber,uint8_t blockaddress,uint8_t * block)
  386. {
  387.   pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
  388.   pn532_packetbuffer[1] = cardnumber;  // either card 1 or 2 (tested for card 1)
  389.   pn532_packetbuffer[2] = PN532_MIFARE_READ;
  390.   pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card
  391.  
  392.   if (! sendCommandCheckAck(pn532_packetbuffer, 4))
  393.     return false;
  394.  
  395.   // read data packet
  396.   read(pn532_packetbuffer, 18+6);
  397.   // check some basic stuff
  398. #ifdef PN532DEBUG
  399.   Serial.println("READ");
  400. #endif
  401.   for(uint8_t i=8;i<18+6;i++)
  402.   {
  403.     block[i-8] = pn532_packetbuffer[i];
  404. #ifdef PN532DEBUG
  405.     Serial.print(pn532_packetbuffer[i], HEX);
  406.     Serial.print(" ");
  407. #endif
  408.   }
  409.   if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
  410.   {
  411.     return true; //read successful
  412.   }
  413.   else
  414.  
  415.   {
  416.     return false;
  417.   }
  418.  
  419. }
  420.  
  421.  
  422. /****************************************************************************/
  423. /*Function: Write a block(16 bytes) to the tag.                             */
  424. /*Parameter:-uint8_t cardnumber,can be 1 or 2;                              */
  425. /*          -blockaddress,range from 0 to 63;                               */
  426. /*      -uint8_t* block,saves 16bytes that will write to the tag.       */
  427. /*Return:  boolean                              */
  428. /*Note:Donot write to Sector Trailer Block unless you know what you are doing.*/
  429. boolean PN532::writeMemoryBlock(uint8_t cardnumber,uint8_t blockaddress,uint8_t * block)
  430. {
  431.   pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
  432.   pn532_packetbuffer[1] = cardnumber;  // either card 1 or 2 (tested for card 1)
  433.   pn532_packetbuffer[2] = PN532_MIFARE_WRITE;
  434.   pn532_packetbuffer[3] = blockaddress;
  435.  
  436.   for(uint8_t byte=0; byte <16; byte++)
  437.   {
  438.     pn532_packetbuffer[4+byte] = block[byte];
  439.   }
  440.  
  441.   if (! sendCommandCheckAck(pn532_packetbuffer, 20))
  442.     return false;
  443.   // read data packet
  444.   read(pn532_packetbuffer, 2+6);
  445.  
  446. #ifdef PN532DEBUG
  447.   // check some basic stuff
  448.   Serial.println("WRITE");
  449.   for(uint8_t i=0;i<2+6;i++)
  450.   {
  451.     Serial.print(pn532_packetbuffer[i], HEX);
  452.     Serial.println(" ");
  453.   }
  454. #endif
  455.  
  456.   if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
  457.   {
  458.     return true; //write successful
  459.   }
  460.   else
  461.   {
  462.     return false;
  463.   }
  464. }
  465.  
  466. uint32_t PN532::readPassiveTargetID(uint8_t cardbaudrate)
  467. {
  468.   uint32_t cid;
  469.  
  470.   pn532_packetbuffer[0] = PN532_INLISTPASSIVETARGET;
  471.   pn532_packetbuffer[1] = 1;  // max 1 cards at once (we can set this to 2 later)
  472.   pn532_packetbuffer[2] = cardbaudrate;
  473.  
  474.   if (! sendCommandCheckAck(pn532_packetbuffer, 3))
  475.     return 0x0;  // no cards read
  476.  
  477.   // read data packet
  478.   read(pn532_packetbuffer, 20);
  479.   // check some basic stuff
  480.  
  481.   Serial.print("Found ");
  482.   Serial.print(pn532_packetbuffer[7], DEC);
  483.   Serial.println(" tags");
  484.   if (pn532_packetbuffer[7] != 1)
  485.     return 0;
  486.  
  487.   uint16_t sens_res = pn532_packetbuffer[9];
  488.   sens_res <<= 8;
  489.   sens_res |= pn532_packetbuffer[10];
  490.   Serial.print("Sens Response: 0x");  
  491.   Serial.println(sens_res, HEX);
  492.   Serial.print("Sel Response: 0x");  
  493.   Serial.println(pn532_packetbuffer[11], HEX);
  494.   cid = 0;
  495.   for (uint8_t i=0; i< pn532_packetbuffer[12]; i++) {
  496.     cid <<= 8;
  497.     cid |= pn532_packetbuffer[13+i];
  498.     Serial.print(" 0x");
  499.     Serial.print(pn532_packetbuffer[13+i], HEX);
  500.   }
  501.  
  502. #ifdef PN532DEBUG
  503.   Serial.println("TargetID");
  504.   for(uint8_t i=0;i<20;i++)
  505.   {
  506.     Serial.print(pn532_packetbuffer[i], HEX);
  507.     Serial.println(" ");
  508.   }
  509. #endif  
  510.   return cid;
  511. }
  512.  
  513. /************** low level SPI ********/
  514. /*Function:Transmit a byte to PN532 through the SPI interface. */
  515. inline void PN532::write(uint8_t _data)
  516. {
  517.   pn532_SPI.transfer(_data);
  518. }
  519.  
  520. /*Function:Receive a byte from PN532 through the SPI interface */
  521. inline uint8_t PN532::read(void)
  522. {
  523.   uint8_t data_ = pn532_SPI.transfer(0);
  524.   return data_;
  525. }
  526.  
  527.  
  528. /************** mid level SPI */
  529.  
  530. uint8_t PN532::readSpiStatus(void)
  531. {
  532.   digitalWrite(_cs, LOW);
  533.   delay(2);
  534.   write(PN532_SPI_STATREAD);
  535.   uint8_t status = read();
  536.   digitalWrite(_cs, HIGH);
  537.   return status;
  538. }
  539.  
  540. /************** high level SPI */
  541. boolean PN532::checkSpiAck()
  542. {
  543.   uint8_t ackbuff[6];
  544.  
  545.   read(ackbuff, 6);
  546.  
  547.   return (0 == strncmp((char *)ackbuff, (char *)pn532ack, 6));
  548. }
  549. /**********************************************************************/
  550. /*Function: Read n bytes data and it stores in the parameter .        */
  551. /*Parameter:-uint8_t* buff,saves the data read from PN532;            */
  552. /*      -uint8_t n,tells it wll read n bytes.                     */
  553. /*Return:  void                                                       */
  554. void PN532::read(uint8_t* buff, uint8_t n)
  555. {
  556.   digitalWrite(_cs, LOW);
  557.   delay(2);
  558.   write(PN532_SPI_DATAREAD);
  559.  
  560. #ifdef PN532DEBUG
  561.   Serial.print("Reading: ");
  562. #endif
  563.   for (uint8_t i=0; i < n; i ++)
  564.   {
  565.     delay(1);
  566.     buff[i] = read();
  567. #ifdef PN532DEBUG
  568.     Serial.print(" 0x");
  569.     Serial.print(buff[i], HEX);
  570. #endif
  571.   }
  572.  
  573. #ifdef PN532DEBUG
  574.   Serial.println();
  575. #endif
  576.  
  577.   digitalWrite(_cs, HIGH);
  578. }
  579.  
  580. void PN532::writeCommand(uint8_t* cmd, uint8_t cmd_len)
  581. {
  582.   uint8_t checksum;
  583.  
  584.   cmd_len++;
  585.  
  586. #ifdef PN532DEBUG
  587.   Serial.print("\nSending: ");
  588. #endif
  589.  
  590.   digitalWrite(_cs, LOW);
  591.   delay(2);     // or whatever the delay is for waking up the board
  592.   write(PN532_SPI_DATAWRITE);
  593.  
  594.   checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
  595.   write(PN532_PREAMBLE);
  596.   write(PN532_PREAMBLE);
  597.   write(PN532_STARTCODE2);
  598.  
  599.   write(cmd_len);
  600.   uint8_t cmdlen_1=~cmd_len + 1;
  601.   write(cmdlen_1);
  602.  
  603.   write(PN532_HOSTTOPN532);
  604.   checksum += PN532_HOSTTOPN532;
  605.  
  606. #ifdef PN532DEBUG
  607.   Serial.print(" 0x");
  608.   Serial.print(PN532_PREAMBLE, HEX);
  609.   Serial.print(" 0x");
  610.   Serial.print(PN532_PREAMBLE, HEX);
  611.   Serial.print(" 0x");
  612.   Serial.print(PN532_STARTCODE2, HEX);
  613.   Serial.print(" 0x");
  614.   Serial.print(cmd_len, HEX);
  615.   Serial.print(" 0x");
  616.   Serial.print(cmdlen_1, HEX);
  617.   Serial.print(" 0x");
  618.   Serial.print(PN532_HOSTTOPN532, HEX);
  619. #endif
  620.  
  621.   for (uint8_t i=0; i<cmd_len-1; i++)
  622.   {
  623.     write(cmd[i]);
  624.     checksum += cmd[i];
  625. #ifdef PN532DEBUG
  626.     Serial.print(" 0x");
  627.     Serial.print(cmd[i], HEX);
  628. #endif
  629.   }
  630.   uint8_t checksum_1=~checksum;
  631.   write(checksum_1);
  632.   write(PN532_POSTAMBLE);
  633.   digitalWrite(_cs, HIGH);
  634.  
  635. #ifdef PN532DEBUG
  636.   Serial.print(" 0x");
  637.   Serial.print(checksum_1, HEX);
  638.   Serial.print(" 0x");
  639.   Serial.print(PN532_POSTAMBLE, HEX);
  640.   Serial.println();
  641. #endif
  642. }
Add Comment
Please, Sign In to add comment