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:
- 2:9e108187ccfe
- Parent:
- 1:5d157700a95d
- Child:
- 4:07e752ff8dce
--- a/NECnfc.cpp Sun Sep 02 08:06:05 2012 +0000 +++ b/NECnfc.cpp Thu Aug 27 05:41:24 2015 +0000 @@ -6,12 +6,11 @@ /** @file * @brief NEC Near Field Communication RF module library for mbed - * @note H001-000003-001 (950MHz), TY24FM-E2024 (2.4GHz) + * @note H001-000003-001 (950MHz), TY24FM-E2024 (2.4GHz), H001-000013-001 (920MHz) */ #include "mbed.h" #include "NECnfc.h" -#include "dbg.h" #include <string.h> @@ -40,9 +39,10 @@ // high power, retry 0 char rfconf9[] = {power, ch, baud, 1, 0, uart, 1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0x00, 0x00, 0x83, 0x41}; - char rfconf2[] = {ch, power, 0, 1, 2, 1, 0, 1, 0, 1, 2, 0xff, 0xff, - 0, 0, 0, 1, 0, 0x00, 0x00, 0x83, 0x41}; + (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); @@ -52,14 +52,23 @@ reset = 1; wait_ms(100); - if (type == NECTYPE_950MHz) { + 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)); - } else { - return 0; - rfconf2[1] = (power == NECPWR_HIGH ? 8 : 0); + 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)); } - DBG("rfconf %d\r\n", no); if (! read(&ifmsg) || ifmsg.msgid != NECMSG_ACK || ifmsg.msgno != no) { return -1; } @@ -92,7 +101,7 @@ char dat; dat = rf.getc(); - DBG("%02x ", dat); + DBG(" %02x", dat); if (dat == 0x0f) { // start (1/2) @@ -132,12 +141,12 @@ } } -int NECnfc::send (int msgid, unsigned long dest, char *param, int len) { +int NECnfc::send (NECMSG msgid, unsigned long dest, char *param, int len) { int i; struct ifMessage ifmsg; unsigned char *buf = (unsigned char *)&ifmsg; - if (len > 240) len = 240; + if (len > NEC_MAXLENGTH) len = NEC_MAXLENGTH; msgno ++; ifmsg.start = htons(0x0f5a); ifmsg.length = 13 + len; @@ -147,10 +156,12 @@ 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("_%02x", buf[i]); } + DBG("\r\n"); return ifmsg.msgno; }