21#ifdef ARDUINO_ARCH_SAMD
24 _maxBufferSize = I2C_BUFFER_LENGTH;
59 defined(ESP8266) || (defined(ARDUINO_ARCH_AVR) && !defined(WIRE_HAS_END)) || \
60 defined(ARDUINO_ARCH_ESP32))
74 if (!_begun && !
begin()) {
79 _wire->beginTransmission(_addr);
80 if (_wire->endTransmission() == 0) {
82 DEBUG_SERIAL.println(F(
"Detected"));
87 DEBUG_SERIAL.println(F(
"Not detected"));
106 const uint8_t* buffer,
size_t len,
bool stop,
const uint8_t* prefix_buffer,
size_t prefix_len)
113 DEBUG_SERIAL.println(F(
"\tI2CDevice could not write such a large buffer"));
118 _wire->beginTransmission(_addr);
121 if ((prefix_len != 0) && (prefix_buffer !=
nullptr)) {
122 if (_wire->write(prefix_buffer, prefix_len) != prefix_len) {
124 DEBUG_SERIAL.println(F(
"\tI2CDevice failed to write"));
131 if (_wire->write(buffer, len) != len) {
133 DEBUG_SERIAL.println(F(
"\tI2CDevice failed to write"));
140 DEBUG_SERIAL.print(F(
"\tI2CWRITE @ 0x"));
141 DEBUG_SERIAL.print(_addr, HEX);
142 DEBUG_SERIAL.print(F(
" :: "));
143 if ((prefix_len != 0) && (prefix_buffer !=
nullptr)) {
144 for (uint16_t i = 0; i < prefix_len; i++) {
145 DEBUG_SERIAL.print(F(
"0x"));
146 DEBUG_SERIAL.print(prefix_buffer[i], HEX);
147 DEBUG_SERIAL.print(F(
", "));
150 for (uint16_t i = 0; i < len; i++) {
151 DEBUG_SERIAL.print(F(
"0x"));
152 DEBUG_SERIAL.print(buffer[i], HEX);
153 DEBUG_SERIAL.print(F(
", "));
155 DEBUG_SERIAL.println();
160 DEBUG_SERIAL.print(
"\tSTOP");
164 if (_wire->endTransmission(stop) == 0) {
166 DEBUG_SERIAL.println();
172 DEBUG_SERIAL.println(
"\tFailed to send!");
191 bool read_stop = (pos < (len - read_len)) ?
false : stop;
192 if (!_read(buffer + pos, read_len, read_stop))
return false;
198bool MiniR4_I2CDevice::_read(uint8_t* buffer,
size_t len,
bool stop)
201#if defined(TinyWireM_h)
202 size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len);
203#elif defined(ARDUINO_ARCH_MEGAAVR)
204 size_t recv = _wire->requestFrom(_addr, len, stop);
206 size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len, (uint8_t)stop);
212 DEBUG_SERIAL.print(F(
"\tI2CDevice did not receive enough data: "));
213 DEBUG_SERIAL.println(recv);
218 for (uint16_t i = 0; i < len; i++) {
219 buffer[i] = _wire->read();
223 DEBUG_SERIAL.print(F(
"\tI2CREAD @ 0x"));
224 DEBUG_SERIAL.print(_addr, HEX);
225 DEBUG_SERIAL.print(F(
" :: "));
226 for (uint16_t i = 0; i < len; i++) {
227 DEBUG_SERIAL.print(F(
"0x"));
228 DEBUG_SERIAL.print(buffer[i], HEX);
229 DEBUG_SERIAL.print(F(
", "));
230 if (len % 32 == 31) {
231 DEBUG_SERIAL.println();
234 DEBUG_SERIAL.println();
252 const uint8_t* write_buffer,
size_t write_len, uint8_t* read_buffer,
size_t read_len,
bool stop)
254 if (!
write(write_buffer, write_len, stop)) {
258 return read(read_buffer, read_len);
279#if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
282 if ((F_CPU / 18) < desiredclk) {
284 Serial.println(F(
"I2C.setSpeed too high."));
288 uint32_t atwbr = ((F_CPU / desiredclk) - 16) / 2;
291 Serial.println(F(
"I2C.setSpeed too low."));
299 }
else if (atwbr <= 1020) {
302 }
else if (atwbr <= 4080) {
312 Serial.print(F(
"TWSR prescaler = "));
313 Serial.println(pow(4, TWSR));
314 Serial.print(F(
"TWBR = "));
315 Serial.println(atwbr);
318#elif (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER) && !defined(TinyWireM_h)
319 _wire->setClock(desiredclk);
328void MiniR4_I2CDevice::i2cMUXSelect()
332 _wire->write((1 <<
_ch));
333 _wire->endTransmission(1);
334 delayMicroseconds(300);
MiniR4 I2C low level functions.
MiniR4_I2CDevice(uint8_t addr, TwoWire *theWire=&Wire, uint8_t mux_ch=-1)
Create an I2C device at a given address.
bool write(const uint8_t *buffer, size_t len, bool stop=true, const uint8_t *prefix_buffer=nullptr, size_t prefix_len=0)
Write a buffer or two to the I2C device. Cannot be more than maxBufferSize() bytes.
uint8_t address(void)
Returns the 7-bit address of this device.
bool setSpeed(uint32_t desiredclk)
Change the I2C clock speed to desired (relies on underlying Wire support!
void end(void)
De-initialize device, turn off the Wire interface.
bool write_then_read(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, bool stop=false)
Write some data, then read some data from I2C into another buffer. Cannot be more than maxBufferSize(...
size_t maxBufferSize()
How many bytes we can read in a transaction.
bool detected(void)
Scans I2C for the address - note will give a false-positive if there's no pullups on I2C.
bool read(uint8_t *buffer, size_t len, bool stop=true)
Read from I2C into a buffer from the I2C device. Cannot be more than maxBufferSize() bytes.
bool begin(bool addr_detect=true)
Initializes and does basic address detection.