upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
SM130 Class Reference

API for the SM130 RFID Reader Module. More...

Detailed Description

This module defines the SM130 interface for the sm130 RFID library

This module was developed using an SM130 and a Sparkfun RFID Evaluation shield using a UART for communications. It should be fairly trivial to add support for I2C communication in the future, if you have the correct firmware on the SM130.

sm130.jpg


SM130 RFID Reader image provided by SparkFun* under CC BY-NC-SA-3.0.

// Instantiate a UART based SM130 RFID Module using defaults
upm::SM130* sensor = new upm::SM130();
// set the baud rate. 19200 baud is the default.
if (sensor->setBaudRate(19200))
{
cerr << "Failed to set baud rate" << endl;
return 1;
}
cout << "Resetting..." << endl;
sensor->reset();
cout << "Firmware revision: " << sensor->getFirmwareVersion() << endl;
cout << "Waiting up to 5 seconds for a tag..." << endl;
if (sensor->waitForTag(5000))
{
cout << "Found tag, UID: "
<< sensor->string2HexString(sensor->getUID()) << endl;
cout << "Tag Type: " << sensor->tag2String(sensor->getTagType())
<< endl;
}
else
{
// error
cout << "waitForTag failed: " << sensor->getLastErrorString() << endl;
}

Public Types

enum  CMD_T {
  CMD_RESET = 0x80, CMD_VERSION = 0x81, CMD_SEEK_TAG = 0x82, CMD_SELECT_TAG = 0x83,
  CMD_AUTHENTICATE = 0x85, CMD_READ16 = 0x86, CMD_READ_VALUE = 0x87, CMD_WRITE16 = 0x89,
  CMD_WRITE_VALUE = 0x8a, CMD_WRITE4 = 0x8b, CMD_WRITE_KEY = 0x8c, CMD_INC_VALUE = 0x8d,
  CMD_DEC_VALUE = 0x8e, CMD_ANTENNA_POWER = 0x90, CMD_READ_PORT = 0x91, CMD_WRITE_PORT = 0x92,
  CMD_HALT_TAG = 0x93, CMD_SET_BAUD = 0x94, CMD_SLEEP = 0x96
}
 
enum  TAG_TYPE_T {
  TAG_NONE = 0x00, TAG_MIFARE_ULTRALIGHT = 0x01, TAG_MIFARE_1K = 0x02, TAG_MIFARE_4K = 0x03,
  TAG_UNKNOWN = 0xff
}
 
enum  KEY_TYPES_T {
  KEY_TYPE_EEPROM_A0 = 0x10, KEY_TYPE_EEPROM_A1 = 0x11, KEY_TYPE_EEPROM_A2 = 0x12, KEY_TYPE_EEPROM_A3 = 0x13,
  KEY_TYPE_EEPROM_A4 = 0x14, KEY_TYPE_EEPROM_A5 = 0x15, KEY_TYPE_EEPROM_A6 = 0x16, KEY_TYPE_EEPROM_A7 = 0x17,
  KEY_TYPE_EEPROM_A8 = 0x18, KEY_TYPE_EEPROM_A9 = 0x19, KEY_TYPE_EEPROM_A10 = 0x1a, KEY_TYPE_EEPROM_A11 = 0x1b,
  KEY_TYPE_EEPROM_A12 = 0x1c, KEY_TYPE_EEPROM_A13 = 0x1d, KEY_TYPE_EEPROM_A14 = 0x1e, KEY_TYPE_EEPROM_A15 = 0x1f,
  KEY_TYPE_EEPROM_B0 = 0x20, KEY_TYPE_EEPROM_B1 = 0x21, KEY_TYPE_EEPROM_B2 = 0x22, KEY_TYPE_EEPROM_B3 = 0x23,
  KEY_TYPE_EEPROM_B4 = 0x24, KEY_TYPE_EEPROM_B5 = 0x25, KEY_TYPE_EEPROM_B6 = 0x26, KEY_TYPE_EEPROM_B7 = 0x27,
  KEY_TYPE_EEPROM_B8 = 0x28, KEY_TYPE_EEPROM_B9 = 0x29, KEY_TYPE_EEPROM_B10 = 0x2a, KEY_TYPE_EEPROM_B11 = 0x2b,
  KEY_TYPE_EEPROM_B12 = 0x2c, KEY_TYPE_EEPROM_B13 = 0x2d, KEY_TYPE_EEPROM_B14 = 0x2e, KEY_TYPE_EEPROM_B15 = 0x2f,
  KEY_TYPE_A = 0xaa, KEY_TYPE_B = 0xbb, KEY_TYPE_A_AND_TRANSPORT_F = 0xff
}
 

