Suga koubou / NECnfc

Dependents:   NECnfc_sample Drone_air Drone_ground

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NECnfc.h Source File

NECnfc.h

Go to the documentation of this file.
00001 /**
00002  * NEC Near Field Communication RF module library for mbed
00003  * Copyright (c) 2015 Suga
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 /** @file
00008  * @brief NEC Near Field Communication RF module library for mbed
00009  * @note H001-000003-001 (950MHz), TY24FM-E2024 (2.4GHz), H001-000013-001 (920MHz)
00010  */
00011 
00012 #include "mbed.h"
00013 
00014 #define NEC_MAXLENGTH 240
00015 #define NEC_HEADER_SIZE 13
00016 #define NEC_SYSTEMID  0x0000
00017 #define NEC_PRODUCTID 0x8341
00018 #define NEC_DUMMYID 0xffffffff
00019 
00020 #define NEC_TIMEOUT 3 // sec
00021 
00022 //#define DEBUG_DUMP
00023 //#define DBG(...) printf("" __VA_ARGS__) 
00024 #define DBG(...)
00025 
00026 // host to network short
00027 #define htons( x ) ( (( (x) << 8 ) & 0xFF00) | (( (x) >> 8 ) & 0x00FF) )
00028 #define ntohs( x ) htons(x)
00029 // host to network long
00030 #define htonl( x ) ( (( (x) << 24 ) & 0xFF000000)  \
00031                    | (( (x) <<  8 ) & 0x00FF0000)  \
00032                    | (( (x) >>  8 ) & 0x0000FF00)  \
00033                    | (( (x) >> 24 ) & 0x000000FF)  )
00034 #define ntohl( x ) htonl(x)
00035 
00036 
00037 /**
00038  * NECnfc class
00039  */
00040 class NECnfc {
00041 public:
00042     enum NECTYPE {
00043         TYPE_950MHz,
00044         TYPE_2400MHz,
00045         TYPE_920MHz,
00046     };
00047 
00048     enum NECMSG {
00049         NECMSG_ACK           = 0x00,
00050         NECMSG_NOACK         = 0x01,
00051         NECMSG_SEARCH        = 0x10,
00052         NECMSG_SEND_DAT      = 0x11,
00053         NECMSG_RESEND        = 0x12,
00054         NECMSG_SEND_NOACK    = 0x13,
00055         NECMSG_ENERGY_DETECT = 0x16,
00056         NECMSG_SEND_CMD      = 0x17,
00057         NECMSG_WRITE_CHANNEL = 0x20,
00058         NECMSG_WRITE_RFCONF  = 0x21,
00059         NECMSG_READ_RSSI     = 0x24,
00060         NECMSG_READ_CONFIG   = 0x29,
00061         NECMSG_WRITE_CONFIG  = 0x2A,
00062         NECMSG_READ_DEFAULT  = 0x7D,
00063         NECMSG_WRITE_DEFAULT = 0x7E,
00064         NECMSG_RESET         = 0x77,
00065     };
00066 
00067     enum NECPWR {
00068         PWR_LOW           = 0x00,
00069         PWR_MID           = 0x01,
00070         PWR_HIGH          = 0x02,
00071         PWR_MAX           = 0x03,
00072     };
00073 
00074     enum NECBAUD {
00075         BAUD_9600           = 0x02,
00076         BAUD_50k            = 0x06,
00077         BAUD_100k           = 0x07,
00078     };
00079 
00080     enum NECUART {
00081         UART_4800           = 0x01,
00082         UART_9600           = 0x02,
00083         UART_19200          = 0x04,
00084         UART_38400          = 0x05,
00085         UART_56700          = 0x06,
00086         UART_115200         = 0x08,
00087     };
00088 
00089     enum Mode {
00090         MODE_READY,
00091         MODE_DATA,
00092     };
00093 
00094     struct ifMessage {
00095         uint16_t start;
00096         uint8_t  length;
00097         uint8_t  msgid;
00098         uint8_t  msgno;
00099         uint32_t dstid;
00100         uint32_t srcid;
00101         uint8_t  parameter[NEC_MAXLENGTH + 2];
00102     } __attribute__((packed));
00103 
00104     // ----- NECnfc.cpp -----
00105     NECnfc (PinName tx, PinName rx, PinName reset, int baud = 38400, NECTYPE type = TYPE_920MHz);
00106 
00107     NECnfc (PinName tx, PinName rx, PinName reset, PinName wakeup, PinName mode, int baud = 38400, NECTYPE type = TYPE_920MHz);
00108 
00109     void poll ();
00110     int sendData(int dest, const char *data, int len);
00111     int readData(int *dest, int *src, char *data, int len);
00112 
00113     void attach (void(*fptr)() = NULL) {
00114         _func.attach(fptr);
00115     }
00116     template<typename T>
00117     void attach (T* tptr, void (T::*mptr)()) {
00118         if ((mptr != NULL) && (tptr != NULL)) {
00119             _func.attach(tptr, mptr);
00120         }
00121     }
00122 
00123     // ----- NECnfc_msg.cpp -----
00124     int send (NECMSG msgid, unsigned int dest, const char *param, int len);
00125 
00126     // ----- NECnfc_util.cpp -----
00127     int setRfConfig (enum NECPWR power, int ch, NECBAUD baud);
00128     unsigned int getId ();
00129     unsigned int getSystemId ();
00130     int setSystemId (unsigned int id);
00131     int setSleepMode (int sleep_time, int rev_time); // sleep * 1000ms, rev * 1ms
00132     int getRssi ();
00133     int search ();
00134 
00135     // ----- NECnfc_hal.cpp -----
00136     int sleep (int wait = 0);
00137     int wakeup (int wait = 0);
00138 
00139 protected:
00140     RawSerial _nec;
00141     DigitalInOut *_reset;
00142     DigitalOut *_wakeup;
00143     DigitalIn *_wmode;
00144     enum NECTYPE _type;
00145     FunctionPointer _func;
00146 
00147     enum Mode _mode;
00148     struct ifMessage _rxmsg;
00149     char *_rxbuf;
00150     int _rxlen;
00151     int _rxcount;
00152     int _msgno;
00153     volatile int _ack, _noack, _resend;
00154     int _txmsg;
00155     unsigned int _id;
00156     int _rssi;
00157     int _received;
00158 
00159     // ----- NECnfc_msg.cpp -----
00160     void recvData (char c);
00161     void parseMessage ();
00162 
00163     // ----- NECnfc_hal.cpp -----
00164     void setReset (bool flg);
00165     void isrUart ();
00166     int getUart ();
00167     void putUart (char c);
00168     int lockUart (int ms);
00169     void unlockUart ();
00170     void initUart (PinName reset, PinName wakeup, PinName mode, int baud);
00171 
00172 };