NEC Near Field Communication RF module library for mbed H001-000003-001 (950MHz), H001-000013-001 (920MHz), TY24FM-E2024 (2.4GHz)

Dependents:   NECnfc_sample Drone_air Drone_ground

NEC Near Field Communication RF module library

NEC製の近距離無線モジュール用のライブラリです。

詳細はこちら

Revision:
4:07e752ff8dce
Parent:
3:71a16ce27889
Child:
6:2e1fc47e5bca
diff -r 71a16ce27889 -r 07e752ff8dce NECnfc.h
--- a/NECnfc.h	Thu Aug 27 07:31:07 2015 +0000
+++ b/NECnfc.h	Tue Sep 15 06:17:57 2015 +0000
@@ -1,6 +1,6 @@
 /**
  * NEC Near Field Communication RF module library for mbed
- * Copyright (c) 2012 Suga
+ * Copyright (c) 2015 Suga
  * Released under the MIT License: http://mbed.org/license/mit
  */
 
@@ -12,12 +12,28 @@
 #include "mbed.h"
 
 #define NEC_MAXLENGTH 240
+#define NEC_HEADER_SIZE 13
 #define NEC_SYSTEMID  0x0000
 #define NEC_PRODUCTID 0x8341
+#define NEC_DUMMYID 0xffffffff
 
+#define NEC_TIMEOUT 3 // sec
+
+//#define DEBUG_DUMP
 //#define DBG(...) printf("" __VA_ARGS__) 
 #define DBG(...)
 
+// host to network short
+#define htons( x ) ( (( (x) << 8 ) & 0xFF00) | (( (x) >> 8 ) & 0x00FF) )
+#define ntohs( x ) htons(x)
+// host to network long
+#define htonl( x ) ( (( (x) << 24 ) & 0xFF000000)  \
+                   | (( (x) <<  8 ) & 0x00FF0000)  \
+                   | (( (x) >>  8 ) & 0x0000FF00)  \
+                   | (( (x) >> 24 ) & 0x000000FF)  )
+#define ntohl( x ) htonl(x)
+
+
 /**
  * NECnfc class
  */
@@ -38,6 +54,7 @@
         NECMSG_SEND_NOACK    = 0x13,
         NECMSG_ENERGY_DETECT = 0x16,
         NECMSG_SEND_CMD      = 0x17,
+        NECMSG_WRITE_CHANNEL = 0x20,
         NECMSG_WRITE_RFCONF  = 0x21,
         NECMSG_READ_RSSI     = 0x24,
         NECMSG_READ_CONFIG   = 0x29,
@@ -69,6 +86,11 @@
         UART_115200         = 0x08,
     };
 
+    enum Mode {
+        MODE_READY,
+        MODE_DATA,
+    };
+
     struct ifMessage {
         uint16_t start;
         uint8_t  length;
@@ -79,38 +101,70 @@
         uint8_t  parameter[NEC_MAXLENGTH + 2];
     } __attribute__((packed));
 
-    /**
-     * Default constructor
-     */
-    NECnfc (PinName p_tx, PinName p_rx, PinName p_reset);
+    // ----- NECnfc.cpp -----
+    NECnfc (PinName tx, PinName rx, PinName reset, int baud = 38400, NECTYPE type = TYPE_920MHz);
+
+    NECnfc (PinName tx, PinName rx, PinName reset, PinName wakeup, PinName mode, int baud = 38400, NECTYPE type = TYPE_920MHz);
+
+    void poll ();
+    int sendData(int dest, const char *data, int len);
+    int readData(int *dest, int *src, char *data, int len);
+
+    void attach (void(*fptr)() = NULL) {
+        _func.attach(fptr);
+    }
+    template<typename T>
+    void attach (T* tptr, void (T::*mptr)()) {
+        if ((mptr != NULL) && (tptr != NULL)) {
+            _func.attach(tptr, mptr);
+        }
+    }
+
+    // ----- NECnfc_msg.cpp -----
+    int send (NECMSG msgid, unsigned int dest, const char *param, int len);
+
+    // ----- NECnfc_util.cpp -----
+    int setRfConfig (enum NECPWR power, int ch, NECBAUD baud);
+    unsigned int getId ();
+    unsigned int getSystemId ();
+    int setSystemId (unsigned int id);
+    int setSleepMode (int sleep_time, int rev_time); // sleep * 1000ms, rev * 1ms
+    int getRssi ();
+    int search ();
 
-    /**
-     * configuration
-     */
-    int begin (int ch, NECBAUD baud = BAUD_50k, NECUART uart = UART_38400, NECPWR power = PWR_LOW, NECTYPE type = TYPE_920MHz);
-    /**
-     * send
-     */
-    int send (NECMSG msgid, unsigned long dest, char *param, int len);
-    /**
-     * read
-     */
-    int read (struct ifMessage *ifmsg, int timeout = 5000);
-    /**
-     * read data ready
-     */
-    int readable ();
-    
+    // ----- NECnfc_hal.cpp -----
+    int sleep (int wait = 0);
+    int wakeup (int wait = 0);
+
 protected:
-    void isr_rx ();
-    
-private:
-    Serial rf;
-    DigitalOut reset;
-    int msgno;
-    struct ifMessage rxmsg;
-    char *rxbuf;
-    int rxlen, rxmode;
-    volatile int rxflg;
+    RawSerial _nec;
+    DigitalInOut *_reset;
+    DigitalOut *_wakeup;
+    DigitalIn *_wmode;
+    enum NECTYPE _type;
+    FunctionPointer _func;
+
+    enum Mode _mode;
+    struct ifMessage _rxmsg;
+    char *_rxbuf;
+    int _msgno;
+    volatile int _ack, _noack, _resend;
+    int _txmsg;
+    unsigned int _id;
+    int _rssi;
+    int _received;
+
+    // ----- NECnfc_msg.cpp -----
+    void recvData (char c);
+    void parseMessage ();
+
+    // ----- NECnfc_hal.cpp -----
+    void setReset (bool flg);
+    void isrUart ();
+    int getUart ();
+    void putUart (char c);
+    int lockUart (int ms);
+    void unlockUart ();
+    void initUart (PinName reset, PinName wakeup, PinName mode, int baud);
 
 };