00001 /** 00002 * @file SM130.h 00003 * @brief Header file for SM130 library 00004 * @author Marc Boon <http://www.marcboon.com> 00005 * @date May 2009 00006 */ 00007 00008 #ifndef SM130_h 00009 #define SM130_h 00010 00011 #include "WProgram.h" 00012 00013 #define SIZE_PAYLOAD 18 // maximum payload size of I2C packet 00014 #define SIZE_PACKET (SIZE_PAYLOAD + 2) // total I2C packet size, including length byte and checksum 00015 00016 #define halt haltTag // deprecated function halt() renamed to haltTag() 00017 00018 // Global functions 00019 void printArrayAscii(byte array[], byte len); 00020 void printArrayHex(byte array[], byte len); 00021 void printHex(byte val); 00022 00023 /** Class representing a <a href="http://www.sonmicro.com/en/index.php?option=com_content&view=article&id=57&Itemid=70">SonMicro SM130 RFID module</a>. 00024 * 00025 * Nearly complete implementation of the <a href="http://www.sonmicro.com/en/downloads/Mifare/ds_SM130.pdf">SM130 datasheet</a>.<br> 00026 * Functions dealing with value blocks and stored keys are not implemented. 00027 */ 00028 class SM130 00029 { 00030 byte data[SIZE_PACKET]; //!< packet data 00031 char versionString[8]; //!< version string 00032 byte tagNumber[7]; //!< tag number as byte array 00033 byte tagLength; //!< length of tag number in bytes (4 or 7) 00034 char tagString[15]; //!< tag number as hex string 00035 byte tagType; //!< type of tag 00036 char errorCode; //!< error code from some commands 00037 byte antennaPower; //!< antenna power level 00038 byte cmd; //!< last sent command 00039 unsigned long t; //!< timer for sending I2C commands 00040 00041 public: 00042 static const int VERSION = 1; //!< version of this library 00043 00044 static const byte MIFARE_ULTRALIGHT = 1; 00045 static const byte MIFARE_1K = 2; 00046 static const byte MIFARE_4K = 3; 00047 00048 static const byte CMD_RESET = 0x80; 00049 static const byte CMD_VERSION = 0x81; 00050 static const byte CMD_SEEK_TAG = 0x82; 00051 static const byte CMD_SELECT_TAG = 0x83; 00052 static const byte CMD_AUTHENTICATE = 0x85; 00053 static const byte CMD_READ16 = 0x86; 00054 static const byte CMD_READ_VALUE = 0x87; 00055 static const byte CMD_WRITE16 = 0x89; 00056 static const byte CMD_WRITE_VALUE = 0x8a; 00057 static const byte CMD_WRITE4 = 0x8b; 00058 static const byte CMD_WRITE_KEY = 0x8c; 00059 static const byte CMD_INC_VALUE = 0x8d; 00060 static const byte CMD_DEC_VALUE = 0x8e; 00061 static const byte CMD_ANTENNA_POWER = 0x90; 00062 static const byte CMD_READ_PORT = 0x91; 00063 static const byte CMD_WRITE_PORT = 0x92; 00064 static const byte CMD_HALT_TAG = 0x93; 00065 static const byte CMD_SET_BAUD = 0x94; 00066 static const byte CMD_SLEEP = 0x96; 00067 00068 boolean debug; //!< debug mode, prints all I2C communication to Serial port 00069 byte address; //!< I2C address (default 0x42) 00070 byte pinRESET; //!< RESET pin (default 3) 00071 byte pinDREADY; //!< DREADY pin (default 4) 00072 00073 //! Constructor 00074 SM130(); 00075 //! Hardware or software reset of the SM130 module 00076 void reset(); 00077 //! Returns a null-terminated string with the firmware version of the SM130 module 00078 const char* getFirmwareVersion(); 00079 //! Returns true if a response packet is available 00080 boolean available(); 00081 //! Returns a pointer to the response packet 00082 byte* getRawData() { return data; }; 00083 //! Returns the last executed command 00084 byte getCommand() { return data[1]; }; 00085 //! Returns the packet length, excluding checksum 00086 byte getPacketLength() { return data[0]; }; 00087 //! Returns the checksum 00088 byte getCheckSum() { return data[data[0]+1]; }; 00089 //! Returns a pointer to the packet payload 00090 byte* getPayload() { return data+2; }; 00091 //! Returns the block number for read/write commands 00092 byte getBlockNumber() { return data[2]; }; 00093 //! Returns a pointer to the read block (with a length of 16 bytes) 00094 byte* getBlock() { return data+3; }; 00095 //! Returns the tag's serial number as a byte array 00096 byte* getTagNumber() { return tagNumber; }; 00097 //! Returns the length of the tag's serial number obtained by getTagNumer() 00098 byte getTagLength() { return tagLength; }; 00099 //! Returns the tag's serial number as a hexadecimal null-terminated string 00100 const char* getTagString() { return tagString; }; 00101 //! Returns the tag type (SM130::MIFARE_XX) 00102 byte getTagType() { return tagType; }; 00103 //! Returns the tag type as a null-terminated string 00104 const char* getTagName() { return tagName(tagType); }; 00105 //! Returns the error code of the last executed command 00106 char getErrorCode() { return errorCode; }; 00107 //! Returns a human-readable error message corresponding to the error code 00108 const char* getErrorMessage(); 00109 //! Returns the antenna power level (0 or 1) 00110 byte getAntennaPower() { return antennaPower; }; 00111 //! Sends a SEEK_TAG command 00112 void seekTag() { sendCommand(CMD_SEEK_TAG); }; 00113 //! Sends a SELECT_TAG command 00114 void selectTag() { sendCommand(CMD_SELECT_TAG); }; 00115 //! Sends a HALT_TAG command 00116 void haltTag() { sendCommand(CMD_HALT_TAG); }; 00117 //! Set antenna power (on/off) 00118 void setAntennaPower(byte level); 00119 //! Sends a SLEEP command (can only wake-up with hardware reset!) 00120 void sleep() { sendCommand(CMD_SLEEP); }; 00121 //! Writes a null-terminated string of maximum 15 characters 00122 void writeBlock(byte block, const char* message); 00123 //! Writes a null-terminated string of maximum 3 characters to a Mifare Ultralight 00124 void writeFourByteBlock(byte block, const char* message); 00125 //! Sends a AUTHENTICATE command using the transport key 00126 void authenticate(byte block); 00127 //! Sends a AUTHENTICATE command using the specified key 00128 void authenticate(byte block, byte keyType, byte key[6]); 00129 //! Reads a 16-byte block 00130 void readBlock(byte block); 00131 00132 private: 00133 //! Send single-byte command 00134 void sendCommand(byte cmd); 00135 //! Transmit command packet over I2C 00136 void transmitData(); 00137 //! Receive response packet over I2C 00138 byte receiveData(byte length); 00139 //! Returns human-readable tag name corresponding to tag type 00140 const char* tagName(byte type); 00141 }; 00142 00143 #endif // SM130_h
1.5.9