2017-02-25 10:15:18 +00:00
|
|
|
/**
|
2016-03-26 23:25:28 +00:00
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 21:06:53 +00:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-03-26 23:25:28 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-04 08:25:55 +00:00
|
|
|
#pragma once
|
2016-03-26 23:25:28 +00:00
|
|
|
|
2018-10-03 03:09:41 +00:00
|
|
|
#include "../core/macros.h"
|
2016-04-20 19:35:56 +00:00
|
|
|
|
2016-08-11 06:04:18 +00:00
|
|
|
#include <Wire.h>
|
|
|
|
|
2016-04-20 19:35:56 +00:00
|
|
|
// Print debug messages with M111 S2 (Uses 236 bytes of PROGMEM)
|
|
|
|
//#define DEBUG_TWIBUS
|
|
|
|
|
2016-08-12 00:57:22 +00:00
|
|
|
typedef void (*twiReceiveFunc_t)(int bytes);
|
|
|
|
typedef void (*twiRequestFunc_t)();
|
2016-08-11 06:04:18 +00:00
|
|
|
|
2016-08-15 04:45:28 +00:00
|
|
|
#define TWIBUS_BUFFER_SIZE 32
|
|
|
|
|
2016-03-26 23:25:28 +00:00
|
|
|
/**
|
|
|
|
* TWIBUS class
|
|
|
|
*
|
2017-06-27 07:36:19 +00:00
|
|
|
* This class implements a wrapper around the two wire (I2C) bus, allowing
|
|
|
|
* Marlin to send and request data from any slave device on the bus.
|
2016-03-26 23:25:28 +00:00
|
|
|
*
|
2017-06-27 07:36:19 +00:00
|
|
|
* The two main consumers of this class are M260 and M261. M260 provides a way
|
|
|
|
* to send an I2C packet to a device (no repeated starts) by caching up to 32
|
|
|
|
* bytes in a buffer and then sending the buffer.
|
|
|
|
* M261 requests data from a device. The received data is relayed to serial out
|
|
|
|
* for the host to interpret.
|
|
|
|
*
|
|
|
|
* For more information see
|
2017-07-02 03:32:04 +00:00
|
|
|
* - http://marlinfw.org/docs/gcode/M260.html
|
|
|
|
* - http://marlinfw.org/docs/gcode/M261.html
|
2016-03-26 23:25:28 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class TWIBus {
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* @brief Number of bytes on buffer
|
2016-08-14 01:06:10 +00:00
|
|
|
* @description Number of bytes in the buffer waiting to be flushed to the bus
|
2016-03-26 23:25:28 +00:00
|
|
|
*/
|
|
|
|
uint8_t buffer_s = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Internal buffer
|
2016-08-11 06:04:18 +00:00
|
|
|
* @details A fixed buffer. TWI commands can be no longer than this.
|
2016-03-26 23:25:28 +00:00
|
|
|
*/
|
2016-08-15 04:45:28 +00:00
|
|
|
char buffer[TWIBUS_BUFFER_SIZE];
|
2016-03-26 23:25:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
public:
|
2016-08-11 07:16:14 +00:00
|
|
|
/**
|
|
|
|
* @brief Target device address
|
|
|
|
* @description The target device address. Persists until changed.
|
|
|
|
*/
|
|
|
|
uint8_t addr = 0;
|
|
|
|
|
2016-03-26 23:25:28 +00:00
|
|
|
/**
|
|
|
|
* @brief Class constructor
|
2016-08-11 06:04:18 +00:00
|
|
|
* @details Initialize the TWI bus and clear the buffer
|
2016-03-26 23:25:28 +00:00
|
|
|
*/
|
|
|
|
TWIBus();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reset the buffer
|
2016-08-11 06:04:18 +00:00
|
|
|
* @details Set the buffer to a known-empty state
|
2016-03-26 23:25:28 +00:00
|
|
|
*/
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Send the buffer data to the bus
|
2016-08-14 01:06:10 +00:00
|
|
|
* @details Flush the buffer to the target address
|
2016-03-26 23:25:28 +00:00
|
|
|
*/
|
|
|
|
void send();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add one byte to the buffer
|
2016-08-11 06:04:18 +00:00
|
|
|
* @details Add a byte to the end of the buffer.
|
|
|
|
* Silently fails if the buffer is full.
|
2016-03-26 23:25:28 +00:00
|
|
|
*
|
|
|
|
* @param c a data byte
|
|
|
|
*/
|
2016-08-11 06:04:18 +00:00
|
|
|
void addbyte(const char c);
|
2016-03-26 23:25:28 +00:00
|
|
|
|
2016-08-14 01:06:10 +00:00
|
|
|
/**
|
|
|
|
* @brief Add some bytes to the buffer
|
|
|
|
* @details Add bytes to the end of the buffer.
|
|
|
|
* Concatenates at the buffer size.
|
|
|
|
*
|
|
|
|
* @param src source data address
|
|
|
|
* @param bytes the number of bytes to add
|
|
|
|
*/
|
|
|
|
void addbytes(char src[], uint8_t bytes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add a null-terminated string to the buffer
|
|
|
|
* @details Add bytes to the end of the buffer up to a nul.
|
|
|
|
* Concatenates at the buffer size.
|
|
|
|
*
|
|
|
|
* @param str source string address
|
|
|
|
*/
|
|
|
|
void addstring(char str[]);
|
|
|
|
|
2016-03-26 23:25:28 +00:00
|
|
|
/**
|
2016-08-11 06:04:18 +00:00
|
|
|
* @brief Set the target slave address
|
2016-08-14 01:06:10 +00:00
|
|
|
* @details The target slave address for sending the full packet
|
2016-03-26 23:25:28 +00:00
|
|
|
*
|
2016-08-14 01:06:10 +00:00
|
|
|
* @param adr 7-bit integer address
|
2016-03-26 23:25:28 +00:00
|
|
|
*/
|
2016-08-14 01:06:10 +00:00
|
|
|
void address(const uint8_t adr);
|
2016-08-11 06:04:18 +00:00
|
|
|
|
2016-08-15 04:45:28 +00:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
|
2016-08-11 06:04:18 +00:00
|
|
|
/**
|
2016-08-14 01:06:10 +00:00
|
|
|
* @brief Echo data on the bus to serial
|
|
|
|
* @details Echo some number of bytes from the bus
|
|
|
|
* to serial in a parser-friendly format.
|
2016-08-11 06:04:18 +00:00
|
|
|
*
|
|
|
|
* @param bytes the number of bytes to request
|
|
|
|
*/
|
2016-08-14 01:06:10 +00:00
|
|
|
static void echodata(uint8_t bytes, const char prefix[], uint8_t adr);
|
2016-03-26 23:25:28 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-15 04:45:28 +00:00
|
|
|
* @brief Echo data in the buffer to serial
|
|
|
|
* @details Echo the entire buffer to serial
|
|
|
|
* to serial in a parser-friendly format.
|
|
|
|
*
|
|
|
|
* @param bytes the number of bytes to request
|
|
|
|
*/
|
|
|
|
void echobuffer(const char prefix[], uint8_t adr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Request data from the slave device and wait.
|
2016-08-14 01:06:10 +00:00
|
|
|
* @details Request a number of bytes from a slave device.
|
2016-08-28 00:00:45 +00:00
|
|
|
* Wait for the data to arrive, and return true
|
|
|
|
* on success.
|
2016-08-15 04:45:28 +00:00
|
|
|
*
|
|
|
|
* @param bytes the number of bytes to request
|
2016-08-28 00:00:45 +00:00
|
|
|
* @return status of the request: true=success, false=fail
|
2016-08-15 04:45:28 +00:00
|
|
|
*/
|
|
|
|
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.
|
2016-03-26 23:25:28 +00:00
|
|
|
*
|
|
|
|
* @param bytes the number of bytes to request
|
|
|
|
*/
|
2016-08-15 04:45:28 +00:00
|
|
|
void relay(const uint8_t bytes);
|
2016-08-11 06:04:18 +00:00
|
|
|
|
|
|
|
#if I2C_SLAVE_ADDRESS > 0
|
|
|
|
|
|
|
|
/**
|
2016-08-12 00:57:22 +00:00
|
|
|
* @brief Register a slave receive handler
|
2016-08-14 01:06:10 +00:00
|
|
|
* @details Set a handler to receive data addressed to us
|
2016-08-11 06:04:18 +00:00
|
|
|
*
|
|
|
|
* @param handler A function to handle receiving bytes
|
|
|
|
*/
|
2016-08-12 00:57:22 +00:00
|
|
|
inline void onReceive(const twiReceiveFunc_t handler) { Wire.onReceive(handler); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Register a slave request handler
|
2016-08-14 01:06:10 +00:00
|
|
|
* @details Set a handler to send data requested from us
|
2016-08-12 00:57:22 +00:00
|
|
|
*
|
|
|
|
* @param handler A function to handle receiving bytes
|
|
|
|
*/
|
|
|
|
inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
|
2016-08-11 06:04:18 +00:00
|
|
|
|
2016-08-14 01:06:10 +00:00
|
|
|
/**
|
|
|
|
* @brief Default handler to receive
|
|
|
|
* @details Receive bytes sent to our slave address
|
|
|
|
* and simply echo them to serial.
|
|
|
|
*/
|
|
|
|
void receive(uint8_t bytes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Send a reply to the bus
|
|
|
|
* @details Send the buffer and clear it.
|
2016-08-15 04:45:28 +00:00
|
|
|
* If a string is passed, write it into the buffer first.
|
2016-08-14 01:06:10 +00:00
|
|
|
*/
|
|
|
|
void reply(char str[]=NULL);
|
2016-08-14 04:05:44 +00:00
|
|
|
inline void reply(const char str[]) { this->reply((char*)str); }
|
2016-08-14 01:06:10 +00:00
|
|
|
|
2016-08-11 06:04:18 +00:00
|
|
|
#endif
|
2016-04-20 19:35:56 +00:00
|
|
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Prints a debug message
|
|
|
|
* @details Prints a simple debug message "TWIBus::function: value"
|
|
|
|
*/
|
2016-08-14 01:06:10 +00:00
|
|
|
static void prefix(const char func[]);
|
|
|
|
static void debug(const char func[], uint32_t adr);
|
|
|
|
static void debug(const char func[], char c);
|
|
|
|
static void debug(const char func[], char adr[]);
|
|
|
|
static inline void debug(const char func[], uint8_t v) { debug(func, (uint32_t)v); }
|
2016-04-20 19:35:56 +00:00
|
|
|
|
|
|
|
#endif
|
2016-03-26 23:25:28 +00:00
|
|
|
};
|