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.cpp@5:e7e4fe1eb562, 2012-08-16 (annotated)
- Committer:
- AppNearMe
- Date:
- Thu Aug 16 10:42:58 2012 +0000
- Revision:
- 5:e7e4fe1eb562
- Parent:
- 3:0b949b2d3b55
- Child:
- 8:c6c0016c5597
Missing #define directives in MuNFC.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AppNearMe | 5:e7e4fe1eb562 | 1 | /* |
AppNearMe | 5:e7e4fe1eb562 | 2 | MuNFC.cpp |
AppNearMe | 5:e7e4fe1eb562 | 3 | Copyright (c) Donatien Garnier 2012 |
AppNearMe | 5:e7e4fe1eb562 | 4 | donatien.garnier@appnearme.com |
AppNearMe | 5:e7e4fe1eb562 | 5 | http://www.appnearme.com/ |
AppNearMe | 5:e7e4fe1eb562 | 6 | */ |
AppNearMe | 5:e7e4fe1eb562 | 7 | |
AppNearMe | 5:e7e4fe1eb562 | 8 | |
AppNearMe | 5:e7e4fe1eb562 | 9 | #include "MuNFC.h" |
AppNearMe | 5:e7e4fe1eb562 | 10 | #include "MuNFCConfig.h" |
AppNearMe | 5:e7e4fe1eb562 | 11 | |
AppNearMe | 5:e7e4fe1eb562 | 12 | #include "mbed.h" |
AppNearMe | 5:e7e4fe1eb562 | 13 | #if MUNFC_RTOS |
AppNearMe | 5:e7e4fe1eb562 | 14 | #include "rtos/rtos.h" |
AppNearMe | 5:e7e4fe1eb562 | 15 | #endif |
AppNearMe | 5:e7e4fe1eb562 | 16 | |
AppNearMe | 5:e7e4fe1eb562 | 17 | #include "munfc/core/fwk.h" |
AppNearMe | 5:e7e4fe1eb562 | 18 | #include "munfc/target/event.h" |
AppNearMe | 5:e7e4fe1eb562 | 19 | #include "munfc/target/nfctype2.h" |
AppNearMe | 5:e7e4fe1eb562 | 20 | #include "munfc/target/nfctype4.h" |
AppNearMe | 5:e7e4fe1eb562 | 21 | #include "munfc/ndef/appnearme_ndef.h" |
AppNearMe | 5:e7e4fe1eb562 | 22 | #include "munfc/ndef/appnearme_ndef_tlv.h" |
AppNearMe | 5:e7e4fe1eb562 | 23 | |
AppNearMe | 5:e7e4fe1eb562 | 24 | #include <cstring> //For memcpy, strlen |
AppNearMe | 5:e7e4fe1eb562 | 25 | |
AppNearMe | 5:e7e4fe1eb562 | 26 | //TODO add handles in NFC stack to avoid ugly things like that |
AppNearMe | 5:e7e4fe1eb562 | 27 | //extern DigitalIn* nfc_irq_pin_int; |
AppNearMe | 5:e7e4fe1eb562 | 28 | extern InterruptIn* nfc_irq_pin_isr; |
AppNearMe | 5:e7e4fe1eb562 | 29 | extern DigitalOut* nfc_cs_pin; |
AppNearMe | 5:e7e4fe1eb562 | 30 | extern SPI* nfc_spi; |
AppNearMe | 5:e7e4fe1eb562 | 31 | |
AppNearMe | 5:e7e4fe1eb562 | 32 | #define SIGNAL_START 0x01 |
AppNearMe | 5:e7e4fe1eb562 | 33 | |
AppNearMe | 5:e7e4fe1eb562 | 34 | MuNFC::MuNFC(char appHash[16], uint32_t version, |
AppNearMe | 5:e7e4fe1eb562 | 35 | PinName mosi, PinName miso, PinName sclk, PinName cs, PinName isr) : |
AppNearMe | 5:e7e4fe1eb562 | 36 | /* m_irq_pin_int(isr),*/ m_irq_pin_isr(isr), m_cs_pin(cs), m_spi(mosi, miso, sclk) |
AppNearMe | 5:e7e4fe1eb562 | 37 | #if MUNFC_RTOS |
AppNearMe | 5:e7e4fe1eb562 | 38 | , m_pThread(NULL)//m_thread(MuNFC::staticCallback, (void*)this) |
AppNearMe | 5:e7e4fe1eb562 | 39 | #endif |
AppNearMe | 5:e7e4fe1eb562 | 40 | { |
AppNearMe | 5:e7e4fe1eb562 | 41 | //nfc_irq_pin_int = &m_irq_pin_int; |
AppNearMe | 5:e7e4fe1eb562 | 42 | nfc_irq_pin_isr = &m_irq_pin_isr; |
AppNearMe | 5:e7e4fe1eb562 | 43 | nfc_cs_pin = &m_cs_pin; |
AppNearMe | 5:e7e4fe1eb562 | 44 | nfc_spi = &m_spi; |
AppNearMe | 5:e7e4fe1eb562 | 45 | |
AppNearMe | 5:e7e4fe1eb562 | 46 | appnearme_ndef_init(appHash, version); |
AppNearMe | 5:e7e4fe1eb562 | 47 | m_eventCb.init(target_register_event_callback); |
AppNearMe | 5:e7e4fe1eb562 | 48 | m_encodeCb.init(appnearme_ndef_register_encode_callback); |
AppNearMe | 5:e7e4fe1eb562 | 49 | m_decodeCb.init(appnearme_ndef_register_decode_callback); |
AppNearMe | 5:e7e4fe1eb562 | 50 | } |
AppNearMe | 5:e7e4fe1eb562 | 51 | |
AppNearMe | 5:e7e4fe1eb562 | 52 | MuNFC::~MuNFC() |
AppNearMe | 5:e7e4fe1eb562 | 53 | { |
AppNearMe | 5:e7e4fe1eb562 | 54 | #if MUNFC_RTOS |
AppNearMe | 5:e7e4fe1eb562 | 55 | if(m_pThread != NULL) |
AppNearMe | 5:e7e4fe1eb562 | 56 | { |
AppNearMe | 5:e7e4fe1eb562 | 57 | delete m_pThread; |
AppNearMe | 5:e7e4fe1eb562 | 58 | } |
AppNearMe | 5:e7e4fe1eb562 | 59 | #endif |
AppNearMe | 5:e7e4fe1eb562 | 60 | } |
AppNearMe | 5:e7e4fe1eb562 | 61 | |
AppNearMe | 5:e7e4fe1eb562 | 62 | bool MuNFC::init() |
AppNearMe | 5:e7e4fe1eb562 | 63 | { |
AppNearMe | 5:e7e4fe1eb562 | 64 | #if MUNFC_RTOS |
AppNearMe | 5:e7e4fe1eb562 | 65 | if(m_pThread == NULL) |
AppNearMe | 5:e7e4fe1eb562 | 66 | { |
AppNearMe | 5:e7e4fe1eb562 | 67 | m_pThread = new Thread(MuNFC::staticCallback, (void*)this); |
AppNearMe | 5:e7e4fe1eb562 | 68 | } |
AppNearMe | 5:e7e4fe1eb562 | 69 | #endif |
AppNearMe | 5:e7e4fe1eb562 | 70 | #if NFC_CONTROLLER == PN512 |
AppNearMe | 5:e7e4fe1eb562 | 71 | int ret = target_nfctype2_start(); |
AppNearMe | 5:e7e4fe1eb562 | 72 | #elif NFC_CONTROLLER == PN532 |
AppNearMe | 5:e7e4fe1eb562 | 73 | int ret = target_nfctype4_start(); |
AppNearMe | 5:e7e4fe1eb562 | 74 | #endif |
AppNearMe | 5:e7e4fe1eb562 | 75 | if(ret != OK) |
AppNearMe | 5:e7e4fe1eb562 | 76 | { |
AppNearMe | 5:e7e4fe1eb562 | 77 | return false; |
AppNearMe | 5:e7e4fe1eb562 | 78 | } |
AppNearMe | 5:e7e4fe1eb562 | 79 | return true; |
AppNearMe | 5:e7e4fe1eb562 | 80 | } |
AppNearMe | 5:e7e4fe1eb562 | 81 | |
AppNearMe | 5:e7e4fe1eb562 | 82 | #if MUNFC_RTOS |
AppNearMe | 5:e7e4fe1eb562 | 83 | void MuNFC::run() |
AppNearMe | 5:e7e4fe1eb562 | 84 | { |
AppNearMe | 5:e7e4fe1eb562 | 85 | //Start NFC thread |
AppNearMe | 5:e7e4fe1eb562 | 86 | m_pThread->signal_set(SIGNAL_START); |
AppNearMe | 5:e7e4fe1eb562 | 87 | } |
AppNearMe | 5:e7e4fe1eb562 | 88 | |
AppNearMe | 5:e7e4fe1eb562 | 89 | void MuNFC::process() |
AppNearMe | 5:e7e4fe1eb562 | 90 | { |
AppNearMe | 5:e7e4fe1eb562 | 91 | Thread::signal_wait(SIGNAL_START); |
AppNearMe | 5:e7e4fe1eb562 | 92 | do |
AppNearMe | 5:e7e4fe1eb562 | 93 | { |
AppNearMe | 5:e7e4fe1eb562 | 94 | poll(-1); |
AppNearMe | 5:e7e4fe1eb562 | 95 | } while(true); |
AppNearMe | 5:e7e4fe1eb562 | 96 | } |
AppNearMe | 5:e7e4fe1eb562 | 97 | #endif |
AppNearMe | 5:e7e4fe1eb562 | 98 | |
AppNearMe | 5:e7e4fe1eb562 | 99 | void MuNFC::poll(int timeoutMs) //TODO add Timeout |
AppNearMe | 5:e7e4fe1eb562 | 100 | { |
AppNearMe | 5:e7e4fe1eb562 | 101 | #if NFC_CONTROLLER == PN512 |
AppNearMe | 5:e7e4fe1eb562 | 102 | target_nfctype2_process(); |
AppNearMe | 5:e7e4fe1eb562 | 103 | #elif NFC_CONTROLLER == PN532 |
AppNearMe | 5:e7e4fe1eb562 | 104 | target_nfctype4_process(); |
AppNearMe | 5:e7e4fe1eb562 | 105 | #endif |
AppNearMe | 5:e7e4fe1eb562 | 106 | } |
AppNearMe | 5:e7e4fe1eb562 | 107 | |
AppNearMe | 5:e7e4fe1eb562 | 108 | #if MUNFC_RTOS |
AppNearMe | 5:e7e4fe1eb562 | 109 | /*static*/ void MuNFC::staticCallback(void const* p) |
AppNearMe | 5:e7e4fe1eb562 | 110 | { |
AppNearMe | 5:e7e4fe1eb562 | 111 | ((MuNFC*)p)->process(); |
AppNearMe | 5:e7e4fe1eb562 | 112 | } |
AppNearMe | 5:e7e4fe1eb562 | 113 | #endif |