new xadow NFC module

Dependents:   xadow_smartstrap_for_pebble

Files at this revision

API Documentation at this revision

Comitter:
KillingJacky
Date:
Wed Nov 04 09:59:37 2015 +0000
Child:
1:27242e2abd99
Commit message:
init commit

Changed in this revision

XadowNFC.cpp Show annotated file Show diff for this revision Revisions of this file
XadowNFC.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XadowNFC.cpp	Wed Nov 04 09:59:37 2015 +0000
@@ -0,0 +1,142 @@
+
+#include <stdlib.h>
+#include "mbed.h"
+#include "XadowNFC.h"
+#include "USBSerial.h"
+
+
+unsigned char uid[10] = {0};
+static char cmd[2];
+
+extern I2C i2c;
+extern USBSerial dbg_serial;
+
+unsigned char nfc_check_online(void)
+{
+    unsigned char data[6];
+    unsigned char i;
+
+    //dlc_i2c_configure(NFC_DEVICE_ADDR, 100);
+    //dlc_i2c_send_byte(NFC_SCAN_ID);
+    //dlc_i2c_send_byte(0);
+    
+    cmd[0] = NFC_SCAN_ID;
+    cmd[1] = 0;
+    i2c.write(NFC_DEVICE_ADDR, cmd, 1);  //the nfc module's i2c supports only 1 byte read/write per burst
+    i2c.write(NFC_DEVICE_ADDR, cmd+1, 1);
+
+
+    for(i=0;i<6;i++)
+    {
+        //data[i] = dlc_i2c_receive_byte();
+        i2c.read(NFC_DEVICE_ADDR, (char *)&data[i], 1);  //the nfc module's i2c supports only 1 byte read/write per burst
+    }
+
+    if(data[5] == (NFC_DEVICE_ADDR>>1))return 1;
+    else return 0;
+}
+
+void nfc_adapter_init(void)
+{
+    cmd[0] = NFC_ADAPTER_INIT_ID;
+    cmd[1] = 0;
+    i2c.write(NFC_DEVICE_ADDR, cmd, 1);
+    i2c.write(NFC_DEVICE_ADDR, cmd+1, 1);
+}
+
+unsigned char *nfc_adapter_get_uid(void)
+{
+    unsigned char i;
+
+    cmd[0] = NFC_ADAPTER_GET_UID_ID;
+    cmd[1] = 0;
+    i2c.write(NFC_DEVICE_ADDR, cmd, 1);
+    i2c.write(NFC_DEVICE_ADDR, cmd+1, 1);
+
+
+    for(i=0;i<2;i++)
+    {
+        //uid[i] = dlc_i2c_receive_byte();
+        i2c.read(NFC_DEVICE_ADDR, (char *)&uid[i], 1);
+    }
+
+    for(i=0;i<uid[1];i++)
+    {
+        //uid[i + 2]= dlc_i2c_receive_byte();
+        i2c.read(NFC_DEVICE_ADDR, (char *)&uid[i+2], 1);
+    }
+    uid[uid[1] + 2] = '\0';
+
+    return (unsigned char *)uid;
+}
+
+unsigned char *nfc_adapter_read(void)
+{
+    unsigned char data[2];
+    unsigned char i;
+
+    cmd[0] = NFC_ADAPTER_READ_ID;
+    cmd[1] = 0;
+    i2c.write(NFC_DEVICE_ADDR, cmd, 1);
+    i2c.write(NFC_DEVICE_ADDR, cmd+1, 1);
+
+    for(i=0;i<2;i++)
+    {
+        //data[i] = dlc_i2c_receive_byte();
+        i2c.read(NFC_DEVICE_ADDR, (char *)&data[i], 1);
+    }
+
+    unsigned char length = data[1];
+    unsigned char *ptr = (unsigned char *)malloc(length + 2 + 1);
+    *ptr = data[0];
+    *(ptr + 1) = data[1];
+    *(ptr + length + 2) = '\0';
+
+    for(i=0;i<*(ptr + 1);i++)
+    {
+        //*(ptr + i + 2) = dlc_i2c_receive_byte();
+        i2c.read(NFC_DEVICE_ADDR, (char *)(ptr + i + 2), 1);
+    }
+    
+    return ptr;
+}
+
+void nfc_adapter_write(unsigned char *data, unsigned char length)
+{
+    unsigned char i;
+
+    cmd[0] = NFC_ADAPTER_WRITE_ID;
+    cmd[1] = length;
+    i2c.write(NFC_DEVICE_ADDR, cmd, 1);
+    i2c.write(NFC_DEVICE_ADDR, cmd+1, 1);
+
+    for(i=0;i<length;i++)
+    {
+        //dlc_i2c_send_byte(*(string + i));
+        i2c.write(NFC_DEVICE_ADDR, (char *)(data + i), 1);
+    }
+}
+
+void nfc_adapter_erase(void)
+{    
+    cmd[0] = NFC_ADAPTER_ERASE_ID;
+    cmd[1] = 0;
+    i2c.write(NFC_DEVICE_ADDR, cmd, 1);
+    i2c.write(NFC_DEVICE_ADDR, cmd+1, 1);
+}
+
+void nfc_emulate_init(unsigned char *id)
+{
+    unsigned char i;
+    
+    cmd[0] = NFC_EMULATE_INIT_ID;
+    cmd[1] = 3;
+    i2c.write(NFC_DEVICE_ADDR, cmd, 1);
+    i2c.write(NFC_DEVICE_ADDR, cmd+1, 1);
+
+    for(i=0;i<3;i++)
+    {
+        //dlc_i2c_send_byte(*(id + i));
+        i2c.write(NFC_DEVICE_ADDR, (char *)(id + i), 1);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XadowNFC.h	Wed Nov 04 09:59:37 2015 +0000
@@ -0,0 +1,119 @@
+/**
+* Xadow NFC can work in 2 modes:
+* 1) adapter, can read uid, read NDEF data, write NDEF, erase NDEF memory
+*             supported card: Mifare classic, Mifare ultralight
+* 2) emulate as a TAG, with 106 kbps DEP, support setting the uid of the emulated tag.
+*/
+
+
+
+#ifndef _XADOW_NFC_H_
+#define _XADOW_NFC_H_
+
+#include "mbed.h"
+
+/*!
+    \def NFC_DEVICE_ADDR
+    The I2C address of NFC
+
+    \def NFC_SCAN_ID
+    The id of scan data, the format is 0, 0, 0, Device address
+
+    \def NFC_ADAPTER_INIT_ID
+    The id of NFC initialize as a adapter
+
+    \def NFC_ADAPTER_GET_UID_ID
+    The id of NFC adapter to get uid
+
+    \def NFC_ADAPTER_READ_ID
+    The id of NFC adapter to read NDEF message
+
+    \def NFC_ADAPTER_WRITE_ID
+    The id of NFC adapter to write NDEF message
+
+    \def NFC_ADAPTER_ERASE_ID
+    The id of NFC adapter to erase NDEF message
+
+    \def NFC_EMULATE_INIT_ID
+    The id of NFC emulate as a tag
+*/
+
+/*
+ *  Data format : ID, Length (n bytes, form 1 to n), Data1, Data2, ... ,Data n
+ * For example, get the scan data.
+ * First, Send: 0x00(NFC_SCAN_ID),0x00(Data length) to device.
+ * Second, Receive scan data(ID + Data length + Data 1, Data 2, Data 3, Data 4 = 6 bytes).
+ * Third, The scan data begin from the third data of received.
+ * End
+ */
+
+#define NFC_DEVICE_ADDR             (0x06<<1) //
+#define NFC_SCAN_ID                 0 //
+#define NFC_ADAPTER_INIT_ID         1 //
+#define NFC_ADAPTER_GET_UID_ID      2 //
+#define NFC_ADAPTER_READ_ID         3 //
+#define NFC_ADAPTER_WRITE_ID        4 //
+#define NFC_ADAPTER_ERASE_ID        5 //
+#define NFC_EMULATE_INIT_ID         6 //
+
+/**
+ *  \brief Get the status of the NFC device.
+ *
+ *  \return Return TRUE or FALSE. TRUE is on line, FALSE is off line.
+ *
+ */
+unsigned char nfc_check_online(void);
+
+/**
+ *  \brief Initialize NFC as a adapter, that can read and write a tag.
+ *
+ *  \return Return Null.
+ *
+ */
+void nfc_adapter_init(void);
+
+/**
+ *  \brief Get the uid data from a tag.
+ *
+ *  \return Return the pointer of uid data, the format is  NFC_ADAPTER_GET_UID_ID, Data length, Data 1, Data2, ...
+ *
+ */
+unsigned char *nfc_adapter_get_uid(void);
+
+/**
+ *  \brief Read the NDEF data from a tag.
+ *
+ *  \return Return the pointer of NEDF data, the format is  NFC_ADAPTER_READ_ID, Data length, Data 1, Data2, ...
+ *
+ */
+unsigned char *nfc_adapter_read(void);
+
+/**
+ *  \brief Write the NDEF data to a tag.
+ *
+ *  \param [in] *data the NEDF data want to write. 
+ *  \param [in] len   the length of the NEDF data want to write. max length is 256.
+ *  \return Return NULL
+ *
+ */
+void nfc_adapter_write(unsigned char *data, unsigned char len);
+
+/**
+ *  \brief Earse the NDEF data.
+ *
+ *  \return Return Null.
+ *
+ */
+void nfc_adapter_erase(void);
+
+/**
+ *  \brief Emulate NFC as a target
+ *
+ * \param [in] *id the target uid, the first data is fixed, so the format is 0x??, 0x??, 0x??(3 bytes)
+ *  \return Return Null.
+ *
+ */
+void nfc_emulate_init(unsigned char *id);
+
+
+#endif