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製の近距離無線モジュール用のライブラリです。

詳細はこちら

NECnfc.h

Committer:
okini3939
Date:
2015-08-27
Revision:
2:9e108187ccfe
Parent:
0:337524afec9b
Child:
3:71a16ce27889

File content as of revision 2:9e108187ccfe:

/**
 * NEC Near Field Communication RF module library for mbed
 * Copyright (c) 2012 Suga
 * Released under the MIT License: http://mbed.org/license/mit
 */

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

#include "mbed.h"

#define NEC_MAXLENGTH 240
#define NEC_SYSTEMID  0x0000
#define NEC_PRODUCTID 0x8341

#define DBG(...) printf("" __VA_ARGS__) 
//#define DBG(...)

/**
 * NECnfc class
 */
class NECnfc {
public:
    enum NECTYPE {
        TYPE_950MHz,
        TYPE_2400MHz,
        TYPE_920MHz,
    };

    enum NECMSG {
        NECMSG_ACK           = 0x00,
        NECMSG_NOACK         = 0x01,
        NECMSG_SEARCH        = 0x10,
        NECMSG_SEND_DAT      = 0x11,
        NECMSG_RESEND        = 0x12,
        NECMSG_SEND_NOACK    = 0x13,
        NECMSG_ENERGY_DETECT = 0x16,
        NECMSG_SEND_CMD      = 0x17,
        NECMSG_WRITE_RFCONF  = 0x21,
        NECMSG_READ_RSSI     = 0x24,
        NECMSG_READ_CONFIG   = 0x29,
        NECMSG_WRITE_CONFIG  = 0x2A,
        NECMSG_READ_DEFAULT  = 0x7D,
        NECMSG_WRITE_DEFAULT = 0x7E,
        NECMSG_RESET         = 0x77,
    };

    enum NECPWR {
        PWR_LOW           = 0x00,
        PWR_MID           = 0x01,
        PWR_HIGH          = 0x02,
        PWR_MAX           = 0x03,
    };

    enum NECBAUD {
        BAUD_9600           = 0x02,
        BAUD_50k            = 0x06,
        BAUD_100k           = 0x07,
    };

    enum NECUART {
        UART_4800           = 0x01,
        UART_9600           = 0x02,
        UART_19200          = 0x04,
        UART_38400          = 0x05,
        UART_56700          = 0x06,
        UART_115200         = 0x08,
    };

    struct ifMessage {
        uint16_t start;
        uint8_t  length;
        uint8_t  msgid;
        uint8_t  msgno;
        uint32_t dstid;
        uint32_t srcid;
        uint8_t  parameter[NEC_MAXLENGTH + 2];
    } __attribute__((packed));

    /**
     * Default constructor
     */
    NECnfc (PinName p_tx, PinName p_rx, PinName p_reset);

    /**
     * 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 ();
    
protected:
    void isr_rx ();
    
private:
    Serial rf;
    DigitalOut reset;
    int msgno;
    struct ifMessage rxmsg;
    char *rxbuf;
    int rxlen, rxmode;
    volatile int rxflg;

};