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@2:9e108187ccfe, 2015-08-27 (annotated)
- Committer:
- okini3939
- Date:
- Thu Aug 27 05:41:24 2015 +0000
- Revision:
- 2:9e108187ccfe
- Parent:
- 0:337524afec9b
- Child:
- 3:71a16ce27889
920MHz supported
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 | 0:337524afec9b | 3 | * Copyright (c) 2012 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 | 2:9e108187ccfe | 15 | #define NEC_SYSTEMID 0x0000 |
okini3939 | 2:9e108187ccfe | 16 | #define NEC_PRODUCTID 0x8341 |
okini3939 | 0:337524afec9b | 17 | |
okini3939 | 2:9e108187ccfe | 18 | #define DBG(...) printf("" __VA_ARGS__) |
okini3939 | 2:9e108187ccfe | 19 | //#define DBG(...) |
okini3939 | 0:337524afec9b | 20 | |
okini3939 | 0:337524afec9b | 21 | /** |
okini3939 | 0:337524afec9b | 22 | * NECnfc class |
okini3939 | 0:337524afec9b | 23 | */ |
okini3939 | 0:337524afec9b | 24 | class NECnfc { |
okini3939 | 0:337524afec9b | 25 | public: |
okini3939 | 2:9e108187ccfe | 26 | enum NECTYPE { |
okini3939 | 2:9e108187ccfe | 27 | TYPE_950MHz, |
okini3939 | 2:9e108187ccfe | 28 | TYPE_2400MHz, |
okini3939 | 2:9e108187ccfe | 29 | TYPE_920MHz, |
okini3939 | 2:9e108187ccfe | 30 | }; |
okini3939 | 2:9e108187ccfe | 31 | |
okini3939 | 2:9e108187ccfe | 32 | enum NECMSG { |
okini3939 | 2:9e108187ccfe | 33 | NECMSG_ACK = 0x00, |
okini3939 | 2:9e108187ccfe | 34 | NECMSG_NOACK = 0x01, |
okini3939 | 2:9e108187ccfe | 35 | NECMSG_SEARCH = 0x10, |
okini3939 | 2:9e108187ccfe | 36 | NECMSG_SEND_DAT = 0x11, |
okini3939 | 2:9e108187ccfe | 37 | NECMSG_RESEND = 0x12, |
okini3939 | 2:9e108187ccfe | 38 | NECMSG_SEND_NOACK = 0x13, |
okini3939 | 2:9e108187ccfe | 39 | NECMSG_ENERGY_DETECT = 0x16, |
okini3939 | 2:9e108187ccfe | 40 | NECMSG_SEND_CMD = 0x17, |
okini3939 | 2:9e108187ccfe | 41 | NECMSG_WRITE_RFCONF = 0x21, |
okini3939 | 2:9e108187ccfe | 42 | NECMSG_READ_RSSI = 0x24, |
okini3939 | 2:9e108187ccfe | 43 | NECMSG_READ_CONFIG = 0x29, |
okini3939 | 2:9e108187ccfe | 44 | NECMSG_WRITE_CONFIG = 0x2A, |
okini3939 | 2:9e108187ccfe | 45 | NECMSG_READ_DEFAULT = 0x7D, |
okini3939 | 2:9e108187ccfe | 46 | NECMSG_WRITE_DEFAULT = 0x7E, |
okini3939 | 2:9e108187ccfe | 47 | NECMSG_RESET = 0x77, |
okini3939 | 2:9e108187ccfe | 48 | }; |
okini3939 | 2:9e108187ccfe | 49 | |
okini3939 | 2:9e108187ccfe | 50 | enum NECPWR { |
okini3939 | 2:9e108187ccfe | 51 | PWR_LOW = 0x00, |
okini3939 | 2:9e108187ccfe | 52 | PWR_MID = 0x01, |
okini3939 | 2:9e108187ccfe | 53 | PWR_HIGH = 0x02, |
okini3939 | 2:9e108187ccfe | 54 | PWR_MAX = 0x03, |
okini3939 | 2:9e108187ccfe | 55 | }; |
okini3939 | 2:9e108187ccfe | 56 | |
okini3939 | 2:9e108187ccfe | 57 | enum NECBAUD { |
okini3939 | 2:9e108187ccfe | 58 | BAUD_9600 = 0x02, |
okini3939 | 2:9e108187ccfe | 59 | BAUD_50k = 0x06, |
okini3939 | 2:9e108187ccfe | 60 | BAUD_100k = 0x07, |
okini3939 | 2:9e108187ccfe | 61 | }; |
okini3939 | 2:9e108187ccfe | 62 | |
okini3939 | 2:9e108187ccfe | 63 | enum NECUART { |
okini3939 | 2:9e108187ccfe | 64 | UART_4800 = 0x01, |
okini3939 | 2:9e108187ccfe | 65 | UART_9600 = 0x02, |
okini3939 | 2:9e108187ccfe | 66 | UART_19200 = 0x04, |
okini3939 | 2:9e108187ccfe | 67 | UART_38400 = 0x05, |
okini3939 | 2:9e108187ccfe | 68 | UART_56700 = 0x06, |
okini3939 | 2:9e108187ccfe | 69 | UART_115200 = 0x08, |
okini3939 | 2:9e108187ccfe | 70 | }; |
okini3939 | 2:9e108187ccfe | 71 | |
okini3939 | 2:9e108187ccfe | 72 | struct ifMessage { |
okini3939 | 2:9e108187ccfe | 73 | uint16_t start; |
okini3939 | 2:9e108187ccfe | 74 | uint8_t length; |
okini3939 | 2:9e108187ccfe | 75 | uint8_t msgid; |
okini3939 | 2:9e108187ccfe | 76 | uint8_t msgno; |
okini3939 | 2:9e108187ccfe | 77 | uint32_t dstid; |
okini3939 | 2:9e108187ccfe | 78 | uint32_t srcid; |
okini3939 | 2:9e108187ccfe | 79 | uint8_t parameter[NEC_MAXLENGTH + 2]; |
okini3939 | 2:9e108187ccfe | 80 | } __attribute__((packed)); |
okini3939 | 2:9e108187ccfe | 81 | |
okini3939 | 0:337524afec9b | 82 | /** |
okini3939 | 0:337524afec9b | 83 | * Default constructor |
okini3939 | 0:337524afec9b | 84 | */ |
okini3939 | 0:337524afec9b | 85 | NECnfc (PinName p_tx, PinName p_rx, PinName p_reset); |
okini3939 | 0:337524afec9b | 86 | |
okini3939 | 0:337524afec9b | 87 | /** |
okini3939 | 0:337524afec9b | 88 | * configuration |
okini3939 | 0:337524afec9b | 89 | */ |
okini3939 | 2:9e108187ccfe | 90 | int begin (int ch, NECBAUD baud = BAUD_50k, NECUART uart = UART_38400, NECPWR power = PWR_LOW, NECTYPE type = TYPE_920MHz); |
okini3939 | 0:337524afec9b | 91 | /** |
okini3939 | 0:337524afec9b | 92 | * send |
okini3939 | 0:337524afec9b | 93 | */ |
okini3939 | 2:9e108187ccfe | 94 | int send (NECMSG msgid, unsigned long dest, char *param, int len); |
okini3939 | 0:337524afec9b | 95 | /** |
okini3939 | 0:337524afec9b | 96 | * read |
okini3939 | 0:337524afec9b | 97 | */ |
okini3939 | 0:337524afec9b | 98 | int read (struct ifMessage *ifmsg, int timeout = 5000); |
okini3939 | 0:337524afec9b | 99 | /** |
okini3939 | 0:337524afec9b | 100 | * read data ready |
okini3939 | 0:337524afec9b | 101 | */ |
okini3939 | 0:337524afec9b | 102 | int readable (); |
okini3939 | 0:337524afec9b | 103 | |
okini3939 | 0:337524afec9b | 104 | protected: |
okini3939 | 0:337524afec9b | 105 | void isr_rx (); |
okini3939 | 0:337524afec9b | 106 | |
okini3939 | 0:337524afec9b | 107 | private: |
okini3939 | 0:337524afec9b | 108 | Serial rf; |
okini3939 | 0:337524afec9b | 109 | DigitalOut reset; |
okini3939 | 0:337524afec9b | 110 | int msgno; |
okini3939 | 0:337524afec9b | 111 | struct ifMessage rxmsg; |
okini3939 | 0:337524afec9b | 112 | char *rxbuf; |
okini3939 | 0:337524afec9b | 113 | int rxlen, rxmode; |
okini3939 | 0:337524afec9b | 114 | volatile int rxflg; |
okini3939 | 0:337524afec9b | 115 | |
okini3939 | 0:337524afec9b | 116 | }; |