SONY製のNFCカードリーダ RC-S620/S をイニシエータとして使用し、NFC Type-A/Bと通信するライブラリです。FeliCaとは通信できません。(This library can't communicate with FeliCa! Only for NFC Type-A/B.)

Dependents:   DLC_STARTER

Committer:
hmizuno
Date:
Fri May 29 09:12:00 2020 +0000
Revision:
0:16a4197a4dfb
Child:
2:08ccf5062b69
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hmizuno 0:16a4197a4dfb 1 #ifndef RCS620S_AB_H
hmizuno 0:16a4197a4dfb 2 #define RCS620S_AB_H
hmizuno 0:16a4197a4dfb 3
hmizuno 0:16a4197a4dfb 4 #include "mbed.h"
hmizuno 0:16a4197a4dfb 5
hmizuno 0:16a4197a4dfb 6 //コマンドは別ファイルへ保存
hmizuno 0:16a4197a4dfb 7 #include "RCS620S_command.h"
hmizuno 0:16a4197a4dfb 8
hmizuno 0:16a4197a4dfb 9 /**
hmizuno 0:16a4197a4dfb 10 @def READ_BUF_SIZE
hmizuno 0:16a4197a4dfb 11 受信バッファのバイト数 (Number of bytes in the receive buffer)
hmizuno 0:16a4197a4dfb 12 */
hmizuno 0:16a4197a4dfb 13 #define READ_BUF_SIZE 256
hmizuno 0:16a4197a4dfb 14
hmizuno 0:16a4197a4dfb 15
hmizuno 0:16a4197a4dfb 16 /** RCS620S_AB class.
hmizuno 0:16a4197a4dfb 17 * RC-S620/Sをイニシエータとして使用しNFC Type-A/Bカードと通信するためのライブラリです。
hmizuno 0:16a4197a4dfb 18 * (This is library to communicate with NFC Type-A/B card by using RC-S620/S as an initiator.)
hmizuno 0:16a4197a4dfb 19 */
hmizuno 0:16a4197a4dfb 20 class RCS620S_AB{
hmizuno 0:16a4197a4dfb 21 public:
hmizuno 0:16a4197a4dfb 22 /**
hmizuno 0:16a4197a4dfb 23 * @brief クラス内受信バッファのバイト数です。(The number of bytes in the receive buffer within the class.)
hmizuno 0:16a4197a4dfb 24 *
hmizuno 0:16a4197a4dfb 25 * マクロで定義した READ_BUF_SIZE の値が代入されています。クラス呼び出し元でバッファ配列初期化時の要素数定義にご利用ください。
hmizuno 0:16a4197a4dfb 26 * (The value of READ_BUF_SIZE defined by the macro is assigned.
hmizuno 0:16a4197a4dfb 27 * Please use it to define the number of elements when the buffer array is initialized by the class caller.)
hmizuno 0:16a4197a4dfb 28 */
hmizuno 0:16a4197a4dfb 29 static const int RETURN_ENVELOPE_SIZE = READ_BUF_SIZE;
hmizuno 0:16a4197a4dfb 30
hmizuno 0:16a4197a4dfb 31 /**
hmizuno 0:16a4197a4dfb 32 * @brief ボーレートは115200固定です(RC-S620/S デフォルト値)。(The baud rate is fixed to 115200 (RC-S620/S default value).)
hmizuno 0:16a4197a4dfb 33 * @param tx UART TX pin
hmizuno 0:16a4197a4dfb 34 * @param rx UART RX pin
hmizuno 0:16a4197a4dfb 35 */
hmizuno 0:16a4197a4dfb 36 RCS620S_AB(PinName tx, PinName rx);
hmizuno 0:16a4197a4dfb 37
hmizuno 0:16a4197a4dfb 38 /***************/
hmizuno 0:16a4197a4dfb 39 /**** 制御系 ****/
hmizuno 0:16a4197a4dfb 40 /***************/
hmizuno 0:16a4197a4dfb 41
hmizuno 0:16a4197a4dfb 42 /**
hmizuno 0:16a4197a4dfb 43 * @brief パワーダウンコマンドを送信します。
hmizuno 0:16a4197a4dfb 44 * (Send powerDown command.)
hmizuno 0:16a4197a4dfb 45 *
hmizuno 0:16a4197a4dfb 46 * wakeup条件はUARTを指定しています。変更される場合、RCS620S_command.h ファイル内の wired_data_RFConfiguration_PowerDown[] を変更ください。
hmizuno 0:16a4197a4dfb 47 *
hmizuno 0:16a4197a4dfb 48 * (The wakeup condition specifies UART.
hmizuno 0:16a4197a4dfb 49 * If you want to change it, please change "wired_data_RFConfiguration_PowerDown[]" in "RCS620S_command.h".)
hmizuno 0:16a4197a4dfb 50 */
hmizuno 0:16a4197a4dfb 51 void powerDown();
hmizuno 0:16a4197a4dfb 52
hmizuno 0:16a4197a4dfb 53 /**
hmizuno 0:16a4197a4dfb 54 * @brief ウェイクアップコマンド(0x55)を送信します(Send wakeUp command(0x55).)
hmizuno 0:16a4197a4dfb 55 */
hmizuno 0:16a4197a4dfb 56 void wakeUp();
hmizuno 0:16a4197a4dfb 57
hmizuno 0:16a4197a4dfb 58 /**
hmizuno 0:16a4197a4dfb 59 * @brief RF停止コマンド送信後、リセットコマンドを送信します。
hmizuno 0:16a4197a4dfb 60 * (Send not only Reset command but also RFoff command.)
hmizuno 0:16a4197a4dfb 61 *
hmizuno 0:16a4197a4dfb 62 * RF停止は、"カードを"リセットするために送信します。
hmizuno 0:16a4197a4dfb 63 *
hmizuno 0:16a4197a4dfb 64 * (RFoff is used to reset a "card".)
hmizuno 0:16a4197a4dfb 65 */
hmizuno 0:16a4197a4dfb 66 void reset();
hmizuno 0:16a4197a4dfb 67
hmizuno 0:16a4197a4dfb 68 /***************/
hmizuno 0:16a4197a4dfb 69 /**** 受信系 ****/
hmizuno 0:16a4197a4dfb 70 /***************/
hmizuno 0:16a4197a4dfb 71
hmizuno 0:16a4197a4dfb 72 /**
hmizuno 0:16a4197a4dfb 73 * @brief 受信バッファ中の全データ(ACK~レスポンスのポストアンブルまで)を取得します。主としてデバッグ用です。
hmizuno 0:16a4197a4dfb 74 * (Get all data (ACK to response postamble) in the receive buffer. Mainly for debugging use.)
hmizuno 0:16a4197a4dfb 75 * @param ansArray[] データを返すための配列へのポインタ。サイズの初期化には RETURN_ENVELOPE_SIZE をご利用ください。
hmizuno 0:16a4197a4dfb 76 * (A pointer to an array to return the data. Please use RETURN_ENVELOPE_SIZE to initialize the size.)
hmizuno 0:16a4197a4dfb 77 * @param *ansLen データ長(バイト数)を返すための変数へのポインタ。(Pointer to a variable to return the data length(number of bytes).)
hmizuno 0:16a4197a4dfb 78 */
hmizuno 0:16a4197a4dfb 79 void getAllRes(uint8_t ansArray[], int *ansLen);
hmizuno 0:16a4197a4dfb 80
hmizuno 0:16a4197a4dfb 81 /**
hmizuno 0:16a4197a4dfb 82 * @brief 受信バッファからカードリーダのレスポンスデータを取得します。
hmizuno 0:16a4197a4dfb 83 * (Get the response data of the card reader from the receive buffer.)
hmizuno 0:16a4197a4dfb 84 * @param ansArray[] データを返すための配列へのポインタ。サイズの初期化には RETURN_ENVELOPE_SIZE をご利用ください。
hmizuno 0:16a4197a4dfb 85 * (A pointer to an array to return the data. Please use RETURN_ENVELOPE_SIZE to initialize the size.)
hmizuno 0:16a4197a4dfb 86 * @param *ansLen データ長(バイト数)を返すための変数へのポインタ。(Pointer to a variable to return the data length(number of bytes).)
hmizuno 0:16a4197a4dfb 87 */
hmizuno 0:16a4197a4dfb 88 void getRes(uint8_t ansArray[], int *ansLen);
hmizuno 0:16a4197a4dfb 89
hmizuno 0:16a4197a4dfb 90 /**
hmizuno 0:16a4197a4dfb 91 * @brief inListPasseveTarget 及び inDataExchange レスポンスからカードからのレスポンス部だけを取得します。前記2つのコマンド以外のレスポンスに対しては使用できません。
hmizuno 0:16a4197a4dfb 92 * (Get only the response part from the card is acquired from the inListPasseveTarget and inDataExchange responses.
hmizuno 0:16a4197a4dfb 93 * It cannot be used for responses other than the above two commands.)
hmizuno 0:16a4197a4dfb 94 * @param ansArray[] データを返すための配列へのポインタ。サイズの初期化には RETURN_ENVELOPE_SIZE をご利用ください。
hmizuno 0:16a4197a4dfb 95 * (A pointer to an array to return the data. Please use RETURN_ENVELOPE_SIZE to initialize the size.)
hmizuno 0:16a4197a4dfb 96 * @param *ansLen データ長(バイト数)を返すための変数へのポインタ。(Pointer to a variable to return the data length(number of bytes).)
hmizuno 0:16a4197a4dfb 97 */
hmizuno 0:16a4197a4dfb 98 void getCardRes(uint8_t ansArray[], int *ansLen);
hmizuno 0:16a4197a4dfb 99
hmizuno 0:16a4197a4dfb 100 /***************/
hmizuno 0:16a4197a4dfb 101 /**** 送信系 ****/
hmizuno 0:16a4197a4dfb 102 /***************/
hmizuno 0:16a4197a4dfb 103
hmizuno 0:16a4197a4dfb 104 /**
hmizuno 0:16a4197a4dfb 105 * @brief カードリーダへコマンドを送信し、レスポンスを受信します。受信完了までwait()します。本メソッド実行後にgetRse()でレスポンスデータを取出してください。
hmizuno 0:16a4197a4dfb 106 * (This method sends commands to the card reader and receives responses. Wait() until the reception is completed.
hmizuno 0:16a4197a4dfb 107 * After executing this method, please use the getRse() to retrieve the response data.)
hmizuno 0:16a4197a4dfb 108 *
hmizuno 0:16a4197a4dfb 109 * 【重要】送受信中に、割り込み処理や別の重い処理が発生しないようご配慮ください。
hmizuno 0:16a4197a4dfb 110 * レスポンスを受信完了するまでwait()で処理を停止しますが、割り込みで別の重い処理が走るとデータを取りこぼします。
hmizuno 0:16a4197a4dfb 111 * ただし、レスポンスの受信にUART受信割込みを使用しているため、割り込み禁止を使用する際はuartまで止めないようご注意ください。
hmizuno 0:16a4197a4dfb 112 *
hmizuno 0:16a4197a4dfb 113 * (IMPORTANT! Please take care not to cause interrupt processing or other heavy processing during transmission and reception.
hmizuno 0:16a4197a4dfb 114 * The process is stopped by wait () until the response is received, but the data is dropped when another heavy process runs by the interrupt.
hmizuno 0:16a4197a4dfb 115 * However, since the UART reception interrupt is used to receive the response, please be careful not to stop until uart when using the interrupt disable.)
hmizuno 0:16a4197a4dfb 116 *
hmizuno 0:16a4197a4dfb 117 * @param wired_packet_data[] コマンドのデータ部(0xD4~DCSの直前まで)
hmizuno 0:16a4197a4dfb 118 * (Command data part (From 0xD4 to just before DCS))
hmizuno 0:16a4197a4dfb 119 * @param wired_packet_data_len コマンドのデータ長(バイト数)。 sizeof(command array) で与えてください。
hmizuno 0:16a4197a4dfb 120 * (Data length(number of bytes) of command.Give it as sizeof (command array).)
hmizuno 0:16a4197a4dfb 121 */
hmizuno 0:16a4197a4dfb 122 void sendCommand_and_RecieveRes(const uint8_t wired_packet_data[], uint16_t wired_packet_data_len);
hmizuno 0:16a4197a4dfb 123
hmizuno 0:16a4197a4dfb 124 /**
hmizuno 0:16a4197a4dfb 125 * @brief InDataExchangeを使ってICカードへNFCコマンドを送信し、レスポンスを受信します。内部でsendCommand_and_RecieveRes()を呼び出しています。
hmizuno 0:16a4197a4dfb 126 * 本メソッド実行後にgetCardRes()またはgetRse()でレスポンスデータを取出してください。
hmizuno 0:16a4197a4dfb 127 * (NFC command is sent to the IC card via InDataExchange and the response is received.
hmizuno 0:16a4197a4dfb 128 * This method internally calls sendCommand_and_RecieveRes().
hmizuno 0:16a4197a4dfb 129 * After executing this method, please use getCardRes() or getRse() to retrieve the response data.)
hmizuno 0:16a4197a4dfb 130 *
hmizuno 0:16a4197a4dfb 131 * ※inDataExchange以外の方法でカードと通信したい場合、NFCコマンドを元に使いたいコマンドを組み立ててsendCommand_and_RecieveRes()で送信してください。
hmizuno 0:16a4197a4dfb 132 *
hmizuno 0:16a4197a4dfb 133 * (If you want to communicate with the card by a method other than inDataExchange,
hmizuno 0:16a4197a4dfb 134 * assemble the command you want to use based on the NFC command and send it with sendCommand_and_RecieveRes().)
hmizuno 0:16a4197a4dfb 135 *
hmizuno 0:16a4197a4dfb 136 * @param card_command[] NFCコマンド(NFC command)
hmizuno 0:16a4197a4dfb 137 * @param card_command_len NFCコマンドのデータ長(バイト数)。 sizeof(command array) で与えてください。
hmizuno 0:16a4197a4dfb 138 * (Data length(number of bytes) of NFC command.Give it as sizeof (command array).)
hmizuno 0:16a4197a4dfb 139 */
hmizuno 0:16a4197a4dfb 140 void sendInDataExchange_and_RecieveRes(const uint8_t card_command[], uint16_t card_command_len);
hmizuno 0:16a4197a4dfb 141
hmizuno 0:16a4197a4dfb 142 /**
hmizuno 0:16a4197a4dfb 143 * @brief inListPassiveTargetを使ってNFC-TypeAをポーリングし、レスポンスを受信します。内部でsendCommand_and_RecieveRes()を呼び出しています。
hmizuno 0:16a4197a4dfb 144 * 本メソッド実行後にgetCardRes()またはgetRse()でレスポンスデータを取出してください。
hmizuno 0:16a4197a4dfb 145 * (This method polls NFC-TypeA via inListPassiveTarget and receives the response. This method internally calls sendCommand_and_RecieveRes().
hmizuno 0:16a4197a4dfb 146 * After executing this method, please use getCardRes() or getRse() to retrieve the response data.)
hmizuno 0:16a4197a4dfb 147 *
hmizuno 0:16a4197a4dfb 148 * inListPassiveTargetのパラメータを変更したい場合は、"RCS620S_command.h"内の"wired_data_InListPassiveTarget_typeA[]"を書き換えてください。
hmizuno 0:16a4197a4dfb 149 * タイムアウト等の設定値はRC-S620/Sデフォルトのままです。必要に応じ別途RFConfigurationコマンドで設定してください。
hmizuno 0:16a4197a4dfb 150 *
hmizuno 0:16a4197a4dfb 151 * (If you want to change the parameters of inListPassiveTarget, rewrite "wired_data_InListPassiveTarget_typeA []" in "RCS620S_command.h".
hmizuno 0:16a4197a4dfb 152 * The set values such as timeout are the default of RC-S620/S. Please set it separately with the RF Configuration command if necessary.)
hmizuno 0:16a4197a4dfb 153 */
hmizuno 0:16a4197a4dfb 154 void sendInListPasseveTarget_typeA();
hmizuno 0:16a4197a4dfb 155
hmizuno 0:16a4197a4dfb 156 /**
hmizuno 0:16a4197a4dfb 157 * @brief inListPassiveTargetを使ってNFC-TypeBをポーリングし、レスポンスを受信します。内部でsendCommand_and_RecieveRes()を呼び出しています。
hmizuno 0:16a4197a4dfb 158 * 本メソッド実行後にgetCardRes()またはgetRse()でレスポンスデータを取出してください。
hmizuno 0:16a4197a4dfb 159 * (This method polls NFC-TypeB via inListPassiveTarget and receives the response. This method internally calls sendCommand_and_RecieveRes().
hmizuno 0:16a4197a4dfb 160 * After executing this method, please use getCardRes() or getRse() to retrieve the response data.)
hmizuno 0:16a4197a4dfb 161 *
hmizuno 0:16a4197a4dfb 162 * inListPassiveTargetのパラメータを変更したい場合は、"RCS620S_command.h"内の"wired_data_InListPassiveTarget_typeB[]"を書き換えてください。
hmizuno 0:16a4197a4dfb 163 * (AFI は 0x00 (用途を指定しない) を設定しています。)
hmizuno 0:16a4197a4dfb 164 *
hmizuno 0:16a4197a4dfb 165 * タイムアウト等の設定値はRC-S620/Sデフォルトのままです。必要に応じ別途RFConfigurationコマンドで設定してください。
hmizuno 0:16a4197a4dfb 166 *
hmizuno 0:16a4197a4dfb 167 * (If you want to change the parameters of inListPassiveTarget, rewrite "wired_data_InListPassiveTarget_typeA []" in "RCS620S_command.h".
hmizuno 0:16a4197a4dfb 168 * (AFI sets 0x00 (no purpose specified).)
hmizuno 0:16a4197a4dfb 169 *
hmizuno 0:16a4197a4dfb 170 * The set values such as timeout are the default of RC-S620/S. Please set it separately with the RF Configuration command if necessary.)
hmizuno 0:16a4197a4dfb 171 */
hmizuno 0:16a4197a4dfb 172 void sendInListPassiveTarget_typeB();
hmizuno 0:16a4197a4dfb 173
hmizuno 0:16a4197a4dfb 174 private:
hmizuno 0:16a4197a4dfb 175
hmizuno 0:16a4197a4dfb 176 Serial _serial;
hmizuno 0:16a4197a4dfb 177 uint8_t recieveBuffer[READ_BUF_SIZE];
hmizuno 0:16a4197a4dfb 178 int recieveLEN;
hmizuno 0:16a4197a4dfb 179
hmizuno 0:16a4197a4dfb 180 /***************/
hmizuno 0:16a4197a4dfb 181 /**** 制御系 ****/
hmizuno 0:16a4197a4dfb 182 /***************/
hmizuno 0:16a4197a4dfb 183
hmizuno 0:16a4197a4dfb 184 void initialize();
hmizuno 0:16a4197a4dfb 185
hmizuno 0:16a4197a4dfb 186 /***************/
hmizuno 0:16a4197a4dfb 187 /**** 受信系 ****/
hmizuno 0:16a4197a4dfb 188 /***************/
hmizuno 0:16a4197a4dfb 189
hmizuno 0:16a4197a4dfb 190 //カードリーダ UART受信割込み
hmizuno 0:16a4197a4dfb 191 void serialRxIrq();
hmizuno 0:16a4197a4dfb 192
hmizuno 0:16a4197a4dfb 193 void clearRecieveBuffer();
hmizuno 0:16a4197a4dfb 194
hmizuno 0:16a4197a4dfb 195 //読み取り完了まで待機
hmizuno 0:16a4197a4dfb 196 void waitWhileRead(int cycleTime_ms);
hmizuno 0:16a4197a4dfb 197
hmizuno 0:16a4197a4dfb 198 //受信バッファからrecieveBuffer[start] ~ recieveBuffer[end] を抜き出す
hmizuno 0:16a4197a4dfb 199 void extractRes(int start, int end, uint8_t ansArray[], int *ansLen);
hmizuno 0:16a4197a4dfb 200
hmizuno 0:16a4197a4dfb 201 /***************/
hmizuno 0:16a4197a4dfb 202 /**** 検査系 ****/
hmizuno 0:16a4197a4dfb 203 /***************/
hmizuno 0:16a4197a4dfb 204
hmizuno 0:16a4197a4dfb 205 bool checkACK();
hmizuno 0:16a4197a4dfb 206
hmizuno 0:16a4197a4dfb 207 bool checkCardRes();
hmizuno 0:16a4197a4dfb 208
hmizuno 0:16a4197a4dfb 209 //受信データをチェックしてOKならLENを返す、異常なら0を返す
hmizuno 0:16a4197a4dfb 210 uint8_t checkRxData_and_getLEN();
hmizuno 0:16a4197a4dfb 211 };
hmizuno 0:16a4197a4dfb 212
hmizuno 0:16a4197a4dfb 213
hmizuno 0:16a4197a4dfb 214
hmizuno 0:16a4197a4dfb 215 #endif