Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
42e1a45
Apply meatpack conditionally to the serial port themselves and not on…
X-Ryl669 Mar 10, 2021
428cacd
Fix bad type
X-Ryl669 Mar 10, 2021
fc49340
Make meatpack non static
X-Ryl669 Mar 10, 2021
34edff4
Merge branch 'bugfix-2.0.x' into fixMultiserial
X-Ryl669 Mar 10, 2021
cf96951
Let info method be static
X-Ryl669 Mar 10, 2021
d8ac4d2
Revert "Let info method be static"
thinkyhead Mar 10, 2021
884029d
Add missing configuration file
X-Ryl669 Mar 10, 2021
ab7e36a
cleanup
thinkyhead Mar 10, 2021
24428d9
updated test
thinkyhead Mar 10, 2021
22f6a7c
disregard unused serial
thinkyhead Mar 10, 2021
03c6048
test for BINARY_FILE_TRANSFER
rhapsodyv Mar 10, 2021
7cc95bc
fix test
thinkyhead Mar 10, 2021
8a15dd6
Merge branch 'fixMultiserial' of https://github.com/X-Ryl669/MarlinFo…
thinkyhead Mar 10, 2021
1316a5f
sp
thinkyhead Mar 10, 2021
f7223ad
ayw
thinkyhead Mar 10, 2021
ffbcfd5
Fix build issue
X-Ryl669 Mar 10, 2021
7e8d851
renumber
thinkyhead Mar 10, 2021
c090c67
you know it
thinkyhead Mar 10, 2021
a265b8d
forward type
thinkyhead Mar 10, 2021
006c8c8
Serial#Type
thinkyhead Mar 10, 2021
372ce41
more specific type
thinkyhead Mar 10, 2021
df8f20b
Add sanity check
X-Ryl669 Mar 10, 2021
c8b19fb
also
thinkyhead Mar 10, 2021
7f1dd2d
HAS_MEATPACK
thinkyhead Mar 10, 2021
49632f2
Number DefaultSerial from 1 too
thinkyhead Mar 10, 2021
41d8dd2
spel
thinkyhead Mar 10, 2021
ad944eb
Define MSerial0 directly
thinkyhead Mar 10, 2021
450a773
Merge remote-tracking branch 'upstream/bugfix-2.0.x' into pr/21306
thinkyhead Mar 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions Marlin/src/core/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ PGMSTR(SP_A_STR, " A"); PGMSTR(SP_B_STR, " B"); PGMSTR(SP_C_STR, " C");
PGMSTR(SP_X_STR, " X"); PGMSTR(SP_Y_STR, " Y"); PGMSTR(SP_Z_STR, " Z"); PGMSTR(SP_E_STR, " E");
PGMSTR(SP_X_LBL, " X:"); PGMSTR(SP_Y_LBL, " Y:"); PGMSTR(SP_Z_LBL, " Z:"); PGMSTR(SP_E_LBL, " E:");


// Hook Meatpack if it's enabled on the first leaf
#ifdef MEATPACK_ENABLED_ON_SERIAL_PORT_0
SerialLeafT0 mpSerial0(false, _SERIAL_LEAF_0);
#endif
#ifdef MEATPACK_ENABLED_ON_SERIAL_PORT_1
SerialLeafT1 mpSerial1(false, _SERIAL_LEAF_1);
#endif

// Step 2: For multiserial, handle the second serial port as well
#if HAS_MULTI_SERIAL
#ifdef SERIAL_CATCHALL
SerialOutputT multiSerial(MYSERIAL, SERIAL_CATCHALL);
#else
#if HAS_ETHERNET
// Runtime checking of the condition variable
ConditionalSerial<decltype(MYSERIAL1)> serialOut1(ethernet.have_telnet_client, MYSERIAL1, false); // Takes reference here
#else
// Don't pay for runtime checking a true variable, instead use the output directly
#define serialOut1 MYSERIAL1
#endif
SerialOutputT multiSerial(MYSERIAL0, serialOut1);
#if HAS_ETHERNET
// We need a definition here
SerialLeafT1 msSerial1(ethernet.have_telnet_client, MYSERIAL1, false);
#endif
#endif

#if ENABLED(MEATPACK)
MeatpackSerial<decltype(_SERIAL_IMPL)> mpSerial(false, _SERIAL_IMPL);
SerialOutputT multiSerial(SERIAL_LEAF_0, SERIAL_LEAF_1);
#endif

