Advertisement
Guest User

Untitled

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