Public Member Functions

 SM130 (int uart=SM130_DEFAULT_UART, int reset=SM130_DEFAULT_RESET_PIN)
 
 ~SM130 ()
 
mraa::Result setBaudRate (int baud=19200)
 
std::string getFirmwareVersion ()
 
bool reset ()
 
void hardwareReset ()
 
bool select ()
 
bool waitForTag (uint32_t timeout)
 
bool authenticate (uint8_t block, KEY_TYPES_T keyType, std::string key="")
 
std::string readBlock16 (uint8_t block)
 
int32_t readValueBlock (uint8_t block)
 
bool writeBlock16 (uint8_t block, std::string contents)
 
bool writeValueBlock (uint8_t block, int32_t value)
 
bool writeBlock4 (uint8_t block, std::string contents)
 
bool writeKey (uint8_t eepromSector, KEY_TYPES_T keyType, std::string key)
 
int32_t adjustValueBlock (uint8_t block, int32_t value, bool incr)
 
bool setAntennaPower (bool on)
 
uint8_t readPorts ()
 
bool writePorts (uint8_t val)
 
bool haltTag ()
 
bool setSM130BaudRate (int baud)
 
bool sleep ()
 
char getLastErrorCode ()
 
std::string getLastErrorString ()
 
int getUIDLen ()
 
std::string getUID ()
 
TAG_TYPE_T getTagType ()
 
std::string tag2String (TAG_TYPE_T tag)
 
std::string string2HexString (std::string input)
 

Protected Member Functions

std::string sendCommand (CMD_T cmd, std::string data)
 
void initClock ()
 
uint32_t getMillis ()
 

Protected Attributes

mraa::Uart m_uart
 
mraa::Gpio m_gpioReset
 

Constructor & Destructor Documentation

SM130 ( int  uart = SM130_DEFAULT_UART,
int  reset = SM130_DEFAULT_RESET_PIN 
)

Instantiates an SM130 object

Parameters
uartThe UART port. Default is 0.
resetThe Reset pin. Default is 13.
~SM130 ( )

SM130 object destructor

Member Function Documentation

mraa::Result setBaudRate ( int  baud = 19200)

Sets the baud rate for the device. The default is 19200.

Parameters
baudDesired baud rate, default 19200
Returns
mraa::Result value

Here is the caller graph for this function:

string getFirmwareVersion ( )

Gets the firmware version string.

Returns
The firmware revision
bool reset ( )

Issues a reset command to the device.

Returns
true if successful
void hardwareReset ( )

Resets the device using the hardware RESET pin. This is required if the device has been put to sleep using the sleep() method.

bool select ( )

Checks to see if a tag is in the RF field, and selects it if one is present.

Returns
true if a tag was detected, false if no tag is present or an error was detected.

Here is the caller graph for this function:

bool waitForTag ( uint32_t  timeout)

Waits for a tag to enter the RF field for up to 'timeout' milliseconds. It will call select() every 100ms until 'timeout' has been exceeded.

Parameters
timeoutThe number of milliseconds to wait for a tag to appear
Returns
true if a tag was detected, false if no tag was detected within the timeout value, or an error occurred

Here is the call graph for this function:

bool authenticate ( uint8_t  block,
KEY_TYPES_T  keyType,
std::string  key = "" 
)

Set the authentication key for a block. Depending on the permissions on the tag, the correct key must be authenticated for that block in order to perform read and write operations.

Parameters
blockThe block to authenticate for
keyTypeone of the KEY_TYPE_T values
keyThe 6 byte key to use for Type A and Type B keys
Returns
true if authentication was successful, false otherwise
string readBlock16 ( uint8_t  block)

Read a 16 byte block. Depending on the tag, authentication of the block may be required for this method to succeed.

Parameters
blockThe block to read
Returns
The 16 byte block if successful, an empty string otherwise
int32_t readValueBlock ( uint8_t  block)

Read a 4 byte value block. Depending on the tag, authentication of the block may be required for this method to succeed.

Parameters
blockThe block to read
Returns
The 4 byte signed integer value block if successful, 0 otherwise
bool writeBlock16 ( uint8_t  block,
std::string  contents 
)

Write 16 bytes to a block. Depending on the tag, authentication of the block may be required for this method to succeed.

Parameters
blockThe block to write
contentsA 16 byte string containing the data to write
Returns
true if successful, false otherwise
bool writeValueBlock ( uint8_t  block,
int32_t  value 
)

Write to a 4 byte value block. Depending on the tag, authentication of the block may be required for this method to succeed.

