muele-marlin/Marlin/twibus.cpp

163 lines
3.9 KiB
C++
Raw Normal View History

/*
* 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 19:34:55 +00:00
TWIBus::TWIBus() {
2016-08-11 06:04:18 +00: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
this->reset();
}
void TWIBus::reset() {
this->buffer_s = 0;
this->buffer[0] = 0x00;
}
2016-08-14 01:06:10 +00:00
void TWIBus::address(const uint8_t adr) {
this->addr = adr;
#if ENABLED(DEBUG_TWIBUS)
2016-08-14 01:06:10 +00:00
debug(PSTR("address"), adr);
#endif
}
2016-08-11 06:04:18 +00:00
void TWIBus::addbyte(const char c) {
2016-08-14 01:06:10 +00:00
if (this->buffer_s >= COUNT(this->buffer)) return;
this->buffer[this->buffer_s++] = c;
2016-08-14 01:06:10 +00: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-08-14 01:06:10 +00:00
void TWIBus::addstring(char str[]) {
#if ENABLED(DEBUG_TWIBUS)
2016-08-14 01:06:10 +00:00
debug(PSTR("addstring"), str);
#endif
2016-08-14 01:06:10 +00:00
while (char c = *str++) this->addbyte(c);
}
void TWIBus::send() {
if (!this->addr) return;
#if ENABLED(DEBUG_TWIBUS)
2016-08-14 01:06:10 +00:00
debug(PSTR("send"), this->addr);
#endif
Wire.beginTransmission(this->addr);
Wire.write(this->buffer, this->buffer_s);
Wire.endTransmission();
this->reset();
}
2016-08-14 01:06:10 +00:00
void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
SERIAL_ECHO_START;
serialprintPGM(prefix);
SERIAL_ECHOPAIR(": from:", adr);
SERIAL_ECHOPAIR(" bytes:", bytes);
SERIAL_ECHOPGM (" data:");
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
SERIAL_EOL;
}
2016-08-11 06:04:18 +00:00
void TWIBus::reqbytes(const uint8_t bytes) {
if (!this->addr) return;
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("reqbytes"), bytes);
#endif
2016-08-11 06:04:18 +00:00
// requestFrom() is a blocking function
Wire.requestFrom(this->addr, bytes);
2016-08-14 01:06:10 +00:00
// Wait until all bytes arrive, or timeout
millis_t t = millis() + this->timeout;
while (Wire.available() < bytes && PENDING(millis(), t)) { /*nada*/ }
2016-08-14 01:06:10 +00:00
// Simply echo the data to the bus
this->echodata(bytes, PSTR("i2c-reply"), this->addr);
2016-08-11 06:04:18 +00:00
}
2016-08-14 01:06:10 +00:00
#if I2C_SLAVE_ADDRESS > 0
2016-08-14 01:06:10 +00:00
void TWIBus::receive(uint8_t bytes) {
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("receive"), bytes);
#endif
this->echodata(bytes, PSTR("i2c-receive"), 0);
}
void TWIBus::reply(char str[]/*=NULL*/) {
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("reply"), str);
#endif
2016-08-14 01:06:10 +00:00
if (str) {
this->reset();
this->addstring(str);
}
2016-08-14 01:06:10 +00:00
Wire.write(this->buffer, this->buffer_s);
this->reset();
}
#endif
#if ENABLED(DEBUG_TWIBUS)
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); }
}
#endif
#endif //EXPERIMENTAL_I2CBUS