-
Notifications
You must be signed in to change notification settings - Fork 9
/
due_wire.h
126 lines (106 loc) · 3.22 KB
/
due_wire.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* TwoWire.h - TWI/I2C library for Arduino Due
* Copyright (c) 2011 Cristian Maglie <[email protected]>.
* All rights 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 TwoWire_h
#define TwoWire_h
// Include Atmel CMSIS driver
#include <include/twi.h>
#include "Stream.h"
#include "variant.h"
//this buffer is much larger than the normal Arduino one
//It allows for a 256 byte buffer to be sent along with
//extra control data.
#define BUFFER_LENGTH 260
class TwoWire : public Stream {
public:
TwoWire(Twi *twi, void(*begin_cb)(void));
void begin();
void begin(uint8_t);
void begin(uint16_t);
void begin(uint32_t);
void begin(uint8_t, uint32_t);
void begin(uint16_t, uint32_t);
void begin(uint32_t, uint32_t);
void beginTransmission(uint8_t);
void beginTransmission(int);
uint8_t endTransmission(void);
uint8_t endTransmission(uint8_t);
uint8_t requestFrom(uint8_t, uint16_t);
uint8_t requestFrom(uint8_t, uint16_t, uint8_t);
//uint8_t requestFrom(int, int);
//uint8_t requestFrom(int, int, int);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *, uint16_t);
virtual size_t write(const uint8_t *, int);
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;
void onService(void);
private:
//TWI clock freq
uint32_t twiSpeed;
// RX Buffer
uint8_t rxBuffer[BUFFER_LENGTH];
uint16_t rxBufferIndex;
uint16_t rxBufferLength;
// TX Buffer
uint8_t txAddress;
uint16_t txBuffer[BUFFER_LENGTH];
uint16_t txBufferLength;
// Service buffer
uint8_t srvBuffer[BUFFER_LENGTH];
uint16_t srvBufferIndex;
uint16_t srvBufferLength;
// Callback user functions
void (*onRequestCallback)(void);
void (*onReceiveCallback)(int);
// Called before initialization
void (*onBeginCallback)(void);
// TWI instance
Twi *twi;
// TWI state
enum TwoWireStatus {
UNINITIALIZED,
MASTER_IDLE,
MASTER_SEND,
MASTER_RECV,
SLAVE_IDLE,
SLAVE_RECV,
SLAVE_SEND
};
TwoWireStatus status;
// Timeouts (
static const uint32_t RECV_TIMEOUT = 100000;
static const uint32_t XMIT_TIMEOUT = 100000;
};
#if WIRE_INTERFACES_COUNT > 0
extern TwoWire Wire;
#endif
#if WIRE_INTERFACES_COUNT > 1
extern TwoWire Wire1;
#endif
#endif