Merge pull request #4625 from thinkyhead/rc_i2c_request_capture_flush
Add request, capture, flush to TWIBus
This commit is contained in:
commit
165d7abb1b
@ -5285,8 +5285,8 @@ inline void gcode_M121() { endstops.enable_globally(false); }
|
|||||||
|
|
||||||
uint8_t bytes = code_seen('B') ? code_value_byte() : 1;
|
uint8_t bytes = code_seen('B') ? code_value_byte() : 1;
|
||||||
|
|
||||||
if (i2c.addr > 0 && bytes > 0 && bytes <= 32) {
|
if (i2c.addr && bytes && bytes <= TWIBUS_BUFFER_SIZE) {
|
||||||
i2c.reqbytes(bytes);
|
i2c.relay(bytes);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
SERIAL_ERROR_START;
|
SERIAL_ERROR_START;
|
||||||
|
@ -86,32 +86,72 @@ void TWIBus::send() {
|
|||||||
this->reset();
|
this->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
|
// static
|
||||||
|
void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
|
||||||
SERIAL_ECHO_START;
|
SERIAL_ECHO_START;
|
||||||
serialprintPGM(prefix);
|
serialprintPGM(prefix);
|
||||||
SERIAL_ECHOPAIR(": from:", adr);
|
SERIAL_ECHOPAIR(": from:", adr);
|
||||||
SERIAL_ECHOPAIR(" bytes:", bytes);
|
SERIAL_ECHOPAIR(" bytes:", bytes);
|
||||||
SERIAL_ECHOPGM (" data:");
|
SERIAL_ECHOPGM (" data:");
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
|
||||||
|
echoprefix(bytes, prefix, adr);
|
||||||
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
||||||
SERIAL_EOL;
|
SERIAL_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TWIBus::reqbytes(const uint8_t bytes) {
|
void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
|
||||||
if (!this->addr) return;
|
echoprefix(this->buffer_s, prefix, adr);
|
||||||
|
for (uint8_t i = 0; i < this->buffer_s; i++) SERIAL_CHAR(this->buffer[i]);
|
||||||
|
SERIAL_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TWIBus::request(const uint8_t bytes) {
|
||||||
|
if (!this->addr) return false;
|
||||||
|
|
||||||
#if ENABLED(DEBUG_TWIBUS)
|
#if ENABLED(DEBUG_TWIBUS)
|
||||||
debug(PSTR("reqbytes"), bytes);
|
debug(PSTR("request"), bytes);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// requestFrom() is a blocking function
|
// requestFrom() is a blocking function
|
||||||
Wire.requestFrom(this->addr, bytes);
|
Wire.requestFrom(this->addr, bytes);
|
||||||
|
|
||||||
// Wait until all bytes arrive, or timeout
|
// Wait for all bytes to arrive
|
||||||
millis_t t = millis() + this->timeout;
|
millis_t t = millis() + this->timeout;
|
||||||
while (Wire.available() < bytes && PENDING(millis(), t)) { /*nada*/ }
|
while (Wire.available() < bytes)
|
||||||
|
if (ELAPSED(millis(), t)) {
|
||||||
|
#if ENABLED(DEBUG_TWIBUS)
|
||||||
|
SERIAL_ECHO_START;
|
||||||
|
SERIAL_ECHOLNPGM("i2c timeout");
|
||||||
|
#endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Simply echo the data to the bus
|
return true;
|
||||||
this->echodata(bytes, PSTR("i2c-reply"), this->addr);
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void TWIBus::flush() {
|
||||||
|
while (Wire.available()) Wire.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if I2C_SLAVE_ADDRESS > 0
|
#if I2C_SLAVE_ADDRESS > 0
|
||||||
@ -120,7 +160,7 @@ void TWIBus::reqbytes(const uint8_t bytes) {
|
|||||||
#if ENABLED(DEBUG_TWIBUS)
|
#if ENABLED(DEBUG_TWIBUS)
|
||||||
debug(PSTR("receive"), bytes);
|
debug(PSTR("receive"), bytes);
|
||||||
#endif
|
#endif
|
||||||
this->echodata(bytes, PSTR("i2c-receive"), 0);
|
echodata(bytes, PSTR("i2c-receive"), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TWIBus::reply(char str[]/*=NULL*/) {
|
void TWIBus::reply(char str[]/*=NULL*/) {
|
||||||
@ -142,6 +182,7 @@ void TWIBus::reqbytes(const uint8_t bytes) {
|
|||||||
|
|
||||||
#if ENABLED(DEBUG_TWIBUS)
|
#if ENABLED(DEBUG_TWIBUS)
|
||||||
|
|
||||||
|
// static
|
||||||
void TWIBus::prefix(const char func[]) {
|
void TWIBus::prefix(const char func[]) {
|
||||||
SERIAL_ECHOPGM("TWIBus::");
|
SERIAL_ECHOPGM("TWIBus::");
|
||||||
serialprintPGM(func);
|
serialprintPGM(func);
|
||||||
|
@ -33,6 +33,8 @@
|
|||||||
typedef void (*twiReceiveFunc_t)(int bytes);
|
typedef void (*twiReceiveFunc_t)(int bytes);
|
||||||
typedef void (*twiRequestFunc_t)();
|
typedef void (*twiRequestFunc_t)();
|
||||||
|
|
||||||
|
#define TWIBUS_BUFFER_SIZE 32
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TWIBUS class
|
* TWIBUS class
|
||||||
*
|
*
|
||||||
@ -70,7 +72,7 @@ class TWIBus {
|
|||||||
* @brief Internal buffer
|
* @brief Internal buffer
|
||||||
* @details A fixed buffer. TWI commands can be no longer than this.
|
* @details A fixed buffer. TWI commands can be no longer than this.
|
||||||
*/
|
*/
|
||||||
char buffer[32];
|
char buffer[TWIBUS_BUFFER_SIZE];
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -134,6 +136,14 @@ class TWIBus {
|
|||||||
*/
|
*/
|
||||||
void address(const uint8_t adr);
|
void address(const uint8_t adr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Prefix for echo to serial
|
||||||
|
* @details Echo a label, length, address, and "data:"
|
||||||
|
*
|
||||||
|
* @param bytes the number of bytes to request
|
||||||
|
*/
|
||||||
|
static void echoprefix(uint8_t bytes, const char prefix[], uint8_t adr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Echo data on the bus to serial
|
* @brief Echo data on the bus to serial
|
||||||
* @details Echo some number of bytes from the bus
|
* @details Echo some number of bytes from the bus
|
||||||
@ -144,14 +154,48 @@ class TWIBus {
|
|||||||
static void echodata(uint8_t bytes, const char prefix[], uint8_t adr);
|
static void echodata(uint8_t bytes, const char prefix[], uint8_t adr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Request data from the slave device
|
* @brief Echo data in the buffer to serial
|
||||||
* @details Request a number of bytes from a slave device.
|
* @details Echo the entire buffer to serial
|
||||||
* This implementation simply sends the data to serial
|
* to serial in a parser-friendly format.
|
||||||
* in a parser-friendly format.
|
|
||||||
*
|
*
|
||||||
* @param bytes the number of bytes to request
|
* @param bytes the number of bytes to request
|
||||||
*/
|
*/
|
||||||
void reqbytes(const uint8_t bytes);
|
void echobuffer(const char prefix[], uint8_t adr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Request data from the slave device and wait.
|
||||||
|
* @details Request a number of bytes from a slave device.
|
||||||
|
* Wait for the data to arrive until the timeout
|
||||||
|
* interval expires. Return true on success.
|
||||||
|
*
|
||||||
|
* @param bytes the number of bytes to request
|
||||||
|
* @return status of the request: true=success, false=timeout
|
||||||
|
*/
|
||||||
|
bool request(const uint8_t bytes);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Capture data from the bus into the buffer.
|
||||||
|
* @details Capture data after a request has succeeded.
|
||||||
|
*
|
||||||
|
* @param bytes the number of bytes to request
|
||||||
|
* @return the number of bytes captured to the buffer
|
||||||
|
*/
|
||||||
|
uint8_t capture(char *dst, const uint8_t bytes);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Flush the i2c bus.
|
||||||
|
* @details Get all bytes on the bus and throw them away.
|
||||||
|
*/
|
||||||
|
static void flush();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Request data from the slave device, echo to serial.
|
||||||
|
* @details Request a number of bytes from a slave device and output
|
||||||
|
* the returned data to serial in a parser-friendly format.
|
||||||
|
*
|
||||||
|
* @param bytes the number of bytes to request
|
||||||
|
*/
|
||||||
|
void relay(const uint8_t bytes);
|
||||||
|
|
||||||
#if I2C_SLAVE_ADDRESS > 0
|
#if I2C_SLAVE_ADDRESS > 0
|
||||||
|
|
||||||
@ -181,6 +225,7 @@ class TWIBus {
|
|||||||
/**
|
/**
|
||||||
* @brief Send a reply to the bus
|
* @brief Send a reply to the bus
|
||||||
* @details Send the buffer and clear it.
|
* @details Send the buffer and clear it.
|
||||||
|
* If a string is passed, write it into the buffer first.
|
||||||
*/
|
*/
|
||||||
void reply(char str[]=NULL);
|
void reply(char str[]=NULL);
|
||||||
inline void reply(const char str[]) { this->reply((char*)str); }
|
inline void reply(const char str[]) { this->reply((char*)str); }
|
||||||
|
Loading…
Reference in New Issue
Block a user