void serialprintPGM(PGM_P str) {
Expand Down
51 changes: 41 additions & 10 deletions Marlin/src/core/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,61 @@ extern uint8_t marlin_debug_flags;
//
// Serial redirection
//
// Step 1: Find what's the first serial leaf
#if BOTH(HAS_MULTI_SERIAL, SERIAL_CATCHALL)
#define _SERIAL_LEAF_0 MYSERIAL
#else
#define _SERIAL_LEAF_0 MYSERIAL0
#endif

// Hook Meatpack if it's enabled on the first leaf
#ifdef MEATPACK_ENABLED_ON_SERIAL_PORT_0
typedef MeatpackSerial<decltype(_SERIAL_LEAF_0)> SerialLeafT0;
extern SerialLeafT0 mpSerial0;
#define SERIAL_LEAF_0 mpSerial0
#else
#define SERIAL_LEAF_0 _SERIAL_LEAF_0
#endif

// Step 2: For multiserial, handle the second serial port as well
#if HAS_MULTI_SERIAL
#define _PORT_REDIRECT(n,p) REMEMBER(n,multiSerial.portMask,p)
#define _PORT_RESTORE(n,p) RESTORE(n)
#define SERIAL_ASSERT(P) if(multiSerial.portMask!=(P)){ debugger(); }
// If we have a catchall, use that directly
#ifdef SERIAL_CATCHALL
typedef MultiSerial<decltype(MYSERIAL), decltype(SERIAL_CATCHALL), 0> SerialOutputT;
#define _SERIAL_LEAF_1 SERIAL_CATCHALL
#else
typedef MultiSerial<decltype(MYSERIAL0), TERN(HAS_ETHERNET, ConditionalSerial<decltype(MYSERIAL1)>, decltype(MYSERIAL1)), 0> SerialOutputT;
#if HAS_ETHERNET
// We need to create an instance here
typedef ConditionalSerial<decltype(MYSERIAL1)> SerialLeafT1;
extern SerialLeafT1 msSerial1;
#define _SERIAL_LEAF_1 msSerial1
#else
// Don't create a useless instance here, directly use the existing instance
#define _SERIAL_LEAF_1 MYSERIAL1
#endif
#endif

// Hook Meatpack if it's enabled on the second leaf
#ifdef MEATPACK_ENABLED_ON_SERIAL_PORT_1
typedef MeatpackSerial<decltype(_SERIAL_LEAF_1)> SerialLeafT1;
extern SerialLeafT1 mpSerial1;
#define SERIAL_LEAF_1 mpSerial1
#else
#define SERIAL_LEAF_1 _SERIAL_LEAF_1
#endif

typedef MultiSerial<decltype(SERIAL_LEAF_0), decltype(SERIAL_LEAF_1), 0> SerialOutputT;
extern SerialOutputT multiSerial;
#define _SERIAL_IMPL multiSerial
#define SERIAL_IMPL multiSerial
#else
#define _PORT_REDIRECT(n,p) NOOP
#define _PORT_RESTORE(n) NOOP
#define SERIAL_ASSERT(P) NOOP
#define _SERIAL_IMPL MYSERIAL0
#define SERIAL_IMPL SERIAL_LEAF_0
#endif

#if ENABLED(MEATPACK)
extern MeatpackSerial<decltype(_SERIAL_IMPL)> mpSerial;
#define SERIAL_IMPL mpSerial
#else
#define SERIAL_IMPL _SERIAL_IMPL
#endif

#define SERIAL_OUT(WHAT, V...) (void)SERIAL_IMPL.WHAT(V)

Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/feature/binary_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
#include "../libs/heatshrink/heatshrink_decoder.h"
#endif

inline bool bs_serial_data_available(const uint8_t index) {
inline bool bs_serial_data_available(const serial_index_t index) {
return SERIAL_IMPL.available(index);
}

inline int bs_read_serial(const uint8_t index) {
inline int bs_read_serial(const serial_index_t index) {
return SERIAL_IMPL.read(index);
}

Expand Down
8 changes: 0 additions & 8 deletions Marlin/src/feature/meatpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ MeatPack meatpack;
#define DEBUG_OUT ENABLED(MP_DEBUG)
#include "../core/debug_out.h"

bool MeatPack::cmd_is_next = false; // A command is pending
uint8_t MeatPack::state = 0; // Configuration state OFF
uint8_t MeatPack::second_char = 0; // The unpacked 2nd character from an out-of-sequence packed pair
uint8_t MeatPack::cmd_count = 0, // Counts how many command bytes are received (need 2)
MeatPack::full_char_count = 0, // Counts how many full-width characters are to be received
MeatPack::char_out_count = 0; // Stores number of characters to be read out.
uint8_t MeatPack::char_out_buf[2]; // Output buffer for caching up to 2 characters

// The 15 most-common characters used in G-code, ~90-95% of all G-code uses these characters
// Stored in SRAM for performance.
uint8_t meatPackLookupTable[16] = {
Expand Down
41 changes: 20 additions & 21 deletions Marlin/src/feature/meatpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,43 +90,46 @@ class MeatPack {
static const uint8_t kSpaceCharIdx = 11;
static const char kSpaceCharReplace = 'E';

static bool cmd_is_next; // A command is pending
static uint8_t state; // Configuration state
static uint8_t second_char; // Buffers a character if dealing with out-of-sequence pairs
static uint8_t cmd_count, // Counter of command bytes received (need 2)
full_char_count, // Counter for full-width characters to be received
char_out_count; // Stores number of characters to be read out.
static uint8_t char_out_buf[2]; // Output buffer for caching up to 2 characters
bool cmd_is_next; // A command is pending
uint8_t state; // Configuration state
uint8_t second_char; // Buffers a character if dealing with out-of-sequence pairs
uint8_t cmd_count, // Counter of command bytes received (need 2)
full_char_count, // Counter for full-width characters to be received
char_out_count; // Stores number of characters to be read out.
uint8_t char_out_buf[2]; // Output buffer for caching up to 2 characters

public:
// Pass in a character rx'd by SD card or serial. Automatically parses command/ctrl sequences,
// and will control state internally.
static void handle_rx_char(const uint8_t c, const serial_index_t serial_ind);
void handle_rx_char(const uint8_t c, const serial_index_t serial_ind);

/**
* After passing in rx'd char using above method, call this to get characters out.
* Can return from 0 to 2 characters at once.
* @param out [in] Output pointer for unpacked/processed data.
* @return Number of characters returned. Range from 0 to 2.
*/
static uint8_t get_result_char(char* const __restrict out);

static void reset_state();
static void report_state();
static uint8_t unpack_chars(const uint8_t pk, uint8_t* __restrict const chars_out);
static void handle_command(const MeatPack_Command c);
static void handle_output_char(const uint8_t c);
static void handle_rx_char_inner(const uint8_t c);
uint8_t get_result_char(char* const __restrict out);

void reset_state();
void report_state();
uint8_t unpack_chars(const uint8_t pk, uint8_t* __restrict const chars_out);
void handle_command(const MeatPack_Command c);
void handle_output_char(const uint8_t c);
void handle_rx_char_inner(const uint8_t c);

MeatPack() : cmd_is_next(false), state(0), second_char(0), cmd_count(0), full_char_count(0), char_out_count(0) {}
};

extern MeatPack meatpack;


// Implement the MeatPack serial class so it's transparent to rest of the code
template <typename SerialT>
struct MeatpackSerial : public SerialBase <MeatpackSerial < SerialT >> {
typedef SerialBase< MeatpackSerial<SerialT> > BaseClassT;

SerialT & out;
MeatPack meatpack;

char serialBuffer[2];
uint8_t charCount;
Expand All @@ -143,10 +146,6 @@ struct MeatpackSerial : public SerialBase <MeatpackSerial < SerialT >> {
void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }

int available(serial_index_t index) {
// There is a potential issue here with multiserial, since it'll return its decoded buffer whatever the serial index here.
// So, instead of doing MeatpackSerial<MultiSerial<...>> we should do MultiSerial<MeatpackSerial<...>, MeatpackSerial<...>>
// TODO, let's fix this later on

if (charCount) return charCount; // The buffer still has data
if (out.available(index) <= 0) return 0; // No data to read

Expand Down
20 changes: 11 additions & 9 deletions Marlin/src/gcode/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void GCodeQueue::flush_and_request_resend(const serial_index_t serial_ind) {
SERIAL_ECHOLN(serial_state[serial_ind.index].last_N + 1);
}

inline bool serial_data_available(serial_index_t index) {
static bool serial_data_available(serial_index_t index) {
const int a = SERIAL_IMPL.available(index);
#if BOTH(RX_BUFFER_MONITOR, RX_BUFFER_SIZE)
if (a > RX_BUFFER_SIZE - 2) {
Expand All @@ -283,13 +283,15 @@ inline bool serial_data_available(serial_index_t index) {
return a > 0;
}

// Multiserial already handles dispatch to/from multiple ports
inline bool any_serial_data_available() {
LOOP_L_N(p, NUM_SERIAL)
if (serial_data_available(p))
return true;
return false;
}
#if NO_TIMEOUTS > 0
// Multiserial already handles dispatch to/from multiple ports
static bool any_serial_data_available() {
LOOP_L_N(p, NUM_SERIAL)
if (serial_data_available(p))
return true;
return false;
}
#endif

inline int read_serial(const serial_index_t index) { return SERIAL_IMPL.read(index); }

Expand Down Expand Up @@ -393,7 +395,7 @@ void GCodeQueue::get_serial_commands() {
* receive buffer (which limits the packet size to MAX_CMD_SIZE).
* The receive buffer also limits the packet size for reliable transmission.
*/
binaryStream[card.transfer_port_index].receive(serial_state[card.transfer_port_index].line_buffer);
binaryStream[card.transfer_port_index.index].receive(serial_state[card.transfer_port_index.index].line_buffer);
return;
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/sd/cardreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class CardReader {
// Fast! binary file transfer
#if ENABLED(BINARY_FILE_TRANSFER)
#if HAS_MULTI_SERIAL
static int8_t transfer_port_index;
static serial_index_t transfer_port_index;
#else
static constexpr int8_t transfer_port_index = 0;
static constexpr serial_index_t transfer_port_index = 0;
#endif
#endif

Expand Down