├── LICENSE
├── M24SR.cpp
├── M24SR.h
├── README.md
└── examples
├── DisplaySystemFile
└── DisplaySystemFile.ino
├── NDefWrite
└── NDefWrite.ino
└── NfcTagDisplay
└── NfcTagDisplay.ino
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
--------------------------------------------------------------------------------
/M24SR.cpp:
--------------------------------------------------------------------------------
1 | /* Arduino library for ST M24SR Dynamic NFC/RFID tag IC with EEPROM, NFC Forum Type 4 Tag and I2C interface
2 | (c) 2014 by ReNa http://regnerischernachmittag.wordpress.com/
3 | */
4 |
5 | #include
6 | #include
7 | #include
8 | #include //prog_char
9 |
10 | prog_char AID_NDEF_TAG_APPLICATION2[] PROGMEM = "\xD2\x76\x00\x00\x85\x01\x01";
11 | prog_char FILE_CC[] PROGMEM = "\xE1\x03";
12 | prog_char FILE_SYSTEM[] PROGMEM = "\xE1\x01";
13 | prog_char DEFAULT_PASSWORD[] PROGMEM = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
14 |
15 | //prog_char SELECT_NDEF_Tag_Application2[] PROGMEM = "\x00\xA4\x04\x00\x07\xD2\x76\x00\x00\x85\x01\x01";
16 | //prog_char SELECT_CC[] PROGMEM = "\x00\xA4\x00\x0C\x02\xE1\x03";
17 | //prog_char SELECT_NDEF_file[] PROGMEM = "\x00\xA4\x00\x0C\x02\x00\x01";
18 | //prog_char SELECT_system_file[] PROGMEM = "\x00\xA4\x00\x0C\x02\xE1\x01";
19 | //prog_char UPDATE_BINARY_NDEF_MSG_LEN0[] PROGMEM = "\x00\xD6\x00\x00\x02\x00\x00";
20 | //prog_char READ_BINARY_LENGTH[] PROGMEM = "\x02\x00\xB0\x00\x00\x02";
21 | //prog_char READ_BINARY[] PROGMEM = "\x02\x00\xB0\x00\x00";
22 | //prog_char READ_BINARY_DEF_MSG[] PROGMEM = "\x02\x00\xB0\x00\x02";
23 |
24 | // 0x45
25 | prog_char SAMPLE_NDEF_message1[] PROGMEM = "\x91\x01\x10\x55\x01\x73\x74\x2E\x63\x6F\x6D\x2F\x6E\x66\x63\x2D\x72\x66\x69\x64\x54\x18\x16\x73\x74\x2E\x63\x6F\x6D\x3A\x6D\x32\x34\x73\x72\x5F\x70\x72\x6F\x70\x72\x69\x65\x74\x61\x72\x73\x4D\x32\x34\x53\x52\x20\x70\x72\x6F\x70\x72\x69\x65\x74\x61\x72\x79\x20\x64\x61\x74\x61";
26 | //prog_char SAMPLE_NDEF_message2[] PROGMEM = "\xd1\x01\x0a\x55\x03\x6e\x6f\x6b\x69\x61\x2e\x63\x6f\x6d";
27 |
28 | // 0x0F www.st.com
29 | //prog_char SAMPLE_NDEF_message2[] PROGMEM = "\xD1\x01\x0b\x55\x01\x77\x77\x77\x2E\x73\x74\x2E\x63\x6F\x6D";
30 |
31 | // 0x0c www.nfc.com
32 | //prog_char SAMPLE_NDEF_message2[] PROGMEM = "\xD1\x01\x08\x55\x01\x6E\x66\x63\x2E\x63\x6F\x6D";
33 |
34 | // 0x11 phone: +358 9 1234567
35 | prog_char SAMPLE_NDEF_message2[] PROGMEM = "\xD1\x01\x0D\x55\x05\x2B\x33\x35\x38\x39\x31\x32\x33\x34\x35\x36\x37";
36 |
37 | // 0x13 txt: "hello, world"
38 | prog_char SAMPLE_NDEF_message3[] PROGMEM = "\xD1\x01\x0F\x54\x02\x65\x6e\x68\x65\x6c\x6c\x6f\x2c\x20\x77\x6f\x72\x6c\x64";
39 |
40 | // EMPTY Tag
41 | // 00 03 D0 00 00
42 |
43 | M24SR::M24SR(void) {
44 | _verbose = false;
45 | _cmds = false;
46 | }
47 |
48 | M24SR::M24SR(uint8_t gpo) {
49 | _verbose = false;
50 | _cmds = false;
51 | _gpo_pin = gpo;
52 | }
53 |
54 | void M24SR::_setup() {
55 | if (_verbose)
56 | Serial.println(F("_setup"));
57 | _lastGPO = 1;
58 | _sendGetI2cSession = true;
59 | _deviceaddress = 0x56;
60 | _blockNo = 0;
61 | _responseLength = 0x15;
62 | _response = (byte*)malloc(_responseLength);
63 |
64 | Wire.begin(); // join i2c bus (address optional for master)
65 | if (_gpo_pin != 0) {
66 | pinMode(_gpo_pin, INPUT);
67 | writeGPO(0x61);
68 | }
69 | }
70 |
71 | M24SR::~M24SR() {
72 | if (_responseLength)
73 | {
74 | free(_response);
75 | }
76 | }
77 |
78 | void M24SR::writeGPO(uint8_t value) {
79 | if (_verbose)
80 | Serial.println(F("\r\nwriteGPO"));
81 | if (!verifyI2cPassword()) {
82 | Serial.println(F("\r\nwrong password!!!"));
83 | return;
84 | }
85 | sendApdu_P(0x00, INS_SELECT_FILE, 0x00, 0x0C, 0x02, FILE_SYSTEM);
86 | receiveResponse(2 + 3);
87 |
88 | //write system file at offset 0x0004 GPO
89 | sendApdu(0x00, INS_UPDATE_BINARY, 0x00, 0x04, 0x01, &value);
90 | receiveResponse(2 + 3);
91 | sendDESELECT();
92 | }
93 |
94 | void M24SR::selectFile_NDEF_App() {
95 | if (_verbose)
96 | Serial.println(F("\r\nselectFile_NDEF_App"));
97 | _sendGetI2cSession = true;
98 | sendApdu_P(0x00, INS_SELECT_FILE, 0x04, 0x00, 0x07, AID_NDEF_TAG_APPLICATION2);
99 | receiveResponse(2 + 3);
100 | }
101 |
102 | boolean M24SR::verifyI2cPassword() {
103 | if (_verbose)
104 | Serial.println(F("\r\nverifyI2cPassword"));
105 | selectFile_NDEF_App();
106 | sendApdu_P(0x00, INS_VERIFY, 0x00, 0x03, 0x10, DEFAULT_PASSWORD);
107 | receiveResponse(2 + 3);
108 | return ((_response[0] == 0x90) && (_response[1] == 0));
109 | }
110 |
111 | boolean M24SR::checkGPOTrigger()
112 | {
113 | uint8_t newval = digitalRead(_gpo_pin);
114 | if (newval == 1 && _lastGPO == 0) {
115 | _lastGPO = newval;
116 | return true;
117 | }
118 | _lastGPO = newval;
119 | return false;
120 | }
121 |
122 | void M24SR::updateBinaryLen(int len) {
123 | if (_verbose)
124 | Serial.println(F("\r\nupdateBinaryLen"));
125 | uint8_t len_bytes[] = "\x00\x00";
126 | len_bytes[0] = (len >> 8) & 0xff;
127 | len_bytes[1] = (len & 0xff);
128 | sendApdu(0x00, INS_UPDATE_BINARY, 0x00, 0x00, 0x02, len_bytes);
129 | }
130 |
131 | void M24SR::updateBinary(char* Data, uint8_t len) {
132 | if (_verbose)
133 | Serial.println(F("\r\nupdateBinary"));
134 | dumpHex((uint8_t*)Data, len);
135 | uint8_t pos = 0;
136 | while(pos < len) {
137 | uint8_t chunk_len = (len - pos);
138 | if (chunk_len > (BUFFER_LENGTH - 8))
139 | chunk_len = BUFFER_LENGTH - 8;
140 | Serial.print(F("\r\nchunk_len:"));
141 | Serial.print(chunk_len, DEC);
142 | Serial.print(F(", pos:"));
143 | Serial.print(pos, DEC);
144 | sendApdu(0x00, INS_UPDATE_BINARY, ((pos+2) >> 8) & 0xff, (pos+2) & 0xff, chunk_len, (uint8_t*)&Data[pos]);
145 | pos += chunk_len;
146 | if (pos < len)
147 | receiveResponse(2 + 3);
148 | }
149 | }
150 |
151 | void M24SR::updateBinary(unsigned int offset, char* data, uint8_t len) {
152 | if (_verbose)
153 | Serial.println(F("\r\nupdateBinary offset"));
154 | sendApdu(0x00, INS_UPDATE_BINARY, (offset >> 8) & 0xff, (offset & 0xff), len, (uint8_t*)data);
155 | }
156 |
157 | void M24SR::displaySystemFile() {
158 | selectFile_NDEF_App();
159 | sendApdu_P(0x00, INS_SELECT_FILE, 0x00, 0x0C, 0x02, FILE_SYSTEM);
160 | receiveResponse(2 + 3);
161 |
162 | sendApdu(0x00, INS_READ_BINARY, 0x00, 0x00, 0x02);
163 | receiveResponse(2 + 2 + 3);
164 |
165 | if (_verbose) {
166 | Serial.print(F("\r\nresponse[1]: "));
167 | Serial.print(_response[1] & 0xff, HEX);
168 | }
169 |
170 | sendApdu(0x00, INS_READ_BINARY, 0x00, 0x00, _response[1]);
171 | receiveResponse((_response[1] & 0xff) + 2 + 3);
172 |
173 | //display settings
174 | Serial.print(F("\r\nUID: "));
175 | dumpHex(&_response[8], 7);
176 | Serial.print(F("\r\nMemory Size: 0x"));
177 | if ((_response[0xf] & 0xff) < 0x10)
178 | Serial.print("0");
179 | Serial.print((_response[0xf] & 0xff), HEX);
180 | if ((_response[0x10] & 0xff) < 0x10)
181 | Serial.print("0");
182 | Serial.print((_response[0x10] & 0xff), HEX);
183 | Serial.print(F("\r\nProduct Code: 0x"));
184 | if ((_response[0x11] & 0xff) < 0x10)
185 | Serial.print("0");
186 | Serial.print((_response[0x11] & 0xff), HEX);
187 |
188 | sendDESELECT();
189 | }
190 |
191 | /*
192 | end of a I2c Session:
193 | 5.4 S-Block format
194 | 0xC2: for S(DES) when the DID field is not present
195 | */
196 | void M24SR::sendDESELECT() {
197 | if (_verbose)
198 | Serial.print(F("\r\nsend DESELECT"));
199 | sendSBLOCK(0xC2);//PCB field
200 | }
201 |
202 | void M24SR::sendSBLOCK(byte sblock) {
203 | _data[0] = sblock;
204 | sendCommand(/*_data,*/ 1, false);
205 | receiveResponse(0 + 3) ;
206 | }
207 |
208 | void M24SR::sendApdu(uint8_t CLA, uint8_t INS, uint8_t P1, uint8_t P2, uint8_t Lc, uint8_t* Data) {
209 | _data[1] = CLA;
210 | _data[2] = INS;
211 | _data[3] = P1;
212 | _data[4] = P2;
213 | _data[5] = Lc;
214 | memcpy(&_data[6], Data, Lc);
215 | sendCommand(/*_data,*/ 1+5+Lc, true);
216 | }
217 |
218 | void M24SR::sendApdu_P(uint8_t CLA, uint8_t INS, uint8_t P1, uint8_t P2, uint8_t Lc, const prog_char* Data) {
219 | _data[1] = CLA;
220 | _data[2] = INS;
221 | _data[3] = P1;
222 | _data[4] = P2;
223 | _data[5] = Lc;
224 | memcpy_P(&_data[6], Data, Lc);
225 | sendCommand(/*_data,*/ 1+5+Lc, true);
226 | }
227 |
228 | //APDU case 2
229 | void M24SR::sendApdu(uint8_t CLA, uint8_t INS, uint8_t P1, uint8_t P2, uint8_t Le) {
230 | _data[1] = CLA;
231 | _data[2] = INS;
232 | _data[3] = P1;
233 | _data[4] = P2;
234 | _data[5] = Le;
235 | sendCommand(/*_data, */1+5, true);
236 | }
237 |
238 | void M24SR::sendCommand(/*char* data,*/ int len) {
239 | sendCommand(/*data,*/ len, true);
240 | }
241 |
242 | // len (incl PCB byte)
243 | void M24SR::sendCommand(/*char* data, */int len, boolean setPCB) {
244 | uint8_t v;
245 | if (setPCB) {
246 | if (_blockNo == 0) {
247 | _data[0] = 0x02;
248 | _blockNo = 1;
249 | } else {
250 | _data[0] = 0x03;
251 | _blockNo = 0;
252 | }
253 | }
254 | if (_sendGetI2cSession) {
255 | Wire.beginTransmission(_deviceaddress); // transmit to device 0x2D
256 | Wire.write(byte(CMD_GETI2CSESSION)); // GetI2Csession
257 | _err = Wire.endTransmission(); // stop transmitting
258 | if (_verbose) {
259 | Serial.print(F("\r\nGetI2Csession: "));
260 | Serial.print(_err, HEX);
261 | }
262 | else
263 | delay(1);
264 | }
265 |
266 | if (_cmds)
267 | Serial.print(F("\r\n=> "));
268 | else
269 | delay(1);
270 |
271 | Wire.beginTransmission(_deviceaddress);
272 |
273 | for(int i=0; i < len; i++) {
274 | v = (_data[i] & 0xff);
275 | if (_cmds) {
276 | if (v < 0x10)
277 | Serial.print(F("0"));
278 | Serial.print(v, HEX);
279 | } else
280 | delay(5);
281 | Wire.write(byte(v & 0xff));
282 | if (_cmds)
283 | Serial.print(F(" "));
284 | else
285 | delay(1);
286 | }
287 |
288 | //5.5 CRC of the I2C and RF frame ISO/IEC 13239. The initial register content shall be 0x6363
289 | int chksum = crcsum((unsigned char*) _data, len, 0x6363 );
290 |
291 | v = chksum & 0xff;
292 | if (_cmds) {
293 | if (v < 0x10) {
294 | Serial.print(F("0"));
295 | }
296 | Serial.print(v, HEX);
297 | } else
298 | delay(1);
299 |
300 | Wire.write(byte(v & 0xff));
301 |
302 | //EOD field
303 | v = (chksum >> 8) & 0xff;
304 | if (_cmds) {
305 | if (v < 0x10) {
306 | Serial.print(F("0"));
307 | }
308 | Serial.print(v, HEX);
309 | } else
310 | delay(1);
311 | Wire.write(byte(v & 0xff));
312 |
313 | _err = Wire.endTransmission();
314 | if (_cmds) {
315 | Serial.print(F("\r\n"));
316 | }
317 | else {
318 | delay(1);
319 | }
320 | //TODO does this really work?
321 | if (_err != 0) {
322 | Serial.print(F("write err: "));
323 | Serial.print(_err, HEX);
324 | }
325 | }
326 |
327 | void M24SR::selectFile_NDEF_file() {
328 | if (_verbose)
329 | Serial.print(F("\r\nselectFile_NDEF_file"));
330 | uint8_t ndef_file[] = "\x00\x01";
331 | sendApdu(0x00, INS_SELECT_FILE, 0x00, 0x0C, 0x02, ndef_file);
332 | receiveResponse(2 + 3);
333 | }
334 |
335 | void M24SR::updateBinary_NdefMsgLen0() {
336 | if (_verbose)
337 | Serial.print(F("\r\nupdateBinary_NdefMsgLen0"));
338 | uint8_t len0[] = "\x00\x00";
339 | sendApdu(0x00, INS_UPDATE_BINARY, 0x00, 0x00, 0x02, len0);
340 | receiveResponse(2 + 3);
341 | }
342 |
343 | void M24SR::writeSampleMsg(uint8_t msgNo) {
344 | if (_verbose) {
345 | Serial.print(F("\r\nwriteSampleMsg "));
346 | Serial.print(msgNo, DEC);
347 | Serial.println("");
348 | }
349 | byte len = 0;
350 | selectFile_NDEF_App();
351 | selectFile_NDEF_file();
352 | updateBinary_NdefMsgLen0();
353 |
354 | switch(msgNo) {
355 | case 0:
356 | len = 0x45;
357 | memcpy_P(&_data[0], &SAMPLE_NDEF_message1[0], len);
358 | break;
359 | case 1:
360 | len = 0x11;
361 | memcpy_P(&_data[0], &SAMPLE_NDEF_message2[0], len);
362 | break;
363 | case 2:
364 | len = 0x13;
365 | memcpy_P(&_data[0], &SAMPLE_NDEF_message3[0], len);
366 | break;
367 | default:
368 | break;
369 | }
370 | updateBinary(_data, len);
371 | receiveResponse(2 + 3);
372 |
373 | updateBinaryLen(len);
374 | receiveResponse(2 + 3);
375 | sendDESELECT();
376 | }
377 |
378 | NdefMessage* M24SR::getNdefMessage()
379 | {
380 | unsigned int len = 0;
381 |
382 | selectFile_NDEF_App();
383 | selectFile_NDEF_file();
384 | //Read NDEF message length 00 B0 00 00 02
385 | uint16_t ndef_len = getNdefMessageLength();
386 | if (_verbose) {
387 | Serial.print(F("\r\nndef_len: "));
388 | Serial.println(ndef_len, DEC);
389 | }
390 |
391 | if (ndef_len < 255)
392 | sendApdu(0x00, INS_READ_BINARY, 0x00, 0x02, ndef_len & 0xff);
393 | else
394 | {
395 | Serial.println(F("TODO: ndef_len > 255"));
396 | return (NdefMessage*)NULL;
397 | }
398 | //uint8_t ndeflen[2];
399 | //ndeflen[0] = ndef_len >> 8;
400 | //ndeflen[1] = ndef_len & 0xff;
401 | //sendApdu(0x00, INS_READ_BINARY, 0x00, 0x00, ndeflen);
402 | receiveResponse((_response[1] & 0xff) + 2 + 3);
403 | NdefMessage* pNdefMsg = new NdefMessage((byte *)&_response[0], ndef_len);
404 |
405 | /* TODO
406 | NdefRecord rec = pNdefMsg->getRecord(0);
407 |
408 | String txt = rec.toString();
409 | tft.fillScreen(ST7735_BLACK);
410 | tft.setCursor(0,0);
411 |
412 | tft.setTextColor(ST7735_WHITE);
413 | char szBuf[txt.length()+1];
414 | txt.getBytes((unsigned char*)szBuf, txt.length()+1);
415 | tft.print(szBuf);
416 | */
417 | //delete pNdefMsg;
418 | sendDESELECT();
419 | return pNdefMsg;
420 | }
421 |
422 |
423 | int M24SR::receiveResponse(unsigned int len) {
424 | int index=0;
425 | boolean WTX = false;
426 | boolean loop = false;
427 | if (_verbose) {
428 | Serial.print(F("\r\nreceiveResponse, len="));
429 | Serial.print(len, DEC);
430 | Serial.println();
431 | }
432 | if (_responseLength < len) {
433 | free(_response);
434 | if (_verbose) {
435 | Serial.print(F("\r\n_responseLength="));
436 | Serial.print(len, DEC);
437 | }
438 | _responseLength = len;
439 | _response = (byte*)malloc(_responseLength);
440 | }
441 | else
442 | delay(1);
443 | do {
444 | WTX = false;
445 | loop = false;
446 | Wire.requestFrom(_deviceaddress, len);
447 | if (_cmds)
448 | Serial.print(F("<= "));
449 | else
450 | delay(1);
451 | while ((Wire.available() && index < len && !WTX) ||
452 | (WTX && index < len-1)) {
453 | int c = (Wire.read() & 0xff);
454 | if (_cmds) {
455 | if (c < 0x10)
456 | Serial.print(F("0"));
457 | Serial.print(c, HEX);
458 | Serial.print(F(" "));
459 | }
460 | else
461 | delay(1);
462 | if (c == 0xF2 && index == 0) {
463 | WTX = true;
464 | }
465 | if (index >= 1) {
466 | _response[index-1] = c;
467 | }
468 | index++;
469 | }
470 | if (WTX) {
471 | Serial.print(F("\r\nWTX"));
472 | delay(200 * _response[0]);
473 | //send WTX response
474 | //sendSBLOCK(0xF2);
475 | _data[0] = 0xF2;//WTX
476 | _data[1] = _response[0];
477 | sendCommand(/*_data,*/ 2, false);
478 | loop = true;
479 | index = 0;
480 | }
481 | }while(loop);
482 | return index;
483 | }
484 |
485 | void M24SR::writeNdefMessage(NdefMessage* pNDefMsg)
486 | {
487 | if (pNDefMsg != NULL) {
488 | pNDefMsg->print();
489 | NdefRecord rec = pNDefMsg->getRecord(0);
490 | Serial.print(F("NDefRecord: "));
491 | Serial.println(rec.toString());
492 |
493 | selectFile_NDEF_App();
494 | selectFile_NDEF_file();
495 | updateBinary_NdefMsgLen0();
496 | uint8_t len = pNDefMsg->getEncodedSize();
497 | uint8_t* mem = (uint8_t*)malloc(len);
498 | //TODO memcpy(&_data[0], &SAMPLE_NDEF_message1[0], len);
499 | //uint8_t encoded[pNDefMsg->getEncodedSize()];
500 | pNDefMsg->encode((uint8_t*)mem);
501 |
502 | updateBinary((char*)mem, len);
503 | receiveResponse(2 + 3);
504 | free(mem);
505 |
506 | updateBinaryLen(len);
507 | receiveResponse(2 + 3);
508 | sendDESELECT();
509 | }
510 | }
511 |
512 | unsigned int M24SR::getNdefMessageLength()
513 | {
514 | sendApdu(0x00, INS_READ_BINARY, 0x00, 0x00, 0x02);
515 | receiveResponse(2 + 2 + 3);
516 | return ((_response[0] & 0xff) << 8) | (_response[1] & 0xff);
517 | }
518 |
519 | void M24SR::print()
520 | {
521 | Serial.print(F("\nM24SR GPO:"));
522 | Serial.println(_gpo_pin);
523 | }
524 |
525 | char text[4];
526 |
527 | void M24SR::dumpHex(uint8_t* buffer, uint8_t len)
528 | {
529 | for(byte i=0; i < len; i++) {
530 | sprintf(text, "%02X \x00", (uint8_t)(*(buffer + i)));
531 | Serial.print(text);
532 | if ((i % 16) == 15) {
533 | Serial.println("");
534 | }
535 | }
536 | }
537 |
538 |
--------------------------------------------------------------------------------
/M24SR.h:
--------------------------------------------------------------------------------
1 | /* Arduino library for ST M24SR Dynamic NFC/RFID tag IC with EEPROM, NFC Forum Type 4 Tag and I2C interface
2 | (c) 2014 by ReNa http://regnerischernachmittag.wordpress.com/
3 | */
4 |
5 | #ifndef M24SR_h
6 | #define M24SR_h
7 |
8 | //#include
9 | #include
10 | //#include
11 | #include
12 | #include //prog_char
13 |
14 | #include
15 | #include
16 |
17 | #define CMD_GETI2CSESSION 0x26
18 | #define CMD_KILLRFSESSION 0x52
19 |
20 | /*
21 |
22 | TODO
23 | ----
24 | - clean-up code and add TODOs
25 | - test: > 1 NDef record in NDef message
26 | - read/write data (without NDef classes)
27 | - what to do with writeSampleMsg?
28 | - ndef_len > 255
29 | - password handling
30 | - dynamic data buffer
31 | - if (len > BUFFER_LENGTH - 8) update for-loop
32 |
33 |
34 | INFO
35 | ----
36 |
37 | NOTE: The Arduino Wire library only has a 32 character buffer, so that is the maximun we can send using Arduino. This buffer includes the two address bytes which limits our data payload to 30 bytes
38 |
39 | AN4433 Storing data into the NDEF memory of M24SR http://www.st.com/web/en/resource/technical/document/application_note/DM00105043.pdf
40 | Datasheet http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00097458.pdf
41 |
42 | */
43 |
44 | #define INS_SELECT_FILE 0xA4
45 | #define INS_UPDATE_BINARY 0xD6
46 | #define INS_READ_BINARY 0xB0
47 | #define INS_VERIFY 0x20
48 |
49 | class M24SR
50 | {
51 | public:
52 | M24SR();
53 | M24SR(uint8_t gpo);
54 | ~M24SR();
55 | void print();
56 | boolean checkGPOTrigger();
57 | void updateBinary_NdefMsgLen0();
58 | void writeGPO(uint8_t data);
59 | //getUID()
60 | void sendDESELECT();
61 | NdefMessage* getNdefMessage();
62 | unsigned int getNdefMessageLength();
63 | void writeNdefMessage(NdefMessage* message);
64 | void selectFile_NDEF_file();
65 | void sendSBLOCK(uint8_t sblock);
66 | void selectFile_NDEF_App();
67 | void updateBinary(char* data, uint8_t len);
68 | void updateBinary(unsigned int offset, char* data, uint8_t len);
69 | void updateBinaryLen(int len);
70 | boolean verifyI2cPassword();
71 | //TODO boolean verifyI2cPassword(uint8_t* pwd);
72 | //TODO boolean setI2cPassword(uint8_t* old_password, uint8_t* new_password);
73 | void checkCRC(char* data, int len);
74 | void sendApdu(uint8_t CLA, uint8_t INS, uint8_t P1, uint8_t P2, uint8_t Lc, uint8_t* Data);
75 | void sendApdu(uint8_t CLA, uint8_t INS, uint8_t P1, uint8_t P2, uint8_t Le);
76 | void sendApdu_P(uint8_t CLA, uint8_t INS, uint8_t P1, uint8_t P2, uint8_t Lc, const prog_char* Data);
77 | void sendCommand(/*char* data,*/ int len);
78 | void sendCommand(/*char* data,*/ int len, boolean setPCB);
79 | void selfTest();
80 |
81 | void writeSampleMsg(uint8_t msgNo);
82 | void displaySystemFile();
83 | int receiveResponse(unsigned int len);
84 | void _setup();
85 |
86 | void dumpHex(uint8_t* buffer, uint8_t len);
87 | boolean _verbose;
88 | boolean _cmds;
89 | char _data[100]; //TODO dynamic buffer
90 | private:
91 |
92 | uint8_t _gpo_pin;
93 | uint8_t _lastGPO;
94 | uint8_t _deviceaddress;
95 | boolean _sendGetI2cSession;
96 | uint8_t _err;
97 | uint8_t _blockNo;
98 |
99 | uint8_t _responseLength;
100 | uint8_t* _response;
101 | };
102 |
103 | #endif
104 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ArduinoM24SR
2 | ============
3 |
4 | Arduino library for ST M24SR NFC dynamic tag
5 |
--------------------------------------------------------------------------------
/examples/DisplaySystemFile/DisplaySystemFile.ino:
--------------------------------------------------------------------------------
1 | /* Example: DisplaySystemFile (displays system properties of the NFC chip)
2 | Arduino library for ST M24SR Dynamic NFC/RFID tag IC with EEPROM, NFC Forum Type 4 Tag and I2C interface
3 | (c) 2014 by ReNa http://regnerischernachmittag.wordpress.com/
4 | */
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | /*
13 |
14 | How to use/install:
15 | 1. connect M24SR
16 | Pinout:
17 | -------------------------------------------------------------------------------
18 | M24SR -> Arduino / resistor / antenna
19 | -------------------------------------------------------------------------------
20 | 1 RF disable -> TODO not used
21 | 2 AC0 (antenna) -> Antenna
22 | 3 AC1 (antenna) -> Antenna
23 | 4 VSS (GND) -> Arduino Gnd
24 | 5 SDA (I2C data) -> Arduino A4
25 | 6 SCL (I2C clock) -> Arduino A5
26 | 7 GPO -> Arduino D7 + Pull-Up resistor (>4.7kOhm) to VCC
27 | 8 VCC (2...5V) -> Arduino 5V
28 | -------------------------------------------------------------------------------
29 | 2. download CRC lib
30 | http://code.google.com/p/arduino/source/browse/avrdude-5.4-arduino/crc16.c
31 | http://code.google.com/p/arduino/source/browse/avrdude-5.4-arduino/crc16.h
32 | and store the two files in ~/sketchbook/libraries/crc16
33 |
34 | 3. download NDEF/PN532 Lib
35 | https://github.com/don/NDEF has no toString support !!!
36 | TODO use my patched lib
37 | */
38 |
39 | #define gpo_pin 7
40 | M24SR m24sr(gpo_pin);
41 |
42 | //http://playground.arduino.cc/Code/AvailableMemory
43 | int freeRam () {
44 | extern int __heap_start, *__brkval;
45 | int v;
46 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
47 | }
48 |
49 | void setup()
50 | {
51 | Serial.begin(9600);
52 | //for debug purpose
53 | //m24sr._verbose = true;
54 | //m24sr._cmds = true;
55 | m24sr._setup();
56 |
57 | displayFreeRAM();
58 | m24sr.displaySystemFile();
59 | }
60 |
61 | void loop()
62 | {
63 | }
64 |
65 | void displayFreeRAM() {
66 | Serial.print(F("free RAM: "));
67 | Serial.println(freeRam(), DEC);
68 | }
69 |
70 |
--------------------------------------------------------------------------------
/examples/NDefWrite/NDefWrite.ino:
--------------------------------------------------------------------------------
1 | /* Example: NDefWrite (1 of 3 NDEF URL Records will be written after reset - tap the tag with your NFC phone)
2 | Arduino library for ST M24SR Dynamic NFC/RFID tag IC with EEPROM, NFC Forum Type 4 Tag and I2C interface
3 | (c) 2014 by ReNa http://regnerischernachmittag.wordpress.com/
4 | */
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | /*
13 |
14 | How to use/install:
15 | 1. connect M24SR
16 | Pinout:
17 | -------------------------------------------------------------------------------
18 | M24SR -> Arduino / resistor / antenna
19 | -------------------------------------------------------------------------------
20 | 1 RF disable -> TODO not used
21 | 2 AC0 (antenna) -> Antenna
22 | 3 AC1 (antenna) -> Antenna
23 | 4 VSS (GND) -> Arduino Gnd
24 | 5 SDA (I2C data) -> Arduino A4
25 | 6 SCL (I2C clock) -> Arduino A5
26 | 7 GPO -> Arduino D7 + Pull-Up resistor (>4.7kOhm) to VCC
27 | 8 VCC (2...5V) -> Arduino 5V
28 | -------------------------------------------------------------------------------
29 | 2. download CRC lib
30 | http://code.google.com/p/arduino/source/browse/avrdude-5.4-arduino/crc16.c
31 | http://code.google.com/p/arduino/source/browse/avrdude-5.4-arduino/crc16.h
32 | and store the two files in ~/sketchbook/libraries/crc16
33 |
34 | 3. download NDEF/PN532 Lib
35 | https://github.com/don/NDEF has no toString support !!!
36 | TODO use my patched lib
37 | */
38 |
39 | #define gpo_pin 7
40 | M24SR m24sr(gpo_pin);
41 |
42 | //http://playground.arduino.cc/Code/AvailableMemory
43 | int freeRam () {
44 | extern int __heap_start, *__brkval;
45 | int v;
46 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
47 | }
48 |
49 | prog_char URI1[] PROGMEM = "http://regnerischernachmittag.wordpress.com/?writesample";
50 | prog_char URI2[] PROGMEM = "https://github.com/rena2019/ArduinoM24SR";
51 | prog_char URI3[] PROGMEM = "https://twitter.com/regnerischerTag";
52 |
53 | void setup()
54 | {
55 | Serial.begin(9600);
56 | randomSeed(analogRead(0));
57 | //for debug purpose
58 | //m24sr._verbose = true;
59 | //m24sr._cmds = true;
60 | m24sr._setup();
61 | displayFreeRAM();
62 | NdefMessage message = NdefMessage();
63 | switch (random(3)) {
64 | case 0: message.addUriRecord(&URI1[0]);
65 | break;
66 | case 1: message.addUriRecord(&URI2[0]);
67 | break;
68 | default: message.addUriRecord(&URI3[0]);
69 | break;
70 | }
71 |
72 | displayFreeRAM();
73 | m24sr.writeNdefMessage(&message);
74 | Serial.print(F("\r\nUse NFC phone/reader to read out NFC tag content!"));
75 | }
76 |
77 | void loop()
78 | {
79 | }
80 |
81 | void displayFreeRAM() {
82 | Serial.print(F("\r\nfree RAM: "));
83 | Serial.println(freeRam(), DEC);
84 | }
85 |
86 |
--------------------------------------------------------------------------------
/examples/NfcTagDisplay/NfcTagDisplay.ino:
--------------------------------------------------------------------------------
1 | /* Example: NfcTagDisplay (Displays the content of the EEPROM after changing the NDef record with a NFC phone/Smartcard reader)
2 | Arduino library for ST M24SR Dynamic NFC/RFID tag IC with EEPROM, NFC Forum Type 4 Tag and I2C interface
3 | (c) 2014 by ReNa http://regnerischernachmittag.wordpress.com/
4 | */
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | /*
13 |
14 | How to use/install:
15 | 1. connect M24SR
16 | Pinout:
17 | -------------------------------------------------------------------------------
18 | M24SR -> Arduino / resistor / antenna
19 | -------------------------------------------------------------------------------
20 | 1 RF disable -> TODO not used
21 | 2 AC0 (antenna) -> Antenna
22 | 3 AC1 (antenna) -> Antenna
23 | 4 VSS (GND) -> Arduino Gnd
24 | 5 SDA (I2C data) -> Arduino A4
25 | 6 SCL (I2C clock) -> Arduino A5
26 | 7 GPO -> Arduino D7 + Pull-Up resistor (>4.7kOhm) to VCC
27 | 8 VCC (2...5V) -> Arduino 5V
28 | -------------------------------------------------------------------------------
29 | 2. download CRC lib
30 | http://code.google.com/p/arduino/source/browse/avrdude-5.4-arduino/crc16.c
31 | http://code.google.com/p/arduino/source/browse/avrdude-5.4-arduino/crc16.h
32 | and store the two files in ~/sketchbook/libraries/crc16
33 |
34 | 3. download NDEF/PN532 Lib
35 | https://github.com/don/NDEF has no toString support !!!
36 | TODO use my patched lib
37 |
38 | *optional* (if you want to connect a TFT and display the NDEF record on it):
39 | 4. download Adafruit_GFX lib
40 | https://github.com/adafruit/Adafruit-GFX-Library
41 | and store files under ~/sketchbook/libraries
42 |
43 | download Adafruit_ST7735 lib
44 | https://github.com/adafruit/Adafruit-ST7735-Library
45 | and store files under ~/sketchbook/libraries
46 |
47 | 5. connect TFT
48 | Arduino TFT
49 | ---------------------
50 | 5V (VCC) LED, VCC
51 | GND GND
52 | D13 (SCK) SCK
53 | D11 (MOSI) SDA
54 | D10 (SS) CS
55 | D9 A0
56 | D8 RESET
57 | */
58 |
59 | // next 5 lines are required (due to Arduino bug) if _TFT_SUPPORT_ is undefined
60 | // BOF preprocessor bug prevent - insert me on top of your arduino-code
61 | // From: http://www.a-control.de/arduino-fehler/?lang=en
62 | #if 1
63 | __asm volatile ("nop");
64 | #endif
65 |
66 | //uncomment next line if you have a TFT connected to your arduino
67 | //#define _TFT_SUPPORT_
68 |
69 | #ifdef _TFT_SUPPORT_
70 | #include // Adafruit graphic-lib
71 | #include // Adafruit ST7735-lib
72 | #include
73 | // Arduino 5V (VCC) to TFT: LED, VCC
74 | // Arduino GND to TFT: GND
75 | // Arduino D13 (SCK) to TFT: SCK
76 | // Arduino D11 (MOSI) to TFT:
77 | #define cs 10 // Arduino D10 (SS) to TFT: CS
78 | #define dc 9 // Arduino D9 to TFT: A0
79 | #define rst 8 // Arduino-D8 to TFT: RESET
80 | Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
81 | #endif
82 |
83 | #define gpo_pin 7
84 | M24SR m24sr(gpo_pin);
85 |
86 | //http://playground.arduino.cc/Code/AvailableMemory
87 | int freeRam () {
88 | extern int __heap_start, *__brkval;
89 | int v;
90 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
91 | }
92 |
93 | void setup()
94 | {
95 | Serial.begin(9600);
96 | //for debug purpose
97 | //m24sr._verbose = true;
98 | //m24sr._cmds = true;
99 | m24sr._setup();
100 |
101 | #ifdef _TFT_SUPPORT_
102 | tft.initR(INITR_BLACKTAB); // ST7735-Chip initialize
103 | tft.setTextColor(ST7735_WHITE);
104 | tft.setTextSize(1);
105 | tft.setTextWrap(true);
106 | tft.fillScreen(ST7735_BLACK);
107 | #endif
108 |
109 | displayFreeRAM();
110 | m24sr.displaySystemFile();
111 | #ifdef _TFT_SUPPORT_
112 | Serial.println(F("\r\nwith TFT"));
113 | #else
114 | Serial.println(F("\r\nwithout TFT"));
115 | #endif
116 | Serial.print(F("Write/change NFC tag (with your NFC phone) now!"));
117 | }
118 |
119 | void loop()
120 | {
121 | if (m24sr.checkGPOTrigger()) {
122 | displayNDefRecord();
123 | }
124 | delay(200);
125 | }
126 |
127 | void displayNDefRecord() {
128 | //read NDef message from memory
129 | NdefMessage* pNDefMsg = m24sr.getNdefMessage();
130 | displayFreeRAM();
131 | if (pNDefMsg != NULL) {
132 | pNDefMsg->print();
133 | NdefRecord rec = pNDefMsg->getRecord(0);
134 | String txt = rec.toString();
135 | Serial.print(F("NDefRecord: "));
136 | Serial.println(txt);
137 |
138 | #ifdef _TFT_SUPPORT_
139 | //display text on TFT
140 | tft.fillScreen(ST7735_BLACK);
141 | tft.setCursor(0,0);
142 | tft.setTextColor(ST7735_WHITE);
143 | char szBuf[txt.length()+1];
144 | txt.getBytes((unsigned char*)szBuf, txt.length()+1);
145 | tft.print(szBuf);
146 | #endif
147 | delete pNDefMsg;
148 | }
149 | }
150 |
151 | void displayFreeRAM() {
152 | Serial.print(F("\r\nfree RAM: "));
153 | Serial.println(freeRam(), DEC);
154 | }
155 |
156 |
--------------------------------------------------------------------------------