Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Transmit data from UART (Arduino + NFC reader) and Planner
- // Author: Jamiver Montaner
- // Date: 15 Dec 2012
- // License: To be decided
- const PN532_PREAMBLE= 0x00;
- const PN532_STARTCODE1= 0x00;
- const PN532_STARTCODE2= 0xFF;
- const PN532_POSTAMBLE= 0x00;
- const PN532_HOSTTOPN532= 0xD4;
- const PN532_FIRMWAREVERSION= 0x02;
- const PN532_GETGENERALSTATUS= 0x04;
- const PN532_SAMCONFIGURATION= 0x14;
- const PN532_INLISTPASSIVETARGET= 0x4A;
- const PN532_RFCONFIGURATION= 0x32;
- const PN532_INDATAEXCHANGE= 0x40;
- const PN532_MIFARE_READ= 0x30;
- const PN532_MIFARE_WRITE= 0xA0;
- const PN532_AUTH_WITH_KEYA= 0x60;
- const PN532_AUTH_WITH_KEYB= 0x61;
- const PN532_WAKEUP= 0x55;
- const PN532_SPI_STATREAD= 0x02;
- const PN532_SPI_DATAWRITE= 0x01;
- const PN532_SPI_DATAREAD= 0x03;
- const PN532_SPI_READY= 0x01;
- const PN532_MIFARE_ISO14443A= 0x0;
- const PN532_MAX_RETRIES= 0x05;
- local pn532ack = [0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00];
- local pn532response_firmwarevers = [0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03];
- local tagId=""
- function intToHex(inputByte) //assumes 1 byte value 0-255
- {
- local result = "";
- if ((inputByte>255)||(inputByte<0)) return "00";
- local digit= inputByte/16; //high nibble
- if (digit<10) result=digit.tostring();
- else {
- digit=(0x41 + digit-10) //"A"..."F"
- result=digit.tochar();
- }
- digit= inputByte%16; //low nibble
- if (digit<10) result+=digit.tostring();
- else {
- digit=(0x41 + digit-10) //"A"..."F"
- result+=digit.tochar();
- }
- return result;
- }
- function begin() {
- hardware.pin1.write(0);//pull CS low
- imp.sleep(1);
- // not exactly sure why but we have to send a dummy command to get synced up
- sendCommandCheckAck([PN532_FIRMWAREVERSION], 1, 100);
- // ignore response!
- }
- function RFConfiguration(mxRtyPassiveActivation) {
- sendCommandCheckAck([PN532_RFCONFIGURATION, PN532_MAX_RETRIES, 0xFF, 0x01, mxRtyPassiveActivation], 5,100);
- server.show("RFConfiguration")
- // ignore response!
- }
- function startNFC(){
- begin();
- RFConfiguration(0x14); // default is 0xFF (try forever; ultimately it does time out but after a long while
- local versiondata = getFirmwareVersion();
- if (! versiondata) {
- server.show("Didn't find PN53x board");
- }else {
- // Got ok data, print it out!
- server.show("Found chip PN5");
- server.show(versiondata);
- // nfc.SAMConfig();
- }
- }
- function getFirmwareVersion() {
- local response;
- local buf=[0,1,2,3,4,5,6,7,8,9,10,11];
- if (! sendCommandCheckAck([PN532_FIRMWAREVERSION], 1,100))
- return 0;
- // read data packet
- readspidata(buf, 12);
- // check some basic stuff
- for (local i=0; i<6; i++) {
- if (buf[i] != pn532response_firmwarevers[i]) return false;
- }
- response = buf[6];
- response = response<<8;
- response += buf[7];
- response = response<<8;
- response += buf[8];
- response = response<<8;
- response += buf[9];
- return response;
- }
- function sendCommandCheckAck(cmd, cmdlen, timeout) {
- local timer = 0;
- // write the command
- spiwritecommand(cmd, cmdlen);
- // Wait for chip to say its ready!
- while (readspistatus() != PN532_SPI_READY) {
- if (timeout != 0) {
- timer+=10;
- if (timer > timeout){
- server.show("No response READY");
- return false;
- }
- }
- imp.sleep(0.01);
- }
- // read acknowledgement
- if (!spi_readack()) {
- server.show("Wrong ACK");
- return false;
- }
- timer = 0;
- // Wait for chip to say its ready!
- while (readspistatus() != PN532_SPI_READY) {
- if (timeout != 0) {
- timer+=10;
- if (timer > timeout)
- return false;
- }
- imp.sleep(0.01);
- }
- return true; // ack'd command
- }
- function spi_readack() {
- local ackbuff =[0,1,2,3,4,5];
- readspidata(ackbuff, 6);
- for (local i=0; i<6; i++) {
- if (ackbuff[i] != pn532ack[i]) return false;
- }
- return true;
- }
- function readspidata(buff, length) {
- hardware.pin1.write(0);//pull CS low
- imp.sleep(0.002);
- spiwrite(PN532_SPI_DATAREAD); //read leading byte DR and discard
- for (local i=0; i<length; i++) {
- imp.sleep(0.001);
- buff[i] = spiwrite(PN532_SPI_STATREAD);
- server.show(buff[i]);
- }
- hardware.pin1.write(1);//pull CS high
- }
- function readspistatus() {
- hardware.pin1.write(0);//pull CS low
- imp.sleep(0.002);
- spiwrite(PN532_SPI_STATREAD);
- local value= hardware.spi257.read(1);
- hardware.pin1.write(1);//pull CS high
- return value;
- }
- function spiwritecommand(cmd, cmdlen) {
- local checksum;
- hardware.pin1.write(0);//pull CS low
- imp.sleep(0.002);
- cmdlen++;
- spiwrite(PN532_SPI_DATAWRITE);
- checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
- spiwrite(PN532_PREAMBLE);
- spiwrite(PN532_PREAMBLE);
- spiwrite(PN532_STARTCODE2);
- spiwrite(cmdlen);
- local cmdlen_1=256-cmdlen;
- spiwrite(cmdlen_1);
- spiwrite(PN532_HOSTTOPN532);
- checksum += PN532_HOSTTOPN532;
- for (local i=0; i<cmdlen-1; i++) {
- spiwrite(cmd[i]);
- checksum += cmd[i];
- }
- checksum%=256;
- local checksum_1=255-checksum;
- spiwrite(checksum_1);
- spiwrite(PN532_POSTAMBLE);
- hardware.pin1.write(1);//pull CS high
- }
- function spiwrite(byte){
- local tempString="\\x"+intToHex(byte);
- hardware.spi257.write(tempString);
- local resp=hardware.spi257.read(1);
- foreach(i, val in resp){
- server.show(tempString +"-> (" +resp.len() + ") ");
- server.log(val);
- }
- return resp; //read n bytes
- }
- function initSPI()
- {
- hardware.configure(SPI_257);
- hardware.spi257.configure(LSB_FIRST| CLOCK_IDLE_HIGH , 4000); // Configure SPI_257 at about 4MHz | CLOCK_IDLE_LOW
- hardware.pin1.configure(DIGITAL_OUT); //Configure the chip select pin
- hardware.pin1.write(1);//pull CS high
- imp.sleep(0.1);//wait 100 ms
- hardware.pin1.write(0); //pull CS low to start the transmission of temp data
- server.show("init ok");
- }
- // This is where our program actually starts! Previous stuff was all function and variable declaration.
- // This'll configure our impee. It's name is "UartCrossAir", and it has both an input and output to be connected:
- imp.configure("remoteNFC", [], []);
- initSPI(); // Initialize the SPI, called just once
- startNFC();
- // The end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement