Adafruit NFC shield
#21
Posted 24 July 2012 - 09:57 AM
here are some articles on bitwise operations using c#
http://www.blackwasp...BitwiseOps.aspx
http://www.codeproje...operations-in-C
As far as bitbanging (manually setting pin levels) i wouldn't bother as it negates the benefit of the managed SPI code, you should be able to divine the relevant bytes to send based on the defines in the header file in the repository i linked to on github
(Adafruit_PN532.h)
Nak.
#22
Posted 25 July 2012 - 11:42 AM
#23
Posted 25 July 2012 - 01:47 PM
#24
Posted 25 July 2012 - 02:19 PM
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using NFC; using System.Text; namespace GitHub { public class Program { //static SPI PN532 = new SPI(new SPI.Configuration(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_NONE, true, 0, 0, false, true, 400, SPI.SPI_module.SPI1)); static OutputPort NSS = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D10, true); static OutputPort CLK = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D13, true); static OutputPort MOSI = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D11, true); static OutputPort MISO = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D12, true); static byte[] pn532ack = { 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00 }; static byte[] pn532response_firmwarevers = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03}; const byte PN532_PACKBUFFSIZ = 64; static byte[] pn532_packetbuffer=new byte[PN532_PACKBUFFSIZ]; public static void Main() { begin(); uint versiondata = GetFirmwareVersion(); while (versiondata==0) { Debug.Print("\nDidn't find PN53x board"); // halt } // Got ok data, print it out! Debug.Print("\nFound chip PN5"); Debug.Print("\n"+((versiondata>>24) & 0xFF)); Debug.Print("Firmware ver. "); Debug.Print("\n "+((versiondata>>16) & 0xFF).ToString()); Debug.Print("."); Debug.Print("\n "+((versiondata>>8) & 0xFF)); } static void begin() { NSS.Write(false); ; Thread.Sleep(1000); // not exactly sure why but we have to send a dummy command to get synced up pn532_packetbuffer[0] = Constants.PN532_COMMAND_GETFIRMWAREVERSION; sendCommandCheckAck(pn532_packetbuffer, 1,3000); // ignore response! } static Boolean sendCommandCheckAck(byte[] buffer,byte buffer_length,int Timeout) { int Timer=0; // write the command WRITE_SPI_COMMAND(buffer, buffer_length); // Wait for chip to say its ready! while (READ_SPI_STATUS() != Constants.PN532_SPI_READY) { if (Timeout != 0) { Timer+=10; if (Timer > Timeout) return false; } Thread.Sleep(10); } // read acknowledgement if (!SPI_READ_ACK()) { return false; } Timer = 0; // Wait for chip to say its ready! while (READ_SPI_STATUS() != Constants.PN532_SPI_READY) { if (Timeout != 0) { Timer+=10; if (Timer > Timeout) return false; } Thread.Sleep(10); } return true; } static uint GetFirmwareVersion() { uint response; pn532_packetbuffer[0] = Constants.PN532_COMMAND_GETFIRMWAREVERSION; if (!sendCommandCheckAck(pn532_packetbuffer, 1,3000)) return 0; SPI_READ_DATA(pn532_packetbuffer, 12); if (0 != String.Compare(pn532_packetbuffer.ToString(), pn532response_firmwarevers.ToString())) { return 0; } response = pn532_packetbuffer[6]; response <<= 8; response |= pn532_packetbuffer[7]; response <<= 8; response |= pn532_packetbuffer[8]; response <<= 8; response |= pn532_packetbuffer[9]; return response; } static void WRITE_SPI_COMMAND(byte[] buffer,byte buffer_length) { byte checksum; buffer_length++; Debug.Print("\nSending....."); NSS.Write(false); ; Thread.Sleep(2); // or whatever the delay is for waking up the board SPI_WRITE(Constants.PN532_SPI_DATAWRITE); checksum = Constants.PN532_PREAMBLE + Constants.PN532_PREAMBLE + Constants.PN532_STARTCODE2; SPI_WRITE(Constants.PN532_PREAMBLE); SPI_WRITE(Constants.PN532_PREAMBLE); SPI_WRITE(Constants.PN532_STARTCODE2); SPI_WRITE(buffer_length); SPI_WRITE((byte)(~buffer_length+1)); SPI_WRITE(Constants.PN532_HOSTTOPN532); checksum += Constants.PN532_HOSTTOPN532; Debug.Print(Constants.PN532_PREAMBLE.ToString()); Debug.Print(Constants.PN532_PREAMBLE.ToString()); Debug.Print(Constants.PN532_STARTCODE2.ToString()); Debug.Print(buffer_length.ToString()); Debug.Print(((byte)(~buffer_length + 1)).ToString()); Debug.Print(Constants.PN532_HOSTTOPN532.ToString()); for (byte i = 0; i < buffer_length - 1; i++) { SPI_WRITE(buffer[i]); checksum += buffer[i]; Debug.Print(buffer[i].ToString()); } SPI_WRITE((byte)~checksum); SPI_WRITE(Constants.PN532_POSTAMBLE); NSS.Write(true); Debug.Print((byte)(~buffer_length).ToString()); Debug.Print( Constants.PN532_POSTAMBLE.ToString()); } static byte READ_SPI_STATUS() { NSS.Write(false); Thread.Sleep(2); SPI_WRITE(Constants.PN532_SPI_STATREAD); // read byte byte x = SPI_READ(); NSS.Write(true) ; return x; } static Boolean SPI_READ_ACK() { byte[] buffer=new byte[6]; SPI_READ_DATA(buffer,6); return (0 == String.Compare(buffer.ToString(), pn532ack.ToString())); } static void SPI_READ_DATA(byte[] buffer, byte n) { NSS.Write(false); Thread.Sleep(2); SPI_WRITE(Constants.PN532_SPI_DATAREAD); Debug.Print("\nReading....."); for (uint i = 0; i < n; i++) { Thread.Sleep(1); buffer[i] = SPI_READ(); Debug.Print(buffer[i].ToString()); } NSS.Write(true); } static void SPI_WRITE(byte constant) { int i,x=1; CLK.Write(true); for (i = 0; i < 8; i++) { CLK.Write(false); if ((constant & (x<<(i)))==1) { MOSI.Write(true) ; } else { MOSI.Write(false); } CLK.Write(true); } }
I have all the constants in a .cs attached to the proyect.They are the same as the .h file
The code is only to try to have any kind of response from the PN532.
#25
Posted 25 July 2012 - 04:25 PM
#26
Posted 26 July 2012 - 07:38 AM
static byte SPI_READ() { int i,x = 0, z = 1; CLK.Write(true); for (i = 0; i < 8; i++) { if (MISO.Read()) { x |= (z<<(i)); } CLK.Write(false); CLK.Write(true); } return ((byte)x); }
#27
Posted 26 July 2012 - 11:43 AM
I tried it again with I2C, with the GitHub code. But it neither works........here is the code i used:
namespace GitHub_I2C { public class Program { const int I2CAddress = 0x48; const int I2CTimeout = 1000; const int I2CClockRateKhz = 200; static byte[] write_buffer; static byte[] read_buffer; static I2CDevice PN532 = new I2CDevice(new I2CDevice.Configuration(I2CAddress, I2CClockRateKhz)); static OutputPort Reset = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D3, false); static InputPort Irq= new InputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D2,true,Port.ResistorMode.Disabled); static byte[] pn532ack = { 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00 }; static byte[] pn532response_firmwarevers = { 0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03 }; const byte PN532_PACKBUFFSIZ = 64; static byte[] pn532_packetbuffer = new byte[PN532_PACKBUFFSIZ]; public static void Main() { begin(); uint versiondata = GetFirmwareVersion(); while (versiondata == 0) { Debug.Print("\nDidn't find PN53x board"); // halt } // Got ok data, print it out! Debug.Print("\nFound chip PN5"); Debug.Print("\n 0x" + ((versiondata >> 24) & 0xFF)); Debug.Print("Firmware ver. "); Debug.Print("\n " + ((versiondata >> 16) & 0xFF).ToString()); Debug.Print("."); Debug.Print("\n " + ((versiondata >> 8) & 0xFF)); } static void begin() { Reset.Write(true); Reset.Write(false); Thread.Sleep(400); Reset.Write(true); } static Boolean sendCommandCheckAck(byte[] buffer, byte buffer_length, int Timeout) { int Timer = 0; // write the command WRITE_I2C_COMMAND(buffer, buffer_length); // Wait for chip to say its ready! while (READ_I2C_STATUS() != Constants.PN532_SPI_READY) { if (Timeout != 0) { Timer += 10; if (Timer > Timeout) return false; } Thread.Sleep(10); } Debug.Print("\n IRQ Received"); // read acknowledgement if (!I2C_READ_ACK()) { Debug.Print("No ack received"); return false; } return true; } static uint GetFirmwareVersion() { uint response; pn532_packetbuffer[0] = Constants.PN532_COMMAND_GETFIRMWAREVERSION; if (!sendCommandCheckAck(pn532_packetbuffer, 1, 3000)) return 0; I2C_READ_DATA(); if (0 != String.Compare(pn532_packetbuffer.ToString(), pn532response_firmwarevers.ToString())) { return 0; } response = pn532_packetbuffer[6]; response <<= 8; response |= pn532_packetbuffer[7]; response <<= 8; response |= pn532_packetbuffer[8]; response <<= 8; response |= pn532_packetbuffer[9]; return response; } static void WRITE_I2C_COMMAND(byte[] buffer, byte buffer_length) { byte checksum; write_buffer = new byte[8 + buffer_length]; buffer_length++; Debug.Print("\nSending....."); Thread.Sleep(2); // or whatever the delay is for waking up the board checksum = Constants.PN532_PREAMBLE + Constants.PN532_PREAMBLE + Constants.PN532_STARTCODE2; write_buffer[0]=(Constants.PN532_PREAMBLE); write_buffer[1] = (Constants.PN532_PREAMBLE); write_buffer[2] = (Constants.PN532_STARTCODE2); write_buffer[3] = (buffer_length); write_buffer[4] = ((byte)(~buffer_length + 1)); write_buffer[5] = (Constants.PN532_HOSTTOPN532); checksum += Constants.PN532_HOSTTOPN532; Debug.Print(" 0x" + Constants.PN532_PREAMBLE); Debug.Print(" 0x" + Constants.PN532_PREAMBLE); Debug.Print(" 0x" + Constants.PN532_STARTCODE2); Debug.Print(" 0x" + buffer_length); Debug.Print(" 0x" + (byte)(~buffer_length + 1)); Debug.Print(" 0x" + Constants.PN532_HOSTTOPN532); for (byte i = 0; i < buffer_length - 1; i++) { write_buffer[6 + i] = buffer[i] ; checksum += buffer[i]; Debug.Print("0x" + buffer[i]); } write_buffer[6+buffer_length-1]=((byte)~checksum); write_buffer[6 + buffer_length] = (Constants.PN532_POSTAMBLE); var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(write_buffer) }; PN532.Execute(transaction, I2CTimeout); Debug.Print(" 0x" + (byte)(~buffer_length)); Debug.Print(" 0x" + Constants.PN532_POSTAMBLE); } static byte READ_I2C_STATUS() { if (Irq.Read()==true) return Constants.PN532_I2C_BUSY; else return Constants.PN532_I2C_READY; } static Boolean I2C_READ_ACK() { byte[] buffer = new byte[6]; I2C_READ_DATA(); buffer = read_buffer; return (0 == String.Compare(buffer.ToString(), pn532ack.ToString())); } static void I2C_READ_DATA() { Thread.Sleep(2); Debug.Print("\nReading....."); var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(read_buffer) }; PN532.Execute(transaction, I2CTimeout); for (uint i = 0; i < read_buffer.Length; i++) { Thread.Sleep(1); Debug.Print(read_buffer[i].ToString()); } } } }
#28
Posted 26 July 2012 - 12:25 PM
Ok, here you have, it´s only the first test, my idea was to transform it into a library when it was operative..........:
I have all the constants in a .cs attached to the proyect.They are the same as the .h file
The code is only to try to have any kind of response from the PN532.
Looks like you are bitbanging the SPI interface here, will probably be too slow to work properly, try using the managed SPI interface and sending the byte arrays, SPI doesnt have a read command, you can either write a byte array, or do a simultaneous write read, so you write and fill a read buffer at the same time.
If i have time after work i will try and reimplement your code to use the SPI object..
Nak.
#29
Posted 27 July 2012 - 09:24 AM
Maybe I have to try to write the PN532_SPI_READY byte first????
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using NFC; using System.Text; namespace GitHub { public class Program { static SPI PN532 = new SPI(new SPI.Configuration(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D0, true, 0, 0, false, true, 400, SPI.SPI_module.SPI1)); //static OutputPort NSS = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D10, true); //static OutputPort CLK = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D13, true); //static OutputPort MOSI = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D11, true); //static OutputPort MISO = new OutputPort(SecretLabs.NETMF.Hardware.Netduino.Pins.GPIO_PIN_D12, true); static byte[] pn532ack = { 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00 }; static byte[] pn532response_firmwarevers = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03}; const byte PN532_PACKBUFFSIZ = 64; static byte[] pn532_packetbuffer=new byte[PN532_PACKBUFFSIZ]; public static void Main() { begin(); uint versiondata = GetFirmwareVersion(); while (versiondata==0) { Debug.Print("\nDidn't find PN53x board"); // halt } // Got ok data, print it out! Debug.Print("\nFound chip PN5"); Debug.Print("\n 0x"+((versiondata>>24) & 0xFF)); Debug.Print("Firmware ver. "); Debug.Print("\n "+((versiondata>>16) & 0xFF).ToString()); Debug.Print("."); Debug.Print("\n "+((versiondata>>8) & 0xFF)); } static void begin() { //NSS.Write(false); ; Thread.Sleep(1000); // not exactly sure why but we have to send a dummy command to get synced up pn532_packetbuffer[0] = Constants.PN532_COMMAND_GETFIRMWAREVERSION; sendCommandCheckAck(pn532_packetbuffer, 1,500); // ignore response! } static Boolean sendCommandCheckAck(byte[] buffer,byte buffer_length,int Timeout) { int Timer=0; // write the command WRITE_SPI_COMMAND(buffer, buffer_length); // Wait for chip to say its ready! while (READ_SPI_STATUS() != Constants.PN532_SPI_READY) { if (Timeout != 0) { Timer+=10; if (Timer > Timeout) return false; } Thread.Sleep(10); } // read acknowledgement if (!SPI_READ_ACK()) { return false; } Timer = 0; // Wait for chip to say its ready! while (READ_SPI_STATUS() != Constants.PN532_SPI_READY) { if (Timeout != 0) { Timer+=10; if (Timer > Timeout) return false; } Thread.Sleep(10); } return true; } static uint GetFirmwareVersion() { uint response; pn532_packetbuffer[0] = Constants.PN532_COMMAND_GETFIRMWAREVERSION; if (!sendCommandCheckAck(pn532_packetbuffer, 1,500)) return 0; SPI_READ_DATA(pn532_packetbuffer); if (0 != String.Compare(pn532_packetbuffer.ToString(), pn532response_firmwarevers.ToString())) { return 0; } response = pn532_packetbuffer[6]; response <<= 8; response |= pn532_packetbuffer[7]; response <<= 8; response |= pn532_packetbuffer[8]; response <<= 8; response |= pn532_packetbuffer[9]; return response; } static void WRITE_SPI_COMMAND(byte[] buffer,byte buffer_length) { byte checksum; buffer_length++; Debug.Print("\nSending....."); //NSS.Write(false); ; Thread.Sleep(2); // or whatever the delay is for waking up the board SPI_WRITE(Constants.PN532_SPI_DATAWRITE); checksum = Constants.PN532_PREAMBLE + Constants.PN532_PREAMBLE + Constants.PN532_STARTCODE2; SPI_WRITE(Constants.PN532_PREAMBLE); SPI_WRITE(Constants.PN532_PREAMBLE); SPI_WRITE(Constants.PN532_STARTCODE2); SPI_WRITE(buffer_length); SPI_WRITE((byte)(~buffer_length+1)); SPI_WRITE(Constants.PN532_HOSTTOPN532); checksum += Constants.PN532_HOSTTOPN532; Debug.Print(" 0x" + Constants.PN532_PREAMBLE); Debug.Print(" 0x" + Constants.PN532_PREAMBLE); Debug.Print(" 0x" + Constants.PN532_STARTCODE2); Debug.Print(" 0x" + buffer_length); Debug.Print(" 0x" + (byte)(~buffer_length + 1)); Debug.Print(" 0x" + Constants.PN532_HOSTTOPN532); for (byte i = 0; i < buffer_length - 1; i++) { SPI_WRITE(buffer[i]); checksum += buffer[i]; Debug.Print("0x"+buffer[i]); } SPI_WRITE((byte)~checksum); SPI_WRITE(Constants.PN532_POSTAMBLE); //NSS.Write(true); Debug.Print(" 0x" + (byte)(~buffer_length)); Debug.Print(" 0x" + Constants.PN532_POSTAMBLE); } static byte READ_SPI_STATUS() { //NSS.Write(false); Thread.Sleep(2); SPI_WRITE(Constants.PN532_SPI_STATREAD); // read byte byte x = SPI_READ(); //NSS.Write(true) ; return x; } static Boolean SPI_READ_ACK() { byte[] buffer=new byte[6]; SPI_READ_DATA(buffer); return (0 == String.Compare(buffer.ToString(), pn532ack.ToString())); } static void SPI_READ_DATA(byte[] buffer) { //NSS.Write(false); int i=0; Thread.Sleep(2); SPI_WRITE(Constants.PN532_SPI_DATAREAD); Debug.Print("\nReading....."); /*for (uint i = 0; i < n; i++) { Thread.Sleep(1); buffer[i] = SPI_READ(); Debug.Print(" 0x" + buffer[i]); }*/ while(buffer[i]!=Constants.PN532_POSTAMBLE) { Thread.Sleep(1); buffer[i] = SPI_READ(); i++; } //NSS.Write(true); } static void SPI_WRITE(byte constant) { byte[] buffer = new byte[1] ; /*CLK.Write(true); for (i = 0; i < 8; i++) { CLK.Write(false); if ((constant & (x<<(i)))==1) { MOSI.Write(true) ; } else { MOSI.Write(false); } CLK.Write(true); }*/ buffer[0] = constant; PN532.Write(buffer); } static byte SPI_READ() { byte x; byte[] Buffer=new byte[1]; byte[] Read_buffer=new byte[1]; /*CLK.Write(true); for (i = 0; i < 8; i++) { if (MISO.Read()) { x |= (z<<(i)); } CLK.Write(false); CLK.Write(true); }*/ Buffer[0]=Constants.PN532_SPI_DATAREAD; PN532.WriteRead(Buffer,Read_buffer); x = Read_buffer[0]; return (x); } } }
#30
Posted 27 July 2012 - 12:07 PM
#31
Posted 23 August 2012 - 07:37 AM
#32
Posted 27 August 2012 - 10:40 AM
Interesting shield, too bad it's a bit expensive to order for me to just write a driver
Have you got it working on an Arduino? If so, with a logics analyser it's possible to immitate it's signal.
#33
Posted 28 August 2012 - 01:09 PM
#34
Posted 28 August 2012 - 01:31 PM
#35
Posted 14 February 2013 - 01:31 AM
Hi all, has this issue been resolved? Looking to integrate nfc into my current project.
#36
Posted 14 February 2013 - 10:14 AM
Hey guys, I ‘am also interested in hearing about you resolved the problem or found another solution
#37
Posted 14 February 2013 - 03:39 PM
hm iam wondering why you bitbang the data, and not use the normal spi/i2c classes, that would be a lot faster and way less code, less errors and such. i dont have such a device so i cant try, but except some really exotic devices all is kinda "standard" that just works with netduino
- ofc the right code for accessing the right registers at the right time is still needed.
#38
Posted 21 February 2013 - 10:30 AM
Hi all again.
Has someone tried to use I2C with Netduino 2??? I see that the pinset is a bit different know, and matches exactly with the nfc shield ones.............Has something changed in the I2C class in the framework?????? Wicth is the difference between Arduino and Netduino in the way of manage the I2C bus(the CS pin??). Thank you i´m still trying to to get this shield working on Netduino............
#39
Posted 21 February 2013 - 12:04 PM
i2c does not have/need a cs (chipselect) pin. it access the device with an address.
maybe the pin is used for something else.
#40
Posted 26 February 2013 - 08:16 AM
Oh!! Sorry..........you are wright........I got confused with I2C and SPI becouse of the lots of tryes I´ve done. But my cuestion it´s obout the I2C and netduino 2..............finally I bought a logic analyzer (one from ikalogic especially designed to analize comunications) and I´m waiting to it to come. I´ll post the results of the tests to see if we can do something..........
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users