1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-12 23:08:52 +01:00
Arduino/hardware/arduino/sam/libraries/Wire/Wire.h

120 lines
3.0 KiB
C
Raw Normal View History

2011-10-25 14:33:40 +02:00
/*
* TwoWire.h - TWI/I2C library for Arduino Due
2015-02-05 16:47:22 +01:00
* Copyright (c) 2011 Cristian Maglie <c.maglie@arduino.cc>
* All rights reserved.
2011-10-25 14:33:40 +02:00
*
* 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 TwoWire_h
#define TwoWire_h
2011-10-25 14:33:40 +02:00
// Include Atmel CMSIS driver
#include <include/twi.h>
2011-10-25 14:33:40 +02:00
#include "Stream.h"
2011-11-06 14:00:02 +01:00
#include "variant.h"
2011-10-25 14:33:40 +02:00
#define BUFFER_LENGTH 32
class TwoWire : public Stream {
2011-10-25 14:33:40 +02:00
public:
TwoWire(Twi *twi, void(*begin_cb)(void));
2011-10-25 14:33:40 +02:00
void begin();
void begin(uint8_t);
void begin(int);
void setClock(uint32_t);
2011-10-25 14:33:40 +02:00
void beginTransmission(uint8_t);
void beginTransmission(int);
uint8_t endTransmission(void);
uint8_t endTransmission(uint8_t);
2011-10-25 14:33:40 +02:00
uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
2011-10-25 14:33:40 +02:00
uint8_t requestFrom(int, int);
uint8_t requestFrom(int, int, int);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *, size_t);
2011-10-25 14:33:40 +02:00
virtual int available(void);
virtual int read(void);
virtual int peek(void);
virtual void flush(void);
void onReceive(void(*)(int));
void onRequest(void(*)(void));
inline size_t write(unsigned long n) { return write((uint8_t)n); }
inline size_t write(long n) { return write((uint8_t)n); }
inline size_t write(unsigned int n) { return write((uint8_t)n); }
inline size_t write(int n) { return write((uint8_t)n); }
using Print::write;
2011-10-25 14:33:40 +02:00
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);
2011-11-06 14:00:02 +01:00
// Called before initialization
void (*onBeginCallback)(void);
2011-10-25 14:33:40 +02:00
// TWI instance
Twi *twi;
// TWI state
enum TwoWireStatus {
2011-11-06 14:00:02 +01:00
UNINITIALIZED,
MASTER_IDLE,
MASTER_SEND,
MASTER_RECV,
SLAVE_IDLE,
SLAVE_RECV,
SLAVE_SEND
};
TwoWireStatus status;
2011-11-06 14:00:02 +01:00
// TWI clock frequency
static const uint32_t TWI_CLOCK = 100000;
uint32_t twiClock;
2011-11-06 14:00:02 +01:00
// Timeouts (
static const uint32_t RECV_TIMEOUT = 100000;
static const uint32_t XMIT_TIMEOUT = 100000;
2011-10-25 14:33:40 +02:00
};
2011-11-06 14:00:02 +01:00
#if WIRE_INTERFACES_COUNT > 0
extern TwoWire Wire;
2011-11-06 14:00:02 +01:00
#endif
#if WIRE_INTERFACES_COUNT > 1
extern TwoWire Wire1;
2011-11-06 14:00:02 +01:00
#endif
2011-10-25 14:33:40 +02:00
#endif