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/sample_lockWriteTag.cpp@9:9742acd0c015, 2015-12-22 (annotated)
- Committer:
- giovannivisentini
- Date:
- Tue Dec 22 16:22:03 2015 +0000
- Revision:
- 9:9742acd0c015
- Parent:
- 8:a9ecd7687de7
- Child:
- 11:283df715f241
use the new library api
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| giovannivisentini | 8:a9ecd7687de7 | 1 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 2 | ****************************************************************************** |
| giovannivisentini | 8:a9ecd7687de7 | 3 | * @file Sample_lockWriteTag.cpp |
| giovannivisentini | 9:9742acd0c015 | 4 | * @author ST / Central Labs |
| giovannivisentini | 9:9742acd0c015 | 5 | * @date 03 Dic 2015 |
| giovannivisentini | 8:a9ecd7687de7 | 6 | * @brief This demo define a lock/unlock the tag content when the user press |
| giovannivisentini | 8:a9ecd7687de7 | 7 | * the user button |
| giovannivisentini | 8:a9ecd7687de7 | 8 | ****************************************************************************** |
| giovannivisentini | 8:a9ecd7687de7 | 9 | * |
| giovannivisentini | 8:a9ecd7687de7 | 10 | * COPYRIGHT(c) 2015 STMicroelectronics |
| giovannivisentini | 8:a9ecd7687de7 | 11 | * |
| giovannivisentini | 8:a9ecd7687de7 | 12 | * Redistribution and use in source and binary forms, with or without modification, |
| giovannivisentini | 8:a9ecd7687de7 | 13 | * are permitted provided that the following conditions are met: |
| giovannivisentini | 8:a9ecd7687de7 | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
| giovannivisentini | 8:a9ecd7687de7 | 15 | * this list of conditions and the following disclaimer. |
| giovannivisentini | 8:a9ecd7687de7 | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| giovannivisentini | 8:a9ecd7687de7 | 17 | * this list of conditions and the following disclaimer in the documentation |
| giovannivisentini | 8:a9ecd7687de7 | 18 | * and/or other materials provided with the distribution. |
| giovannivisentini | 8:a9ecd7687de7 | 19 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| giovannivisentini | 8:a9ecd7687de7 | 20 | * may be used to endorse or promote products derived from this software |
| giovannivisentini | 8:a9ecd7687de7 | 21 | * without specific prior written permission. |
| giovannivisentini | 8:a9ecd7687de7 | 22 | * |
| giovannivisentini | 8:a9ecd7687de7 | 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| giovannivisentini | 8:a9ecd7687de7 | 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| giovannivisentini | 8:a9ecd7687de7 | 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| giovannivisentini | 8:a9ecd7687de7 | 26 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| giovannivisentini | 8:a9ecd7687de7 | 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| giovannivisentini | 8:a9ecd7687de7 | 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| giovannivisentini | 8:a9ecd7687de7 | 29 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| giovannivisentini | 8:a9ecd7687de7 | 30 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| giovannivisentini | 8:a9ecd7687de7 | 31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| giovannivisentini | 8:a9ecd7687de7 | 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| giovannivisentini | 8:a9ecd7687de7 | 33 | * |
| giovannivisentini | 8:a9ecd7687de7 | 34 | ****************************************************************************** |
| giovannivisentini | 8:a9ecd7687de7 | 35 | */ |
| giovannivisentini | 8:a9ecd7687de7 | 36 | |
| giovannivisentini | 8:a9ecd7687de7 | 37 | #include "mbed.h" |
| giovannivisentini | 8:a9ecd7687de7 | 38 | |
| giovannivisentini | 8:a9ecd7687de7 | 39 | #include "X_NUCLEO_NFC01A1.h" |
| giovannivisentini | 8:a9ecd7687de7 | 40 | |
| giovannivisentini | 8:a9ecd7687de7 | 41 | static volatile bool buttonPress=false; |
| giovannivisentini | 8:a9ecd7687de7 | 42 | |
| giovannivisentini | 8:a9ecd7687de7 | 43 | static void setButtonPress(){ |
| giovannivisentini | 8:a9ecd7687de7 | 44 | buttonPress=true; |
| giovannivisentini | 8:a9ecd7687de7 | 45 | }//if buttonPress |
| giovannivisentini | 8:a9ecd7687de7 | 46 | |
| giovannivisentini | 8:a9ecd7687de7 | 47 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 48 | * return the led that tell if the tag is read only |
| giovannivisentini | 8:a9ecd7687de7 | 49 | * @param nfcNucleo board where the lead lives |
| giovannivisentini | 8:a9ecd7687de7 | 50 | * @return let to switch on if the tag is read only |
| giovannivisentini | 8:a9ecd7687de7 | 51 | */ |
| giovannivisentini | 8:a9ecd7687de7 | 52 | static inline DigitalOut& getReadOnlyLed(X_NUCLEO_NFC01A1 *nfcNucleo){ |
| giovannivisentini | 8:a9ecd7687de7 | 53 | return nfcNucleo->getLed2(); |
| giovannivisentini | 8:a9ecd7687de7 | 54 | } |
| giovannivisentini | 8:a9ecd7687de7 | 55 | |
| giovannivisentini | 8:a9ecd7687de7 | 56 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 57 | * return the led that tell if the tag is write only |
| giovannivisentini | 8:a9ecd7687de7 | 58 | * @param nfcNucleo board where the lead lives |
| giovannivisentini | 8:a9ecd7687de7 | 59 | * @return let to switch on if the tag is write only |
| giovannivisentini | 8:a9ecd7687de7 | 60 | */ |
| giovannivisentini | 8:a9ecd7687de7 | 61 | static inline DigitalOut& getWriteableLed(X_NUCLEO_NFC01A1 *nfcNucleo){ |
| giovannivisentini | 8:a9ecd7687de7 | 62 | return nfcNucleo->getLed3(); |
| giovannivisentini | 8:a9ecd7687de7 | 63 | } |
| giovannivisentini | 8:a9ecd7687de7 | 64 | |
| giovannivisentini | 8:a9ecd7687de7 | 65 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 66 | * set the tag as read only and switch the led on |
| giovannivisentini | 8:a9ecd7687de7 | 67 | * @param nfcNucleo board where the lead lives |
| giovannivisentini | 8:a9ecd7687de7 | 68 | * @return true if the change has effect |
| giovannivisentini | 8:a9ecd7687de7 | 69 | */ |
| giovannivisentini | 9:9742acd0c015 | 70 | bool enableReadOnly( X_NUCLEO_NFC01A1 *nfcNucleo){ |
| giovannivisentini | 8:a9ecd7687de7 | 71 | |
| giovannivisentini | 9:9742acd0c015 | 72 | if(nfcNucleo->getM24SR().enableReadOnly()==NFC_SUCCESS){ |
| giovannivisentini | 8:a9ecd7687de7 | 73 | getReadOnlyLed(nfcNucleo)=true; |
| giovannivisentini | 8:a9ecd7687de7 | 74 | getWriteableLed(nfcNucleo)=false; |
| giovannivisentini | 8:a9ecd7687de7 | 75 | return true; |
| giovannivisentini | 8:a9ecd7687de7 | 76 | }else |
| giovannivisentini | 8:a9ecd7687de7 | 77 | return false; |
| giovannivisentini | 8:a9ecd7687de7 | 78 | } |
| giovannivisentini | 8:a9ecd7687de7 | 79 | |
| giovannivisentini | 8:a9ecd7687de7 | 80 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 81 | * set the tag as read/write and switch the led on |
| giovannivisentini | 8:a9ecd7687de7 | 82 | * @param nfcNucleo board where the lead lives |
| giovannivisentini | 8:a9ecd7687de7 | 83 | * @return true if the change has effect |
| giovannivisentini | 8:a9ecd7687de7 | 84 | */ |
| giovannivisentini | 9:9742acd0c015 | 85 | bool disableReadOnly( X_NUCLEO_NFC01A1 *nfcNucleo){ |
| giovannivisentini | 8:a9ecd7687de7 | 86 | |
| giovannivisentini | 9:9742acd0c015 | 87 | if(nfcNucleo->getM24SR().disableReadOnly()==NFC_SUCCESS){ |
| giovannivisentini | 8:a9ecd7687de7 | 88 | getReadOnlyLed(nfcNucleo)=false; |
| giovannivisentini | 8:a9ecd7687de7 | 89 | getWriteableLed(nfcNucleo)=true; |
| giovannivisentini | 8:a9ecd7687de7 | 90 | return true; |
| giovannivisentini | 8:a9ecd7687de7 | 91 | }else |
| giovannivisentini | 8:a9ecd7687de7 | 92 | return false; |
| giovannivisentini | 8:a9ecd7687de7 | 93 | } |
| giovannivisentini | 8:a9ecd7687de7 | 94 | |
| giovannivisentini | 8:a9ecd7687de7 | 95 | void sample_lockTagContent() { |
| giovannivisentini | 8:a9ecd7687de7 | 96 | Serial console(SERIAL_TX, SERIAL_RX); |
| giovannivisentini | 8:a9ecd7687de7 | 97 | |
| giovannivisentini | 8:a9ecd7687de7 | 98 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
| giovannivisentini | 8:a9ecd7687de7 | 99 | |
| giovannivisentini | 8:a9ecd7687de7 | 100 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel); |
| giovannivisentini | 9:9742acd0c015 | 101 | |
| giovannivisentini | 9:9742acd0c015 | 102 | NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag(); |
| giovannivisentini | 8:a9ecd7687de7 | 103 | |
| giovannivisentini | 8:a9ecd7687de7 | 104 | //when the user press the button set buttonPress to true |
| giovannivisentini | 8:a9ecd7687de7 | 105 | InterruptIn userButton(USER_BUTTON); |
| giovannivisentini | 8:a9ecd7687de7 | 106 | userButton.fall(setButtonPress); |
| giovannivisentini | 9:9742acd0c015 | 107 | |
| giovannivisentini | 8:a9ecd7687de7 | 108 | //restore the board as read/write |
| giovannivisentini | 8:a9ecd7687de7 | 109 | tag.openSession(true); |
| giovannivisentini | 9:9742acd0c015 | 110 | nfcNucleo->getLed1()=nfcNucleo->getM24SR().disableAllPassword()==NFC_SUCCESS; |
| giovannivisentini | 8:a9ecd7687de7 | 111 | tag.closeSession(); |
| giovannivisentini | 8:a9ecd7687de7 | 112 | getReadOnlyLed(nfcNucleo)=false; |
| giovannivisentini | 8:a9ecd7687de7 | 113 | getWriteableLed(nfcNucleo)=true; |
| giovannivisentini | 8:a9ecd7687de7 | 114 | bool tagIsLock=false; |
| giovannivisentini | 8:a9ecd7687de7 | 115 | |
| giovannivisentini | 8:a9ecd7687de7 | 116 | |
| giovannivisentini | 8:a9ecd7687de7 | 117 | while(true){ |
| giovannivisentini | 8:a9ecd7687de7 | 118 | |
| giovannivisentini | 8:a9ecd7687de7 | 119 | if(buttonPress){ |
| giovannivisentini | 8:a9ecd7687de7 | 120 | if(tag.openSession()){ |
| giovannivisentini | 8:a9ecd7687de7 | 121 | bool changeStatusResult; |
| giovannivisentini | 8:a9ecd7687de7 | 122 | |
| giovannivisentini | 8:a9ecd7687de7 | 123 | if(tagIsLock) |
| giovannivisentini | 9:9742acd0c015 | 124 | changeStatusResult=enableReadOnly(nfcNucleo); |
| giovannivisentini | 8:a9ecd7687de7 | 125 | else |
| giovannivisentini | 9:9742acd0c015 | 126 | changeStatusResult=disableReadOnly(nfcNucleo); |
| giovannivisentini | 8:a9ecd7687de7 | 127 | |
| giovannivisentini | 8:a9ecd7687de7 | 128 | if(changeStatusResult) |
| giovannivisentini | 8:a9ecd7687de7 | 129 | tagIsLock=!tagIsLock; |
| giovannivisentini | 8:a9ecd7687de7 | 130 | else |
| giovannivisentini | 8:a9ecd7687de7 | 131 | console.printf("Error change the tag status"); |
| giovannivisentini | 8:a9ecd7687de7 | 132 | tag.closeSession(); |
| giovannivisentini | 8:a9ecd7687de7 | 133 | }else{ |
| giovannivisentini | 8:a9ecd7687de7 | 134 | console.printf("Error opening the session, tag is in use"); |
| giovannivisentini | 8:a9ecd7687de7 | 135 | } |
| giovannivisentini | 8:a9ecd7687de7 | 136 | buttonPress=false; |
| giovannivisentini | 8:a9ecd7687de7 | 137 | }//if |
| giovannivisentini | 8:a9ecd7687de7 | 138 | |
| giovannivisentini | 8:a9ecd7687de7 | 139 | //wait next event |
| giovannivisentini | 8:a9ecd7687de7 | 140 | __WFE(); |
| giovannivisentini | 8:a9ecd7687de7 | 141 | }//while |
| giovannivisentini | 8:a9ecd7687de7 | 142 | |
| giovannivisentini | 8:a9ecd7687de7 | 143 | }//sample_countClick |
| giovannivisentini | 8:a9ecd7687de7 | 144 | |
| giovannivisentini | 8:a9ecd7687de7 | 145 | |
| giovannivisentini | 8:a9ecd7687de7 | 146 | |
| giovannivisentini | 8:a9ecd7687de7 | 147 | |
| giovannivisentini | 8:a9ecd7687de7 | 148 |
