AppNearMe µNFC stack for the NXP PN532 chip License: You can use the stack free of charge to prototype with mbed; if you want to use the stack with your commercial product, get in touch!
Dependents: IOT_sensor_nfc AppNearMe_MuNFC_PN532_Test p2p_nfc_test NFCMoodLamp ... more
License
You can use the stack free of charge to prototype with mbed; if you want to use the stack with your commercial product, get in touch!
PN532/MuNFC.h@0:480387549d89, 2012-07-26 (annotated)
- Committer:
- AppNearMe
- Date:
- Thu Jul 26 09:12:27 2012 +0000
- Revision:
- 0:480387549d89
- Child:
- 2:913eb8fdfd9d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AppNearMe | 0:480387549d89 | 1 | /* |
AppNearMe | 0:480387549d89 | 2 | MuNFC.h |
AppNearMe | 0:480387549d89 | 3 | Copyright (c) Donatien Garnier 2012 |
AppNearMe | 0:480387549d89 | 4 | donatien.garnier@appnearme.com |
AppNearMe | 0:480387549d89 | 5 | http://www.appnearme.com/ |
AppNearMe | 0:480387549d89 | 6 | */ |
AppNearMe | 0:480387549d89 | 7 | |
AppNearMe | 0:480387549d89 | 8 | |
AppNearMe | 0:480387549d89 | 9 | #ifndef MUNFC_H_ |
AppNearMe | 0:480387549d89 | 10 | #define MUNFC_H_ |
AppNearMe | 0:480387549d89 | 11 | |
AppNearMe | 0:480387549d89 | 12 | #include "MuNFCConfig.h" |
AppNearMe | 0:480387549d89 | 13 | |
AppNearMe | 0:480387549d89 | 14 | #include <cstdint> //For uint_*t |
AppNearMe | 0:480387549d89 | 15 | #include <cstring> //For size_t |
AppNearMe | 0:480387549d89 | 16 | |
AppNearMe | 0:480387549d89 | 17 | using std::uint8_t; |
AppNearMe | 0:480387549d89 | 18 | using std::uint16_t; |
AppNearMe | 0:480387549d89 | 19 | using std::uint32_t; |
AppNearMe | 0:480387549d89 | 20 | using std::size_t; |
AppNearMe | 0:480387549d89 | 21 | |
AppNearMe | 0:480387549d89 | 22 | #include "NFCEvent.h" |
AppNearMe | 0:480387549d89 | 23 | #include "NdefCallback.h" |
AppNearMe | 0:480387549d89 | 24 | #include "EventCallback.h" |
AppNearMe | 0:480387549d89 | 25 | #include "TLVList.h" |
AppNearMe | 0:480387549d89 | 26 | |
AppNearMe | 0:480387549d89 | 27 | #include "mbed.h" |
AppNearMe | 0:480387549d89 | 28 | #include "rtos.h" |
AppNearMe | 0:480387549d89 | 29 | |
AppNearMe | 0:480387549d89 | 30 | /** A library for embedded NFC applications using NXP's PN512/PN532 NFC transceivers. |
AppNearMe | 0:480387549d89 | 31 | * Visit http://www.appnearme.com/ |
AppNearMe | 0:480387549d89 | 32 | */ |
AppNearMe | 0:480387549d89 | 33 | class MuNFC |
AppNearMe | 0:480387549d89 | 34 | { |
AppNearMe | 0:480387549d89 | 35 | public: |
AppNearMe | 0:480387549d89 | 36 | |
AppNearMe | 0:480387549d89 | 37 | |
AppNearMe | 0:480387549d89 | 38 | /** Instantiate the µNFC stack for the following mobile app and using the following PN512/PN532 chip. |
AppNearMe | 0:480387549d89 | 39 | * @param appHash 16 chars-long hash of the corresponding mobile app |
AppNearMe | 0:480387549d89 | 40 | * @param version Minimum version of the mobile app to use in BCD format encoded as an uint32_t (0x01000000 is version 1.0.0.0) |
AppNearMe | 0:480387549d89 | 41 | * @param mosi MOSI pin of the SPI interface |
AppNearMe | 0:480387549d89 | 42 | * @param miso MISO pin of the SPI interface |
AppNearMe | 0:480387549d89 | 43 | * @param sclk SCLK pin of the SPI interface |
AppNearMe | 0:480387549d89 | 44 | * @param cs CS pin connected to the chip |
AppNearMe | 0:480387549d89 | 45 | * @param isr ISR pin connected to the chip |
AppNearMe | 0:480387549d89 | 46 | */ |
AppNearMe | 0:480387549d89 | 47 | MuNFC(char appHash[16], uint32_t version, |
AppNearMe | 0:480387549d89 | 48 | PinName mosi, PinName miso, PinName sclk, PinName cs, PinName isr); |
AppNearMe | 0:480387549d89 | 49 | |
AppNearMe | 0:480387549d89 | 50 | /** Set Encode Callback. |
AppNearMe | 0:480387549d89 | 51 | * The encode callback will be called on each start of NFC transaction. |
AppNearMe | 0:480387549d89 | 52 | * to populate the data structure that will be transmitted to the reader |
AppNearMe | 0:480387549d89 | 53 | * @param fn pointer to the function to be called |
AppNearMe | 0:480387549d89 | 54 | * @param arg argument that will be passed to the callback |
AppNearMe | 0:480387549d89 | 55 | */ |
AppNearMe | 0:480387549d89 | 56 | inline void encode(void (*fn)(TLVList*, void*), void* arg) |
AppNearMe | 0:480387549d89 | 57 | { |
AppNearMe | 0:480387549d89 | 58 | m_encodeCb.attach(fn, arg); |
AppNearMe | 0:480387549d89 | 59 | } |
AppNearMe | 0:480387549d89 | 60 | |
AppNearMe | 0:480387549d89 | 61 | /** Set Encode Callback. |
AppNearMe | 0:480387549d89 | 62 | * The encode callback will be called on each start of NFC transaction. |
AppNearMe | 0:480387549d89 | 63 | * to populate the data structure that will be transmitted to the reader |
AppNearMe | 0:480387549d89 | 64 | * @param inst pointer to the object on which to call the member |
AppNearMe | 0:480387549d89 | 65 | * @param member pointer to the object's member to be called |
AppNearMe | 0:480387549d89 | 66 | */ |
AppNearMe | 0:480387549d89 | 67 | template <class T> |
AppNearMe | 0:480387549d89 | 68 | inline void encode(T* inst, void (T::*member)(TLVList*)) |
AppNearMe | 0:480387549d89 | 69 | { |
AppNearMe | 0:480387549d89 | 70 | m_encodeCb.attach(inst, member); |
AppNearMe | 0:480387549d89 | 71 | } |
AppNearMe | 0:480387549d89 | 72 | |
AppNearMe | 0:480387549d89 | 73 | /** Set Decode Callback. |
AppNearMe | 0:480387549d89 | 74 | * The decode callback will be called on each successful termination of NFC transaction. |
AppNearMe | 0:480387549d89 | 75 | * populated with the data structure that was transmitted by the reader |
AppNearMe | 0:480387549d89 | 76 | * @param fn pointer to the function to be called |
AppNearMe | 0:480387549d89 | 77 | * @param arg argument that will be passed to the callback |
AppNearMe | 0:480387549d89 | 78 | */ |
AppNearMe | 0:480387549d89 | 79 | inline void decode(void (*fn)(TLVList*, void*), void* arg) |
AppNearMe | 0:480387549d89 | 80 | { |
AppNearMe | 0:480387549d89 | 81 | m_decodeCb.attach(fn, arg); |
AppNearMe | 0:480387549d89 | 82 | } |
AppNearMe | 0:480387549d89 | 83 | |
AppNearMe | 0:480387549d89 | 84 | /** Set Decode Callback. |
AppNearMe | 0:480387549d89 | 85 | * The decode callback will be called on each successful termination of NFC transaction. |
AppNearMe | 0:480387549d89 | 86 | * populated with the data structure that was transmitted by the reader |
AppNearMe | 0:480387549d89 | 87 | * @param inst pointer to the object on which to call the member |
AppNearMe | 0:480387549d89 | 88 | * @param member pointer to the object's member to be called |
AppNearMe | 0:480387549d89 | 89 | */ |
AppNearMe | 0:480387549d89 | 90 | template <class T> |
AppNearMe | 0:480387549d89 | 91 | inline void decode(T* inst, void (T::*member)(TLVList*)) |
AppNearMe | 0:480387549d89 | 92 | { |
AppNearMe | 0:480387549d89 | 93 | m_decodeCb.attach(inst, member); |
AppNearMe | 0:480387549d89 | 94 | } |
AppNearMe | 0:480387549d89 | 95 | |
AppNearMe | 0:480387549d89 | 96 | /** Set Event Callback. |
AppNearMe | 0:480387549d89 | 97 | * The event callback will be called on each of the following event: |
AppNearMe | 0:480387549d89 | 98 | * - Transaction started |
AppNearMe | 0:480387549d89 | 99 | * - Transaction successful |
AppNearMe | 0:480387549d89 | 100 | * - Transaction failed |
AppNearMe | 0:480387549d89 | 101 | * @param fn pointer to the function to be called |
AppNearMe | 0:480387549d89 | 102 | * @param arg argument that will be passed to the callback |
AppNearMe | 0:480387549d89 | 103 | */ |
AppNearMe | 0:480387549d89 | 104 | inline void event(void (*fn)(NFCEvent, void*), void* arg) |
AppNearMe | 0:480387549d89 | 105 | { |
AppNearMe | 0:480387549d89 | 106 | m_eventCb.attach(fn, arg); |
AppNearMe | 0:480387549d89 | 107 | } |
AppNearMe | 0:480387549d89 | 108 | |
AppNearMe | 0:480387549d89 | 109 | /** Set Event Callback. |
AppNearMe | 0:480387549d89 | 110 | * The event callback will be called on each of the following event: |
AppNearMe | 0:480387549d89 | 111 | * - Transaction started |
AppNearMe | 0:480387549d89 | 112 | * - Transaction successful |
AppNearMe | 0:480387549d89 | 113 | * - Transaction failed |
AppNearMe | 0:480387549d89 | 114 | * @param fn pointer to the function to be called |
AppNearMe | 0:480387549d89 | 115 | * @param arg argument that will be passed to the callback |
AppNearMe | 0:480387549d89 | 116 | */ |
AppNearMe | 0:480387549d89 | 117 | template <class T> |
AppNearMe | 0:480387549d89 | 118 | inline void event(T* inst, void (T::*member)(NFCEvent)); |
AppNearMe | 0:480387549d89 | 119 | |
AppNearMe | 0:480387549d89 | 120 | /** Initialize stack. |
AppNearMe | 0:480387549d89 | 121 | * @return true if stack was initialized correctly, false otherwise |
AppNearMe | 0:480387549d89 | 122 | */ |
AppNearMe | 0:480387549d89 | 123 | bool init(); |
AppNearMe | 0:480387549d89 | 124 | |
AppNearMe | 0:480387549d89 | 125 | //#if MUNFC_RTOS -- flasg must be disabled for proper doxygen support |
AppNearMe | 0:480387549d89 | 126 | /** Start NFC thread (threaded mode) |
AppNearMe | 0:480387549d89 | 127 | * |
AppNearMe | 0:480387549d89 | 128 | */ |
AppNearMe | 0:480387549d89 | 129 | void run(); |
AppNearMe | 0:480387549d89 | 130 | //#endif |
AppNearMe | 0:480387549d89 | 131 | |
AppNearMe | 0:480387549d89 | 132 | #if MUNFC_RTOS |
AppNearMe | 0:480387549d89 | 133 | protected: |
AppNearMe | 0:480387549d89 | 134 | /** NFC Thread |
AppNearMe | 0:480387549d89 | 135 | * |
AppNearMe | 0:480387549d89 | 136 | */ |
AppNearMe | 0:480387549d89 | 137 | void process(); |
AppNearMe | 0:480387549d89 | 138 | #endif |
AppNearMe | 0:480387549d89 | 139 | |
AppNearMe | 0:480387549d89 | 140 | #if MUNFC_RTOS |
AppNearMe | 0:480387549d89 | 141 | protected: |
AppNearMe | 0:480387549d89 | 142 | #else |
AppNearMe | 0:480387549d89 | 143 | public: |
AppNearMe | 0:480387549d89 | 144 | #endif |
AppNearMe | 0:480387549d89 | 145 | /** Poll for NFC reader (polling mode). |
AppNearMe | 0:480387549d89 | 146 | * @param timeoutMs (maximum polling time) |
AppNearMe | 0:480387549d89 | 147 | */ |
AppNearMe | 0:480387549d89 | 148 | void poll(int timeoutMs); |
AppNearMe | 0:480387549d89 | 149 | |
AppNearMe | 0:480387549d89 | 150 | private: |
AppNearMe | 0:480387549d89 | 151 | #if MUNFC_RTOS |
AppNearMe | 0:480387549d89 | 152 | static void staticCallback(void const* p); |
AppNearMe | 0:480387549d89 | 153 | #endif |
AppNearMe | 0:480387549d89 | 154 | |
AppNearMe | 0:480387549d89 | 155 | NdefCallback m_encodeCb; |
AppNearMe | 0:480387549d89 | 156 | NdefCallback m_decodeCb; |
AppNearMe | 0:480387549d89 | 157 | EventCallback m_eventCb; |
AppNearMe | 0:480387549d89 | 158 | |
AppNearMe | 0:480387549d89 | 159 | //DigitalIn m_irq_pin_int; |
AppNearMe | 0:480387549d89 | 160 | InterruptIn m_irq_pin_isr; |
AppNearMe | 0:480387549d89 | 161 | DigitalOut m_cs_pin; |
AppNearMe | 0:480387549d89 | 162 | SPI m_spi; |
AppNearMe | 0:480387549d89 | 163 | |
AppNearMe | 0:480387549d89 | 164 | #if MUNFC_RTOS |
AppNearMe | 0:480387549d89 | 165 | Thread m_thread; |
AppNearMe | 0:480387549d89 | 166 | #endif |
AppNearMe | 0:480387549d89 | 167 | |
AppNearMe | 0:480387549d89 | 168 | }; |
AppNearMe | 0:480387549d89 | 169 | |
AppNearMe | 0:480387549d89 | 170 | |
AppNearMe | 0:480387549d89 | 171 | #endif /* MUNFC_H_ */ |