nxp pn532 NFC SPI initiator code, with all functions minimized and made as fast as possible (code for target is also there, you just need to change the functions in the main). Code advises are welcome, i am an electronist not a programmer

Dependencies:   mbed

Committer:
marius90
Date:
Fri Nov 15 12:58:57 2013 +0000
Revision:
0:f07ca719c12e
working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marius90 0:f07ca719c12e 1 #ifndef __PN532_H__
marius90 0:f07ca719c12e 2 #define __PN532_H__
marius90 0:f07ca719c12e 3
marius90 0:f07ca719c12e 4 #define PN532_PREAMBLE 0x00
marius90 0:f07ca719c12e 5 #define PN532_STARTCODE1 0x00
marius90 0:f07ca719c12e 6 #define PN532_STARTCODE2 0xFF
marius90 0:f07ca719c12e 7 #define PN532_POSTAMBLE 0x00
marius90 0:f07ca719c12e 8
marius90 0:f07ca719c12e 9 #define PN532_HOSTTOPN532 0xD4
marius90 0:f07ca719c12e 10
marius90 0:f07ca719c12e 11 #define PN532_FIRMWAREVERSION 0x02
marius90 0:f07ca719c12e 12 #define PN532_GETGENERALSTATUS 0x04
marius90 0:f07ca719c12e 13 #define PN532_SAMCONFIGURATION 0x14
marius90 0:f07ca719c12e 14 #define PN532_INLISTPASSIVETARGET 0x4A
marius90 0:f07ca719c12e 15 #define PN532_INDATAEXCHANGE 0x40
marius90 0:f07ca719c12e 16 #define PN532_INJUMPFORDEP 0x56
marius90 0:f07ca719c12e 17 #define PN532_TGINITASTARGET 0x8C
marius90 0:f07ca719c12e 18 #define PN532_TGGETDATA 0x86
marius90 0:f07ca719c12e 19 #define PN532_TGSETDATA 0x8E
marius90 0:f07ca719c12e 20
marius90 0:f07ca719c12e 21
marius90 0:f07ca719c12e 22
marius90 0:f07ca719c12e 23 #define PN532_WAKEUP 0x55
marius90 0:f07ca719c12e 24 #define PN532_SPI_STATREAD 0x02
marius90 0:f07ca719c12e 25 #define PN532_SPI_DATAWRITE 0x01
marius90 0:f07ca719c12e 26 #define PN532_SPI_DATAREAD 0x03
marius90 0:f07ca719c12e 27 #define PN532_SPI_READY 0x01
marius90 0:f07ca719c12e 28
marius90 0:f07ca719c12e 29 #define PN532_MIFARE_ISO14443A 0x0
marius90 0:f07ca719c12e 30
marius90 0:f07ca719c12e 31 #define KEY_A 1
marius90 0:f07ca719c12e 32 #define KEY_B 2
marius90 0:f07ca719c12e 33
marius90 0:f07ca719c12e 34 #define mosi1 p5
marius90 0:f07ca719c12e 35 #define miso1 p6
marius90 0:f07ca719c12e 36 #define sclk1 p7
marius90 0:f07ca719c12e 37 #define ss1 p8
marius90 0:f07ca719c12e 38
marius90 0:f07ca719c12e 39 #define REVERSE_BITS_ORDER(b) b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; \
marius90 0:f07ca719c12e 40 b = (b & 0xCC) >> 2 | (b & 0x33) << 2; \
marius90 0:f07ca719c12e 41 b = (b & 0xAA) >> 1 | (b & 0x55) << 1
marius90 0:f07ca719c12e 42
marius90 0:f07ca719c12e 43 class PN532
marius90 0:f07ca719c12e 44 {
marius90 0:f07ca719c12e 45 public:
marius90 0:f07ca719c12e 46
marius90 0:f07ca719c12e 47 PN532(PinName mosi, PinName miso, PinName sclk, PinName ss);
marius90 0:f07ca719c12e 48 void begin(void);
marius90 0:f07ca719c12e 49 bool SAMConfig(void);
marius90 0:f07ca719c12e 50 uint32_t getFirmwareVersion(void);
marius90 0:f07ca719c12e 51 uint32_t configurePeerAsInitiator(void);
marius90 0:f07ca719c12e 52 uint32_t configurePeerAsTarget(void);
marius90 0:f07ca719c12e 53 bool initiatorTxRx(char* dataOut, char* dataIn);
marius90 0:f07ca719c12e 54 uint32_t targetTxRx(char* dataOut, char* dataIn);
marius90 0:f07ca719c12e 55 bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000);
marius90 0:f07ca719c12e 56
marius90 0:f07ca719c12e 57 private:
marius90 0:f07ca719c12e 58 DigitalOut *_ss;
marius90 0:f07ca719c12e 59 SPI *_spi;
marius90 0:f07ca719c12e 60
marius90 0:f07ca719c12e 61 uint8_t read(uint8_t buff[], uint8_t n);
marius90 0:f07ca719c12e 62 uint8_t readSpiStatus(void);
marius90 0:f07ca719c12e 63 bool checkSpiAck(void);
marius90 0:f07ca719c12e 64 void writeCommand(uint8_t buf[], uint8_t len);
marius90 0:f07ca719c12e 65 void wakeup (void);
marius90 0:f07ca719c12e 66 inline void write(uint8_t data) {
marius90 0:f07ca719c12e 67 REVERSE_BITS_ORDER(data);
marius90 0:f07ca719c12e 68 _spi->write(data);
marius90 0:f07ca719c12e 69 }
marius90 0:f07ca719c12e 70 inline uint8_t read() {
marius90 0:f07ca719c12e 71 uint8_t data = _spi->write(0);
marius90 0:f07ca719c12e 72 REVERSE_BITS_ORDER(data);
marius90 0:f07ca719c12e 73 return data;
marius90 0:f07ca719c12e 74 }
marius90 0:f07ca719c12e 75
marius90 0:f07ca719c12e 76
marius90 0:f07ca719c12e 77 };
marius90 0:f07ca719c12e 78
marius90 0:f07ca719c12e 79 #endif