1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-04 15:24:12 +01:00
Arduino/hardware/sam/libraries/Wire/Wire.h
2011-11-06 14:00:02 +01:00

108 lines
2.5 KiB
C++

/*
* DueWire.h - TWI/I2C library for Arduino Due
* Copyright (c) 2011 Cristian Maglie <c.maglie@bug.st>.
* All right reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef DueWire_h
#define DueWire_h
#include "Stream.h"
#include "twi.h"
#include "variant.h"
#define BUFFER_LENGTH 32
class DueWire: public Stream {
public:
DueWire(Twi *twi, void(*begin_cb)(void));
void begin();
void begin(uint8_t);
void begin(int);
void beginTransmission(uint8_t);
void beginTransmission(int);
uint8_t endTransmission(void);
uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(int, int);
virtual void write(uint8_t);
virtual void write(const char *);
virtual void write(const uint8_t *, size_t);
virtual int available(void);
virtual int read(void);
virtual int peek(void);
virtual void flush(void);
void onReceive(void(*)(int));
void onRequest(void(*)(void));
void onService(void);
private:
// RX Buffer
uint8_t rxBuffer[BUFFER_LENGTH];
uint8_t rxBufferIndex;
uint8_t rxBufferLength;
// TX Buffer
uint8_t txAddress;
uint8_t txBuffer[BUFFER_LENGTH];
uint8_t txBufferLength;
// Service buffer
uint8_t srvBuffer[BUFFER_LENGTH];
uint8_t srvBufferIndex;
uint8_t srvBufferLength;
// Callback user functions
void (*onRequestCallback)(void);
void (*onReceiveCallback)(int);
// Called before initialization
void (*onBeginCallback)(void);
// TWI instance
Twi *twi;
// TWI state
enum DueWireStatus {
UNINITIALIZED,
MASTER_IDLE,
MASTER_SEND,
MASTER_RECV,
SLAVE_IDLE,
SLAVE_RECV,
SLAVE_SEND
};
DueWireStatus status;
// TWI clock frequency
static const uint32_t TWI_CLOCK = 100000;
// Timeouts (
static const uint32_t RECV_TIMEOUT = 100000;
static const uint32_t XMIT_TIMEOUT = 100000;
};
#if WIRE_INTERFACES_COUNT > 0
extern DueWire Wire;
#endif
#if WIRE_INTERFACES_COUNT > 1
extern DueWire Wire1;
#endif
#endif