Merge upstream changes from Marlin 2.1.1
This commit is contained in:
@@ -28,13 +28,24 @@
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
#include "../libs/hex_print.h"
|
||||
|
||||
TWIBus i2c;
|
||||
|
||||
TWIBus::TWIBus() {
|
||||
#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)
|
||||
Wire.setSDA(pin_t(I2C_SDA_PIN));
|
||||
Wire.setSCL(pin_t(I2C_SCL_PIN));
|
||||
#endif
|
||||
|
||||
Wire.begin(); // No address joins the BUS as the master
|
||||
|
||||
#else
|
||||
Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave
|
||||
|
||||
Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave
|
||||
|
||||
#endif
|
||||
reset();
|
||||
}
|
||||
@@ -45,33 +56,32 @@ void TWIBus::reset() {
|
||||
}
|
||||
|
||||
void TWIBus::address(const uint8_t adr) {
|
||||
if (!WITHIN(adr, 8, 127)) {
|
||||
if (!WITHIN(adr, 8, 127))
|
||||
SERIAL_ECHO_MSG("Bad I2C address (8-127)");
|
||||
}
|
||||
|
||||
addr = adr;
|
||||
|
||||
debug(PSTR("address"), adr);
|
||||
debug(F("address"), adr);
|
||||
}
|
||||
|
||||
void TWIBus::addbyte(const char c) {
|
||||
if (buffer_s >= COUNT(buffer)) return;
|
||||
buffer[buffer_s++] = c;
|
||||
debug(PSTR("addbyte"), c);
|
||||
debug(F("addbyte"), c);
|
||||
}
|
||||
|
||||
void TWIBus::addbytes(char src[], uint8_t bytes) {
|
||||
debug(PSTR("addbytes"), bytes);
|
||||
debug(F("addbytes"), bytes);
|
||||
while (bytes--) addbyte(*src++);
|
||||
}
|
||||
|
||||
void TWIBus::addstring(char str[]) {
|
||||
debug(PSTR("addstring"), str);
|
||||
debug(F("addstring"), str);
|
||||
while (char c = *str++) addbyte(c);
|
||||
}
|
||||
|
||||
void TWIBus::send() {
|
||||
debug(PSTR("send"), addr);
|
||||
debug(F("send"), addr);
|
||||
|
||||
Wire.beginTransmission(I2C_ADDRESS(addr));
|
||||
Wire.write(buffer, buffer_s);
|
||||
@@ -81,21 +91,60 @@ void TWIBus::send() {
|
||||
}
|
||||
|
||||
// static
|
||||
void TWIBus::echoprefix(uint8_t bytes, const char pref[], uint8_t adr) {
|
||||
void TWIBus::echoprefix(uint8_t bytes, FSTR_P const pref, uint8_t adr) {
|
||||
SERIAL_ECHO_START();
|
||||
SERIAL_ECHOPGM_P(pref);
|
||||
SERIAL_ECHOF(pref);
|
||||
SERIAL_ECHOPGM(": from:", adr, " bytes:", bytes, " data:");
|
||||
}
|
||||
|
||||
// static
|
||||
void TWIBus::echodata(uint8_t bytes, const char pref[], uint8_t adr) {
|
||||
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;
|
||||
|
||||
echoprefix(bytes, pref, adr);
|
||||
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
SERIAL_EOL();
|
||||
}
|
||||
|
||||
void TWIBus::echobuffer(const char pref[], uint8_t adr) {
|
||||
echoprefix(buffer_s, pref, adr);
|
||||
void TWIBus::echobuffer(FSTR_P const prefix, uint8_t adr) {
|
||||
echoprefix(buffer_s, prefix, adr);
|
||||
LOOP_L_N(i, buffer_s) SERIAL_CHAR(buffer[i]);
|
||||
SERIAL_EOL();
|
||||
}
|
||||
@@ -103,22 +152,22 @@ void TWIBus::echobuffer(const char pref[], uint8_t adr) {
|
||||
bool TWIBus::request(const uint8_t bytes) {
|
||||
if (!addr) return false;
|
||||
|
||||
debug(PSTR("request"), bytes);
|
||||
debug(F("request"), bytes);
|
||||
|
||||
// requestFrom() is a blocking function
|
||||
if (Wire.requestFrom(I2C_ADDRESS(addr), bytes) == 0) {
|
||||
debug("request fail", I2C_ADDRESS(addr));
|
||||
debug(F("request fail"), I2C_ADDRESS(addr));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TWIBus::relay(const uint8_t bytes) {
|
||||
debug(PSTR("relay"), bytes);
|
||||
void TWIBus::relay(const uint8_t bytes, const uint8_t style/*=0*/) {
|
||||
debug(F("relay"), bytes);
|
||||
|
||||
if (request(bytes))
|
||||
echodata(bytes, PSTR("i2c-reply"), addr);
|
||||
echodata(bytes, F("i2c-reply"), addr, style);
|
||||
}
|
||||
|
||||
uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
|
||||
@@ -127,7 +176,7 @@ uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
|
||||
while (count < bytes && Wire.available())
|
||||
dst[count++] = Wire.read();
|
||||
|
||||
debug(PSTR("capture"), count);
|
||||
debug(F("capture"), count);
|
||||
|
||||
return count;
|
||||
}
|
||||
@@ -140,12 +189,12 @@ void TWIBus::flush() {
|
||||
#if I2C_SLAVE_ADDRESS > 0
|
||||
|
||||
void TWIBus::receive(uint8_t bytes) {
|
||||
debug(PSTR("receive"), bytes);
|
||||
echodata(bytes, PSTR("i2c-receive"), 0);
|
||||
debug(F("receive"), bytes);
|
||||
echodata(bytes, F("i2c-receive"), 0);
|
||||
}
|
||||
|
||||
void TWIBus::reply(char str[]/*=nullptr*/) {
|
||||
debug(PSTR("reply"), str);
|
||||
debug(F("reply"), str);
|
||||
|
||||
if (str) {
|
||||
reset();
|
||||
@@ -170,18 +219,16 @@ void TWIBus::flush() {
|
||||
#if ENABLED(DEBUG_TWIBUS)
|
||||
|
||||
// static
|
||||
void TWIBus::prefix(const char func[]) {
|
||||
SERIAL_ECHOPGM("TWIBus::");
|
||||
SERIAL_ECHOPGM_P(func);
|
||||
SERIAL_ECHOPGM(": ");
|
||||
void TWIBus::prefix(FSTR_P const func) {
|
||||
SERIAL_ECHOPGM("TWIBus::", func, ": ");
|
||||
}
|
||||
void TWIBus::debug(const char func[], uint32_t adr) {
|
||||
void TWIBus::debug(FSTR_P const func, uint32_t adr) {
|
||||
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
|
||||
}
|
||||
void TWIBus::debug(const char func[], char c) {
|
||||
void TWIBus::debug(FSTR_P const func, char c) {
|
||||
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
|
||||
}
|
||||
void TWIBus::debug(const char func[], char str[]) {
|
||||
void TWIBus::debug(FSTR_P const func, char str[]) {
|
||||
if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user