
A simple application providing an example of asynchronous access to the X-NUCLEO_NFC01A1 Dynamic NFC Tag board.
Dependencies: NDefLib X_NUCLEO_NFC01A1 mbed
Fork of HelloWord_Async_NFC01A1 by
X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board Firmware Package
Introduction
This firmware package includes Components Device Drivers, Board Support Package and example applications for STMicroelectronics X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board based on M24SR.
Example Application
The application provides a simple example of asynchronous access to the X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board. The program writes a URI link to the M24SR dynamic tag using the asynchronous programming model. The URI can then be retrieved from an NFC enabled smartphone/tablet.
A simpler example providing synchronous access to the tag is also available.
main.cpp@0:79807a600c87, 2016-01-28 (annotated)
- Committer:
- giovannivisentini
- Date:
- Thu Jan 28 16:39:49 2016 +0000
- Revision:
- 0:79807a600c87
- Child:
- 1:f008324c60ab
hello word done in async mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 0:79807a600c87 | 1 | /** |
giovannivisentini | 0:79807a600c87 | 2 | ****************************************************************************** |
giovannivisentini | 0:79807a600c87 | 3 | * @file Sample_async.cpp |
giovannivisentini | 0:79807a600c87 | 4 | * @date 22/01/2016 |
giovannivisentini | 0:79807a600c87 | 5 | * @brief Test the async comunication api |
giovannivisentini | 0:79807a600c87 | 6 | ****************************************************************************** |
giovannivisentini | 0:79807a600c87 | 7 | * |
giovannivisentini | 0:79807a600c87 | 8 | * COPYRIGHT(c) 2015 STMicroelectronics |
giovannivisentini | 0:79807a600c87 | 9 | * |
giovannivisentini | 0:79807a600c87 | 10 | * Redistribution and use in source and binary forms, with or without modification, |
giovannivisentini | 0:79807a600c87 | 11 | * are permitted provided that the following conditions are met: |
giovannivisentini | 0:79807a600c87 | 12 | * 1. Redistributions of source code must retain the above copyright notice, |
giovannivisentini | 0:79807a600c87 | 13 | * this list of conditions and the following disclaimer. |
giovannivisentini | 0:79807a600c87 | 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
giovannivisentini | 0:79807a600c87 | 15 | * this list of conditions and the following disclaimer in the documentation |
giovannivisentini | 0:79807a600c87 | 16 | * and/or other materials provided with the distribution. |
giovannivisentini | 0:79807a600c87 | 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
giovannivisentini | 0:79807a600c87 | 18 | * may be used to endorse or promote products derived from this software |
giovannivisentini | 0:79807a600c87 | 19 | * without specific prior written permission. |
giovannivisentini | 0:79807a600c87 | 20 | * |
giovannivisentini | 0:79807a600c87 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
giovannivisentini | 0:79807a600c87 | 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
giovannivisentini | 0:79807a600c87 | 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
giovannivisentini | 0:79807a600c87 | 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
giovannivisentini | 0:79807a600c87 | 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
giovannivisentini | 0:79807a600c87 | 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
giovannivisentini | 0:79807a600c87 | 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
giovannivisentini | 0:79807a600c87 | 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
giovannivisentini | 0:79807a600c87 | 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
giovannivisentini | 0:79807a600c87 | 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
giovannivisentini | 0:79807a600c87 | 31 | * |
giovannivisentini | 0:79807a600c87 | 32 | ****************************************************************************** |
giovannivisentini | 0:79807a600c87 | 33 | */ |
giovannivisentini | 0:79807a600c87 | 34 | |
giovannivisentini | 0:79807a600c87 | 35 | #include "mbed.h" |
giovannivisentini | 0:79807a600c87 | 36 | |
giovannivisentini | 0:79807a600c87 | 37 | #include "X_NUCLEO_NFC01A1.h" |
giovannivisentini | 0:79807a600c87 | 38 | #include "m24sr/NDefNfcTagM24SR.h" |
giovannivisentini | 0:79807a600c87 | 39 | #include "NDefLib/RecordType/RecordURI.h" |
giovannivisentini | 0:79807a600c87 | 40 | |
giovannivisentini | 0:79807a600c87 | 41 | /** |
giovannivisentini | 0:79807a600c87 | 42 | * Chain of callback that will crate a Uri record and write it. |
giovannivisentini | 0:79807a600c87 | 43 | * After each operation the class will switch on a led |
giovannivisentini | 0:79807a600c87 | 44 | */ |
giovannivisentini | 0:79807a600c87 | 45 | class WriteUriCallbacks : public NDefLib::NDefNfcTag::Callback{ |
giovannivisentini | 0:79807a600c87 | 46 | |
giovannivisentini | 0:79807a600c87 | 47 | DigitalOut &mOnOpenSession; |
giovannivisentini | 0:79807a600c87 | 48 | DigitalOut &mOnWrite; |
giovannivisentini | 0:79807a600c87 | 49 | DigitalOut &mOnCloseSession; |
giovannivisentini | 0:79807a600c87 | 50 | |
giovannivisentini | 0:79807a600c87 | 51 | public: |
giovannivisentini | 0:79807a600c87 | 52 | |
giovannivisentini | 0:79807a600c87 | 53 | /** |
giovannivisentini | 0:79807a600c87 | 54 | * create the callback chain |
giovannivisentini | 0:79807a600c87 | 55 | * @param onOpenSession led to switch on when the session open |
giovannivisentini | 0:79807a600c87 | 56 | * @param onWrite led to switch on when the write end |
giovannivisentini | 0:79807a600c87 | 57 | * @param onCloseSession led to switch on when the session end |
giovannivisentini | 0:79807a600c87 | 58 | */ |
giovannivisentini | 0:79807a600c87 | 59 | WriteUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onWrite, |
giovannivisentini | 0:79807a600c87 | 60 | DigitalOut &onCloseSession):mOnOpenSession(onOpenSession), |
giovannivisentini | 0:79807a600c87 | 61 | mOnWrite(onWrite),mOnCloseSession(onCloseSession){}; |
giovannivisentini | 0:79807a600c87 | 62 | |
giovannivisentini | 0:79807a600c87 | 63 | /** |
giovannivisentini | 0:79807a600c87 | 64 | * crate the new message and write it |
giovannivisentini | 0:79807a600c87 | 65 | * @param tag tag where write the message |
giovannivisentini | 0:79807a600c87 | 66 | * @param success true if the session correctly open |
giovannivisentini | 0:79807a600c87 | 67 | */ |
giovannivisentini | 0:79807a600c87 | 68 | virtual void onSessionOpen(NDefLib::NDefNfcTag *tag,bool success){ |
giovannivisentini | 0:79807a600c87 | 69 | if(!success){ |
giovannivisentini | 0:79807a600c87 | 70 | printf("Error OpenSession\n\r"); |
giovannivisentini | 0:79807a600c87 | 71 | }//else |
giovannivisentini | 0:79807a600c87 | 72 | printf("Session Open\n\r"); |
giovannivisentini | 0:79807a600c87 | 73 | //ask to have an interrupt when the command finish |
giovannivisentini | 0:79807a600c87 | 74 | mOnOpenSession=1; |
giovannivisentini | 0:79807a600c87 | 75 | NDefLib::Message msg; |
giovannivisentini | 0:79807a600c87 | 76 | |
giovannivisentini | 0:79807a600c87 | 77 | NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com"); |
giovannivisentini | 0:79807a600c87 | 78 | msg.addRecord(&rUri); |
giovannivisentini | 0:79807a600c87 | 79 | |
giovannivisentini | 0:79807a600c87 | 80 | tag->write(msg); |
giovannivisentini | 0:79807a600c87 | 81 | } |
giovannivisentini | 0:79807a600c87 | 82 | |
giovannivisentini | 0:79807a600c87 | 83 | /** |
giovannivisentini | 0:79807a600c87 | 84 | * request to close the session |
giovannivisentini | 0:79807a600c87 | 85 | * @param tag tag where close the session |
giovannivisentini | 0:79807a600c87 | 86 | * @param success true if the message is correctly wrote |
giovannivisentini | 0:79807a600c87 | 87 | * @param message wrote |
giovannivisentini | 0:79807a600c87 | 88 | */ |
giovannivisentini | 0:79807a600c87 | 89 | virtual void onMessageWrite(NDefLib::NDefNfcTag *tag,bool success, |
giovannivisentini | 0:79807a600c87 | 90 | const NDefLib::Message&){ |
giovannivisentini | 0:79807a600c87 | 91 | |
giovannivisentini | 0:79807a600c87 | 92 | if(!success) |
giovannivisentini | 0:79807a600c87 | 93 | printf("Error writing tag!\n\r"); |
giovannivisentini | 0:79807a600c87 | 94 | else{ |
giovannivisentini | 0:79807a600c87 | 95 | printf("Tag Wrote!\n\r"); |
giovannivisentini | 0:79807a600c87 | 96 | mOnWrite=1; |
giovannivisentini | 0:79807a600c87 | 97 | }//if-else |
giovannivisentini | 0:79807a600c87 | 98 | tag->closeSession(); |
giovannivisentini | 0:79807a600c87 | 99 | } |
giovannivisentini | 0:79807a600c87 | 100 | |
giovannivisentini | 0:79807a600c87 | 101 | /** |
giovannivisentini | 0:79807a600c87 | 102 | * switch on the led |
giovannivisentini | 0:79807a600c87 | 103 | * @param tag where the session is closed |
giovannivisentini | 0:79807a600c87 | 104 | * @param success true if the session is correctly close |
giovannivisentini | 0:79807a600c87 | 105 | */ |
giovannivisentini | 0:79807a600c87 | 106 | virtual void onSessionClose(NDefLib::NDefNfcTag*,bool success){ |
giovannivisentini | 0:79807a600c87 | 107 | if(success){ |
giovannivisentini | 0:79807a600c87 | 108 | printf("Session closed\n\r"); |
giovannivisentini | 0:79807a600c87 | 109 | mOnCloseSession=1; |
giovannivisentini | 0:79807a600c87 | 110 | }else |
giovannivisentini | 0:79807a600c87 | 111 | printf("Error Session closed\n\r"); |
giovannivisentini | 0:79807a600c87 | 112 | } |
giovannivisentini | 0:79807a600c87 | 113 | |
giovannivisentini | 0:79807a600c87 | 114 | }; |
giovannivisentini | 0:79807a600c87 | 115 | |
giovannivisentini | 0:79807a600c87 | 116 | /** variable set to true when we receive an interrupt from the nfc component*/ |
giovannivisentini | 0:79807a600c87 | 117 | static bool nfcInterruptFlag; |
giovannivisentini | 0:79807a600c87 | 118 | |
giovannivisentini | 0:79807a600c87 | 119 | /** Nfc ISR called when the nfc component has a message ready*/ |
giovannivisentini | 0:79807a600c87 | 120 | static void nfcInterruptCallback(){ |
giovannivisentini | 0:79807a600c87 | 121 | nfcInterruptFlag=true; |
giovannivisentini | 0:79807a600c87 | 122 | }//nfcInterruptCallback |
giovannivisentini | 0:79807a600c87 | 123 | |
giovannivisentini | 0:79807a600c87 | 124 | int main(int argc,char *args[]){ |
giovannivisentini | 0:79807a600c87 | 125 | (void)argc; (void)args; |
giovannivisentini | 0:79807a600c87 | 126 | |
giovannivisentini | 0:79807a600c87 | 127 | //create the nfc component |
giovannivisentini | 0:79807a600c87 | 128 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
giovannivisentini | 0:79807a600c87 | 129 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,&nfcInterruptCallback, |
giovannivisentini | 0:79807a600c87 | 130 | X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN, |
giovannivisentini | 0:79807a600c87 | 131 | X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN, |
giovannivisentini | 0:79807a600c87 | 132 | X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN); |
giovannivisentini | 0:79807a600c87 | 133 | |
giovannivisentini | 0:79807a600c87 | 134 | //No call back needed since default behavior is sync |
giovannivisentini | 0:79807a600c87 | 135 | nfcNucleo->getM24SR().GetSession(); |
giovannivisentini | 0:79807a600c87 | 136 | nfcNucleo->getM24SR().ManageI2CGPO(I2C_ANSWER_READY); //switch to async mode |
giovannivisentini | 0:79807a600c87 | 137 | |
giovannivisentini | 0:79807a600c87 | 138 | NDefLib::NDefNfcTag &tag = nfcNucleo->getM24SR().getNDefTag(); |
giovannivisentini | 0:79807a600c87 | 139 | printf("System Init done!\n\r"); |
giovannivisentini | 0:79807a600c87 | 140 | |
giovannivisentini | 0:79807a600c87 | 141 | //crate the callback to use for write a tag |
giovannivisentini | 0:79807a600c87 | 142 | WriteUriCallbacks NDefCallback(nfcNucleo->getLed1(),nfcNucleo->getLed2(),nfcNucleo->getLed3()); |
giovannivisentini | 0:79807a600c87 | 143 | tag.setCallBack(&NDefCallback); //set the callback |
giovannivisentini | 0:79807a600c87 | 144 | tag.openSession(); //start the callback chain |
giovannivisentini | 0:79807a600c87 | 145 | |
giovannivisentini | 0:79807a600c87 | 146 | printf("Start Main Loop\n\r"); |
giovannivisentini | 0:79807a600c87 | 147 | while(true){ |
giovannivisentini | 0:79807a600c87 | 148 | if(nfcInterruptFlag){ |
giovannivisentini | 0:79807a600c87 | 149 | nfcInterruptFlag=false; |
giovannivisentini | 0:79807a600c87 | 150 | //manage an async event from the nfc component |
giovannivisentini | 0:79807a600c87 | 151 | nfcNucleo->getM24SR().ManageEvent(); |
giovannivisentini | 0:79807a600c87 | 152 | |
giovannivisentini | 0:79807a600c87 | 153 | }//if |
giovannivisentini | 0:79807a600c87 | 154 | __WFE(); |
giovannivisentini | 0:79807a600c87 | 155 | }//while |
giovannivisentini | 0:79807a600c87 | 156 | |
giovannivisentini | 0:79807a600c87 | 157 | //return 0; |
giovannivisentini | 0:79807a600c87 | 158 | } |