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

詳細はこちら

Committer:
okini3939
Date:
Tue Sep 15 06:17:57 2015 +0000
Revision:
4:07e752ff8dce
Parent:
2:9e108187ccfe
Child:
5:e5a358e9ed94
revamp functions;

Who changed what in which revision?

UserRevisionLine numberNew 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 #include "NECnfc.h"
okini3939 0:337524afec9b 14 #include <string.h>
okini3939 0:337524afec9b 15
okini3939 0:337524afec9b 16
okini3939 4:07e752ff8dce 17 NECnfc::NECnfc (PinName tx, PinName rx, PinName reset, int baud, NECTYPE type) : _nec(tx, rx) {
okini3939 0:337524afec9b 18
okini3939 4:07e752ff8dce 19 _rxbuf = (char*)&_rxmsg;
okini3939 4:07e752ff8dce 20 _type = type;
okini3939 4:07e752ff8dce 21 _msgno = 0;
okini3939 4:07e752ff8dce 22 _id = NEC_DUMMYID;
okini3939 4:07e752ff8dce 23 _received = 0;
okini3939 0:337524afec9b 24
okini3939 4:07e752ff8dce 25 initUart(reset, NC, NC, baud);
okini3939 4:07e752ff8dce 26 setReset(true);
okini3939 4:07e752ff8dce 27 wait_ms(20);
okini3939 4:07e752ff8dce 28 setReset(false);
okini3939 4:07e752ff8dce 29 wait_ms(100);
okini3939 0:337524afec9b 30 }
okini3939 0:337524afec9b 31
okini3939 0:337524afec9b 32
okini3939 4:07e752ff8dce 33 NECnfc::NECnfc (PinName tx, PinName rx, PinName reset, PinName wakeup, PinName mode, int baud, NECTYPE type) : _nec(tx, rx) {
okini3939 0:337524afec9b 34
okini3939 4:07e752ff8dce 35 _rxbuf = (char*)&_rxmsg;
okini3939 4:07e752ff8dce 36 _type = type;
okini3939 4:07e752ff8dce 37 _msgno = 0;
okini3939 4:07e752ff8dce 38 _id = NEC_DUMMYID;
okini3939 4:07e752ff8dce 39 _received = 0;
okini3939 0:337524afec9b 40
okini3939 4:07e752ff8dce 41 initUart(reset, wakeup, mode, baud);
okini3939 4:07e752ff8dce 42 setReset(true);
okini3939 4:07e752ff8dce 43 wait_ms(20);
okini3939 4:07e752ff8dce 44 setReset(false);
okini3939 4:07e752ff8dce 45 wait_ms(100);
okini3939 0:337524afec9b 46 }
okini3939 0:337524afec9b 47
okini3939 4:07e752ff8dce 48 void NECnfc::poll () {
okini3939 4:07e752ff8dce 49
okini3939 4:07e752ff8dce 50 if (_received) {
okini3939 4:07e752ff8dce 51 _func.call();
okini3939 4:07e752ff8dce 52 _received = 0;
okini3939 0:337524afec9b 53 }
okini3939 0:337524afec9b 54 }
okini3939 0:337524afec9b 55
okini3939 4:07e752ff8dce 56 int NECnfc::sendData(int dest, const char *data, int len) {
okini3939 0:337524afec9b 57
okini3939 4:07e752ff8dce 58 if (dest == NEC_DUMMYID) {
okini3939 4:07e752ff8dce 59 if (send(NECMSG_SEND_NOACK, dest, data, len)) {
okini3939 4:07e752ff8dce 60 return -1;
okini3939 0:337524afec9b 61 }
okini3939 4:07e752ff8dce 62 } else {
okini3939 4:07e752ff8dce 63 if (send(NECMSG_SEND_DAT, dest, data, len)) {
okini3939 4:07e752ff8dce 64 return -1;
okini3939 4:07e752ff8dce 65 }
okini3939 4:07e752ff8dce 66 _rssi = _rxmsg.parameter[2];
okini3939 0:337524afec9b 67 }
okini3939 0:337524afec9b 68 return 0;
okini3939 0:337524afec9b 69 }
okini3939 0:337524afec9b 70
okini3939 4:07e752ff8dce 71 int NECnfc::readData(int *dest, int *src, char *data, int len) {
okini3939 4:07e752ff8dce 72 int r;
okini3939 4:07e752ff8dce 73
okini3939 4:07e752ff8dce 74 if (!_received) return -1;
okini3939 4:07e752ff8dce 75
okini3939 4:07e752ff8dce 76 r = _rxmsg.length - NEC_HEADER_SIZE;
okini3939 4:07e752ff8dce 77 memcpy(data, _rxmsg.parameter, r < len ? r : len);
okini3939 4:07e752ff8dce 78 if (dest) *dest = ntohl(_rxmsg.dstid);
okini3939 4:07e752ff8dce 79 if (src) *src = ntohl(_rxmsg.srcid);
okini3939 4:07e752ff8dce 80 return r;
okini3939 0:337524afec9b 81 }