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