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@6:2e1fc47e5bca, 2016-04-07 (annotated)
- Committer:
- okini3939
- Date:
- Thu Apr 07 00:53:53 2016 +0000
- Revision:
- 6:2e1fc47e5bca
- Parent:
- 4:07e752ff8dce
bug fix;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:337524afec9b | 1 | /** |
okini3939 | 0:337524afec9b | 2 | * NEC Near Field Communication RF module library for mbed |
okini3939 | 4:07e752ff8dce | 3 | * Copyright (c) 2015 Suga |
okini3939 | 0:337524afec9b | 4 | * Released under the MIT License: http://mbed.org/license/mit |
okini3939 | 0:337524afec9b | 5 | */ |
okini3939 | 0:337524afec9b | 6 | |
okini3939 | 0:337524afec9b | 7 | /** @file |
okini3939 | 0:337524afec9b | 8 | * @brief NEC Near Field Communication RF module library for mbed |
okini3939 | 2:9e108187ccfe | 9 | * @note H001-000003-001 (950MHz), TY24FM-E2024 (2.4GHz), H001-000013-001 (920MHz) |
okini3939 | 0:337524afec9b | 10 | */ |
okini3939 | 0:337524afec9b | 11 | |
okini3939 | 0:337524afec9b | 12 | #include "mbed.h" |
okini3939 | 0:337524afec9b | 13 | |
okini3939 | 2:9e108187ccfe | 14 | #define NEC_MAXLENGTH 240 |
okini3939 | 4:07e752ff8dce | 15 | #define NEC_HEADER_SIZE 13 |
okini3939 | 2:9e108187ccfe | 16 | #define NEC_SYSTEMID 0x0000 |
okini3939 | 2:9e108187ccfe | 17 | #define NEC_PRODUCTID 0x8341 |
okini3939 | 4:07e752ff8dce | 18 | #define NEC_DUMMYID 0xffffffff |
okini3939 | 0:337524afec9b | 19 | |
okini3939 | 4:07e752ff8dce | 20 | #define NEC_TIMEOUT 3 // sec |
okini3939 | 4:07e752ff8dce | 21 | |
okini3939 | 4:07e752ff8dce | 22 | //#define DEBUG_DUMP |
okini3939 | 3:71a16ce27889 | 23 | //#define DBG(...) printf("" __VA_ARGS__) |
okini3939 | 3:71a16ce27889 | 24 | #define DBG(...) |
okini3939 | 0:337524afec9b | 25 | |
okini3939 | 4:07e752ff8dce | 26 | // host to network short |
okini3939 | 4:07e752ff8dce | 27 | #define htons( x ) ( (( (x) << 8 ) & 0xFF00) | (( (x) >> 8 ) & 0x00FF) ) |
okini3939 | 4:07e752ff8dce | 28 | #define ntohs( x ) htons(x) |
okini3939 | 4:07e752ff8dce | 29 | // host to network long |
okini3939 | 4:07e752ff8dce | 30 | #define htonl( x ) ( (( (x) << 24 ) & 0xFF000000) \ |
okini3939 | 4:07e752ff8dce | 31 | | (( (x) << 8 ) & 0x00FF0000) \ |
okini3939 | 4:07e752ff8dce | 32 | | (( (x) >> 8 ) & 0x0000FF00) \ |
okini3939 | 4:07e752ff8dce | 33 | | (( (x) >> 24 ) & 0x000000FF) ) |
okini3939 | 4:07e752ff8dce | 34 | #define ntohl( x ) htonl(x) |
okini3939 | 4:07e752ff8dce | 35 | |
okini3939 | 4:07e752ff8dce | 36 | |
okini3939 | 0:337524afec9b | 37 | /** |
okini3939 | 0:337524afec9b | 38 | * NECnfc class |
okini3939 | 0:337524afec9b | 39 | */ |
okini3939 | 0:337524afec9b | 40 | class NECnfc { |
okini3939 | 0:337524afec9b | 41 | public: |
okini3939 | 2:9e108187ccfe | 42 | enum NECTYPE { |
okini3939 | 2:9e108187ccfe | 43 | TYPE_950MHz, |
okini3939 | 2:9e108187ccfe | 44 | TYPE_2400MHz, |
okini3939 | 2:9e108187ccfe | 45 | TYPE_920MHz, |
okini3939 | 2:9e108187ccfe | 46 | }; |
okini3939 | 2:9e108187ccfe | 47 | |
okini3939 | 2:9e108187ccfe | 48 | enum NECMSG { |
okini3939 | 2:9e108187ccfe | 49 | NECMSG_ACK = 0x00, |
okini3939 | 2:9e108187ccfe | 50 | NECMSG_NOACK = 0x01, |
okini3939 | 2:9e108187ccfe | 51 | NECMSG_SEARCH = 0x10, |
okini3939 | 2:9e108187ccfe | 52 | NECMSG_SEND_DAT = 0x11, |
okini3939 | 2:9e108187ccfe | 53 | NECMSG_RESEND = 0x12, |
okini3939 | 2:9e108187ccfe | 54 | NECMSG_SEND_NOACK = 0x13, |
okini3939 | 2:9e108187ccfe | 55 | NECMSG_ENERGY_DETECT = 0x16, |
okini3939 | 2:9e108187ccfe | 56 | NECMSG_SEND_CMD = 0x17, |
okini3939 | 4:07e752ff8dce | 57 | NECMSG_WRITE_CHANNEL = 0x20, |
okini3939 | 2:9e108187ccfe | 58 | NECMSG_WRITE_RFCONF = 0x21, |
okini3939 | 2:9e108187ccfe | 59 | NECMSG_READ_RSSI = 0x24, |
okini3939 | 2:9e108187ccfe | 60 | NECMSG_READ_CONFIG = 0x29, |
okini3939 | 2:9e108187ccfe | 61 | NECMSG_WRITE_CONFIG = 0x2A, |
okini3939 | 2:9e108187ccfe | 62 | NECMSG_READ_DEFAULT = 0x7D, |
okini3939 | 2:9e108187ccfe | 63 | NECMSG_WRITE_DEFAULT = 0x7E, |
okini3939 | 2:9e108187ccfe | 64 | NECMSG_RESET = 0x77, |
okini3939 | 2:9e108187ccfe | 65 | }; |
okini3939 | 2:9e108187ccfe | 66 | |
okini3939 | 2:9e108187ccfe | 67 | enum NECPWR { |
okini3939 | 2:9e108187ccfe | 68 | PWR_LOW = 0x00, |
okini3939 | 2:9e108187ccfe | 69 | PWR_MID = 0x01, |
okini3939 | 2:9e108187ccfe | 70 | PWR_HIGH = 0x02, |
okini3939 | 2:9e108187ccfe | 71 | PWR_MAX = 0x03, |
okini3939 | 2:9e108187ccfe | 72 | }; |
okini3939 | 2:9e108187ccfe | 73 | |
okini3939 | 2:9e108187ccfe | 74 | enum NECBAUD { |
okini3939 | 2:9e108187ccfe | 75 | BAUD_9600 = 0x02, |
okini3939 | 2:9e108187ccfe | 76 | BAUD_50k = 0x06, |
okini3939 | 2:9e108187ccfe | 77 | BAUD_100k = 0x07, |
okini3939 | 2:9e108187ccfe | 78 | }; |
okini3939 | 2:9e108187ccfe | 79 | |
okini3939 | 2:9e108187ccfe | 80 | enum NECUART { |
okini3939 | 2:9e108187ccfe | 81 | UART_4800 = 0x01, |
okini3939 | 2:9e108187ccfe | 82 | UART_9600 = 0x02, |
okini3939 | 2:9e108187ccfe | 83 | UART_19200 = 0x04, |
okini3939 | 2:9e108187ccfe | 84 | UART_38400 = 0x05, |
okini3939 | 2:9e108187ccfe | 85 | UART_56700 = 0x06, |
okini3939 | 2:9e108187ccfe | 86 | UART_115200 = 0x08, |
okini3939 | 2:9e108187ccfe | 87 | }; |
okini3939 | 2:9e108187ccfe | 88 | |
okini3939 | 4:07e752ff8dce | 89 | enum Mode { |
okini3939 | 4:07e752ff8dce | 90 | MODE_READY, |
okini3939 | 4:07e752ff8dce | 91 | MODE_DATA, |
okini3939 | 4:07e752ff8dce | 92 | }; |
okini3939 | 4:07e752ff8dce | 93 | |
okini3939 | 2:9e108187ccfe | 94 | struct ifMessage { |
okini3939 | 2:9e108187ccfe | 95 | uint16_t start; |
okini3939 | 2:9e108187ccfe | 96 | uint8_t length; |
okini3939 | 2:9e108187ccfe | 97 | uint8_t msgid; |
okini3939 | 2:9e108187ccfe | 98 | uint8_t msgno; |
okini3939 | 2:9e108187ccfe | 99 | uint32_t dstid; |
okini3939 | 2:9e108187ccfe | 100 | uint32_t srcid; |
okini3939 | 2:9e108187ccfe | 101 | uint8_t parameter[NEC_MAXLENGTH + 2]; |
okini3939 | 2:9e108187ccfe | 102 | } __attribute__((packed)); |
okini3939 | 2:9e108187ccfe | 103 | |
okini3939 | 4:07e752ff8dce | 104 | // ----- NECnfc.cpp ----- |
okini3939 | 4:07e752ff8dce | 105 | NECnfc (PinName tx, PinName rx, PinName reset, int baud = 38400, NECTYPE type = TYPE_920MHz); |
okini3939 | 4:07e752ff8dce | 106 | |
okini3939 | 4:07e752ff8dce | 107 | NECnfc (PinName tx, PinName rx, PinName reset, PinName wakeup, PinName mode, int baud = 38400, NECTYPE type = TYPE_920MHz); |
okini3939 | 4:07e752ff8dce | 108 | |
okini3939 | 4:07e752ff8dce | 109 | void poll (); |
okini3939 | 4:07e752ff8dce | 110 | int sendData(int dest, const char *data, int len); |
okini3939 | 4:07e752ff8dce | 111 | int readData(int *dest, int *src, char *data, int len); |
okini3939 | 4:07e752ff8dce | 112 | |
okini3939 | 4:07e752ff8dce | 113 | void attach (void(*fptr)() = NULL) { |
okini3939 | 4:07e752ff8dce | 114 | _func.attach(fptr); |
okini3939 | 4:07e752ff8dce | 115 | } |
okini3939 | 4:07e752ff8dce | 116 | template<typename T> |
okini3939 | 4:07e752ff8dce | 117 | void attach (T* tptr, void (T::*mptr)()) { |
okini3939 | 4:07e752ff8dce | 118 | if ((mptr != NULL) && (tptr != NULL)) { |
okini3939 | 4:07e752ff8dce | 119 | _func.attach(tptr, mptr); |
okini3939 | 4:07e752ff8dce | 120 | } |
okini3939 | 4:07e752ff8dce | 121 | } |
okini3939 | 4:07e752ff8dce | 122 | |
okini3939 | 4:07e752ff8dce | 123 | // ----- NECnfc_msg.cpp ----- |
okini3939 | 4:07e752ff8dce | 124 | int send (NECMSG msgid, unsigned int dest, const char *param, int len); |
okini3939 | 4:07e752ff8dce | 125 | |
okini3939 | 4:07e752ff8dce | 126 | // ----- NECnfc_util.cpp ----- |
okini3939 | 4:07e752ff8dce | 127 | int setRfConfig (enum NECPWR power, int ch, NECBAUD baud); |
okini3939 | 4:07e752ff8dce | 128 | unsigned int getId (); |
okini3939 | 4:07e752ff8dce | 129 | unsigned int getSystemId (); |
okini3939 | 4:07e752ff8dce | 130 | int setSystemId (unsigned int id); |
okini3939 | 4:07e752ff8dce | 131 | int setSleepMode (int sleep_time, int rev_time); // sleep * 1000ms, rev * 1ms |
okini3939 | 4:07e752ff8dce | 132 | int getRssi (); |
okini3939 | 4:07e752ff8dce | 133 | int search (); |
okini3939 | 0:337524afec9b | 134 | |
okini3939 | 4:07e752ff8dce | 135 | // ----- NECnfc_hal.cpp ----- |
okini3939 | 4:07e752ff8dce | 136 | int sleep (int wait = 0); |
okini3939 | 4:07e752ff8dce | 137 | int wakeup (int wait = 0); |
okini3939 | 4:07e752ff8dce | 138 | |
okini3939 | 0:337524afec9b | 139 | protected: |
okini3939 | 4:07e752ff8dce | 140 | RawSerial _nec; |
okini3939 | 4:07e752ff8dce | 141 | DigitalInOut *_reset; |
okini3939 | 4:07e752ff8dce | 142 | DigitalOut *_wakeup; |
okini3939 | 4:07e752ff8dce | 143 | DigitalIn *_wmode; |
okini3939 | 4:07e752ff8dce | 144 | enum NECTYPE _type; |
okini3939 | 4:07e752ff8dce | 145 | FunctionPointer _func; |
okini3939 | 4:07e752ff8dce | 146 | |
okini3939 | 4:07e752ff8dce | 147 | enum Mode _mode; |
okini3939 | 4:07e752ff8dce | 148 | struct ifMessage _rxmsg; |
okini3939 | 4:07e752ff8dce | 149 | char *_rxbuf; |
okini3939 | 6:2e1fc47e5bca | 150 | int _rxlen; |
okini3939 | 6:2e1fc47e5bca | 151 | int _rxcount; |
okini3939 | 4:07e752ff8dce | 152 | int _msgno; |
okini3939 | 4:07e752ff8dce | 153 | volatile int _ack, _noack, _resend; |
okini3939 | 4:07e752ff8dce | 154 | int _txmsg; |
okini3939 | 4:07e752ff8dce | 155 | unsigned int _id; |
okini3939 | 4:07e752ff8dce | 156 | int _rssi; |
okini3939 | 4:07e752ff8dce | 157 | int _received; |
okini3939 | 4:07e752ff8dce | 158 | |
okini3939 | 4:07e752ff8dce | 159 | // ----- NECnfc_msg.cpp ----- |
okini3939 | 4:07e752ff8dce | 160 | void recvData (char c); |
okini3939 | 4:07e752ff8dce | 161 | void parseMessage (); |
okini3939 | 4:07e752ff8dce | 162 | |
okini3939 | 4:07e752ff8dce | 163 | // ----- NECnfc_hal.cpp ----- |
okini3939 | 4:07e752ff8dce | 164 | void setReset (bool flg); |
okini3939 | 4:07e752ff8dce | 165 | void isrUart (); |
okini3939 | 4:07e752ff8dce | 166 | int getUart (); |
okini3939 | 4:07e752ff8dce | 167 | void putUart (char c); |
okini3939 | 4:07e752ff8dce | 168 | int lockUart (int ms); |
okini3939 | 4:07e752ff8dce | 169 | void unlockUart (); |
okini3939 | 4:07e752ff8dce | 170 | void initUart (PinName reset, PinName wakeup, PinName mode, int baud); |
okini3939 | 0:337524afec9b | 171 | |
okini3939 | 0:337524afec9b | 172 | }; |