2017-02-25 11:15:18 +01:00
|
|
|
/**
|
2016-03-27 00:25:28 +01:00
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 22:06:53 +01:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-03-27 00:25:28 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "../inc/MarlinConfig.h"
|
2016-03-27 00:25:28 +01:00
|
|
|
|
|
|
|
#if ENABLED(EXPERIMENTAL_I2CBUS)
|
|
|
|
|
|
|
|
#include "twibus.h"
|
2017-09-06 13:28:32 +02:00
|
|
|
|
2016-03-27 00:25:28 +01:00
|
|
|
#include <Wire.h>
|
|
|
|
|
2016-04-20 21:34:55 +02:00
|
|
|
TWIBus::TWIBus() {
|
2016-08-11 08:04:18 +02: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-27 00:25:28 +01:00
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TWIBus::reset() {
|
|
|
|
this->buffer_s = 0;
|
|
|
|
this->buffer[0] = 0x00;
|
|
|
|
}
|
|
|
|
|
2016-08-14 03:06:10 +02:00
|
|
|
void TWIBus::address(const uint8_t adr) {
|
2017-03-31 16:00:49 +02:00
|
|
|
if (!WITHIN(adr, 8, 127)) {
|
2018-11-29 23:58:58 +01:00
|
|
|
SERIAL_ECHO_MSG("Bad I2C address (8-127)");
|
2016-08-12 09:51:25 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 03:06:10 +02:00
|
|
|
this->addr = adr;
|
2016-03-27 00:25:28 +01:00
|
|
|
|
2016-04-20 21:35:56 +02:00
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
2016-08-14 03:06:10 +02:00
|
|
|
debug(PSTR("address"), adr);
|
2016-04-20 21:35:56 +02:00
|
|
|
#endif
|
2016-03-27 00:25:28 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 08:04:18 +02:00
|
|
|
void TWIBus::addbyte(const char c) {
|
2016-08-14 03:06:10 +02:00
|
|
|
if (this->buffer_s >= COUNT(this->buffer)) return;
|
2016-03-27 00:25:28 +01:00
|
|
|
this->buffer[this->buffer_s++] = c;
|
2016-08-14 03:06:10 +02:00
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("addbyte"), c);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void TWIBus::addbytes(char src[], uint8_t bytes) {
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("addbytes"), bytes);
|
|
|
|
#endif
|
|
|
|
while (bytes--) this->addbyte(*src++);
|
|
|
|
}
|
2016-03-27 00:25:28 +01:00
|
|
|
|
2016-08-14 03:06:10 +02:00
|
|
|
void TWIBus::addstring(char str[]) {
|
2016-04-20 21:35:56 +02:00
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
2016-08-14 03:06:10 +02:00
|
|
|
debug(PSTR("addstring"), str);
|
2016-04-20 21:35:56 +02:00
|
|
|
#endif
|
2016-08-14 03:06:10 +02:00
|
|
|
while (char c = *str++) this->addbyte(c);
|
2016-03-27 00:25:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TWIBus::send() {
|
2016-04-20 21:35:56 +02:00
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
2016-08-14 03:06:10 +02:00
|
|
|
debug(PSTR("send"), this->addr);
|
2016-04-20 21:35:56 +02:00
|
|
|
#endif
|
2016-03-27 00:25:28 +01:00
|
|
|
|
2019-02-20 13:26:36 +01:00
|
|
|
Wire.beginTransmission(I2C_ADDRESS(this->addr));
|
2016-03-27 00:25:28 +01:00
|
|
|
Wire.write(this->buffer, this->buffer_s);
|
|
|
|
Wire.endTransmission();
|
|
|
|
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
2016-08-15 06:45:28 +02:00
|
|
|
// static
|
|
|
|
void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
|
2017-06-09 17:51:23 +02:00
|
|
|
SERIAL_ECHO_START();
|
2016-08-14 03:06:10 +02:00
|
|
|
serialprintPGM(prefix);
|
2019-03-05 13:46:19 +01:00
|
|
|
SERIAL_ECHOPAIR(": from:", adr, " bytes:", bytes, " data:");
|
2016-08-15 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
|
|
|
|
echoprefix(bytes, prefix, adr);
|
2016-08-14 03:06:10 +02:00
|
|
|
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
2017-06-09 17:51:23 +02:00
|
|
|
SERIAL_EOL();
|
2016-08-14 03:06:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 06:45:28 +02:00
|
|
|
void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
|
|
|
|
echoprefix(this->buffer_s, prefix, adr);
|
|
|
|
for (uint8_t i = 0; i < this->buffer_s; i++) SERIAL_CHAR(this->buffer[i]);
|
2017-06-09 17:51:23 +02:00
|
|
|
SERIAL_EOL();
|
2016-08-15 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TWIBus::request(const uint8_t bytes) {
|
|
|
|
if (!this->addr) return false;
|
2016-04-20 21:35:56 +02:00
|
|
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
2016-08-15 06:45:28 +02:00
|
|
|
debug(PSTR("request"), bytes);
|
2016-04-20 21:35:56 +02:00
|
|
|
#endif
|
2016-03-27 00:25:28 +01:00
|
|
|
|
2016-08-11 08:04:18 +02:00
|
|
|
// requestFrom() is a blocking function
|
2016-08-28 02:00:45 +02:00
|
|
|
if (Wire.requestFrom(this->addr, bytes) == 0) {
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug("request fail", this->addr);
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-15 06:45:28 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TWIBus::relay(const uint8_t bytes) {
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("relay"), bytes);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (this->request(bytes))
|
|
|
|
echodata(bytes, PSTR("i2c-reply"), this->addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
|
|
|
|
this->reset();
|
|
|
|
uint8_t count = 0;
|
|
|
|
while (count < bytes && Wire.available())
|
|
|
|
dst[count++] = Wire.read();
|
2016-08-28 02:00:45 +02:00
|
|
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("capture"), count);
|
|
|
|
#endif
|
|
|
|
|
2016-08-15 06:45:28 +02:00
|
|
|
return count;
|
|
|
|
}
|
2016-03-27 00:25:28 +01:00
|
|
|
|
2016-08-15 06:45:28 +02:00
|
|
|
// static
|
|
|
|
void TWIBus::flush() {
|
|
|
|
while (Wire.available()) Wire.read();
|
2016-08-11 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 03:06:10 +02:00
|
|
|
#if I2C_SLAVE_ADDRESS > 0
|
2016-03-27 00:25:28 +01:00
|
|
|
|
2016-08-14 03:06:10 +02:00
|
|
|
void TWIBus::receive(uint8_t bytes) {
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("receive"), bytes);
|
|
|
|
#endif
|
2016-08-15 06:45:28 +02:00
|
|
|
echodata(bytes, PSTR("i2c-receive"), 0);
|
2016-08-14 03:06:10 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 18:45:55 +02:00
|
|
|
void TWIBus::reply(char str[]/*=nullptr*/) {
|
2016-08-14 03:06:10 +02:00
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
debug(PSTR("reply"), str);
|
|
|
|
#endif
|
2016-04-20 21:35:56 +02:00
|
|
|
|
2016-08-14 03:06:10 +02:00
|
|
|
if (str) {
|
|
|
|
this->reset();
|
|
|
|
this->addstring(str);
|
2016-04-20 21:35:56 +02:00
|
|
|
}
|
2016-08-14 03:06:10 +02:00
|
|
|
|
|
|
|
Wire.write(this->buffer, this->buffer_s);
|
|
|
|
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
2016-08-15 06:45:28 +02:00
|
|
|
// static
|
2016-08-14 03:06:10 +02:00
|
|
|
void TWIBus::prefix(const char func[]) {
|
|
|
|
SERIAL_ECHOPGM("TWIBus::");
|
|
|
|
serialprintPGM(func);
|
|
|
|
SERIAL_ECHOPGM(": ");
|
|
|
|
}
|
|
|
|
void TWIBus::debug(const char func[], uint32_t adr) {
|
|
|
|
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
|
|
|
|
}
|
|
|
|
void TWIBus::debug(const char func[], char c) {
|
|
|
|
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
|
|
|
|
}
|
|
|
|
void TWIBus::debug(const char func[], char str[]) {
|
|
|
|
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
|
2016-04-20 21:35:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-05-09 19:35:43 +02:00
|
|
|
#endif // EXPERIMENTAL_I2CBUS
|