|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file X_NUCLEO_NFC03A1_HelloWorld.ino |
| 4 | + * @author AST |
| 5 | + * @version V1.0.0 |
| 6 | + * @date 6 December 2017 |
| 7 | + * @brief Arduino test application for the STMicrolectronics X-NUCLEO-NFC03A1 |
| 8 | + * NFC reader/writer expansion board. |
| 9 | + * This application makes use of C++ classes obtained from the C |
| 10 | + * components' drivers. |
| 11 | + ****************************************************************************** |
| 12 | + * @attention |
| 13 | + * |
| 14 | + * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
| 15 | + * |
| 16 | + * Redistribution and use in source and binary forms, with or without modification, |
| 17 | + * are permitted provided that the following conditions are met: |
| 18 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 19 | + * this list of conditions and the following disclaimer. |
| 20 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 21 | + * this list of conditions and the following disclaimer in the documentation |
| 22 | + * and/or other materials provided with the distribution. |
| 23 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 24 | + * may be used to endorse or promote products derived from this software |
| 25 | + * without specific prior written permission. |
| 26 | + * |
| 27 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 28 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 30 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 31 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 32 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 33 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 34 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 35 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 36 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | + * |
| 38 | + ****************************************************************************** |
| 39 | + */ |
| 40 | + |
| 41 | + |
| 42 | +/* Includes ------------------------------------------------------------------*/ |
| 43 | +#include "stdint.h" |
| 44 | +#include "stdbool.h" |
| 45 | +#include "string.h" |
| 46 | +#include "lib_NDEF_URI.h" |
| 47 | +#include "lib_NDEF_SMS.h" |
| 48 | +#include "lib_NDEF_Text.h" |
| 49 | +#include "lib_NDEF_Email.h" |
| 50 | +#include "lib_NDEF_Geo.h" |
| 51 | +#include "lib_95HFConfigManager.h" |
| 52 | +#include "miscellaneous.h" |
| 53 | +#include "lib_95HFConfigManager.h" |
| 54 | +#include "lib_wrapper.h" |
| 55 | +#include "lib_NDEF_URI.h" |
| 56 | +#include "drv_spi.h" |
| 57 | + |
| 58 | +#define SerialPort Serial |
| 59 | + |
| 60 | +/* Exported define -----------------------------------------------------------*/ |
| 61 | +#define BULK_MAX_PACKET_SIZE 0x00000040 |
| 62 | + |
| 63 | +/* Regarding board antenna (and matching) appropriate |
| 64 | +value may be modified to optimized RF performances */ |
| 65 | +/* Analogue configuration register |
| 66 | + ARConfigB bits 7:4 MOD_INDEX Modulation index to modulator |
| 67 | + 3:0 RX_AMP_GAIN Defines receiver amplifier gain |
| 68 | +For type A you can also adjust the Timer Window |
| 69 | +*/ |
| 70 | + |
| 71 | +/****************** PICC ******************/ |
| 72 | +/* ISO14443A */ |
| 73 | +#define PICC_TYPEA_ACConfigA 0x27 /* backscaterring */ |
| 74 | + |
| 75 | +/* ISO14443B */ |
| 76 | +#define PICC_TYPEB_ARConfigD 0x0E /* card demodulation gain */ |
| 77 | +#define PICC_TYPEB_ACConfigA 0x17 /* backscaterring */ |
| 78 | + |
| 79 | +/* Felica */ |
| 80 | +#define PICC_TYPEF_ACConfigA 0x17 /* backscaterring */ |
| 81 | + |
| 82 | +/* Private variables ---------------------------------------------------------*/ |
| 83 | + |
| 84 | +/* TT1 (PCD only)*/ |
| 85 | +uint8_t TT1Tag[NFCT1_MAX_TAGMEMORY]; |
| 86 | + |
| 87 | +/* TT2 */ |
| 88 | +uint8_t TT2Tag[NFCT2_MAX_TAGMEMORY]; |
| 89 | + |
| 90 | +/* TT3 */ |
| 91 | +uint8_t TT3Tag[NFCT3_MAX_TAGMEMORY]; |
| 92 | +uint8_t *TT3AttribInfo = TT3Tag, *TT3NDEFfile = &TT3Tag[NFCT3_ATTRIB_INFO_SIZE]; |
| 93 | + |
| 94 | +/* TT4 */ |
| 95 | +uint8_t CardCCfile [NFCT4_MAX_CCMEMORY]; |
| 96 | +uint8_t CardNDEFfileT4A [NFCT4_MAX_NDEFMEMORY]; |
| 97 | +uint8_t CardNDEFfileT4B [NFCT4_MAX_NDEFMEMORY]; |
| 98 | + |
| 99 | +/* TT5 (PCD only)*/ |
| 100 | +uint8_t TT5Tag[NFCT5_MAX_TAGMEMORY]; |
| 101 | + |
| 102 | +sURI_Info url; |
| 103 | +extern uint8_t NDEF_Buffer []; |
| 104 | +extern DeviceMode_t devicemode; |
| 105 | + |
| 106 | +sRecordInfo_uri RecordStruct; |
| 107 | + |
| 108 | +int8_t TagType = TRACK_NOTHING; |
| 109 | +bool TagDetected = false; |
| 110 | +bool terminal_msg_flag = false ; |
| 111 | +uint8_t status = ERRORCODE_GENERIC; |
| 112 | +static char dataOut[256]; |
| 113 | + |
| 114 | +#define X_NUCLEO_NFC03A1_LED1 D7 |
| 115 | +#define X_NUCLEO_NFC03A1_LED2 D6 |
| 116 | +#define X_NUCLEO_NFC03A1_LED3 D5 |
| 117 | +#define X_NUCLEO_NFC03A1_LED4 D4 |
| 118 | + |
| 119 | +void setup() { |
| 120 | + // 95HF HW Init |
| 121 | + ConfigManager_HWInit(); |
| 122 | + |
| 123 | + // LED1 |
| 124 | + pinMode(X_NUCLEO_NFC03A1_LED1, OUTPUT); |
| 125 | + |
| 126 | + // LED2 |
| 127 | + pinMode(X_NUCLEO_NFC03A1_LED2, OUTPUT); |
| 128 | + |
| 129 | + // LED3 |
| 130 | + pinMode(X_NUCLEO_NFC03A1_LED3, OUTPUT); |
| 131 | + |
| 132 | + // LED4 |
| 133 | + pinMode(X_NUCLEO_NFC03A1_LED4, OUTPUT); |
| 134 | + |
| 135 | + // Configure USB serial interface |
| 136 | + SerialPort.begin(115200); |
| 137 | + |
| 138 | + SerialPort.print("\r\n\r\n---------------------------------------\r\n******Welcome to x-nucleo-nfc03a1 demo******\r\n----------------------------------------"); |
| 139 | + SerialPort.print("\r\n\r\nPlease bring an NFC tag to the board vicinity and Press User Button B1 on the board to start URI Writer/Reader demo on the tag"); |
| 140 | + |
| 141 | + terminal_msg_flag = true; |
| 142 | + |
| 143 | + digitalWrite(X_NUCLEO_NFC03A1_LED1, HIGH); |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +/* Loop ----------------------------------------------------------------------*/ |
| 148 | + |
| 149 | +void loop() |
| 150 | +{ |
| 151 | + devicemode = PCD; |
| 152 | + |
| 153 | + /* Scan to find if there is a tag */ |
| 154 | + TagType = ConfigManager_TagHunting(TRACK_ALL); |
| 155 | + |
| 156 | + switch(TagType) |
| 157 | + { |
| 158 | + case TRACK_NFCTYPE1: |
| 159 | + { |
| 160 | + TagDetected = true; |
| 161 | + |
| 162 | + if(terminal_msg_flag == true ) |
| 163 | + { |
| 164 | + terminal_msg_flag = false ; |
| 165 | + /*---HT UI msg----------*/ |
| 166 | + SerialPort.print( "\r\n\r\nTRACK_NFCTYPE1 NFC tag detected nearby"); |
| 167 | + digitalWrite(X_NUCLEO_NFC03A1_LED2, HIGH); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + break; |
| 172 | + case TRACK_NFCTYPE2: |
| 173 | + { |
| 174 | + TagDetected = true; |
| 175 | + |
| 176 | + if(terminal_msg_flag == true ) |
| 177 | + { |
| 178 | + terminal_msg_flag = false ; |
| 179 | + |
| 180 | + /*---HT UI msg----------*/ |
| 181 | + SerialPort.print( "\r\n\r\nTRACK_NFCTYPE2 NFC tag detected nearby"); |
| 182 | + digitalWrite(X_NUCLEO_NFC03A1_LED2, HIGH); |
| 183 | + } |
| 184 | + } |
| 185 | + break; |
| 186 | + |
| 187 | + case TRACK_NFCTYPE3: |
| 188 | + { |
| 189 | + TagDetected = true; |
| 190 | + |
| 191 | + if(terminal_msg_flag == true ) |
| 192 | + { |
| 193 | + terminal_msg_flag = false ; |
| 194 | + |
| 195 | + /*---HT UI msg----------*/ |
| 196 | + SerialPort.print( "\r\n\r\nTRACK_NFCTYPE3 NFC tag detected nearby"); |
| 197 | + digitalWrite(X_NUCLEO_NFC03A1_LED2, HIGH); |
| 198 | + } |
| 199 | + } |
| 200 | + break; |
| 201 | + |
| 202 | + case TRACK_NFCTYPE4A: |
| 203 | + { |
| 204 | + TagDetected = true; |
| 205 | + |
| 206 | + if(terminal_msg_flag == true ) |
| 207 | + { |
| 208 | + terminal_msg_flag = false ; |
| 209 | + |
| 210 | + /*---HT UI msg----------*/ |
| 211 | + SerialPort.print( "\r\n\r\nTRACK_NFCTYPE4A NFC tag detected nearby"); |
| 212 | + digitalWrite(X_NUCLEO_NFC03A1_LED2, HIGH); |
| 213 | + } |
| 214 | + } |
| 215 | + break; |
| 216 | + |
| 217 | + case TRACK_NFCTYPE4B: |
| 218 | + { |
| 219 | + TagDetected = true; |
| 220 | + |
| 221 | + if(terminal_msg_flag == true ) |
| 222 | + { |
| 223 | + terminal_msg_flag = false ; |
| 224 | + |
| 225 | + /*---HT UI msg----------*/ |
| 226 | + SerialPort.print( "\r\n\r\nTRACK_NFCTYPE4B NFC tag detected nearby"); |
| 227 | + digitalWrite(X_NUCLEO_NFC03A1_LED2, HIGH); |
| 228 | + } |
| 229 | + } |
| 230 | + break; |
| 231 | + |
| 232 | + case TRACK_NFCTYPE5: |
| 233 | + { |
| 234 | + TagDetected = true; |
| 235 | + |
| 236 | + if(terminal_msg_flag == true ) |
| 237 | + { |
| 238 | + terminal_msg_flag = false ; |
| 239 | + |
| 240 | + /*---HT UI msg----------*/ |
| 241 | + SerialPort.print( "\r\n\r\nTRACK_NFCTYPE5 NFC tag detected nearby"); |
| 242 | + digitalWrite(X_NUCLEO_NFC03A1_LED2, HIGH); |
| 243 | + } |
| 244 | + } |
| 245 | + break; |
| 246 | + |
| 247 | + default: |
| 248 | + { |
| 249 | + TagDetected = false; |
| 250 | + |
| 251 | + if(terminal_msg_flag == false) |
| 252 | + { |
| 253 | + terminal_msg_flag = true ; |
| 254 | + /*---HT UI msg----------*/ |
| 255 | + SerialPort.print( "\r\n\r\nCurrently there is no NFC tag in the vicinity"); |
| 256 | + digitalWrite(X_NUCLEO_NFC03A1_LED2, LOW); |
| 257 | + digitalWrite(X_NUCLEO_NFC03A1_LED3, LOW); |
| 258 | + digitalWrite(X_NUCLEO_NFC03A1_LED4, LOW); |
| 259 | + } |
| 260 | + } |
| 261 | + break; |
| 262 | + } |
| 263 | + |
| 264 | + delay(300); |
| 265 | + |
| 266 | + if (TagDetected == true) |
| 267 | + { |
| 268 | + TagDetected = false; |
| 269 | + |
| 270 | + /* Fill the structure of the NDEF URI */ |
| 271 | + strcpy(url.Information,"ST website for near field communication"); |
| 272 | + strcpy(url.protocol,"http://"); |
| 273 | + strcpy(url.URI_Message,"st.com/st25"); |
| 274 | + |
| 275 | + status = NDEF_WriteURI(&url); |
| 276 | + |
| 277 | + delay(500); |
| 278 | + |
| 279 | + if(status == RESULTOK) /*---if URI write passed----------*/ |
| 280 | + { |
| 281 | + status = ERRORCODE_GENERIC; |
| 282 | + |
| 283 | + snprintf( dataOut, 256, "\r\n\r\n--------------------\r\n*****URI Writer*****\r\n--------------------\r\nURI Information written successfully on the tag: \r\n URI Information: [%s], \r\n URI Protocol: [%s] , \r\n URI Message: [%s]", (char *)url.Information, (char *)url.protocol, (char *)url.URI_Message ); |
| 284 | + SerialPort.print( dataOut ); |
| 285 | + |
| 286 | + digitalWrite(X_NUCLEO_NFC03A1_LED3, HIGH); |
| 287 | + |
| 288 | + memset(url.Information,'\0',400); /*Clear url buffer before reading*/ |
| 289 | + |
| 290 | + if (TagType == TRACK_NFCTYPE1) |
| 291 | + { |
| 292 | + status = PCDNFCT1_ReadNDEF(); |
| 293 | + } else if (TagType == TRACK_NFCTYPE2) |
| 294 | + { |
| 295 | + status = PCDNFCT2_ReadNDEF(); |
| 296 | + } else if (TagType == TRACK_NFCTYPE3) |
| 297 | + { |
| 298 | + status = PCDNFCT3_ReadNDEF(); |
| 299 | + } else if (TagType == TRACK_NFCTYPE4A || TagType == TRACK_NFCTYPE4B) |
| 300 | + { |
| 301 | + status = PCDNFCT4_ReadNDEF(); |
| 302 | + } else if (TagType == TRACK_NFCTYPE5) |
| 303 | + { |
| 304 | + status = PCDNFCT5_ReadNDEF(); |
| 305 | + } |
| 306 | + |
| 307 | + if ( status == RESULTOK ) |
| 308 | + { |
| 309 | + status = ERRORCODE_GENERIC; |
| 310 | + |
| 311 | + memset(NDEF_Buffer,'\0',20); /* Avoid printing useless characters */ |
| 312 | + status = NDEF_IdentifyNDEF( &RecordStruct, NDEF_Buffer); |
| 313 | + |
| 314 | + if(status == RESULTOK && RecordStruct.TypeLength != 0) |
| 315 | + { |
| 316 | + if (NDEF_ReadURI(&RecordStruct, &url)==RESULTOK) /*---if URI read passed---*/ |
| 317 | + { |
| 318 | + snprintf( dataOut, 256, "\r\n\r\n--------------------\r\n*****URI Reader*****\r\n--------------------\r\nURI Information read successfully from the tag: \r\n URI Information: [%s], \r\n URI Protocol: [%s] , \r\n URI Message: [%s]", (char *)url.Information, (char *)url.protocol, (char *)url.URI_Message ); |
| 319 | + |
| 320 | + SerialPort.print( dataOut ); |
| 321 | + |
| 322 | + digitalWrite(X_NUCLEO_NFC03A1_LED4, HIGH); |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + } |
| 327 | + } |
| 328 | +} |
| 329 | + |
0 commit comments