A simple mbed OS application providing an example of asynchronous access to the X-NUCLEO_NFC01A1 Dynamic NFC Tag board.

Dependencies:   NDefLib X_NUCLEO_NFC01A1

Fork of mbed-os-example-NFC01A1 by Nicola Capovilla

The application provides a simple example of asynchronous access to the X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board. It represents the multi-threaded mbed OS 5 version of the mbed classic HelloWorld_Async_NFC01A1 application.
The program writes a URI link to the M24SR dynamic tag using the asynchronous programming model. The URI can then be read and printed on the serial console by pressing the user button and/or via RF from an NFC enabled smartphone/tablet.

Committer:
nikapov
Date:
Fri Aug 11 07:58:12 2017 +0000
Revision:
1:951cd8fbff34
Parent:
0:3e88cb856f63
Child:
2:e06d881b4b3c
bug fixing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:3e88cb856f63 1 /**
nikapov 0:3e88cb856f63 2 ******************************************************************************
nikapov 0:3e88cb856f63 3 * @file main.cpp
nikapov 0:3e88cb856f63 4 * @date 22/01/2016
nikapov 0:3e88cb856f63 5 * @brief Test the async comunication api
nikapov 0:3e88cb856f63 6 ******************************************************************************
nikapov 0:3e88cb856f63 7 *
nikapov 0:3e88cb856f63 8 * COPYRIGHT(c) 2015 STMicroelectronics
nikapov 0:3e88cb856f63 9 *
nikapov 0:3e88cb856f63 10 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:3e88cb856f63 11 * are permitted provided that the following conditions are met:
nikapov 0:3e88cb856f63 12 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:3e88cb856f63 13 * this list of conditions and the following disclaimer.
nikapov 0:3e88cb856f63 14 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:3e88cb856f63 15 * this list of conditions and the following disclaimer in the documentation
nikapov 0:3e88cb856f63 16 * and/or other materials provided with the distribution.
nikapov 0:3e88cb856f63 17 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:3e88cb856f63 18 * may be used to endorse or promote products derived from this software
nikapov 0:3e88cb856f63 19 * without specific prior written permission.
nikapov 0:3e88cb856f63 20 *
nikapov 0:3e88cb856f63 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:3e88cb856f63 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:3e88cb856f63 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:3e88cb856f63 24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:3e88cb856f63 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:3e88cb856f63 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:3e88cb856f63 27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:3e88cb856f63 28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:3e88cb856f63 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:3e88cb856f63 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:3e88cb856f63 31 *
nikapov 0:3e88cb856f63 32 ******************************************************************************
nikapov 0:3e88cb856f63 33 */
nikapov 0:3e88cb856f63 34
nikapov 0:3e88cb856f63 35 #include "mbed.h"
nikapov 0:3e88cb856f63 36 #include "XNucleoNFC01A1.h"
nikapov 0:3e88cb856f63 37 #include "ReadUriCallbacks.h"
nikapov 0:3e88cb856f63 38 #include "WriteUriCallbacks.h"
nikapov 0:3e88cb856f63 39 #include "NDefNfcTagM24SR.h"
nikapov 0:3e88cb856f63 40 #include "NDefLib/RecordType/RecordURI.h"
nikapov 0:3e88cb856f63 41
nikapov 0:3e88cb856f63 42 Thread nfcReadThread;
nikapov 0:3e88cb856f63 43 Thread nfcEventsThread;
nikapov 0:3e88cb856f63 44
nikapov 0:3e88cb856f63 45 /** flag to signal an interrupt from the nfc component*/
nikapov 0:3e88cb856f63 46 #define NFC_INTERRUPT_FLAG 0x1
nikapov 0:3e88cb856f63 47
nikapov 0:3e88cb856f63 48 /** flag to signal user button pressure*/
nikapov 0:3e88cb856f63 49 #define BUTTON_PRESSED_FLAG 0x2
nikapov 0:3e88cb856f63 50
nikapov 0:3e88cb856f63 51
nikapov 0:3e88cb856f63 52 NDefLib::NDefNfcTag *tag;
nikapov 0:3e88cb856f63 53
nikapov 0:3e88cb856f63 54 XNucleoNFC01A1 *nfcNucleo;
nikapov 0:3e88cb856f63 55
nikapov 0:3e88cb856f63 56 /** Nfc ISR called when the nfc component has a message ready*/
nikapov 0:3e88cb856f63 57 static void nfc_interrupt_callback() {
nikapov 0:3e88cb856f63 58 nfcEventsThread.signal_set(NFC_INTERRUPT_FLAG);
nikapov 0:3e88cb856f63 59 }//nfcInterruptCallback
nikapov 0:3e88cb856f63 60
nikapov 1:951cd8fbff34 61 /** ISR handling button pressure **/
nikapov 0:3e88cb856f63 62 static void set_button_press() {
nikapov 0:3e88cb856f63 63 nfcReadThread.signal_set(BUTTON_PRESSED_FLAG);
nikapov 0:3e88cb856f63 64 }//buttonPressed event
nikapov 0:3e88cb856f63 65
nikapov 0:3e88cb856f63 66
nikapov 0:3e88cb856f63 67 void nfcEvents() {
nikapov 0:3e88cb856f63 68 while (true) {
nikapov 0:3e88cb856f63 69 osEvent event = nfcEventsThread.signal_wait(NFC_INTERRUPT_FLAG); // wait for nfc event
nikapov 1:951cd8fbff34 70 //printf("Got an NFC event!\n\r");
nikapov 0:3e88cb856f63 71 nfcNucleo->get_M24SR().manage_event();
nikapov 0:3e88cb856f63 72 }
nikapov 0:3e88cb856f63 73 }
nikapov 0:3e88cb856f63 74
nikapov 0:3e88cb856f63 75 void readNfc() {
nikapov 0:3e88cb856f63 76
nikapov 1:951cd8fbff34 77 //create the callback to read a tag
nikapov 0:3e88cb856f63 78 ReadUriCallbacks NDefReadCallback(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3());
nikapov 1:951cd8fbff34 79 //create the callback to write a tag
nikapov 1:951cd8fbff34 80 WriteUriCallbacks NDefWriteCallback(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3());
nikapov 1:951cd8fbff34 81 // write the tag
nikapov 1:951cd8fbff34 82 printf("Writing Tag\n\r");
nikapov 1:951cd8fbff34 83 tag->open_session();
nikapov 0:3e88cb856f63 84
nikapov 0:3e88cb856f63 85 while (true) {
nikapov 0:3e88cb856f63 86 osEvent event = nfcReadThread.signal_wait(BUTTON_PRESSED_FLAG); // wait for button pressed
nikapov 0:3e88cb856f63 87 printf("Button pressed! Reading the Tag.\n\r");
nikapov 0:3e88cb856f63 88 tag->set_callback(&NDefReadCallback);
nikapov 0:3e88cb856f63 89 tag->open_session();
nikapov 0:3e88cb856f63 90 }
nikapov 0:3e88cb856f63 91
nikapov 0:3e88cb856f63 92 }
nikapov 0:3e88cb856f63 93
nikapov 0:3e88cb856f63 94 int main() {
nikapov 0:3e88cb856f63 95
nikapov 0:3e88cb856f63 96 #if defined(TARGET_STM)
nikapov 0:3e88cb856f63 97 InterruptIn userButton(USER_BUTTON);
nikapov 0:3e88cb856f63 98 userButton.fall(set_button_press);
nikapov 0:3e88cb856f63 99 #endif
nikapov 0:3e88cb856f63 100
nikapov 0:3e88cb856f63 101 //create the nfc component
nikapov 0:3e88cb856f63 102 I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN);
nikapov 0:3e88cb856f63 103
nikapov 0:3e88cb856f63 104 nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,&nfc_interrupt_callback,
nikapov 0:3e88cb856f63 105 XNucleoNFC01A1::DEFAULT_GPO_PIN,XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN,
nikapov 0:3e88cb856f63 106 XNucleoNFC01A1::DEFAULT_LED1_PIN,XNucleoNFC01A1::DEFAULT_LED2_PIN,
nikapov 0:3e88cb856f63 107 XNucleoNFC01A1::DEFAULT_LED3_PIN);
nikapov 0:3e88cb856f63 108
nikapov 0:3e88cb856f63 109 //No call back needed since default behavior is sync
nikapov 0:3e88cb856f63 110 nfcNucleo->get_M24SR().get_session();
nikapov 0:3e88cb856f63 111 nfcNucleo->get_M24SR().manage_I2C_GPO(M24SR::I2C_ANSWER_READY); //switch to async mode
nikapov 0:3e88cb856f63 112
nikapov 1:951cd8fbff34 113 tag = &(nfcNucleo->get_M24SR().get_NDef_tag());
nikapov 1:951cd8fbff34 114 printf("System Init done!\n\r");
nikapov 0:3e88cb856f63 115
nikapov 0:3e88cb856f63 116 //Start the nfc event handling thread
nikapov 0:3e88cb856f63 117 nfcEventsThread.start(nfcEvents);
nikapov 1:951cd8fbff34 118 //Start the nfc reading thread - also writes the tag once
nikapov 0:3e88cb856f63 119 nfcReadThread.start(readNfc);
nikapov 1:951cd8fbff34 120
nikapov 0:3e88cb856f63 121 while (true) {
nikapov 0:3e88cb856f63 122 __WFE();
nikapov 0:3e88cb856f63 123 }
nikapov 0:3e88cb856f63 124
nikapov 0:3e88cb856f63 125 }