Skip to content

Commit

Permalink
Optimize serial output code for size (MarlinFirmware#20911)
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Ryl669 authored and kpishere committed Feb 19, 2021
1 parent dead6b1 commit 73ffba0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions Marlin/src/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

#define _FORCE_INLINE_ __attribute__((__always_inline__)) __inline__
#define FORCE_INLINE __attribute__((always_inline)) inline
#define NO_INLINE __attribute__((noinline))
#define _UNUSED __attribute__((unused))
#define _O0 __attribute__((optimize("O0")))
#define _Os __attribute__((optimize("Os")))
Expand Down
26 changes: 13 additions & 13 deletions Marlin/src/core/serial_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ struct SerialBase {
FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
FORCE_INLINE void print(const char* str) { write(str); }
FORCE_INLINE void print(char c, int base = 0) { print((long)c, base); }
FORCE_INLINE void print(unsigned char c, int base = 0) { print((unsigned long)c, base); }
FORCE_INLINE void print(int c, int base = DEC) { print((long)c, base); }
FORCE_INLINE void print(unsigned int c, int base = DEC) { print((unsigned long)c, base); }
NO_INLINE void print(char c, int base = 0) { print((long)c, base); }
NO_INLINE void print(unsigned char c, int base = 0) { print((unsigned long)c, base); }
NO_INLINE void print(int c, int base = DEC) { print((long)c, base); }
NO_INLINE void print(unsigned int c, int base = DEC) { print((unsigned long)c, base); }
void print(long c, int base = DEC) { if (!base) write(c); write((const uint8_t*)"-", c < 0); printNumber(c < 0 ? -c : c, base); }
void print(unsigned long c, int base = DEC) { printNumber(c, base); }
void print(double c, int digits = 2) { printFloat(c, digits); }

FORCE_INLINE void println(const char s[]) { print(s); println(); }
FORCE_INLINE void println(char c, int base = 0) { print(c, base); println(); }
FORCE_INLINE void println(unsigned char c, int base = 0) { print(c, base); println(); }
FORCE_INLINE void println(int c, int base = DEC) { print(c, base); println(); }
FORCE_INLINE void println(unsigned int c, int base = DEC) { print(c, base); println(); }
FORCE_INLINE void println(long c, int base = DEC) { print(c, base); println(); }
FORCE_INLINE void println(unsigned long c, int base = DEC) { print(c, base); println(); }
FORCE_INLINE void println(double c, int digits = 2) { print(c, digits); println(); }
void println() { write("\r\n"); }
NO_INLINE void println(const char s[]) { print(s); println(); }
NO_INLINE void println(char c, int base = 0) { print(c, base); println(); }
NO_INLINE void println(unsigned char c, int base = 0) { print(c, base); println(); }
NO_INLINE void println(int c, int base = DEC) { print(c, base); println(); }
NO_INLINE void println(unsigned int c, int base = DEC) { print(c, base); println(); }
NO_INLINE void println(long c, int base = DEC) { print(c, base); println(); }
NO_INLINE void println(unsigned long c, int base = DEC) { print(c, base); println(); }
NO_INLINE void println(double c, int digits = 2) { print(c, digits); println(); }
NO_INLINE void println() { write('\r'); write('\n'); }

// Print a number with the given base
void printNumber(unsigned long n, const uint8_t base) {
Expand Down
24 changes: 14 additions & 10 deletions Marlin/src/core/serial_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct ConditionalSerial : public SerialBase< ConditionalSerial<SerialT> > {

bool & condition;
SerialT & out;
size_t write(uint8_t c) { if (condition) return out.write(c); return 0; }
NO_INLINE size_t write(uint8_t c) { if (condition) return out.write(c); return 0; }
void flush() { if (condition) out.flush(); }
void begin(long br) { out.begin(br); }
void end() { out.end(); }
Expand All @@ -83,7 +83,7 @@ struct ForwardSerial : public SerialBase< ForwardSerial<SerialT> > {
typedef SerialBase< ForwardSerial<SerialT> > BaseClassT;

SerialT & out;
size_t write(uint8_t c) { return out.write(c); }
NO_INLINE size_t write(uint8_t c) { return out.write(c); }
void flush() { out.flush(); }
void begin(long br) { out.begin(br); }
void end() { out.end(); }
Expand Down Expand Up @@ -111,12 +111,12 @@ struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public Seria
EndOfMessageHook eofHook;
void * userPointer;

size_t write(uint8_t c) {
NO_INLINE size_t write(uint8_t c) {
if (writeHook) writeHook(userPointer, c);
return SerialT::write(c);
}

void msgDone() {
NO_INLINE void msgDone() {
if (eofHook) eofHook(userPointer);
}

Expand All @@ -130,7 +130,11 @@ struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public Seria

using BaseClassT::print;
using BaseClassT::println;


// Underlying implementation might use Arduino's bool operator
bool connected() {
return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected) : static_cast<SerialT*>(this)->operator bool();
}

void setHook(WriteHook writeHook = 0, EndOfMessageHook eofHook = 0, void * userPointer = 0) {
// Order is important here as serial code can be called inside interrupts
Expand Down Expand Up @@ -165,13 +169,13 @@ struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset>
AllMask = FirstOutputMask | SecondOutputMask,
};

size_t write(uint8_t c) {
NO_INLINE size_t write(uint8_t c) {
size_t ret = 0;
if (portMask & FirstOutputMask) ret = serial0.write(c);
if (portMask & SecondOutputMask) ret = serial1.write(c) | ret;
return ret;
}
void msgDone() {
NO_INLINE void msgDone() {
if (portMask & FirstOutputMask) serial0.msgDone();
if (portMask & SecondOutputMask) serial1.msgDone();
}
Expand All @@ -182,7 +186,7 @@ struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset>
default: return false;
}
}
int read(uint8_t index) {
NO_INLINE int read(uint8_t index) {
switch(index) {
case 0 + offset: return serial0.read();
case 1 + offset: return serial1.read();
Expand All @@ -208,11 +212,11 @@ struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset>
using BaseClassT::read;

// Redirect flush
void flush() {
NO_INLINE void flush() {
if (portMask & FirstOutputMask) serial0.flush();
if (portMask & SecondOutputMask) serial1.flush();
}
void flushTX() {
NO_INLINE void flushTX() {
if (portMask & FirstOutputMask) CALL_IF_EXISTS(void, &serial0, flushTX);
if (portMask & SecondOutputMask) CALL_IF_EXISTS(void, &serial1, flushTX);
}
Expand Down

0 comments on commit 73ffba0

Please sign in to comment.