![](/media/cache/group/ST_logo_2020_blue_V_cmyk.jpg.50x50_q85.jpg)
This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.
Dependencies: NDefLib X_NUCLEO_NFC01A1 mbed
Fork of X-MBED-NFC1 by
This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.
The available demos are:
- SAMPLE_WRITE_URL: write a tag with the ST home page URL
- SAMPLE_COUNT_CLICK: create a custom tag to count and report the user button clicks.
- SAMPLE_WRITE_AND_CHANGE_ALL: write a tag with all the supported records and update the tag contents when the user button is pressed.
- SAMPLE_LOCK_TAG_CONTENT: use the M24SR component API to set the NFC tag as read-only.
To enable the different demos comment/uncomment the SAMPLE_* macros provided in main.cpp .
Samples/SampleSync_lockWriteTag.cpp@18:d596eb3f575f, 2017-07-13 (annotated)
- Committer:
- Davidroid
- Date:
- Thu Jul 13 12:46:03 2017 +0000
- Revision:
- 18:d596eb3f575f
- Parent:
- 17:d8d3d2088cac
Aligning to ARM mbed coding style.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 13:685d95525ec8 | 1 | /** |
giovannivisentini | 13:685d95525ec8 | 2 | ****************************************************************************** |
Davidroid | 18:d596eb3f575f | 3 | * @file SampleSync_lockWriteTag.cpp |
giovannivisentini | 13:685d95525ec8 | 4 | * @author ST / Central Labs |
giovannivisentini | 13:685d95525ec8 | 5 | * @date 03 Dic 2015 |
giovannivisentini | 13:685d95525ec8 | 6 | * @brief This demo define a lock/unlock the tag content when the user press |
giovannivisentini | 13:685d95525ec8 | 7 | * the user button |
giovannivisentini | 13:685d95525ec8 | 8 | ****************************************************************************** |
giovannivisentini | 13:685d95525ec8 | 9 | * |
giovannivisentini | 13:685d95525ec8 | 10 | * COPYRIGHT(c) 2015 STMicroelectronics |
giovannivisentini | 13:685d95525ec8 | 11 | * |
giovannivisentini | 13:685d95525ec8 | 12 | * Redistribution and use in source and binary forms, with or without modification, |
giovannivisentini | 13:685d95525ec8 | 13 | * are permitted provided that the following conditions are met: |
giovannivisentini | 13:685d95525ec8 | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
giovannivisentini | 13:685d95525ec8 | 15 | * this list of conditions and the following disclaimer. |
giovannivisentini | 13:685d95525ec8 | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
giovannivisentini | 13:685d95525ec8 | 17 | * this list of conditions and the following disclaimer in the documentation |
giovannivisentini | 13:685d95525ec8 | 18 | * and/or other materials provided with the distribution. |
giovannivisentini | 13:685d95525ec8 | 19 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
giovannivisentini | 13:685d95525ec8 | 20 | * may be used to endorse or promote products derived from this software |
giovannivisentini | 13:685d95525ec8 | 21 | * without specific prior written permission. |
giovannivisentini | 13:685d95525ec8 | 22 | * |
giovannivisentini | 13:685d95525ec8 | 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
giovannivisentini | 13:685d95525ec8 | 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
giovannivisentini | 13:685d95525ec8 | 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
giovannivisentini | 13:685d95525ec8 | 26 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
giovannivisentini | 13:685d95525ec8 | 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
giovannivisentini | 13:685d95525ec8 | 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
giovannivisentini | 13:685d95525ec8 | 29 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
giovannivisentini | 13:685d95525ec8 | 30 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
giovannivisentini | 13:685d95525ec8 | 31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
giovannivisentini | 13:685d95525ec8 | 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
giovannivisentini | 13:685d95525ec8 | 33 | * |
giovannivisentini | 13:685d95525ec8 | 34 | ****************************************************************************** |
giovannivisentini | 13:685d95525ec8 | 35 | */ |
giovannivisentini | 13:685d95525ec8 | 36 | |
giovannivisentini | 13:685d95525ec8 | 37 | #include "mbed.h" |
giovannivisentini | 13:685d95525ec8 | 38 | |
giovannivisentini | 17:d8d3d2088cac | 39 | #include "XNucleoNFC01A1.h" |
giovannivisentini | 13:685d95525ec8 | 40 | |
giovannivisentini | 14:3b604972b89f | 41 | static volatile bool buttonPress=false; |
giovannivisentini | 13:685d95525ec8 | 42 | |
giovannivisentini | 13:685d95525ec8 | 43 | static void setButtonPress(){ |
giovannivisentini | 17:d8d3d2088cac | 44 | printf("button\r\n"); |
giovannivisentini | 13:685d95525ec8 | 45 | buttonPress=true; |
giovannivisentini | 13:685d95525ec8 | 46 | }//if buttonPress |
giovannivisentini | 13:685d95525ec8 | 47 | |
giovannivisentini | 13:685d95525ec8 | 48 | /** |
giovannivisentini | 14:3b604972b89f | 49 | * return the led that tell if the tag is read only |
giovannivisentini | 14:3b604972b89f | 50 | * @param nfcNucleo board where the lead lives |
giovannivisentini | 14:3b604972b89f | 51 | * @return let to switch on if the tag is read only |
giovannivisentini | 13:685d95525ec8 | 52 | */ |
giovannivisentini | 17:d8d3d2088cac | 53 | static inline DigitalOut& getReadOnlyLed(XNucleoNFC01A1 *nfcNucleo){ |
giovannivisentini | 17:d8d3d2088cac | 54 | return nfcNucleo->get_led2(); |
giovannivisentini | 14:3b604972b89f | 55 | } |
giovannivisentini | 13:685d95525ec8 | 56 | |
giovannivisentini | 14:3b604972b89f | 57 | /** |
giovannivisentini | 14:3b604972b89f | 58 | * return the led that tell if the tag is write only |
giovannivisentini | 14:3b604972b89f | 59 | * @param nfcNucleo board where the lead lives |
giovannivisentini | 14:3b604972b89f | 60 | * @return let to switch on if the tag is write only |
giovannivisentini | 14:3b604972b89f | 61 | */ |
giovannivisentini | 17:d8d3d2088cac | 62 | static inline DigitalOut& getWriteableLed(XNucleoNFC01A1 *nfcNucleo){ |
giovannivisentini | 17:d8d3d2088cac | 63 | return nfcNucleo->get_led3(); |
giovannivisentini | 14:3b604972b89f | 64 | } |
giovannivisentini | 13:685d95525ec8 | 65 | |
giovannivisentini | 13:685d95525ec8 | 66 | /** |
giovannivisentini | 14:3b604972b89f | 67 | * set the tag as read only and switch the led on |
giovannivisentini | 14:3b604972b89f | 68 | * @param nfcNucleo board where the lead lives |
giovannivisentini | 14:3b604972b89f | 69 | * @param tag helper class to manage the nfc tag |
giovannivisentini | 14:3b604972b89f | 70 | * @return true if the change has effect |
giovannivisentini | 13:685d95525ec8 | 71 | */ |
giovannivisentini | 17:d8d3d2088cac | 72 | bool enableReadOnly( XNucleoNFC01A1 *nfcNucleo){ |
giovannivisentini | 17:d8d3d2088cac | 73 | if(nfcNucleo->get_M24SR().enable_read_only(M24SR::DEFAULT_PASSWORD) == M24SR::M24SR_SUCCESS){ |
giovannivisentini | 15:94c98d2aa235 | 74 | printf("Enable Read Only\r\n"); |
giovannivisentini | 14:3b604972b89f | 75 | getReadOnlyLed(nfcNucleo)=true; |
giovannivisentini | 14:3b604972b89f | 76 | getWriteableLed(nfcNucleo)=false; |
giovannivisentini | 14:3b604972b89f | 77 | return true; |
giovannivisentini | 17:d8d3d2088cac | 78 | }else{ |
giovannivisentini | 17:d8d3d2088cac | 79 | printf("Error: enabling Read Only\r\n"); |
giovannivisentini | 14:3b604972b89f | 80 | return false; |
giovannivisentini | 17:d8d3d2088cac | 81 | } |
giovannivisentini | 14:3b604972b89f | 82 | } |
giovannivisentini | 13:685d95525ec8 | 83 | |
giovannivisentini | 13:685d95525ec8 | 84 | /** |
giovannivisentini | 14:3b604972b89f | 85 | * set the tag as read/write and switch the led on |
giovannivisentini | 14:3b604972b89f | 86 | * @param nfcNucleo board where the lead lives |
giovannivisentini | 14:3b604972b89f | 87 | * @param tag helper class to manage the nfc tag |
giovannivisentini | 14:3b604972b89f | 88 | * @return true if the change has effect |
giovannivisentini | 13:685d95525ec8 | 89 | */ |
giovannivisentini | 17:d8d3d2088cac | 90 | bool disableReadOnly( XNucleoNFC01A1 *nfcNucleo){ |
giovannivisentini | 13:685d95525ec8 | 91 | |
giovannivisentini | 17:d8d3d2088cac | 92 | if(nfcNucleo->get_M24SR().disable_read_only(M24SR::DEFAULT_PASSWORD)== M24SR::M24SR_SUCCESS){ |
giovannivisentini | 15:94c98d2aa235 | 93 | printf("Disable Read Only\r\n"); |
giovannivisentini | 14:3b604972b89f | 94 | getReadOnlyLed(nfcNucleo)=false; |
giovannivisentini | 14:3b604972b89f | 95 | getWriteableLed(nfcNucleo)=true; |
giovannivisentini | 14:3b604972b89f | 96 | return true; |
giovannivisentini | 17:d8d3d2088cac | 97 | }else{ |
giovannivisentini | 17:d8d3d2088cac | 98 | printf("Error: Disabling Read Only\r\n"); |
giovannivisentini | 14:3b604972b89f | 99 | return false; |
giovannivisentini | 17:d8d3d2088cac | 100 | } |
giovannivisentini | 14:3b604972b89f | 101 | } |
giovannivisentini | 14:3b604972b89f | 102 | |
giovannivisentini | 14:3b604972b89f | 103 | void sampleSync_lockTagContent() { |
giovannivisentini | 17:d8d3d2088cac | 104 | |
giovannivisentini | 17:d8d3d2088cac | 105 | I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN); |
giovannivisentini | 14:3b604972b89f | 106 | |
giovannivisentini | 17:d8d3d2088cac | 107 | XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel); |
giovannivisentini | 14:3b604972b89f | 108 | |
giovannivisentini | 17:d8d3d2088cac | 109 | NDefLib::NDefNfcTag &tag = nfcNucleo->get_M24SR().get_NDef_tag(); |
giovannivisentini | 14:3b604972b89f | 110 | |
giovannivisentini | 13:685d95525ec8 | 111 | //when the user press the button set buttonPress to true |
giovannivisentini | 17:d8d3d2088cac | 112 | |
giovannivisentini | 17:d8d3d2088cac | 113 | #if defined(TARGET_STM) |
giovannivisentini | 17:d8d3d2088cac | 114 | InterruptIn userButton(USER_BUTTON); |
giovannivisentini | 17:d8d3d2088cac | 115 | #else |
giovannivisentini | 17:d8d3d2088cac | 116 | InterruptIn userButton(SW2); |
giovannivisentini | 17:d8d3d2088cac | 117 | #endif |
giovannivisentini | 17:d8d3d2088cac | 118 | |
giovannivisentini | 13:685d95525ec8 | 119 | userButton.fall(setButtonPress); |
giovannivisentini | 15:94c98d2aa235 | 120 | |
giovannivisentini | 15:94c98d2aa235 | 121 | printf("Init Done\r\n"); |
giovannivisentini | 15:94c98d2aa235 | 122 | |
giovannivisentini | 14:3b604972b89f | 123 | //restore the board as read/write |
giovannivisentini | 17:d8d3d2088cac | 124 | if(!tag.open_session(true)){ |
giovannivisentini | 17:d8d3d2088cac | 125 | printf("Error open session\r\n"); |
giovannivisentini | 17:d8d3d2088cac | 126 | return; |
giovannivisentini | 17:d8d3d2088cac | 127 | } |
giovannivisentini | 17:d8d3d2088cac | 128 | if(nfcNucleo->get_M24SR().disable_all_password(M24SR::DEFAULT_PASSWORD)!= M24SR::M24SR_SUCCESS){ |
giovannivisentini | 17:d8d3d2088cac | 129 | printf("Error disable_all_password\r\n"); |
giovannivisentini | 17:d8d3d2088cac | 130 | return; |
giovannivisentini | 17:d8d3d2088cac | 131 | } |
giovannivisentini | 17:d8d3d2088cac | 132 | if(!tag.close_session()){ |
giovannivisentini | 17:d8d3d2088cac | 133 | printf("Error close session\r\n"); |
giovannivisentini | 17:d8d3d2088cac | 134 | return; |
giovannivisentini | 17:d8d3d2088cac | 135 | } |
giovannivisentini | 17:d8d3d2088cac | 136 | |
giovannivisentini | 14:3b604972b89f | 137 | getReadOnlyLed(nfcNucleo)=false; |
giovannivisentini | 14:3b604972b89f | 138 | getWriteableLed(nfcNucleo)=true; |
giovannivisentini | 14:3b604972b89f | 139 | bool tagIsLock=false; |
giovannivisentini | 15:94c98d2aa235 | 140 | |
giovannivisentini | 15:94c98d2aa235 | 141 | printf("All Protection removed\r\n"); |
giovannivisentini | 15:94c98d2aa235 | 142 | |
giovannivisentini | 17:d8d3d2088cac | 143 | nfcNucleo->get_led1()=true; |
giovannivisentini | 13:685d95525ec8 | 144 | while(true){ |
giovannivisentini | 13:685d95525ec8 | 145 | |
giovannivisentini | 14:3b604972b89f | 146 | if(buttonPress){ |
giovannivisentini | 17:d8d3d2088cac | 147 | printf("button pressed\r\n"); |
giovannivisentini | 17:d8d3d2088cac | 148 | if(tag.open_session()){ |
giovannivisentini | 14:3b604972b89f | 149 | bool changeStatusResult; |
giovannivisentini | 17:d8d3d2088cac | 150 | printf("session open\r\n"); |
Davidroid | 18:d596eb3f575f | 151 | if(!tagIsLock) { |
giovannivisentini | 14:3b604972b89f | 152 | changeStatusResult=enableReadOnly(nfcNucleo); |
Davidroid | 18:d596eb3f575f | 153 | } else { |
giovannivisentini | 14:3b604972b89f | 154 | changeStatusResult=disableReadOnly(nfcNucleo); |
Davidroid | 18:d596eb3f575f | 155 | } |
giovannivisentini | 14:3b604972b89f | 156 | |
Davidroid | 18:d596eb3f575f | 157 | if(changeStatusResult) { |
giovannivisentini | 14:3b604972b89f | 158 | tagIsLock=!tagIsLock; |
Davidroid | 18:d596eb3f575f | 159 | } else { |
giovannivisentini | 17:d8d3d2088cac | 160 | printf("Error change the tag status\r\n"); |
Davidroid | 18:d596eb3f575f | 161 | } |
giovannivisentini | 17:d8d3d2088cac | 162 | tag.close_session(); |
giovannivisentini | 14:3b604972b89f | 163 | }else{ |
giovannivisentini | 17:d8d3d2088cac | 164 | printf("Error opening the session, tag is in use\r\n"); |
giovannivisentini | 14:3b604972b89f | 165 | } |
giovannivisentini | 13:685d95525ec8 | 166 | buttonPress=false; |
giovannivisentini | 13:685d95525ec8 | 167 | }//if |
giovannivisentini | 14:3b604972b89f | 168 | |
giovannivisentini | 13:685d95525ec8 | 169 | //wait next event |
giovannivisentini | 13:685d95525ec8 | 170 | __WFE(); |
giovannivisentini | 13:685d95525ec8 | 171 | }//while |
giovannivisentini | 13:685d95525ec8 | 172 | |
giovannivisentini | 17:d8d3d2088cac | 173 | }//sample_countClick |