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:
Sat Jun 13 01:57:30 2020 +0000
Revision:
2:08ccf5062b69
Parent:
0:16a4197a4dfb
Add timeout retry when UART communication fails

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