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@2:913eb8fdfd9d, 2012-07-26 (annotated)
- Committer:
- AppNearMe
- Date:
- Thu Jul 26 10:02:19 2012 +0000
- Revision:
- 2:913eb8fdfd9d
- Parent:
- 0:480387549d89
- Child:
- 3:0b949b2d3b55
NFC Thread is now dynamically allocated for mbed-rtos compatibility
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 | 2:913eb8fdfd9d | 28 | #if MUNFC_RTOS |
AppNearMe | 2:913eb8fdfd9d | 29 | #include "rtos/rtos.h" |
AppNearMe | 2:913eb8fdfd9d | 30 | #endif |
AppNearMe | 0:480387549d89 | 31 | |
AppNearMe | 0:480387549d89 | 32 | /** A library for embedded NFC applications using NXP's PN512/PN532 NFC transceivers. |
AppNearMe | 0:480387549d89 | 33 | * Visit http://www.appnearme.com/ |
AppNearMe | 0:480387549d89 | 34 | */ |
AppNearMe | 0:480387549d89 | 35 | class MuNFC |
AppNearMe | 0:480387549d89 | 36 | { |
AppNearMe | 0:480387549d89 | 37 | public: |
AppNearMe | 0:480387549d89 | 38 | |
AppNearMe | 0:480387549d89 | 39 | |
AppNearMe | 0:480387549d89 | 40 | /** Instantiate the µNFC stack for the following mobile app and using the following PN512/PN532 chip. |
AppNearMe | 0:480387549d89 | 41 | * @param appHash 16 chars-long hash of the corresponding mobile app |
AppNearMe | 0:480387549d89 | 42 | * @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 | 43 | * @param mosi MOSI pin of the SPI interface |
AppNearMe | 0:480387549d89 | 44 | * @param miso MISO pin of the SPI interface |
AppNearMe | 0:480387549d89 | 45 | * @param sclk SCLK pin of the SPI interface |
AppNearMe | 0:480387549d89 | 46 | * @param cs CS pin connected to the chip |
AppNearMe | 0:480387549d89 | 47 | * @param isr ISR pin connected to the chip |
AppNearMe | 0:480387549d89 | 48 | */ |
AppNearMe | 0:480387549d89 | 49 | MuNFC(char appHash[16], uint32_t version, |
AppNearMe | 0:480387549d89 | 50 | PinName mosi, PinName miso, PinName sclk, PinName cs, PinName isr); |
AppNearMe | 2:913eb8fdfd9d | 51 | |
AppNearMe | 2:913eb8fdfd9d | 52 | ~MuNFC(); |
AppNearMe | 0:480387549d89 | 53 | |
AppNearMe | 0:480387549d89 | 54 | /** Set Encode Callback. |
AppNearMe | 0:480387549d89 | 55 | * The encode callback will be called on each start of NFC transaction. |
AppNearMe | 0:480387549d89 | 56 | * to populate the data structure that will be transmitted to the reader |
AppNearMe | 0:480387549d89 | 57 | * @param fn pointer to the function to be called |
AppNearMe | 0:480387549d89 | 58 | * @param arg argument that will be passed to the callback |
AppNearMe | 0:480387549d89 | 59 | */ |
AppNearMe | 0:480387549d89 | 60 | inline void encode(void (*fn)(TLVList*, void*), void* arg) |
AppNearMe | 0:480387549d89 | 61 | { |
AppNearMe | 0:480387549d89 | 62 | m_encodeCb.attach(fn, arg); |
AppNearMe | 0:480387549d89 | 63 | } |
AppNearMe | 0:480387549d89 | 64 | |
AppNearMe | 0:480387549d89 | 65 | /** Set Encode Callback. |
AppNearMe | 0:480387549d89 | 66 | * The encode callback will be called on each start of NFC transaction. |
AppNearMe | 0:480387549d89 | 67 | * to populate the data structure that will be transmitted to the reader |
AppNearMe | 0:480387549d89 | 68 | * @param inst pointer to the object on which to call the member |
AppNearMe | 0:480387549d89 | 69 | * @param member pointer to the object's member to be called |
AppNearMe | 0:480387549d89 | 70 | */ |
AppNearMe | 0:480387549d89 | 71 | template <class T> |
AppNearMe | 0:480387549d89 | 72 | inline void encode(T* inst, void (T::*member)(TLVList*)) |
AppNearMe | 0:480387549d89 | 73 | { |
AppNearMe | 0:480387549d89 | 74 | m_encodeCb.attach(inst, member); |
AppNearMe | 0:480387549d89 | 75 | } |
AppNearMe | 0:480387549d89 | 76 | |
AppNearMe | 0:480387549d89 | 77 | /** Set Decode Callback. |
AppNearMe | 0:480387549d89 | 78 | * The decode callback will be called on each successful termination of NFC transaction. |
AppNearMe | 0:480387549d89 | 79 | * populated with the data structure that was transmitted by the reader |
AppNearMe | 0:480387549d89 | 80 | * @param fn pointer to the function to be called |
AppNearMe | 0:480387549d89 | 81 | * @param arg argument that will be passed to the callback |
AppNearMe | 0:480387549d89 | 82 | */ |
AppNearMe | 0:480387549d89 | 83 | inline void decode(void (*fn)(TLVList*, void*), void* arg) |
AppNearMe | 0:480387549d89 | 84 | { |
AppNearMe | 0:480387549d89 | 85 | m_decodeCb.attach(fn, arg); |
AppNearMe | 0:480387549d89 | 86 | } |
AppNearMe | 0:480387549d89 | 87 | |
AppNearMe | 0:480387549d89 | 88 | /** Set Decode Callback. |
AppNearMe | 0:480387549d89 | 89 | * The decode callback will be called on each successful termination of NFC transaction. |
AppNearMe | 0:480387549d89 | 90 | * populated with the data structure that was transmitted by the reader |
AppNearMe | 0:480387549d89 | 91 | * @param inst pointer to the object on which to call the member |
AppNearMe | 0:480387549d89 | 92 | * @param member pointer to the object's member to be called |
AppNearMe | 0:480387549d89 | 93 | */ |
AppNearMe | 0:480387549d89 | 94 | template <class T> |
AppNearMe | 0:480387549d89 | 95 | inline void decode(T* inst, void (T::*member)(TLVList*)) |
AppNearMe | 0:480387549d89 | 96 | { |
AppNearMe | 0:480387549d89 | 97 | m_decodeCb.attach(inst, member); |
AppNearMe | 0:480387549d89 | 98 | } |
AppNearMe | 0:480387549d89 | 99 | |
AppNearMe | 0:480387549d89 | 100 | /** Set Event Callback. |
AppNearMe | 0:480387549d89 | 101 | * The event callback will be called on each of the following event: |
AppNearMe | 0:480387549d89 | 102 | * - Transaction started |
AppNearMe | 0:480387549d89 | 103 | * - Transaction successful |
AppNearMe | 0:480387549d89 | 104 | * - Transaction failed |
AppNearMe | 0:480387549d89 | 105 | * @param fn pointer to the function to be called |
AppNearMe | 0:480387549d89 | 106 | * @param arg argument that will be passed to the callback |
AppNearMe | 0:480387549d89 | 107 | */ |
AppNearMe | 0:480387549d89 | 108 | inline void event(void (*fn)(NFCEvent, void*), void* arg) |
AppNearMe | 0:480387549d89 | 109 | { |
AppNearMe | 0:480387549d89 | 110 | m_eventCb.attach(fn, arg); |
AppNearMe | 0:480387549d89 | 111 | } |
AppNearMe | 0:480387549d89 | 112 | |
AppNearMe | 0:480387549d89 | 113 | /** Set Event Callback. |
AppNearMe | 0:480387549d89 | 114 | * The event callback will be called on each of the following event: |
AppNearMe | 0:480387549d89 | 115 | * - Transaction started |
AppNearMe | 0:480387549d89 | 116 | * - Transaction successful |
AppNearMe | 0:480387549d89 | 117 | * - Transaction failed |
AppNearMe | 0:480387549d89 | 118 | * @param fn pointer to the function to be called |
AppNearMe | 0:480387549d89 | 119 | * @param arg argument that will be passed to the callback |
AppNearMe | 0:480387549d89 | 120 | */ |
AppNearMe | 0:480387549d89 | 121 | template <class T> |
AppNearMe | 0:480387549d89 | 122 | inline void event(T* inst, void (T::*member)(NFCEvent)); |
AppNearMe | 0:480387549d89 | 123 | |
AppNearMe | 0:480387549d89 | 124 | /** Initialize stack. |
AppNearMe | 0:480387549d89 | 125 | * @return true if stack was initialized correctly, false otherwise |
AppNearMe | 0:480387549d89 | 126 | */ |
AppNearMe | 0:480387549d89 | 127 | bool init(); |
AppNearMe | 0:480387549d89 | 128 | |
AppNearMe | 0:480387549d89 | 129 | //#if MUNFC_RTOS -- flasg must be disabled for proper doxygen support |
AppNearMe | 0:480387549d89 | 130 | /** Start NFC thread (threaded mode) |
AppNearMe | 0:480387549d89 | 131 | * |
AppNearMe | 0:480387549d89 | 132 | */ |
AppNearMe | 0:480387549d89 | 133 | void run(); |
AppNearMe | 0:480387549d89 | 134 | //#endif |
AppNearMe | 0:480387549d89 | 135 | |
AppNearMe | 0:480387549d89 | 136 | #if MUNFC_RTOS |
AppNearMe | 0:480387549d89 | 137 | protected: |
AppNearMe | 0:480387549d89 | 138 | /** NFC Thread |
AppNearMe | 0:480387549d89 | 139 | * |
AppNearMe | 0:480387549d89 | 140 | */ |
AppNearMe | 0:480387549d89 | 141 | void process(); |
AppNearMe | 0:480387549d89 | 142 | #endif |
AppNearMe | 0:480387549d89 | 143 | |
AppNearMe | 0:480387549d89 | 144 | #if MUNFC_RTOS |
AppNearMe | 0:480387549d89 | 145 | protected: |
AppNearMe | 0:480387549d89 | 146 | #else |
AppNearMe | 0:480387549d89 | 147 | public: |
AppNearMe | 0:480387549d89 | 148 | #endif |
AppNearMe | 0:480387549d89 | 149 | /** Poll for NFC reader (polling mode). |
AppNearMe | 0:480387549d89 | 150 | * @param timeoutMs (maximum polling time) |
AppNearMe | 0:480387549d89 | 151 | */ |
AppNearMe | 0:480387549d89 | 152 | void poll(int timeoutMs); |
AppNearMe | 0:480387549d89 | 153 | |
AppNearMe | 0:480387549d89 | 154 | private: |
AppNearMe | 0:480387549d89 | 155 | #if MUNFC_RTOS |
AppNearMe | 0:480387549d89 | 156 | static void staticCallback(void const* p); |
AppNearMe | 0:480387549d89 | 157 | #endif |
AppNearMe | 0:480387549d89 | 158 | |
AppNearMe | 0:480387549d89 | 159 | NdefCallback m_encodeCb; |
AppNearMe | 0:480387549d89 | 160 | NdefCallback m_decodeCb; |
AppNearMe | 0:480387549d89 | 161 | EventCallback m_eventCb; |
AppNearMe | 0:480387549d89 | 162 | |
AppNearMe | 0:480387549d89 | 163 | //DigitalIn m_irq_pin_int; |
AppNearMe | 0:480387549d89 | 164 | InterruptIn m_irq_pin_isr; |
AppNearMe | 0:480387549d89 | 165 | DigitalOut m_cs_pin; |
AppNearMe | 0:480387549d89 | 166 | SPI m_spi; |
AppNearMe | 0:480387549d89 | 167 | |
AppNearMe | 0:480387549d89 | 168 | #if MUNFC_RTOS |
AppNearMe | 2:913eb8fdfd9d | 169 | Thread* m_pThread; |
AppNearMe | 0:480387549d89 | 170 | #endif |
AppNearMe | 0:480387549d89 | 171 | |
AppNearMe | 0:480387549d89 | 172 | }; |
AppNearMe | 0:480387549d89 | 173 | |
AppNearMe | 0:480387549d89 | 174 | |
AppNearMe | 0:480387549d89 | 175 | #endif /* MUNFC_H_ */ |