-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathWire.cpp
213 lines (185 loc) · 5.11 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Wire.cpp - wrapper over mbed I2C / I2CSlave
Part of Arduino - http://www.arduino.cc/
Copyright (c) 2018-2019 Arduino SA
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., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#include "Wire.h"
#include "pinDefinitions.h"
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(digitalPinToPinName(sda)), _scl(digitalPinToPinName(scl)), usedTxBuffer(0) {}
arduino::MbedI2C::MbedI2C(PinName sda, PinName scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {}
void arduino::MbedI2C::begin() {
end();
master = new mbed::I2C(_sda, _scl);
}
void arduino::MbedI2C::begin(uint8_t slaveAddr) {
#ifdef DEVICE_I2CSLAVE
end();
slave = new mbed::I2CSlave((PinName)_sda, (PinName)_scl);
slave->address(slaveAddr << 1);
slave_th = new rtos::Thread(osPriorityNormal, 2048, nullptr, "I2CSlave");
slave_th->start(mbed::callback(this, &arduino::MbedI2C::receiveThd));
#endif
}
void arduino::MbedI2C::end() {
if (master != NULL) {
delete master;
master = NULL;
}
#ifdef DEVICE_I2CSLAVE
if (slave != NULL) {
slave_th->terminate();
slave_th->free_stack();
delete slave_th;
delete slave;
slave = NULL;
}
#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(bool stopBit) {
#ifndef TARGET_PORTENTA_H7
if (usedTxBuffer == 0) {
// we are scanning, return 0 if the addresed device responds with an ACK
char buf[1];
int ret = master->read(_address, buf, 1, !stopBit);
return ret;
}
#endif
if (master->write(_address, (const char *) txBuffer, usedTxBuffer, !stopBit) == 0) return 0;
return 2;
}
uint8_t arduino::MbedI2C::endTransmission(void) {
return endTransmission(true);
}
size_t arduino::MbedI2C::requestFrom(uint8_t address, size_t len, bool stopBit) {
char buf[256];
len = min(len, sizeof(buf));
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;
}
size_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() {
int rv = -1;
core_util_critical_section_enter();
if (rxBuffer.available()) {
rv = rxBuffer.read_char();
}
core_util_critical_section_exit();
return rv;
}
int arduino::MbedI2C::available() {
core_util_critical_section_enter();
int rv = rxBuffer.available();
core_util_critical_section_exit();
return rv;
}
int arduino::MbedI2C::peek() {
return rxBuffer.peek();
}
void arduino::MbedI2C::flush() {
}
#ifdef DEVICE_I2CSLAVE
void arduino::MbedI2C::receiveThd() {
while (1) {
int i = slave->receive();
int c = 0;
int buf_idx = 0;
switch (i) {
case mbed::I2CSlave::ReadAddressed:
if (onRequestCb != NULL) {
onRequestCb();
}
if (usedTxBuffer != 0) {
core_util_critical_section_enter();
slave->write((const char *) txBuffer, usedTxBuffer);
core_util_critical_section_exit();
usedTxBuffer = 0;
}
//slave->stop();
break;
case mbed::I2CSlave::WriteGeneral:
case mbed::I2CSlave::WriteAddressed:
core_util_critical_section_enter();
rxBuffer.clear();
char buf[240];
c = slave->read(buf, sizeof(buf));
for (buf_idx = 0; buf_idx < c; buf_idx++) {
if (rxBuffer.availableForStore()) {
rxBuffer.store_char(uint8_t(buf[buf_idx]));
} else {
break;
}
}
if (rxBuffer.available() > 0 && onReceiveCb != NULL) {
onReceiveCb(rxBuffer.available());
}
core_util_critical_section_exit();
//slave->stop();
break;
case mbed::I2CSlave::NoData:
//slave->stop();
yield();
break;
}
}
}
#endif
void arduino::MbedI2C::onReceive(voidFuncPtrParamInt cb) {
onReceiveCb = cb;
}
void arduino::MbedI2C::onRequest(voidFuncPtr cb) {
onRequestCb = cb;
}
#if WIRE_HOWMANY > 0
arduino::MbedI2C Wire(I2C_SDA, I2C_SCL);
#endif
#if WIRE_HOWMANY > 1
arduino::MbedI2C Wire1(I2C_SDA1, I2C_SCL1);
#endif
#if WIRE_HOWMANY > 2
arduino::MbedI2C Wire2(I2C_SDA2, I2C_SCL2);
#endif