You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
4.7 KiB
C++
205 lines
4.7 KiB
C++
8 years ago
|
/**
|
||
9 years ago
|
* 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>
|
||
|
|
||
9 years ago
|
TWIBus::TWIBus() {
|
||
8 years ago
|
#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
|
||
9 years ago
|
this->reset();
|
||
|
}
|
||
|
|
||
|
void TWIBus::reset() {
|
||
|
this->buffer_s = 0;
|
||
|
this->buffer[0] = 0x00;
|
||
|
}
|
||
|
|
||
8 years ago
|
void TWIBus::address(const uint8_t adr) {
|
||
8 years ago
|
if (!WITHIN(adr, 8, 127)) {
|
||
8 years ago
|
SERIAL_ECHO_START();
|
||
8 years ago
|
SERIAL_ECHOLNPGM("Bad I2C address (8-127)");
|
||
|
}
|
||
|
|
||
8 years ago
|
this->addr = adr;
|
||
9 years ago
|
|
||
9 years ago
|
#if ENABLED(DEBUG_TWIBUS)
|
||
8 years ago
|
debug(PSTR("address"), adr);
|
||
9 years ago
|
#endif
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
void TWIBus::addbyte(const char c) {
|
||
8 years ago
|
if (this->buffer_s >= COUNT(this->buffer)) return;
|
||
9 years ago
|
this->buffer[this->buffer_s++] = c;
|
||
8 years ago
|
#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++);
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
void TWIBus::addstring(char str[]) {
|
||
9 years ago
|
#if ENABLED(DEBUG_TWIBUS)
|
||
8 years ago
|
debug(PSTR("addstring"), str);
|
||
9 years ago
|
#endif
|
||
8 years ago
|
while (char c = *str++) this->addbyte(c);
|
||
9 years ago
|
}
|
||
|
|
||
|
void TWIBus::send() {
|
||
9 years ago
|
#if ENABLED(DEBUG_TWIBUS)
|
||
8 years ago
|
debug(PSTR("send"), this->addr);
|
||
9 years ago
|
#endif
|
||
9 years ago
|
|
||
|
Wire.beginTransmission(this->addr);
|
||
|
Wire.write(this->buffer, this->buffer_s);
|
||
|
Wire.endTransmission();
|
||
|
|
||
|
this->reset();
|
||
|
}
|
||
|
|
||
8 years ago
|
// static
|
||
|
void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
|
||
8 years ago
|
SERIAL_ECHO_START();
|
||
8 years ago
|
serialprintPGM(prefix);
|
||
|
SERIAL_ECHOPAIR(": from:", adr);
|
||
|
SERIAL_ECHOPAIR(" bytes:", bytes);
|
||
7 years ago
|
SERIAL_ECHOPGM(" data:");
|
||
8 years ago
|
}
|
||
|
|
||
|
// static
|
||
|
void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
|
||
|
echoprefix(bytes, prefix, adr);
|
||
8 years ago
|
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
||
8 years ago
|
SERIAL_EOL();
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
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]);
|
||
8 years ago
|
SERIAL_EOL();
|
||
8 years ago
|
}
|
||
|
|
||
|
bool TWIBus::request(const uint8_t bytes) {
|
||
|
if (!this->addr) return false;
|
||
9 years ago
|
|
||
|
#if ENABLED(DEBUG_TWIBUS)
|
||
8 years ago
|
debug(PSTR("request"), bytes);
|
||
9 years ago
|
#endif
|
||
9 years ago
|
|
||
8 years ago
|
// requestFrom() is a blocking function
|
||
8 years ago
|
if (Wire.requestFrom(this->addr, bytes) == 0) {
|
||
|
#if ENABLED(DEBUG_TWIBUS)
|
||
|
debug("request fail", this->addr);
|
||
|
#endif
|
||
|
return false;
|
||
|
}
|
||
8 years ago
|
|
||
|
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();
|
||
8 years ago
|
|
||
|
#if ENABLED(DEBUG_TWIBUS)
|
||
|
debug(PSTR("capture"), count);
|
||
|
#endif
|
||
|
|
||
8 years ago
|
return count;
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
// static
|
||
|
void TWIBus::flush() {
|
||
|
while (Wire.available()) Wire.read();
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
#if I2C_SLAVE_ADDRESS > 0
|
||
9 years ago
|
|
||
8 years ago
|
void TWIBus::receive(uint8_t bytes) {
|
||
|
#if ENABLED(DEBUG_TWIBUS)
|
||
|
debug(PSTR("receive"), bytes);
|
||
|
#endif
|
||
8 years ago
|
echodata(bytes, PSTR("i2c-receive"), 0);
|
||
8 years ago
|
}
|
||
|
|
||
|
void TWIBus::reply(char str[]/*=NULL*/) {
|
||
|
#if ENABLED(DEBUG_TWIBUS)
|
||
|
debug(PSTR("reply"), str);
|
||
|
#endif
|
||
9 years ago
|
|
||
8 years ago
|
if (str) {
|
||
|
this->reset();
|
||
|
this->addstring(str);
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
|
Wire.write(this->buffer, this->buffer_s);
|
||
|
|
||
|
this->reset();
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
#if ENABLED(DEBUG_TWIBUS)
|
||
|
|
||
8 years ago
|
// static
|
||
8 years ago
|
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); }
|
||
9 years ago
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
8 years ago
|
#endif // EXPERIMENTAL_I2CBUS
|