forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWire.h
138 lines (117 loc) · 3.71 KB
/
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
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
TwoWire.h - TWI/I2C library for Arduino & Wiring
Copyright (c) 2006 Nicholas Zambetti. 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
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
*/
#ifndef TwoWire_h
#define TwoWire_h
#include "Stream.h"
#include "Arduino.h"
extern "C" {
#include "utility/twi.h"
}
// Minimal buffer length. Buffers length will be increased when needed,
// but TX buffer is limited to a maximum to avoid too much stack consumption
// Note: Buffer length and max buffer length are limited by uin16_t type
#define BUFFER_LENGTH 32
#if !defined(WIRE_MAX_TX_BUFF_LENGTH)
#define WIRE_MAX_TX_BUFF_LENGTH 1024U
#endif
// WIRE_HAS_END means Wire has end()
#define WIRE_HAS_END 1
class TwoWire : public Stream {
private:
uint8_t *rxBuffer;
uint16_t rxBufferAllocated;
uint16_t rxBufferIndex;
uint16_t rxBufferLength;
uint8_t txAddress;
uint8_t *txBuffer;
uint16_t txBufferAllocated;
uint16_t txDataSize;
uint8_t transmitting;
uint8_t ownAddress;
i2c_t _i2c;
void (*user_onRequest)(void);
void (*user_onReceive)(int);
static void onRequestService(i2c_t *);
static void onReceiveService(i2c_t *);
void allocateRxBuffer(size_t length);
size_t allocateTxBuffer(size_t length);
void resetRxBuffer(void);
void resetTxBuffer(void);
public:
TwoWire();
TwoWire(uint32_t sda, uint32_t scl);
// setSCL/SDA have to be called before begin()
void setSCL(uint32_t scl)
{
_i2c.scl = digitalPinToPinName(scl);
};
void setSDA(uint32_t sda)
{
_i2c.sda = digitalPinToPinName(sda);
};
void setSCL(PinName scl)
{
_i2c.scl = scl;
};
void setSDA(PinName sda)
{
_i2c.sda = sda;
};
void begin(bool generalCall = false);
void begin(uint32_t, uint32_t);
void begin(uint8_t, bool generalCall = false);
void begin(int, bool generalCall = false);
void end();
void setClock(uint32_t);
void beginTransmission(uint8_t);
void beginTransmission(int);
uint8_t endTransmission(void);
uint8_t endTransmission(uint8_t);
uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
uint8_t requestFrom(uint8_t, size_t, bool);
uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_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 *, 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));
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;
};
extern TwoWire Wire;
#endif