2016-03-26 18:25:28 -05:00
|
|
|
/*
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Marlin.h"
|
|
|
|
|
|
|
|
#if ENABLED(EXPERIMENTAL_I2CBUS)
|
|
|
|
|
|
|
|
#include "twibus.h"
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
|
2016-04-20 14:34:55 -05:00
|
|
|
TWIBus::TWIBus() {
|
2016-08-11 01:04:18 -05:00
|
|
|
#if I2C_SLAVE_ADDRESS == 0
|
|
|
|
Wire.begin(); // No address joins the BUS as the master
|
|
|
|
#else
|
|
|
|
Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave
|
|
|
|
#endif
|
2016-03-26 18:25:28 -05:00
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TWIBus::reset() {
|
|
|
|
this->buffer_s = 0;
|
|
|
|
this->buffer[0] = 0x00;
|
|
|
|
}
|
|
|
|
|
2016-08-11 01:04:18 -05:00
|
|
|
void TWIBus::address(const uint8_t addr) {
|
2016-03-26 18:25:28 -05:00
|
|
|
this->addr = addr;
|
|
|
|
|
2016-04-20 14:35:56 -05:00
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
2016-08-11 01:04:18 -05:00
|
|
|
debug(PSTR("address"), this->addr);
|
2016-04-20 14:35:56 -05:00
|
|
|
#endif
|
2016-03-26 18:25:28 -05:00
|
|
|
}
|
|
|
|
|
2016-08-11 01:04:18 -05:00
|
|
|
void TWIBus::addbyte(const char c) {
|
2016-03-26 18:25:28 -05:00
|
|
|
if (buffer_s >= sizeof(this->buffer)) return;
|
|
|
|
this->buffer[this->buffer_s++] = c;
|
|
|
|
|
2016-04-20 14:35:56 -05:00
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("addbyte"), this->buffer[this->buffer_s - 1]);
|
|
|
|
#endif
|
2016-03-26 18:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TWIBus::send() {
|
|
|
|
if (!this->addr) return;
|
2016-04-20 14:35:56 -05:00
|
|
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("send()"));
|
|
|
|
#endif
|
2016-03-26 18:25:28 -05:00
|
|
|
|
|
|
|
Wire.beginTransmission(this->addr);
|
|
|
|
Wire.write(this->buffer, this->buffer_s);
|
|
|
|
Wire.endTransmission();
|
|
|
|
|
2016-08-11 01:04:18 -05:00
|
|
|
// Reset the buffer after sending the data
|
2016-03-26 18:25:28 -05:00
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
2016-08-11 01:04:18 -05:00
|
|
|
void TWIBus::reqbytes(const uint8_t bytes) {
|
2016-03-26 18:25:28 -05:00
|
|
|
if (!this->addr) return;
|
2016-04-20 14:35:56 -05:00
|
|
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("reqbytes"), bytes);
|
|
|
|
#endif
|
2016-03-26 18:25:28 -05:00
|
|
|
|
2016-08-11 01:04:18 -05:00
|
|
|
// requestFrom() is a blocking function
|
2016-04-10 17:55:12 -05:00
|
|
|
millis_t t = millis() + this->timeout;
|
2016-03-26 18:25:28 -05:00
|
|
|
Wire.requestFrom(this->addr, bytes);
|
2016-08-11 01:04:18 -05:00
|
|
|
while (Wire.available() < bytes && PENDING(millis(), t)) { /*nada*/ }
|
2016-03-26 18:25:28 -05:00
|
|
|
|
2016-08-11 01:04:18 -05:00
|
|
|
this->relaydata(bytes);
|
2016-03-26 18:25:28 -05:00
|
|
|
|
2016-08-11 01:04:18 -05:00
|
|
|
// Reset the buffer after sending the data
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TWIBus::relaydata(uint8_t bytes) {
|
2016-03-26 18:25:28 -05:00
|
|
|
SERIAL_ECHO_START;
|
|
|
|
SERIAL_ECHOPAIR("i2c-reply: from:", this->addr);
|
2016-08-11 01:04:18 -05:00
|
|
|
SERIAL_ECHOPAIR(" bytes:", bytes);
|
2016-03-26 18:25:28 -05:00
|
|
|
SERIAL_ECHOPGM (" data:");
|
2016-08-11 01:04:18 -05:00
|
|
|
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
2016-03-26 18:25:28 -05:00
|
|
|
SERIAL_EOL;
|
|
|
|
}
|
|
|
|
|
2016-04-20 14:35:56 -05:00
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
|
|
|
void TWIBus::debug(const char func[], int32_t val/*=-1*/) {
|
|
|
|
if (DEBUGGING(INFO)) {
|
|
|
|
SERIAL_ECHOPGM("TWIBus::");
|
|
|
|
serialprintPGM(func);
|
|
|
|
if (val >= 0) SERIAL_ECHOPAIR(": ", val);
|
|
|
|
SERIAL_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-03-26 18:25:28 -05:00
|
|
|
#endif //EXPERIMENTAL_I2CBUS
|