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製の近距離無線モジュール用のライブラリです。
Diff: NECnfc.cpp
- Revision:
- 4:07e752ff8dce
- Parent:
- 2:9e108187ccfe
- Child:
- 5:e5a358e9ed94
--- a/NECnfc.cpp Thu Aug 27 07:31:07 2015 +0000 +++ b/NECnfc.cpp 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 */ @@ -14,173 +14,68 @@ #include <string.h> -// 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::NECnfc (PinName tx, PinName rx, PinName reset, int baud, NECTYPE type) : _nec(tx, rx) { + _rxbuf = (char*)&_rxmsg; + _type = type; + _msgno = 0; + _id = NEC_DUMMYID; + _received = 0; -NECnfc::NECnfc (PinName p_tx, PinName p_rx, PinName p_reset) : rf(p_tx, p_rx), reset(p_reset) { - rxbuf = (char *)&rxmsg; - msgno = 0; - rxlen = 0; - rxmode = 0; - rxflg = 0; + initUart(reset, NC, NC, baud); + setReset(true); + wait_ms(20); + setReset(false); + wait_ms(100); } -int NECnfc::begin (int ch, NECBAUD baud, NECUART uart, NECPWR power, NECTYPE type) { - int no; - struct ifMessage ifmsg; - // high power, retry 0 - char rfconf9[] = {power, ch, baud, 1, 0, uart, 1, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - (NEC_SYSTEMID >> 8), (NEC_SYSTEMID & 0xff), (NEC_PRODUCTID >> 8), (NEC_PRODUCTID & 0xff)}; - char rfconf2[] = {ch, 0, 0x01, 0x08, 0x08, 0x01, 0x04, 0x0a, 0x05, 0x03, 0x05, - 0xff, 0xff, 0x00, 0, 0, 0x01, 0x51, - (NEC_SYSTEMID >> 8), (NEC_SYSTEMID & 0xff), (NEC_PRODUCTID >> 8), (NEC_PRODUCTID & 0xff)}; - - rf.baud(38400); - rf.attach(this, &NECnfc::isr_rx, Serial::RxIrq); - reset = 0; // reset - wait_ms(100); - reset = 1; - wait_ms(100); +NECnfc::NECnfc (PinName tx, PinName rx, PinName reset, PinName wakeup, PinName mode, int baud, NECTYPE type) : _nec(tx, rx) { - switch (type) { - case TYPE_950MHz: - if (power == PWR_LOW && (ch < 1 || ch > 33)) return -1; - if (power >= PWR_MID && (ch < 17 || ch > 31)) return -1; - no = send(NECMSG_WRITE_CONFIG, 0xffffffff, rfconf9, sizeof(rfconf9)); - break; - case TYPE_920MHz: - if (power <= PWR_MID && (ch < 24 || ch > 38)) return -1; - if (power >= PWR_HIGH && (ch < 24 || ch > 37 || ch == 32)) return -1; - no = send(NECMSG_WRITE_CONFIG, 0xffffffff, rfconf9, sizeof(rfconf9)); - break; - case TYPE_2400MHz: - if (ch < 0 || ch > 15) return -1; - uart = UART_38400; - rfconf2[1] = (power >= PWR_HIGH ? 0x0f : 0x00); - no = send(NECMSG_WRITE_CONFIG, 0xffffffff, rfconf2, sizeof(rfconf2)); - } - if (! read(&ifmsg) || ifmsg.msgid != NECMSG_ACK || ifmsg.msgno != no) { - return -1; - } + _rxbuf = (char*)&_rxmsg; + _type = type; + _msgno = 0; + _id = NEC_DUMMYID; + _received = 0; - switch (uart) { - case UART_4800: - rf.baud(4800); - break; - case UART_9600: - rf.baud(9600); - break; - case UART_19200: - rf.baud(19200); - break; - case UART_38400: - rf.baud(38400); - break; - case UART_56700: - rf.baud(56700); - break; - case UART_115200: - rf.baud(115200); - break; - } - - return 0; + initUart(reset, wakeup, mode, baud); + setReset(true); + wait_ms(20); + setReset(false); + wait_ms(100); } -void NECnfc::isr_rx () { - char dat; - - dat = rf.getc(); - DBG(" %02x", dat); - - if (dat == 0x0f) { - // start (1/2) - rxmode = 1; - } else - if (rxmode == 1) { - if (dat == 0x5a) { - // start (2/2) - rxmode = 2; - rxlen = 0; - rxbuf[rxlen] = 0x0f; - rxlen ++; - rxbuf[rxlen] = dat; - rxlen ++; - } else { - if (rxlen) { - // continue (data) - rxmode = 2; - rxbuf[rxlen] = 0x0f; - rxlen ++; - rxbuf[rxlen] = dat; - rxlen ++; - } else { - // unknown - rxmode = 0; - } - } - } else - if (rxmode == 2) { - // data - rxbuf[rxlen] = dat; - rxlen ++; - if (rxlen >= rxmsg.length) { - rxmode = 3; - rxflg = 1; - } +void NECnfc::poll () { + + if (_received) { + _func.call(); + _received = 0; } } -int NECnfc::send (NECMSG msgid, unsigned long dest, char *param, int len) { - int i; - struct ifMessage ifmsg; - unsigned char *buf = (unsigned char *)&ifmsg; +int NECnfc::sendData(int dest, const char *data, int len) { - if (len > NEC_MAXLENGTH) len = NEC_MAXLENGTH; - msgno ++; - ifmsg.start = htons(0x0f5a); - ifmsg.length = 13 + len; - ifmsg.msgid = msgid; - ifmsg.msgno = msgno; - ifmsg.dstid = htonl(dest); - ifmsg.srcid = htonl(0xffffffff); - memcpy(ifmsg.parameter, param, len); - - DBG("\r\n"); - for (i = 0; i < ifmsg.length; i ++) { - rf.putc(buf[i]); - DBG("_%02x", buf[i]); - } - DBG("\r\n"); - - return ifmsg.msgno; -} - -int NECnfc::read (struct ifMessage *ifmsg, int timeout) { - Timer timer; - - timer.start(); - while (timer.read_ms() < timeout) { - if (rxflg) { - memcpy(ifmsg, &rxmsg, rxlen); - rxflg = 0; - rxmode = 0; - return rxlen; + if (dest == NEC_DUMMYID) { + if (send(NECMSG_SEND_NOACK, dest, data, len)) { + return -1; } + } else { + if (send(NECMSG_SEND_DAT, dest, data, len)) { + return -1; + } + _rssi = _rxmsg.parameter[2]; } return 0; } -int NECnfc::readable () { - return rxflg; +int NECnfc::readData(int *dest, int *src, char *data, int len) { + int r; + + if (!_received) return -1; + + r = _rxmsg.length - NEC_HEADER_SIZE; + memcpy(data, _rxmsg.parameter, r < len ? r : len); + if (dest) *dest = ntohl(_rxmsg.dstid); + if (src) *src = ntohl(_rxmsg.srcid); + return r; }