-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathWire.cpp
179 lines (157 loc) · 3.79 KB
/
Wire.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
// This file is subject to the terms and conditions defined in
// file 'LICENSE.md', which is part of this source code package.
*/
#include "Wire.h"
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {}
void arduino::MbedI2C::begin() {
if(!master){
master = new mbed::I2C((PinName)_sda, (PinName)_scl);
setClock(100000); //Default to 100kHz
}
}
void arduino::MbedI2C::begin(uint8_t slaveAddr) {
#ifdef DEVICE_I2CSLAVE
if(!slave){
slave = new mbed::I2CSlave((PinName)_sda, (PinName)_scl);
}
slave->address(slaveAddr << 1);
slave_th.start(mbed::callback(this, &arduino::MbedI2C::receiveThd));
#endif
}
void arduino::MbedI2C::end() {
if (master != NULL) {
delete master;
}
#ifdef DEVICE_I2CSLAVE
if (slave != NULL) {
delete slave;
}
#endif
}
void arduino::MbedI2C::setClock(uint32_t freq) {
if (master != NULL) {
master->frequency(freq);
}
#ifdef DEVICE_I2CSLAVE
if (slave != NULL) {
slave->frequency(freq);
}
#endif
}
void arduino::MbedI2C::beginTransmission(uint8_t address) {
_address = address << 1;
usedTxBuffer = 0;
}
uint8_t arduino::MbedI2C::endTransmission(void) {
return endTransmission(true);
}
uint8_t arduino::MbedI2C::endTransmission(bool stopBit) {
if (master->write(_address, (const char *) txBuffer, usedTxBuffer, !stopBit) == 0){
return 0;
}
return 2;
}
uint8_t arduino::MbedI2C::requestFrom(uint8_t address, size_t len, bool stopBit) {
char buf[256];
int ret = master->read(address << 1, buf, len, !stopBit);
if (ret != 0) {
return 0;
}
for (size_t i=0; i<len; i++) {
rxBuffer.store_char(buf[i]);
}
return len;
}
uint8_t arduino::MbedI2C::requestFrom(uint8_t address, size_t len) {
return requestFrom(address, len, true);
}
size_t arduino::MbedI2C::write(uint8_t data) {
if (usedTxBuffer == 256){
return 0;
}
txBuffer[usedTxBuffer++] = data;
return 1;
}
size_t arduino::MbedI2C::write(const uint8_t* data, int len) {
if (usedTxBuffer + len > 256){
len = 256 - usedTxBuffer;
}
memcpy(txBuffer + usedTxBuffer, data, len);
usedTxBuffer += len;
return len;
}
int arduino::MbedI2C::read() {
if (rxBuffer.available()) {
return rxBuffer.read_char();
}
return 0;
}
int arduino::MbedI2C::available() {
return rxBuffer.available();
}
int arduino::MbedI2C::peek() {
return rxBuffer.peek();
}
void arduino::MbedI2C::flush() {
}
void arduino::MbedI2C::receiveThd() {
#ifdef DEVICE_I2CSLAVE
while (1) {
int i = slave->receive();
switch (i) {
case mbed::I2CSlave::ReadAddressed:
if (onRequestCb != NULL) {
onRequestCb();
}
slave->write((const char *) txBuffer, usedTxBuffer);
slave->stop();
break;
case mbed::I2CSlave::WriteGeneral:
case mbed::I2CSlave::WriteAddressed:
rxBuffer.clear();
char buf[16];
while (1) {
int c = slave->read(buf, sizeof(buf));
for (int i = 0; i < c; i++) {
rxBuffer.store_char(uint8_t(buf[i]));
}
if (c <= sizeof(buf)) {
break;
}
}
if (rxBuffer.available() > 0 && onReceiveCb != NULL) {
onReceiveCb(rxBuffer.available());
}
slave->stop();
break;
}
}
#else
MBED_ASSERT(0);
#endif
}
void arduino::MbedI2C::onReceive(voidFuncPtrParamInt cb) {
onReceiveCb = cb;
}
void arduino::MbedI2C::onRequest(voidFuncPtr cb) {
onRequestCb = cb;
}
#if VARIANT_WIRE_INTFCS > 0
arduino::MbedI2C Wire(VARIANT_Wire_SDA, VARIANT_Wire_SCL);
#endif
#if VARIANT_WIRE_INTFCS > 1
arduino::MbedI2C Wire1(VARIANT_Wire1_SDA, VARIANT_Wire1_SCL);
#endif
#if VARIANT_WIRE_INTFCS > 2
arduino::MbedI2C Wire2(VARIANT_Wire2_SDA, VARIANT_Wire2_SCL);
#endif
#if VARIANT_WIRE_INTFCS > 3
arduino::MbedI2C Wire3(VARIANT_Wire3_SDA, VARIANT_Wire3_SCL);
#endif
#if VARIANT_WIRE_INTFCS > 4
arduino::MbedI2C Wire4(VARIANT_Wire4_SDA, VARIANT_Wire4_SCL);
#endif
#if VARIANT_WIRE_INTFCS > 5
arduino::MbedI2C Wire5(VARIANT_Wire5_SDA, VARIANT_Wire5_SCL);
#endif