Parameters
blockThe block to write
valuethe signed 4 byte integer to write to the value block
Returns
true if successful, false otherwise
bool writeBlock4 ( uint8_t  block,
std::string  contents 
)

Write 4 bytes to a block. This is typically used for Ultralight tags. Depending on the tag, authentication of the block may be required for this method to succeed.

Parameters
blockThe block to write
contentsA 4 byte string containing the data to write
Returns
true if successful, false otherwise
bool writeKey ( uint8_t  eepromSector,
KEY_TYPES_T  keyType,
std::string  key 
)

Write a key into one of the 16 EEPROM key slots. This can be a Type A or Type B key. It is not possible to read these keys once written. Once stored, the key can be used for authentication without having to send the key itself. You can then use the appropriate KEY_TYPE_EEPROM_* keyTypes in a call to authenticate().

Parameters
eepromSectorA number between 0 and 15, indicating the EEPROM sector you want to store the key in
keyTypeEither KEY_TYPE_A or KEY_TYPE_B
keyThe 6 byte key to store in the EEPROM
Returns
true if successful, false otherwise
int32_t adjustValueBlock ( uint8_t  block,
int32_t  value,
bool  incr 
)

Increment or decrement a value block.

Parameters
blockThe block to adjust
valueThe number to increment or decrement the value block by
incrtrue to increment, false to decrement
Returns
The contents of the value block after the operation has completed.
bool setAntennaPower ( bool  on)

Turn the antenna power on or off. The power is on by default after a reset. If you turn off the antenna, and methods used for interacting with tags will fail until power is re-enabled.

Parameters
ontrue to enable antenna power, false to disable
Returns
true if successful, false otherwise
uint8_t readPorts ( )

Read the status of the 2 onboard GPIO input pins. Bit 0 is for input 0, bit 1 for input 1. All other bits will be 0.

Returns
bitmask of input port status values
bool writePorts ( uint8_t  val)

Set the output status of the 2 onboard gpio outputs. Bit 0 is for output 0, bit 1 for output 1. All other bits will be discarded.

Parameters
valbitmask of output status bits to write
Returns
true if successful, false otherwise
bool haltTag ( )

Halts a tag. Once a tag is halted, it cannot be accessed until it is removed and reinserted into the RF field and selected.

Returns
true if successful, false otherwise
bool setSM130BaudRate ( int  baud)

Changes the baud rate of the SM130. WARNING: This is a potentially dangerous command that could cause you to lose contact with the device. Once the command is validated and issued, the host baudrate will be changed to match, and this method will wait for a response at the new baudrate for up to 1 second.

If this response does not arrive, the old baudrate will be restored, though there is no way to know whether the SM130 actually succeessfully executed the baudrate change.

Once the SM130 has changed it's baudrate, the new value will be stored in it's EEPROM, and any further access to the device will need to use the new baudrate. This is true even after a power on reset.

Parameters
baudThe new baud rate to set. Valid values are 9600, 19200, 38400, 57600, and 115200.
Returns
true if successful, false otherwise

Here is the call graph for this function:

bool sleep ( )

Put the SM130 to sleep. Once the device has been put to sleep, the only way to wake it is via hardwareReset() or a power cycle.

Returns
true if successful, false otherwise
char getLastErrorCode ( )
inline

Get the last error that occurred. After a successful operation, this will be 0. See the datasheet for the various errors that can occur in various circumstances.

Returns
The last error code, or 0 if the last operation succeeded.

Here is the caller graph for this function:

std::string getLastErrorString ( )
inline

Get the text representation of the last error that occurred. The returned string is empty if the last operation completed successfully.

Returns
The last error string if an error occurred, or an empty string if the last operation succeeded.
int getUIDLen ( )
inline

Get the UID length of the currently selected tag.

Returns
The UID length of the currently selected tag, or 0 if no tag is currently selected.
std::string getUID ( )
inline

Get the UID of the currently selected tag.

Returns
The UID of the currently selected tag, or an empty string if no tag is currently selected.
TAG_TYPE_T getTagType ( )
inline

Get the tag type of the currently selected tag.

Returns
The tag type of the currently selected tag, or TAG_NONE if no tag is currently selected.
string tag2String ( TAG_TYPE_T  tag)

Convert the supplied tag type into a human readable string.

Parameters
tagOne of the TAG_TYPE_T values
Returns
A string representation of the supplied tag type
string string2HexString ( std::string  input)

This is a convenience function that converts a supplied string into a space separated hex formatted string. This can be useful for printing out binary data in a human readable format, like the UID.

Parameters
inputThe string to convert
Returns
A string representation of the input in space separated hex values

The documentation for this class was generated from the following files: