Advertisement
Guest User

eImp_SPI_PN532

a guest
Dec 21st, 2012
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Transmit data between NFC reader and Planner
  2.  
  3.  
  4. const PN532_PREAMBLE= 0x00;
  5. const PN532_STARTCODE1= 0x00;
  6. const PN532_STARTCODE2= 0xFF;
  7. const PN532_POSTAMBLE= 0x00;
  8.  
  9. const PN532_HOSTTOPN532= 0xD4;
  10.  
  11. const PN532_FIRMWAREVERSION= 0x02;
  12. const PN532_GETGENERALSTATUS= 0x04;
  13. const PN532_SAMCONFIGURATION= 0x14;
  14. const PN532_INLISTPASSIVETARGET= 0x4A;
  15. const PN532_RFCONFIGURATION= 0x32;
  16. const PN532_INDATAEXCHANGE= 0x40;
  17. const PN532_MIFARE_READ= 0x30;
  18. const PN532_MIFARE_WRITE= 0xA0;
  19.  
  20. const PN532_AUTH_WITH_KEYA= 0x60;
  21. const PN532_AUTH_WITH_KEYB= 0x61;
  22.  
  23.  
  24. const PN532_WAKEUP= 0x55;
  25.  
  26. const PN532_SPI_STATREAD= 0x02;
  27. const PN532_SPI_DATAWRITE= 0x01;
  28. const PN532_SPI_DATAREAD= 0x03;
  29. const PN532_SPI_READY= 0x01;
  30.  
  31. const PN532_MIFARE_ISO14443A= 0x0;
  32. const PN532_MAX_RETRIES= 0x05;
  33. local pn532ack = [0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00];
  34. local pn532response_firmwarevers = [0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03];
  35.  
  36.  
  37. local tagId=""
  38.  
  39. function intToHex(inputByte) //assumes 1 byte value 0-255
  40. {
  41. local result = "";
  42. if ((inputByte>255)||(inputByte<0)) return "00";
  43. local digit= inputByte/16; //high nibble
  44. if (digit<10) result=digit.tostring();
  45. else {
  46. digit=(0x41 + digit-10) //"A"..."F"
  47. result=digit.tochar();
  48. }
  49. digit= inputByte%16; //low nibble
  50. if (digit<10) result+=digit.tostring();
  51. else {
  52. digit=(0x41 + digit-10) //"A"..."F"
  53. result+=digit.tochar();
  54. }
  55. return result;
  56. }
  57.  
  58.  
  59. function begin() {
  60. hardware.pin1.write(0);//pull CS low
  61. imp.sleep(1);
  62. // not exactly sure why but we have to send a dummy command to get synced up
  63. sendCommandCheckAck([PN532_FIRMWAREVERSION], 1, 100);
  64. // ignore response!
  65. }
  66.  
  67. function RFConfiguration(mxRtyPassiveActivation) {
  68. sendCommandCheckAck([PN532_RFCONFIGURATION, PN532_MAX_RETRIES, 0xFF, 0x01, mxRtyPassiveActivation], 5,100);
  69. server.show("RFConfiguration")
  70. // ignore response!
  71. }
  72.  
  73. function startNFC(){
  74. begin();
  75. RFConfiguration(0x14); // default is 0xFF (try forever; ultimately it does time out but after a long while
  76. local versiondata = getFirmwareVersion();
  77. if (! versiondata) {
  78. server.show("Didn't find PN53x board");
  79. }else {
  80. // Got ok data, print it out!
  81. server.show("Found chip PN5");
  82. server.show(versiondata);
  83.  
  84. // nfc.SAMConfig();
  85. }
  86. }
  87.  
  88. function getFirmwareVersion() {
  89. local response;
  90. local buf=[0,1,2,3,4,5,6,7,8,9,10,11];
  91.  
  92. if (! sendCommandCheckAck([PN532_FIRMWAREVERSION], 1,100))
  93. return 0;
  94. // read data packet
  95. readspidata(buf, 12);
  96. // check some basic stuff
  97. for (local i=0; i<6; i++) {
  98. if (buf[i] != pn532response_firmwarevers[i]) return false;
  99. }
  100.  
  101. response = buf[6];
  102. response = response<<8;
  103. response += buf[7];
  104. response = response<<8;
  105. response += buf[8];
  106. response = response<<8;
  107. response += buf[9];
  108.  
  109. return response;
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. function sendCommandCheckAck(cmd, cmdlen, timeout) {
  118. local timer = 0;
  119.  
  120. // write the command
  121. spiwritecommand(cmd, cmdlen);
  122.  
  123. // Wait for chip to say its ready!
  124. while (readspistatus() != PN532_SPI_READY) {
  125. if (timeout != 0) {
  126. timer+=10;
  127. if (timer > timeout){
  128. server.show("No response READY");
  129. return false;
  130. }
  131. }
  132. imp.sleep(0.01);
  133. }
  134.  
  135. // read acknowledgement
  136. if (!spi_readack()) {
  137. server.show("Wrong ACK");
  138. return false;
  139. }
  140.  
  141. timer = 0;
  142. // Wait for chip to say its ready!
  143. while (readspistatus() != PN532_SPI_READY) {
  144. if (timeout != 0) {
  145. timer+=10;
  146. if (timer > timeout)
  147. return false;
  148. }
  149. imp.sleep(0.01);
  150. }
  151.  
  152. return true; // ack'd command
  153. }
  154.  
  155.  
  156. function spi_readack() {
  157. local ackbuff =[0,1,2,3,4,5];
  158.  
  159. readspidata(ackbuff, 6);
  160. for (local i=0; i<6; i++) {
  161. if (ackbuff[i] != pn532ack[i]) return false;
  162. }
  163. return true;
  164. }
  165.  
  166. function readspidata(buff, length) {
  167. hardware.pin1.write(0);//pull CS low
  168. imp.sleep(0.002);
  169. spiwrite(PN532_SPI_DATAREAD); //read leading byte DR and discard
  170.  
  171. for (local i=0; i<length; i++) {
  172. imp.sleep(0.001);
  173. buff[i] = spiwrite(PN532_SPI_STATREAD);
  174. server.show(buff[i]);
  175. }
  176. hardware.pin1.write(1);//pull CS high
  177.  
  178. }
  179.  
  180. function readspistatus() {
  181. hardware.pin1.write(0);//pull CS low
  182. imp.sleep(0.002);
  183. spiwrite(PN532_SPI_STATREAD);
  184. local value= hardware.spi257.read(1);
  185. hardware.pin1.write(1);//pull CS high
  186. return value;
  187.  
  188. }
  189.  
  190.  
  191. function spiwritecommand(cmd, cmdlen) {
  192. local checksum;
  193. hardware.pin1.write(0);//pull CS low
  194. imp.sleep(0.002);
  195. cmdlen++;
  196.  
  197. spiwrite(PN532_SPI_DATAWRITE);
  198.  
  199. checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
  200. spiwrite(PN532_PREAMBLE);
  201. spiwrite(PN532_PREAMBLE);
  202. spiwrite(PN532_STARTCODE2);
  203.  
  204. spiwrite(cmdlen);
  205. local cmdlen_1=256-cmdlen;
  206. spiwrite(cmdlen_1);
  207.  
  208. spiwrite(PN532_HOSTTOPN532);
  209. checksum += PN532_HOSTTOPN532;
  210.  
  211. for (local i=0; i<cmdlen-1; i++) {
  212. spiwrite(cmd[i]);
  213. checksum += cmd[i];
  214. }
  215. checksum%=256;
  216. local checksum_1=255-checksum;
  217. spiwrite(checksum_1);
  218. spiwrite(PN532_POSTAMBLE);
  219.  
  220. hardware.pin1.write(1);//pull CS high
  221.  
  222. }
  223.  
  224. function spiwrite(byte){
  225.  
  226. local tempString="\\x"+intToHex(byte);
  227. hardware.spi257.write(tempString);
  228. local resp=hardware.spi257.read(1);
  229. foreach(i, val in resp){
  230. server.show(tempString +"-> (" +resp.len() + ") ");
  231. server.log(val);
  232. }
  233.  
  234. return resp; //read n bytes
  235.  
  236.  
  237. }
  238.  
  239.  
  240.  
  241. function initSPI()
  242. {
  243. hardware.configure(SPI_257);
  244. hardware.spi257.configure(LSB_FIRST| CLOCK_IDLE_HIGH , 4000); // Configure SPI_257 at about 4MHz | CLOCK_IDLE_LOW
  245. hardware.pin1.configure(DIGITAL_OUT); //Configure the chip select pin
  246. hardware.pin1.write(1);//pull CS high
  247. imp.sleep(0.1);//wait 100 ms
  248. hardware.pin1.write(0); //pull CS low to start the transmission of temp data
  249. server.show("init ok");
  250. }
  251.  
  252.  
  253.  
  254.  
  255. // This is where our program actually starts! Previous stuff was all function and variable declaration.
  256. // This'll configure our impee. It's name is "UartCrossAir", and it has both an input and output to be connected:
  257. imp.configure("remoteNFC", [], []);
  258. initSPI(); // Initialize the SPI, called just once
  259. startNFC();
  260.  
  261. // The end
Advertisement
RAW Paste Data Copied
Advertisement