![](/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@15:94c98d2aa235, 2016-08-31 (annotated)
- Committer:
- giovannivisentini
- Date:
- Wed Aug 31 13:58:17 2016 +0000
- Revision:
- 15:94c98d2aa235
- Parent:
- 14:3b604972b89f
- Child:
- 17:d8d3d2088cac
add printf to the demos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 13:685d95525ec8 | 1 | /** |
giovannivisentini | 13:685d95525ec8 | 2 | ****************************************************************************** |
giovannivisentini | 13:685d95525ec8 | 3 | * @file Sample_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 | 13:685d95525ec8 | 39 | #include "X_NUCLEO_NFC01A1.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 | 13:685d95525ec8 | 44 | buttonPress=true; |
giovannivisentini | 13:685d95525ec8 | 45 | }//if buttonPress |
giovannivisentini | 13:685d95525ec8 | 46 | |
giovannivisentini | 13:685d95525ec8 | 47 | /** |
giovannivisentini | 14:3b604972b89f | 48 | * return the led that tell if the tag is read only |
giovannivisentini | 14:3b604972b89f | 49 | * @param nfcNucleo board where the lead lives |
giovannivisentini | 14:3b604972b89f | 50 | * @return let to switch on if the tag is read only |
giovannivisentini | 13:685d95525ec8 | 51 | */ |
giovannivisentini | 14:3b604972b89f | 52 | static inline DigitalOut& getReadOnlyLed(X_NUCLEO_NFC01A1 *nfcNucleo){ |
giovannivisentini | 14:3b604972b89f | 53 | return nfcNucleo->getLed2(); |
giovannivisentini | 14:3b604972b89f | 54 | } |
giovannivisentini | 13:685d95525ec8 | 55 | |
giovannivisentini | 14:3b604972b89f | 56 | /** |
giovannivisentini | 14:3b604972b89f | 57 | * return the led that tell if the tag is write only |
giovannivisentini | 14:3b604972b89f | 58 | * @param nfcNucleo board where the lead lives |
giovannivisentini | 14:3b604972b89f | 59 | * @return let to switch on if the tag is write only |
giovannivisentini | 14:3b604972b89f | 60 | */ |
giovannivisentini | 14:3b604972b89f | 61 | static inline DigitalOut& getWriteableLed(X_NUCLEO_NFC01A1 *nfcNucleo){ |
giovannivisentini | 14:3b604972b89f | 62 | return nfcNucleo->getLed3(); |
giovannivisentini | 14:3b604972b89f | 63 | } |
giovannivisentini | 13:685d95525ec8 | 64 | |
giovannivisentini | 13:685d95525ec8 | 65 | /** |
giovannivisentini | 14:3b604972b89f | 66 | * set the tag as read only and switch the led on |
giovannivisentini | 14:3b604972b89f | 67 | * @param nfcNucleo board where the lead lives |
giovannivisentini | 14:3b604972b89f | 68 | * @param tag helper class to manage the nfc tag |
giovannivisentini | 14:3b604972b89f | 69 | * @return true if the change has effect |
giovannivisentini | 13:685d95525ec8 | 70 | */ |
giovannivisentini | 14:3b604972b89f | 71 | bool enableReadOnly( X_NUCLEO_NFC01A1 *nfcNucleo){ |
giovannivisentini | 13:685d95525ec8 | 72 | |
giovannivisentini | 14:3b604972b89f | 73 | if(nfcNucleo->getM24SR().EnableReadOnly(M24SR::DEFAULT_PASSWORD)){ |
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 | 14:3b604972b89f | 78 | }else |
giovannivisentini | 14:3b604972b89f | 79 | return false; |
giovannivisentini | 14:3b604972b89f | 80 | } |
giovannivisentini | 13:685d95525ec8 | 81 | |
giovannivisentini | 13:685d95525ec8 | 82 | /** |
giovannivisentini | 14:3b604972b89f | 83 | * set the tag as read/write and switch the led on |
giovannivisentini | 14:3b604972b89f | 84 | * @param nfcNucleo board where the lead lives |
giovannivisentini | 14:3b604972b89f | 85 | * @param tag helper class to manage the nfc tag |
giovannivisentini | 14:3b604972b89f | 86 | * @return true if the change has effect |
giovannivisentini | 13:685d95525ec8 | 87 | */ |
giovannivisentini | 14:3b604972b89f | 88 | bool disableReadOnly( X_NUCLEO_NFC01A1 *nfcNucleo){ |
giovannivisentini | 13:685d95525ec8 | 89 | |
giovannivisentini | 14:3b604972b89f | 90 | if(nfcNucleo->getM24SR().DisableReadOnly(M24SR::DEFAULT_PASSWORD)){ |
giovannivisentini | 15:94c98d2aa235 | 91 | printf("Disable Read Only\r\n"); |
giovannivisentini | 14:3b604972b89f | 92 | getReadOnlyLed(nfcNucleo)=false; |
giovannivisentini | 14:3b604972b89f | 93 | getWriteableLed(nfcNucleo)=true; |
giovannivisentini | 14:3b604972b89f | 94 | return true; |
giovannivisentini | 14:3b604972b89f | 95 | }else |
giovannivisentini | 14:3b604972b89f | 96 | return false; |
giovannivisentini | 14:3b604972b89f | 97 | } |
giovannivisentini | 14:3b604972b89f | 98 | |
giovannivisentini | 14:3b604972b89f | 99 | void sampleSync_lockTagContent() { |
giovannivisentini | 14:3b604972b89f | 100 | Serial console(SERIAL_TX, SERIAL_RX); |
giovannivisentini | 14:3b604972b89f | 101 | |
giovannivisentini | 13:685d95525ec8 | 102 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
giovannivisentini | 14:3b604972b89f | 103 | i2cChannel.frequency(400000); |
giovannivisentini | 14:3b604972b89f | 104 | |
giovannivisentini | 14:3b604972b89f | 105 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel); |
giovannivisentini | 14:3b604972b89f | 106 | |
giovannivisentini | 14:3b604972b89f | 107 | NDefLib::NDefNfcTag &tag = nfcNucleo->getM24SR().getNDefTag(); |
giovannivisentini | 14:3b604972b89f | 108 | |
giovannivisentini | 13:685d95525ec8 | 109 | //when the user press the button set buttonPress to true |
giovannivisentini | 13:685d95525ec8 | 110 | InterruptIn userButton(USER_BUTTON); |
giovannivisentini | 13:685d95525ec8 | 111 | userButton.fall(setButtonPress); |
giovannivisentini | 15:94c98d2aa235 | 112 | |
giovannivisentini | 15:94c98d2aa235 | 113 | printf("Init Done\r\n"); |
giovannivisentini | 15:94c98d2aa235 | 114 | |
giovannivisentini | 14:3b604972b89f | 115 | //restore the board as read/write |
giovannivisentini | 14:3b604972b89f | 116 | tag.openSession(true); |
giovannivisentini | 14:3b604972b89f | 117 | nfcNucleo->getM24SR().DisableAllPassword(M24SR::DEFAULT_PASSWORD); |
giovannivisentini | 14:3b604972b89f | 118 | tag.closeSession(); |
giovannivisentini | 14:3b604972b89f | 119 | getReadOnlyLed(nfcNucleo)=false; |
giovannivisentini | 14:3b604972b89f | 120 | getWriteableLed(nfcNucleo)=true; |
giovannivisentini | 14:3b604972b89f | 121 | bool tagIsLock=false; |
giovannivisentini | 15:94c98d2aa235 | 122 | |
giovannivisentini | 15:94c98d2aa235 | 123 | printf("All Protection removed\r\n"); |
giovannivisentini | 15:94c98d2aa235 | 124 | |
giovannivisentini | 14:3b604972b89f | 125 | nfcNucleo->getLed1()=true; |
giovannivisentini | 13:685d95525ec8 | 126 | while(true){ |
giovannivisentini | 13:685d95525ec8 | 127 | |
giovannivisentini | 14:3b604972b89f | 128 | if(buttonPress){ |
giovannivisentini | 14:3b604972b89f | 129 | if(tag.openSession()){ |
giovannivisentini | 14:3b604972b89f | 130 | bool changeStatusResult; |
giovannivisentini | 14:3b604972b89f | 131 | |
giovannivisentini | 15:94c98d2aa235 | 132 | if(!tagIsLock) |
giovannivisentini | 14:3b604972b89f | 133 | changeStatusResult=enableReadOnly(nfcNucleo); |
giovannivisentini | 14:3b604972b89f | 134 | else |
giovannivisentini | 14:3b604972b89f | 135 | changeStatusResult=disableReadOnly(nfcNucleo); |
giovannivisentini | 14:3b604972b89f | 136 | |
giovannivisentini | 14:3b604972b89f | 137 | if(changeStatusResult) |
giovannivisentini | 14:3b604972b89f | 138 | tagIsLock=!tagIsLock; |
giovannivisentini | 14:3b604972b89f | 139 | else |
giovannivisentini | 14:3b604972b89f | 140 | console.printf("Error change the tag status"); |
giovannivisentini | 14:3b604972b89f | 141 | tag.closeSession(); |
giovannivisentini | 14:3b604972b89f | 142 | }else{ |
giovannivisentini | 14:3b604972b89f | 143 | console.printf("Error opening the session, tag is in use"); |
giovannivisentini | 14:3b604972b89f | 144 | } |
giovannivisentini | 13:685d95525ec8 | 145 | buttonPress=false; |
giovannivisentini | 13:685d95525ec8 | 146 | }//if |
giovannivisentini | 14:3b604972b89f | 147 | |
giovannivisentini | 13:685d95525ec8 | 148 | //wait next event |
giovannivisentini | 13:685d95525ec8 | 149 | __WFE(); |
giovannivisentini | 13:685d95525ec8 | 150 | }//while |
giovannivisentini | 13:685d95525ec8 | 151 | |
giovannivisentini | 13:685d95525ec8 | 152 | }//sample_countClick |