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 09:19:31 2017 +0000
Revision:
2:e06d881b4b3c
Parent:
1:951cd8fbff34
Use one thread only to handle the events.

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 2:e06d881b4b3c 42 Thread thread;
nikapov 0:3e88cb856f63 43
nikapov 0:3e88cb856f63 44 /** flag to signal an interrupt from the nfc component*/
nikapov 0:3e88cb856f63 45 #define NFC_INTERRUPT_FLAG 0x1
nikapov 0:3e88cb856f63 46
nikapov 0:3e88cb856f63 47 /** flag to signal user button pressure*/
nikapov 0:3e88cb856f63 48 #define BUTTON_PRESSED_FLAG 0x2
nikapov 0:3e88cb856f63 49
nikapov 0:3e88cb856f63 50
nikapov 0:3e88cb856f63 51 NDefLib::NDefNfcTag *tag;
nikapov 0:3e88cb856f63 52
nikapov 0:3e88cb856f63 53 XNucleoNFC01A1 *nfcNucleo;
nikapov 0:3e88cb856f63 54
nikapov 0:3e88cb856f63 55 /** Nfc ISR called when the nfc component has a message ready*/
nikapov 0:3e88cb856f63 56 static void nfc_interrupt_callback() {
nikapov 2:e06d881b4b3c 57 thread.signal_set(NFC_INTERRUPT_FLAG);
nikapov 0:3e88cb856f63 58 }//nfcInterruptCallback
nikapov 0:3e88cb856f63 59
nikapov 1:951cd8fbff34 60 /** ISR handling button pressure **/
nikapov 0:3e88cb856f63 61 static void set_button_press() {
nikapov 2:e06d881b4b3c 62 thread.signal_set(BUTTON_PRESSED_FLAG);
nikapov 0:3e88cb856f63 63 }//buttonPressed event
nikapov 0:3e88cb856f63 64
nikapov 0:3e88cb856f63 65
nikapov 2:e06d881b4b3c 66 void handleEvents() {
nikapov 1:951cd8fbff34 67 //create the callback to read a tag
nikapov 0:3e88cb856f63 68 ReadUriCallbacks NDefReadCallback(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3());
nikapov 0:3e88cb856f63 69 while (true) {
nikapov 2:e06d881b4b3c 70 osEvent event = thread.signal_wait(0); // wait for any event
nikapov 2:e06d881b4b3c 71 if (event.value.v == NFC_INTERRUPT_FLAG) {
nikapov 2:e06d881b4b3c 72 nfcNucleo->get_M24SR().manage_event();
nikapov 2:e06d881b4b3c 73 } else if (event.value.v == BUTTON_PRESSED_FLAG) {
nikapov 2:e06d881b4b3c 74 printf("Button pressed! Reading the Tag.\n\r");
nikapov 2:e06d881b4b3c 75 tag->set_callback(&NDefReadCallback);
nikapov 2:e06d881b4b3c 76 tag->open_session();
nikapov 2:e06d881b4b3c 77 }
nikapov 0:3e88cb856f63 78 }
nikapov 0:3e88cb856f63 79 }
nikapov 0:3e88cb856f63 80
nikapov 0:3e88cb856f63 81 int main() {
nikapov 0:3e88cb856f63 82
nikapov 0:3e88cb856f63 83 #if defined(TARGET_STM)
nikapov 0:3e88cb856f63 84 InterruptIn userButton(USER_BUTTON);
nikapov 0:3e88cb856f63 85 userButton.fall(set_button_press);
nikapov 0:3e88cb856f63 86 #endif
nikapov 0:3e88cb856f63 87
nikapov 0:3e88cb856f63 88 //create the nfc component
nikapov 0:3e88cb856f63 89 I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN);
nikapov 0:3e88cb856f63 90
nikapov 0:3e88cb856f63 91 nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,&nfc_interrupt_callback,
nikapov 0:3e88cb856f63 92 XNucleoNFC01A1::DEFAULT_GPO_PIN,XNucleoNFC01A1::DEFAULT_RF_DISABLE_PIN,
nikapov 0:3e88cb856f63 93 XNucleoNFC01A1::DEFAULT_LED1_PIN,XNucleoNFC01A1::DEFAULT_LED2_PIN,
nikapov 0:3e88cb856f63 94 XNucleoNFC01A1::DEFAULT_LED3_PIN);
nikapov 0:3e88cb856f63 95
nikapov 0:3e88cb856f63 96 //No call back needed since default behavior is sync
nikapov 0:3e88cb856f63 97 nfcNucleo->get_M24SR().get_session();
nikapov 0:3e88cb856f63 98 nfcNucleo->get_M24SR().manage_I2C_GPO(M24SR::I2C_ANSWER_READY); //switch to async mode
nikapov 0:3e88cb856f63 99
nikapov 1:951cd8fbff34 100 tag = &(nfcNucleo->get_M24SR().get_NDef_tag());
nikapov 1:951cd8fbff34 101 printf("System Init done!\n\r");
nikapov 0:3e88cb856f63 102
nikapov 2:e06d881b4b3c 103 //Start the event handling thread
nikapov 2:e06d881b4b3c 104 thread.start(handleEvents);
nikapov 2:e06d881b4b3c 105
nikapov 2:e06d881b4b3c 106 //create the callback to write a tag
nikapov 2:e06d881b4b3c 107 WriteUriCallbacks NDefWriteCallback(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3());
nikapov 2:e06d881b4b3c 108 // write the tag
nikapov 2:e06d881b4b3c 109 tag->set_callback(&NDefWriteCallback); //set the callback
nikapov 2:e06d881b4b3c 110 printf("Writing Tag\n\r");
nikapov 2:e06d881b4b3c 111 tag->open_session();
nikapov 1:951cd8fbff34 112
nikapov 0:3e88cb856f63 113 while (true) {
nikapov 0:3e88cb856f63 114 __WFE();
nikapov 0:3e88cb856f63 115 }
nikapov 0:3e88cb856f63 116
nikapov 0:3e88cb856f63 117 }