M24SR NFC example. Simple application to asynchronously write and read an URL from a M24SR tag.
Dependencies: M24SR NDefLib mbed
This simple application provides an example of usage of the M24SR NFC Tag component library.
It is derived from the HelloWorld_Async_NFC01A1 application and currently supports X-NUCLEO-NFC01A1 and ST-Discovery-L475E-IOT01A.
Diff: main.cpp
- Revision:
- 0:651138e1c1af
- Child:
- 1:2805113f2386
diff -r 000000000000 -r 651138e1c1af main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Aug 08 14:35:36 2017 +0000 @@ -0,0 +1,103 @@ +#include "mbed.h" +#include "M24SR.h" +#include "ReadUriCallbacks.h" +#include "WriteUriCallbacks.h" + + +#ifdef TARGET_DISCO_L475VG_IOT01A +// Pins definition for the DISCO_L475VG_IOT01A board +#define NFC_I2C_SDA_PIN PB_11 +#define NFC_I2C_SCL_PIN PB_10 +#define NFC_GPO_PIN PE_4 +#define NFC_RF_DISABLE_PIN PE_2 + +#define LED1_PIN PA_5 +#define LED2_PIN PB_14 +#define LED3_PIN PC_9 + +#else // X-NUCLEO-NFC01A1 + +#define NFC_I2C_SDA_PIN D14 +#define NFC_I2C_SCL_PIN D15 +#define NFC_GPO_PIN D12 +#define NFC_RF_DISABLE_PIN D11 + +#define LED1_PIN D5 +#define LED2_PIN D4 +#define LED3_PIN D2 + +#endif // DISCO_L475VG_IOT01A + +#define M24SR_ADDR 0xAC + + +DigitalOut Led1(LED1_PIN); +DigitalOut Led2(LED2_PIN); +DigitalOut Led3(LED3_PIN); + + +/** Variable set to true when we receive an interrupt from the NFC component */ +static volatile bool nfcInterruptFlag = false; + +/** Variable set to true when the user press the board user button */ +static volatile bool buttonPress = false; + +/** NFC ISR called when the NFC component has a message ready */ +static void nfc_interrupt_callback() +{ + nfcInterruptFlag = true; +} + +static void set_button_press() +{ + buttonPress = true; +} + +int main() +{ + printf("\n\rNFC HelloWord Async example started\n\r"); + + InterruptIn userButton(USER_BUTTON); + userButton.fall(set_button_press); + + // Create the NFC component + I2C i2cChannel(NFC_I2C_SDA_PIN, NFC_I2C_SCL_PIN); + + M24SR nfcTag(M24SR_ADDR,i2cChannel, &nfc_interrupt_callback, NFC_GPO_PIN, NFC_RF_DISABLE_PIN); + + // init needed to enable the component + nfcTag.init(NULL); + // No call back needed since default behavior is sync + nfcTag.get_session(); + nfcTag.manage_I2C_GPO(M24SR::I2C_ANSWER_READY); // Switch to async mode + + NDefLib::NDefNfcTag &tag = nfcTag.get_NDef_tag(); + + printf("Init done\n\r"); + + // Create the callback to use to write a tag + WriteUriCallbacks NDefWriteCallback(Led1,Led2,Led3); + ReadUriCallbacks NDefReadCallback(Led1,Led2,Led3); + tag.set_callback(&NDefWriteCallback); // Set the callback + tag.open_session(); // Start the callback chain + + printf("Start main loop\n\r"); + + while(true) { + + if (nfcInterruptFlag) { + nfcInterruptFlag = false; + // Manage an async event from the NFC component + nfcTag.manage_event(); + } + + if (buttonPress) { + buttonPress = false; + printf("\n\rPressed\n\r"); + tag.set_callback(&NDefReadCallback); + tag.open_session(); + } + + __WFE(); // Wait For Event + } +}