234 lines
5.5 KiB
C++
Raw Normal View History

2017-02-25 04:15:18 -06:00
/**
* Marlin 3D Printer Firmware
2020-02-03 08:00:57 -06:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
2019-06-27 23:57:50 -05:00
* 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
2020-07-23 05:20:14 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2017-09-06 06:28:32 -05:00
#include "../inc/MarlinConfig.h"
#if ENABLED(EXPERIMENTAL_I2CBUS)
#include "twibus.h"
2017-09-06 06:28:32 -05:00
#include <Wire.h>
#include "../libs/hex_print.h"
TWIBus i2c;
2016-04-20 12:34:55 -07:00
TWIBus::TWIBus() {
2016-08-10 23:04:18 -07:00
#if I2C_SLAVE_ADDRESS == 0
Wire.begin( // No address joins the BUS as the master
#if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
pin_t(I2C_SDA_PIN), pin_t(I2C_SCL_PIN)
#endif
);
2016-08-10 23:04:18 -07:00
#else
Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave
#endif
2019-09-25 08:29:59 -05:00
reset();
}
void TWIBus::reset() {
2019-09-25 08:29:59 -05:00
buffer_s = 0;
buffer[0] = 0x00;
}
2016-08-13 18:06:10 -07:00
void TWIBus::address(const uint8_t adr) {
2017-03-31 09:00:49 -05:00
if (!WITHIN(adr, 8, 127)) {
SERIAL_ECHO_MSG("Bad I2C address (8-127)");
}
2019-09-25 08:29:59 -05:00
addr = adr;
2021-09-27 13:46:42 -05:00
debug(F("address"), adr);
}
2016-08-10 23:04:18 -07:00
void TWIBus::addbyte(const char c) {
2019-09-25 08:29:59 -05:00
if (buffer_s >= COUNT(buffer)) return;
buffer[buffer_s++] = c;
2021-09-27 13:46:42 -05:00
debug(F("addbyte"), c);
2016-08-13 18:06:10 -07:00
}
void TWIBus::addbytes(char src[], uint8_t bytes) {
2021-09-27 13:46:42 -05:00
debug(F("addbytes"), bytes);
2019-09-25 08:29:59 -05:00
while (bytes--) addbyte(*src++);
2016-08-13 18:06:10 -07:00
}
2016-08-13 18:06:10 -07:00
void TWIBus::addstring(char str[]) {
2021-09-27 13:46:42 -05:00
debug(F("addstring"), str);
2019-09-25 08:29:59 -05:00
while (char c = *str++) addbyte(c);
}
void TWIBus::send() {
2021-09-27 13:46:42 -05:00
debug(F("send"), addr);
2019-09-25 08:29:59 -05:00
Wire.beginTransmission(I2C_ADDRESS(addr));
Wire.write(buffer, buffer_s);
Wire.endTransmission();
2019-09-25 08:29:59 -05:00
reset();
}
2016-08-14 21:45:28 -07:00
// static
2021-09-27 13:46:42 -05:00
void TWIBus::echoprefix(uint8_t bytes, FSTR_P const pref, uint8_t adr) {
2017-06-09 10:51:23 -05:00
SERIAL_ECHO_START();
2021-09-27 13:46:42 -05:00
SERIAL_ECHOF(pref);
2021-09-09 04:57:05 -05:00
SERIAL_ECHOPGM(": from:", adr, " bytes:", bytes, " data:");
2016-08-14 21:45:28 -07:00
}
// static
void TWIBus::echodata(uint8_t bytes, FSTR_P const pref, uint8_t adr, const uint8_t style/*=0*/) {
union TwoBytesToInt16 { uint8_t bytes[2]; int16_t integervalue; };
TwoBytesToInt16 ConversionUnion;
2021-11-07 01:11:51 -06:00
echoprefix(bytes, pref, adr);
while (bytes-- && Wire.available()) {
int value = Wire.read();
switch (style) {
// Style 1, HEX DUMP
case 1:
SERIAL_CHAR(hex_nybble((value & 0xF0) >> 4));
SERIAL_CHAR(hex_nybble(value & 0x0F));
if (bytes) SERIAL_CHAR(' ');
break;
// Style 2, signed two byte integer (int16)
case 2:
if (bytes == 1)
ConversionUnion.bytes[1] = (uint8_t)value;
else if (bytes == 0) {
ConversionUnion.bytes[0] = (uint8_t)value;
// Output value in base 10 (standard decimal)
SERIAL_ECHO(ConversionUnion.integervalue);
}
break;
// Style 3, unsigned byte, base 10 (uint8)
case 3:
SERIAL_ECHO(value);
if (bytes) SERIAL_CHAR(' ');
break;
// Default style (zero), raw serial output
default:
// This can cause issues with some serial consoles, Pronterface is an example where things go wrong
SERIAL_CHAR(value);
break;
}
}
2017-06-09 10:51:23 -05:00
SERIAL_EOL();
2016-08-13 18:06:10 -07:00
}
void TWIBus::echobuffer(FSTR_P const prefix, uint8_t adr) {
echoprefix(buffer_s, prefix, adr);
2020-03-13 23:18:16 -05:00
LOOP_L_N(i, buffer_s) SERIAL_CHAR(buffer[i]);
2017-06-09 10:51:23 -05:00
SERIAL_EOL();
2016-08-14 21:45:28 -07:00
}
bool TWIBus::request(const uint8_t bytes) {
2019-09-25 08:29:59 -05:00
if (!addr) return false;
2021-09-27 13:46:42 -05:00
debug(F("request"), bytes);
2016-08-10 23:04:18 -07:00
// requestFrom() is a blocking function
if (Wire.requestFrom(I2C_ADDRESS(addr), bytes) == 0) {
2021-09-27 13:46:42 -05:00
debug(F("request fail"), I2C_ADDRESS(addr));
return false;
}
2016-08-14 21:45:28 -07:00
return true;
}
void TWIBus::relay(const uint8_t bytes, const uint8_t style/*=0*/) {
2021-09-27 13:46:42 -05:00
debug(F("relay"), bytes);
2016-08-14 21:45:28 -07:00
2019-09-25 08:29:59 -05:00
if (request(bytes))
echodata(bytes, F("i2c-reply"), addr, style);
2016-08-14 21:45:28 -07:00
}
uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
2019-09-25 08:29:59 -05:00
reset();
2016-08-14 21:45:28 -07:00
uint8_t count = 0;
while (count < bytes && Wire.available())
dst[count++] = Wire.read();
2021-09-27 13:46:42 -05:00
debug(F("capture"), count);
2016-08-14 21:45:28 -07:00
return count;
}
2016-08-14 21:45:28 -07:00
// static
void TWIBus::flush() {
while (Wire.available()) Wire.read();
2016-08-10 23:04:18 -07:00
}
2016-08-13 18:06:10 -07:00
#if I2C_SLAVE_ADDRESS > 0
2016-08-13 18:06:10 -07:00
void TWIBus::receive(uint8_t bytes) {
2021-09-27 13:46:42 -05:00
debug(F("receive"), bytes);
echodata(bytes, F("i2c-receive"), 0);
2016-08-13 18:06:10 -07:00
}
void TWIBus::reply(char str[]/*=nullptr*/) {
2021-09-27 13:46:42 -05:00
debug(F("reply"), str);
2016-08-13 18:06:10 -07:00
if (str) {
2019-09-25 08:29:59 -05:00
reset();
addstring(str);
}
2016-08-13 18:06:10 -07:00
2019-09-25 08:29:59 -05:00
Wire.write(buffer, buffer_s);
2016-08-13 18:06:10 -07:00
2019-09-25 08:29:59 -05:00
reset();
2016-08-13 18:06:10 -07:00
}
void i2c_on_receive(int bytes) { // just echo all bytes received to serial
i2c.receive(bytes);
}
void i2c_on_request() { // just send dummy data for now
i2c.reply("Hello World!\n");
}
2016-08-13 18:06:10 -07:00
#endif
#if ENABLED(DEBUG_TWIBUS)
2016-08-14 21:45:28 -07:00
// static
2021-09-27 13:46:42 -05:00
void TWIBus::prefix(FSTR_P const func) {
SERIAL_ECHOPGM("TWIBus::", func, ": ");
2016-08-13 18:06:10 -07:00
}
2021-09-27 13:46:42 -05:00
void TWIBus::debug(FSTR_P const func, uint32_t adr) {
2016-08-13 18:06:10 -07:00
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
}
2021-09-27 13:46:42 -05:00
void TWIBus::debug(FSTR_P const func, char c) {
2016-08-13 18:06:10 -07:00
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
}
2021-09-27 13:46:42 -05:00
void TWIBus::debug(FSTR_P const func, char str[]) {
2016-08-13 18:06:10 -07:00
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
}
#endif
2017-05-09 12:35:43 -05:00
#endif // EXPERIMENTAL_I2CBUS