PN532 Driver library This library provides an abstract API to drive the pn532 nfc chip, with I2C/HSU/SPI interface. Its based on the Seeed Studio's Arduino version.
Dependents: PN532_ReadUid Nfctest2
Wire.h@0:db8030e71f55, 2016-09-13 (annotated)
- Committer:
- dotnfc
- Date:
- Tue Sep 13 06:01:19 2016 +0000
- Revision:
- 0:db8030e71f55
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dotnfc | 0:db8030e71f55 | 1 | /* |
dotnfc | 0:db8030e71f55 | 2 | * TwoWire.h |
dotnfc | 0:db8030e71f55 | 3 | * Copyright (c) 2011 Cristian Maglie <c.maglie@bug.st>. |
dotnfc | 0:db8030e71f55 | 4 | * All rights reserved. |
dotnfc | 0:db8030e71f55 | 5 | * |
dotnfc | 0:db8030e71f55 | 6 | * This library is free software; you can redistribute it and/or |
dotnfc | 0:db8030e71f55 | 7 | * modify it under the terms of the GNU Lesser General Public |
dotnfc | 0:db8030e71f55 | 8 | * License as published by the Free Software Foundation; either |
dotnfc | 0:db8030e71f55 | 9 | * version 2.1 of the License, or (at your option) any later version. |
dotnfc | 0:db8030e71f55 | 10 | * |
dotnfc | 0:db8030e71f55 | 11 | * This library is distributed in the hope that it will be useful, |
dotnfc | 0:db8030e71f55 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
dotnfc | 0:db8030e71f55 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
dotnfc | 0:db8030e71f55 | 14 | * Lesser General Public License for more details. |
dotnfc | 0:db8030e71f55 | 15 | * |
dotnfc | 0:db8030e71f55 | 16 | * You should have received a copy of the GNU Lesser General Public |
dotnfc | 0:db8030e71f55 | 17 | * License along with this library; if not, write to the Free Software |
dotnfc | 0:db8030e71f55 | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
dotnfc | 0:db8030e71f55 | 19 | */ |
dotnfc | 0:db8030e71f55 | 20 | |
dotnfc | 0:db8030e71f55 | 21 | #ifndef TwoWire_h |
dotnfc | 0:db8030e71f55 | 22 | #define TwoWire_h |
dotnfc | 0:db8030e71f55 | 23 | |
dotnfc | 0:db8030e71f55 | 24 | // Include Atmel CMSIS driver |
dotnfc | 0:db8030e71f55 | 25 | //#include <include/twi.h> |
dotnfc | 0:db8030e71f55 | 26 | |
dotnfc | 0:db8030e71f55 | 27 | #include "Stream.h" |
dotnfc | 0:db8030e71f55 | 28 | #include "variant.h" |
dotnfc | 0:db8030e71f55 | 29 | |
dotnfc | 0:db8030e71f55 | 30 | #define BUFFER_LENGTH 32 |
dotnfc | 0:db8030e71f55 | 31 | |
dotnfc | 0:db8030e71f55 | 32 | class TwoWire : public Stream { |
dotnfc | 0:db8030e71f55 | 33 | public: |
dotnfc | 0:db8030e71f55 | 34 | TwoWire(I2C_TypeDef *twi); |
dotnfc | 0:db8030e71f55 | 35 | void begin(); |
dotnfc | 0:db8030e71f55 | 36 | void begin(uint8_t); |
dotnfc | 0:db8030e71f55 | 37 | void begin(int); |
dotnfc | 0:db8030e71f55 | 38 | void beginTransmission(uint8_t); |
dotnfc | 0:db8030e71f55 | 39 | void beginTransmission(int); |
dotnfc | 0:db8030e71f55 | 40 | uint8_t endTransmission(void); |
dotnfc | 0:db8030e71f55 | 41 | uint8_t endTransmission(uint8_t); |
dotnfc | 0:db8030e71f55 | 42 | uint8_t requestFrom(uint8_t, uint8_t); |
dotnfc | 0:db8030e71f55 | 43 | uint8_t requestFrom(uint8_t, uint8_t, uint8_t); |
dotnfc | 0:db8030e71f55 | 44 | uint8_t requestFrom(int, int); |
dotnfc | 0:db8030e71f55 | 45 | uint8_t requestFrom(int, int, int); |
dotnfc | 0:db8030e71f55 | 46 | virtual size_t write(uint8_t); |
dotnfc | 0:db8030e71f55 | 47 | virtual size_t write(const uint8_t *, size_t); |
dotnfc | 0:db8030e71f55 | 48 | virtual int available(void); |
dotnfc | 0:db8030e71f55 | 49 | virtual int read(void); |
dotnfc | 0:db8030e71f55 | 50 | virtual int peek(void); |
dotnfc | 0:db8030e71f55 | 51 | virtual void flush(void); |
dotnfc | 0:db8030e71f55 | 52 | void onReceive(void(*)(int)); |
dotnfc | 0:db8030e71f55 | 53 | void onRequest(void(*)(void)); |
dotnfc | 0:db8030e71f55 | 54 | |
dotnfc | 0:db8030e71f55 | 55 | inline size_t write(unsigned long n) { return write((uint8_t)n); } |
dotnfc | 0:db8030e71f55 | 56 | inline size_t write(long n) { return write((uint8_t)n); } |
dotnfc | 0:db8030e71f55 | 57 | inline size_t write(unsigned int n) { return write((uint8_t)n); } |
dotnfc | 0:db8030e71f55 | 58 | inline size_t write(int n) { return write((uint8_t)n); } |
dotnfc | 0:db8030e71f55 | 59 | using Print::write; |
dotnfc | 0:db8030e71f55 | 60 | |
dotnfc | 0:db8030e71f55 | 61 | void onService(void); |
dotnfc | 0:db8030e71f55 | 62 | |
dotnfc | 0:db8030e71f55 | 63 | private: |
dotnfc | 0:db8030e71f55 | 64 | // RX Buffer |
dotnfc | 0:db8030e71f55 | 65 | uint8_t rxBuffer[BUFFER_LENGTH]; |
dotnfc | 0:db8030e71f55 | 66 | uint8_t rxBufferIndex; |
dotnfc | 0:db8030e71f55 | 67 | uint8_t rxBufferLength; |
dotnfc | 0:db8030e71f55 | 68 | |
dotnfc | 0:db8030e71f55 | 69 | // TX Buffer |
dotnfc | 0:db8030e71f55 | 70 | uint8_t txAddress; |
dotnfc | 0:db8030e71f55 | 71 | uint8_t txBuffer[BUFFER_LENGTH]; |
dotnfc | 0:db8030e71f55 | 72 | uint8_t txBufferLength; |
dotnfc | 0:db8030e71f55 | 73 | |
dotnfc | 0:db8030e71f55 | 74 | // Service buffer |
dotnfc | 0:db8030e71f55 | 75 | uint8_t srvBuffer[BUFFER_LENGTH]; |
dotnfc | 0:db8030e71f55 | 76 | uint8_t srvBufferIndex; |
dotnfc | 0:db8030e71f55 | 77 | uint8_t srvBufferLength; |
dotnfc | 0:db8030e71f55 | 78 | |
dotnfc | 0:db8030e71f55 | 79 | // Callback user functions |
dotnfc | 0:db8030e71f55 | 80 | void (*onRequestCallback)(void); |
dotnfc | 0:db8030e71f55 | 81 | void (*onReceiveCallback)(int); |
dotnfc | 0:db8030e71f55 | 82 | |
dotnfc | 0:db8030e71f55 | 83 | // Called before initialization |
dotnfc | 0:db8030e71f55 | 84 | void (*onBeginCallback)(void); |
dotnfc | 0:db8030e71f55 | 85 | |
dotnfc | 0:db8030e71f55 | 86 | // TWI instance |
dotnfc | 0:db8030e71f55 | 87 | I2C_TypeDef *twi; |
dotnfc | 0:db8030e71f55 | 88 | I2C_InitTypeDef I2C_InitStructure; |
dotnfc | 0:db8030e71f55 | 89 | |
dotnfc | 0:db8030e71f55 | 90 | // TWI state |
dotnfc | 0:db8030e71f55 | 91 | enum TwoWireStatus { |
dotnfc | 0:db8030e71f55 | 92 | UNINITIALIZED, |
dotnfc | 0:db8030e71f55 | 93 | MASTER_IDLE, |
dotnfc | 0:db8030e71f55 | 94 | MASTER_SEND, |
dotnfc | 0:db8030e71f55 | 95 | MASTER_RECV, |
dotnfc | 0:db8030e71f55 | 96 | SLAVE_IDLE, |
dotnfc | 0:db8030e71f55 | 97 | SLAVE_RECV, |
dotnfc | 0:db8030e71f55 | 98 | SLAVE_SEND |
dotnfc | 0:db8030e71f55 | 99 | }; |
dotnfc | 0:db8030e71f55 | 100 | TwoWireStatus status; |
dotnfc | 0:db8030e71f55 | 101 | |
dotnfc | 0:db8030e71f55 | 102 | // TWI clock frequency |
dotnfc | 0:db8030e71f55 | 103 | static const uint32_t TWI_CLOCK = 100000; |
dotnfc | 0:db8030e71f55 | 104 | |
dotnfc | 0:db8030e71f55 | 105 | // Timeouts ( |
dotnfc | 0:db8030e71f55 | 106 | static const uint32_t RECV_TIMEOUT = 100000; |
dotnfc | 0:db8030e71f55 | 107 | static const uint32_t XMIT_TIMEOUT = 100000; |
dotnfc | 0:db8030e71f55 | 108 | }; |
dotnfc | 0:db8030e71f55 | 109 | |
dotnfc | 0:db8030e71f55 | 110 | extern TwoWire Wire; |
dotnfc | 0:db8030e71f55 | 111 | extern TwoWire Wire1; |
dotnfc | 0:db8030e71f55 | 112 | |
dotnfc | 0:db8030e71f55 | 113 | |
dotnfc | 0:db8030e71f55 | 114 | #endif |
dotnfc | 0:db8030e71f55 | 115 |