From 1e4118948098b1f0897fb28e26eb0fc5dbc5a90b Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 26 Jun 2024 11:29:23 +0200 Subject: [PATCH 1/6] Updated printable to reduce mutex operations when printing newlines --- include/sta/debug/debug.hpp | 15 ++-- include/sta/debug/printing/printable.hpp | 64 ++++++++++------ .../sta/debug/printing/printable_printf.hpp | 11 ++- include/sta/debug/printing/printable_uart.hpp | 11 ++- src/debug/printing/printable.cpp | 73 +++++++++---------- src/debug/printing/printable_printf.cpp | 12 ++- src/debug/printing/printable_uart.cpp | 12 ++- 7 files changed, 126 insertions(+), 72 deletions(-) diff --git a/include/sta/debug/debug.hpp b/include/sta/debug/debug.hpp index 7a09609..9cc3654 100644 --- a/include/sta/debug/debug.hpp +++ b/include/sta/debug/debug.hpp @@ -19,7 +19,7 @@ namespace sta * * @ingroup sta_core_debug */ -# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) +# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) /** * @brief Debug print message followed by new-line to UART. @@ -28,7 +28,7 @@ namespace sta * * @ingroup sta_core_debug */ -# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__) +# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__) /** * @brief Formatted debug printing with new-line. @@ -36,13 +36,16 @@ namespace sta * @param fmt See @ref sta::PrintableUART::printf * @param ... See @ref sta::PrintableUART::printf */ -# define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__) +# define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__) + +# define STA_DEBUG_READ(buffer, length) sta::Debug->read(buffer, length) #else // !STA_DEBUGGING_ENABLED -# define STA_DEBUG_PRINT(...) ((void)0) -# define STA_DEBUG_PRINTLN(...) ((void)0) -# define STA_DEBUG_PRINTF(fmt, ...) ((void)0) +# define STA_DEBUG_PRINT(...) ((void)0) +# define STA_DEBUG_PRINTLN(...) ((void)0) +# define STA_DEBUG_PRINTF(fmt, ...) ((void)0) +# define STA_DEBUG_READ(buffer, length) ((void)0) #endif // STA_DEBUGGING_ENABLED diff --git a/include/sta/debug/printing/printable.hpp b/include/sta/debug/printing/printable.hpp index 531522f..30995db 100644 --- a/include/sta/debug/printing/printable.hpp +++ b/include/sta/debug/printing/printable.hpp @@ -40,62 +40,70 @@ namespace sta /** * @brief Print single character. * - * @param c Character to print + * @param c Character to print + * @param newline If true, send a \r\n to start a new line. */ - void print(char c); + void print(char c, bool newline = false); /** * @brief Print boolean value. * - * @param b Boolean value + * @param b Boolean value + * @param newline If true, send a \r\n to start a new line. */ - void print(bool b); + void print(bool b, bool newline = false); /** * @brief Print floating point value. * - * @param d Floating point value + * @param d Floating point value + * @param newline If true, send a \r\n to start a new line. */ - void print(double d); + void print(double d, bool newline = false); /** * @brief Print integer in selected base. * - * @param num 8-bit unsigned integer - * @param base Integer base + * @param num 8-bit unsigned integer + * @param base Integer base + * @param newline If true, send a \r\n to start a new line. */ - void print(uint8_t num, IntegerBase base = IntegerBase::DEC); + void print(uint8_t num, IntegerBase base = IntegerBase::DEC, bool newline = false); /** * @brief Print integer in selected base. * - * @param num 16-bit unsigned integer - * @param base Integer base + * @param num 16-bit unsigned integer + * @param base Integer base + * @param newline If true, send a \r\n to start a new line. */ - void print(uint16_t num, IntegerBase base = IntegerBase::DEC); + void print(uint16_t num, IntegerBase base = IntegerBase::DEC, bool newline = false); /** * @brief Print integer in selected base. * - * @param num 32-bit unsigned integer - * @param base Integer base + * @param num 32-bit unsigned integer + * @param base Integer base + * @param newline If true, send a \r\n to start a new line. */ - void print(uint32_t num, IntegerBase base = IntegerBase::DEC); + void print(uint32_t num, IntegerBase base = IntegerBase::DEC, bool newline = false); /** * @brief Print c-string. * - * @param str Null terminated string + * @param str Null terminated string + * @param newline If true, send a \r\n to start a new line. */ - void print(const char * str); + void print(const char * str, bool newline = false); /** * @brief Print string. * * @param str String buffer * @param length String length + * @param newline If true, send a \r\n to start a new line. */ - virtual void print(const char * str, size_t length) = 0; + virtual void print(const char * str, size_t length, bool newline = false) = 0; /** * @brief Print new-line. @@ -161,6 +169,14 @@ namespace sta * @param length String length */ void println(const char * str, size_t length); + public: + /** + * @brief Read string. + * + * @param str String buffer + * @param length String length + */ + virtual void read(char* str, size_t length) = 0; private: /** @@ -170,32 +186,36 @@ namespace sta * @param base Integer base * @param fmt printf format string for base 10 * @param size Size of value in bytes + * @param newline If true, send a \r\n to start a new line. */ - void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size); + void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size, bool newline = false); /** * @brief Print unsigned integer in base 10. * * @param value Unsigned integer value * @param fmt printf format string + * @param newline If true, send a \r\n to start a new line. */ - void printDec(uintmax_t value, const char * fmt); + void printDec(uintmax_t value, const char * fmt, bool newline = false); /** * @brief Print unsigned integer in base 2. * * @param value Unsigned integer value * @param digits Number of digits to print + * @param newline If true, send a \r\n to start a new line. */ - void printBin(uintmax_t value, size_t digits); + void printBin(uintmax_t value, size_t digits, bool newline = false); /** * @brief Print unsigned integer in base 16. * * @param value Unsigned integer value * @param digits Number of digits to print + * @param newline If true, send a \r\n to start a new line. */ - void printHex(uintmax_t value, size_t digits); + void printHex(uintmax_t value, size_t digits, bool newline = false); }; } // namespace sta diff --git a/include/sta/debug/printing/printable_printf.hpp b/include/sta/debug/printing/printable_printf.hpp index a0de3cd..a570bd0 100644 --- a/include/sta/debug/printing/printable_printf.hpp +++ b/include/sta/debug/printing/printable_printf.hpp @@ -23,8 +23,17 @@ namespace sta * * @param str String buffer * @param length String length + * @param newline If true, send a \r\n to start a new line. */ - void print(const char * str, size_t length) override; + void print(const char * str, size_t length, bool newline = false) override; + + /** + * @brief Print string. + * + * @param str String buffer + * @param length String length + */ + void read(char * str, size_t length) override; }; } // namespace sta diff --git a/include/sta/debug/printing/printable_uart.hpp b/include/sta/debug/printing/printable_uart.hpp index 93f92c5..0559607 100644 --- a/include/sta/debug/printing/printable_uart.hpp +++ b/include/sta/debug/printing/printable_uart.hpp @@ -32,8 +32,17 @@ namespace sta * * @param str String buffer * @param length String length + * @param newline If true, send a \r\n to start a new line. */ - void print(const char * str, size_t length) override; + void print(const char * str, size_t length, bool newline = false) override; + + /** + * @brief Read string. + * + * @param str String buffer + * @param length String length + */ + void read(char * str, size_t length) override; private: UART * intf_; diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index a3206bb..73b4c5d 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -27,82 +27,76 @@ namespace sta println(str); } - void Printable::print(char c) + void Printable::print(char c, bool newline /* = false */) { - print(&c, 1); + print(&c, 1, newline); } - void Printable::print(bool b) + void Printable::print(bool b, bool newline /* = false */) { - print(b ? "true" : "false"); + print(b ? "true" : "false", newline); } - void Printable::print(double d) + void Printable::print(double d, bool newline /* = false */) { char buffer[64]; snprintf(buffer, sizeof(buffer), "%f", d); - print(buffer); + print(buffer, newline); } - void Printable::print(uint8_t num, IntegerBase base) + void Printable::print(uint8_t num, IntegerBase base, bool newline /* = false */) { - printBase(num, base, "%" PRIu8, sizeof(num)); + printBase(num, base, "%" PRIu8, sizeof(num), newline); } - void Printable::print(uint16_t num, IntegerBase base) + void Printable::print(uint16_t num, IntegerBase base, bool newline /* = false */) { - printBase(num, base, "%" PRIu16, sizeof(num)); + printBase(num, base, "%" PRIu16, sizeof(num), newline); } - void Printable::print(uint32_t num, IntegerBase base) + void Printable::print(uint32_t num, IntegerBase base, bool newline /* = false */) { - printBase(num, base, "%" PRIu32, sizeof(num)); + printBase(num, base, "%" PRIu32, sizeof(num), newline); } - void Printable::print(const char * str) + void Printable::print(const char * str, bool newline /* = false */) { - print(str, strlen(str)); + print(str, strlen(str), newline); } void Printable::println() { - print("\r\n", 2); + print("\r\n", 2, false); } void Printable::println(char c) { - print(&c, 1); - println(); + print(&c, 1, true); } void Printable::println(bool b) { - print(b); - println(); + print(b, true); } void Printable::println(double d) { - print(d); - println(); + print(d, true); } void Printable::println(uint8_t num, IntegerBase base) { - print(num, base); - println(); + print(num, base, true); } void Printable::println(uint16_t num, IntegerBase base) { - print(num, base); - println(); + print(num, base, true); } void Printable::println(uint32_t num, IntegerBase base) { - print(num, base); - println(); + print(num, base, true); } void Printable::println(const char * str) @@ -112,26 +106,25 @@ namespace sta void Printable::println(const char * str, size_t length) { - print(str, length); - println(); + print(str, length, true); } - void Printable::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size) + void Printable::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size, bool newline /* = false */) { switch (base) { case IntegerBase::DEC: - printDec(num, fmt); + printDec(num, fmt, newline); break; case IntegerBase::BIN: // Digits in base 2 = size in bytes * 8 - printBin(num, size * 8); + printBin(num, size * 8, newline); break; case IntegerBase::HEX: // Digits in base 16 = size in bytes * 2 - printHex(num, size * 2); + printHex(num, size * 2, newline); break; default: @@ -139,14 +132,14 @@ namespace sta } } - void Printable::printDec(uintmax_t num, const char * fmt) + void Printable::printDec(uintmax_t num, const char * fmt, bool newline /* = false */) { char buffer[64]; snprintf(buffer, sizeof(buffer), fmt, static_cast(num)); - print(buffer); + print(buffer, newline); } - void Printable::printBin(uintmax_t value, size_t digits) + void Printable::printBin(uintmax_t value, size_t digits, bool newline /* = false */) { // Need 8 digits for every byte char buffer[sizeof(value) * 8]; @@ -168,10 +161,10 @@ namespace sta buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1); } - print(buffer, digits); + print(buffer, digits, newline); } - void Printable::printHex(uintmax_t value, size_t digits) + void Printable::printHex(uintmax_t value, size_t digits, bool newline /* = false */) { // Need 2 digits for every byte char buffer[sizeof(value) * 2]; @@ -179,7 +172,7 @@ namespace sta // Check bounds if (digits > sizeof(buffer)) { - print(""); + print("", newline); return; } // Nothing to do @@ -197,7 +190,7 @@ namespace sta buffer[i] = '0' + hex; } - print(buffer, digits); + print(buffer, digits, newline); } } // namespace sta diff --git a/src/debug/printing/printable_printf.cpp b/src/debug/printing/printable_printf.cpp index 07357f5..9c8a8d8 100644 --- a/src/debug/printing/printable_printf.cpp +++ b/src/debug/printing/printable_printf.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -10,11 +11,20 @@ namespace sta } - void PrintablePrintf::print(const char * str, size_t length) + void PrintablePrintf::print(const char * str, size_t length, bool newline /* = false */) { STA_ASSERT(str != nullptr); STA_ASSERT(length > 0); printf("%.*s", length, str); + printf("\r\n"); + } + + void PrintablePrintf::read(char * str, size_t length) + { + STA_ASSERT(str != nullptr); + STA_ASSERT(length > 0); + + STA_NOT_IMPLEMENTED(); } } // namespace sta diff --git a/src/debug/printing/printable_uart.cpp b/src/debug/printing/printable_uart.cpp index f3f7453..2877c82 100644 --- a/src/debug/printing/printable_uart.cpp +++ b/src/debug/printing/printable_uart.cpp @@ -17,11 +17,21 @@ namespace sta STA_ASSERT(intf->settings().mode == UARTMode::RX || intf->settings().mode == UARTMode::RX_TX); } - void PrintableUART::print(const char * str, size_t length) + void PrintableUART::print(const char * str, size_t length, bool newline /* = false */) { + const char * linebreak = "\r\n"; + intf_->acquire(); intf_->transfer(reinterpret_cast(str), length); + intf_->transfer(reinterpret_cast(linebreak), 2); intf_->release(); } + void PrintableUART::read(char * str, size_t length) + { + intf_->acquire(); + intf_->receive(reinterpret_cast(str), length); + intf_->release(); + } + } // namespace sta From b9ccb2d65fe203cf6097866b4a585b1e278ac3d6 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 26 Jun 2024 17:11:02 +0200 Subject: [PATCH 2/6] Added docstring to STA_DEBUG_READ --- include/sta/debug/debug.hpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/include/sta/debug/debug.hpp b/include/sta/debug/debug.hpp index 9cc3654..dec9b4c 100644 --- a/include/sta/debug/debug.hpp +++ b/include/sta/debug/debug.hpp @@ -15,16 +15,16 @@ namespace sta /** * @brief Debug print message. * - * @param ... See @ref sta::PrintableUART::print + * @param ... See @ref sta::Printable::print * * @ingroup sta_core_debug */ # define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) /** - * @brief Debug print message followed by new-line to UART. + * @brief Debug print message followed by new-line to the printable. * - * @param ... See @ref sta::PrintableUART::println + * @param ... See @ref sta::Printable::println * * @ingroup sta_core_debug */ @@ -33,11 +33,19 @@ namespace sta /** * @brief Formatted debug printing with new-line. * - * @param fmt See @ref sta::PrintableUART::printf - * @param ... See @ref sta::PrintableUART::printf + * @param fmt See @ref sta::Printable::printf + * @param ... See @ref sta::Printable::printf */ # define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__) +/** + * @brief Read string via printable. + * + * @param ... See @ref sta::Printable::read + * + * @ingroup sta_core_debug + * + */ # define STA_DEBUG_READ(buffer, length) sta::Debug->read(buffer, length) #else // !STA_DEBUGGING_ENABLED From ebb14896b8054bdeb6a4bdc5fe555ba4b92163ab Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 26 Jun 2024 17:41:56 +0200 Subject: [PATCH 3/6] Improved printable implementation; data reading with macro --- include/sta/debug/debug.hpp | 5 ++-- include/sta/debug/printing/printable.hpp | 26 ++++++++++++++++++- .../sta/debug/printing/printable_printf.hpp | 10 +++---- include/sta/debug/printing/printable_uart.hpp | 10 +++---- src/debug/printing/printable.cpp | 15 +++++++++++ src/debug/printing/printable_printf.cpp | 2 +- src/debug/printing/printable_uart.cpp | 4 +-- 7 files changed, 56 insertions(+), 16 deletions(-) diff --git a/include/sta/debug/debug.hpp b/include/sta/debug/debug.hpp index dec9b4c..25d757f 100644 --- a/include/sta/debug/debug.hpp +++ b/include/sta/debug/debug.hpp @@ -35,16 +35,17 @@ namespace sta * * @param fmt See @ref sta::Printable::printf * @param ... See @ref sta::Printable::printf + * + * @ingroup sta_core_debug */ # define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__) /** - * @brief Read string via printable. + * @brief Read data via printable. * * @param ... See @ref sta::Printable::read * * @ingroup sta_core_debug - * */ # define STA_DEBUG_READ(buffer, length) sta::Debug->read(buffer, length) diff --git a/include/sta/debug/printing/printable.hpp b/include/sta/debug/printing/printable.hpp index 30995db..d25148b 100644 --- a/include/sta/debug/printing/printable.hpp +++ b/include/sta/debug/printing/printable.hpp @@ -170,13 +170,37 @@ namespace sta */ void println(const char * str, size_t length); public: + /** + * @brief Read bytes. + * + * @param buffer Byte buffer + * @param length Buffer length + */ + virtual void read(uint8_t * buffer, size_t length) = 0; + /** * @brief Read string. * * @param str String buffer * @param length String length */ - virtual void read(char* str, size_t length) = 0; + void read(char * str, size_t length); + + /** + * @brief Read floats. + * + * @param buffer Float buffer + * @param length Buffer length + */ + void read(float * buffer, size_t length); + + /** + * @brief Read doubles. + * + * @param buffer Double buffer + * @param length Buffer length + */ + void read(double * buffer, size_t length); private: /** diff --git a/include/sta/debug/printing/printable_printf.hpp b/include/sta/debug/printing/printable_printf.hpp index a570bd0..4f9f7e5 100644 --- a/include/sta/debug/printing/printable_printf.hpp +++ b/include/sta/debug/printing/printable_printf.hpp @@ -28,12 +28,12 @@ namespace sta void print(const char * str, size_t length, bool newline = false) override; /** - * @brief Print string. - * - * @param str String buffer - * @param length String length + * @brief Read bytes. + * + * @param buffer Byte buffer + * @param length Buffer length */ - void read(char * str, size_t length) override; + void read(uint8_t * buffer, size_t length) override; }; } // namespace sta diff --git a/include/sta/debug/printing/printable_uart.hpp b/include/sta/debug/printing/printable_uart.hpp index 0559607..e97de02 100644 --- a/include/sta/debug/printing/printable_uart.hpp +++ b/include/sta/debug/printing/printable_uart.hpp @@ -37,12 +37,12 @@ namespace sta void print(const char * str, size_t length, bool newline = false) override; /** - * @brief Read string. - * - * @param str String buffer - * @param length String length + * @brief Read bytes. + * + * @param buffer Byte buffer + * @param length Buffer length */ - void read(char * str, size_t length) override; + void read(uint8_t * str, size_t length) override; private: UART * intf_; diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index 73b4c5d..42b0191 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -193,4 +193,19 @@ namespace sta print(buffer, digits, newline); } + void Printable::read(char * str, size_t length) + { + read(reinterpret_cast(str), length * sizeof(char)); + } + + void Printable::read(float * buffer, size_t length) + { + read(reinterpret_cast(buffer), length * sizeof(float)); + } + + void Printable::read(double * buffer, size_t length) + { + read(reinterpret_cast(buffer), length * sizeof(double)); + } + } // namespace sta diff --git a/src/debug/printing/printable_printf.cpp b/src/debug/printing/printable_printf.cpp index 9c8a8d8..35872eb 100644 --- a/src/debug/printing/printable_printf.cpp +++ b/src/debug/printing/printable_printf.cpp @@ -20,7 +20,7 @@ namespace sta printf("\r\n"); } - void PrintablePrintf::read(char * str, size_t length) + void PrintablePrintf::read(uint8_t * str, size_t length) { STA_ASSERT(str != nullptr); STA_ASSERT(length > 0); diff --git a/src/debug/printing/printable_uart.cpp b/src/debug/printing/printable_uart.cpp index 2877c82..b35b376 100644 --- a/src/debug/printing/printable_uart.cpp +++ b/src/debug/printing/printable_uart.cpp @@ -27,10 +27,10 @@ namespace sta intf_->release(); } - void PrintableUART::read(char * str, size_t length) + void PrintableUART::read(uint8_t * buffer, size_t length) { intf_->acquire(); - intf_->receive(reinterpret_cast(str), length); + intf_->receive(buffer, length); intf_->release(); } From 9eddbaa30f615d8c7b499602c4d2233997c912bc Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 26 Jun 2024 23:35:06 +0200 Subject: [PATCH 4/6] Updated feedback --- src/debug/printing/printable_printf.cpp | 4 +++- src/debug/printing/printable_uart.cpp | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/debug/printing/printable_printf.cpp b/src/debug/printing/printable_printf.cpp index 35872eb..900c5fc 100644 --- a/src/debug/printing/printable_printf.cpp +++ b/src/debug/printing/printable_printf.cpp @@ -17,7 +17,9 @@ namespace sta STA_ASSERT(length > 0); printf("%.*s", length, str); - printf("\r\n"); + + if (newline) + printf("\r\n"); } void PrintablePrintf::read(uint8_t * str, size_t length) diff --git a/src/debug/printing/printable_uart.cpp b/src/debug/printing/printable_uart.cpp index b35b376..be750d7 100644 --- a/src/debug/printing/printable_uart.cpp +++ b/src/debug/printing/printable_uart.cpp @@ -19,11 +19,15 @@ namespace sta void PrintableUART::print(const char * str, size_t length, bool newline /* = false */) { - const char * linebreak = "\r\n"; - intf_->acquire(); intf_->transfer(reinterpret_cast(str), length); - intf_->transfer(reinterpret_cast(linebreak), 2); + + if (newline) + { + const char * linebreak = "\r\n"; + intf_->transfer(reinterpret_cast(linebreak), 2); + } + intf_->release(); } From fe3e5d38e40b4a93a099f040f0cbf970b5cfc947 Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 27 Jun 2024 16:29:30 +0200 Subject: [PATCH 5/6] Updated gitignore and added spatz utilities --- .gitignore | 3 ++ include/sta/debug/printing/printable.hpp | 8 ++++ include/sta/debug/profile.hpp | 6 +-- include/sta/debug/spatz.hpp | 56 ++++++++++++++++++++++++ src/debug/spatz.cpp | 42 ++++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 include/sta/debug/spatz.hpp create mode 100644 src/debug/spatz.cpp diff --git a/.gitignore b/.gitignore index 4b5a695..3adcee3 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,9 @@ Debug/ Release/ +!include/sta/debug/ +!src/debug/ + # Doxygen docs/html docs/latex diff --git a/include/sta/debug/printing/printable.hpp b/include/sta/debug/printing/printable.hpp index d25148b..c913bff 100644 --- a/include/sta/debug/printing/printable.hpp +++ b/include/sta/debug/printing/printable.hpp @@ -201,6 +201,14 @@ namespace sta * @param length Buffer length */ void read(double * buffer, size_t length); + public: + virtual void request(uint8_t * txBuffer, size_t txLength, uint8_t * rxBuffer, size_t rxLength) = 0; + + void request(uint8_t * txBuffer, size_t txLength, float * rxBuffer, size_t rxLength); + + void request(uint8_t * txBuffer, size_t txLength, double * rxBuffer, size_t rxLenght); + + private: /** diff --git a/include/sta/debug/profile.hpp b/include/sta/debug/profile.hpp index f256ac5..50100c5 100644 --- a/include/sta/debug/profile.hpp +++ b/include/sta/debug/profile.hpp @@ -5,8 +5,8 @@ * Author: Dario */ -#ifndef STA_DEBUGGING_PROFILING_HPP -#define STA_DEBUGGING_PROFILING_HPP +#ifndef STA_CORE_PROFILE_HPP +#define STA_CORE_PROFILE_HPP #include @@ -44,4 +44,4 @@ namespace sta #endif // // STA_PROFILING_ENABLED -#endif // STA_DEBUGGING_PROFILING_HPP +#endif // STA_CORE_PROFILE_HPP diff --git a/include/sta/debug/spatz.hpp b/include/sta/debug/spatz.hpp new file mode 100644 index 0000000..5f6f54f --- /dev/null +++ b/include/sta/debug/spatz.hpp @@ -0,0 +1,56 @@ +#ifndef STA_CORE_SPATZ_HPP +#define STA_CORE_SPATZ_HPP + +#include +#ifdef STA_SPATZ_ENABLED + +#include +#ifndef STA_DEBUGGING_ENABLED +# error "Cannot use SPATZ without debugging being enabled." +#endif // STA_DEBUGGING_ENABLED + +#include + +namespace sta +{ + namespace spatz + { + /** + * @brief Initialize SPATZ for in-the-loop testing. + * + * @param mutex A mutex used to make SPATZ thread-safe. + */ + void init(Mutex * mutex); + + /** + * @brief Request bytes for a specific sensor id via the printable. + * + * @param id The id of the sensor to request data for. + * @param buffer The byte buffer to write the data to. + * @param length The number of the bytes to request. + */ + void request(uint8_t id, uint8_t * buffer, size_t length); + + /** + * @brief Request floats for a specific sensor id via the printable. + * + * @param id The id of the sensor to request data for. + * @param buffer The float buffer to write the data to. + * @param length The number of floats to request. + */ + void request(uint8_t id, float * buffer, size_t length); + + /** + * @brief Request doubles for a specific sensor id via the printable. + * + * @param id The id of the sensor to request data for. + * @param buffer The double buffer to write the data to. + * @param length The number of doubles to request. + */ + void request(uint8_t id, double * buffer, size_t length); + } // namespace spatz +} // namespace sta + +#endif // STA_SPATZ_ENABLED + +#endif // STA_CORE_SPATZ_HPP diff --git a/src/debug/spatz.cpp b/src/debug/spatz.cpp new file mode 100644 index 0000000..0e3082a --- /dev/null +++ b/src/debug/spatz.cpp @@ -0,0 +1,42 @@ +#include + +#ifdef STA_SPATZ_ENABLED + +namespace sta +{ + namespace spatz + { + sta::Mutex * mutex_; + + void init(sta::Mutex * mutex) + { + mutex_ = mutex; + } + + void request(uint8_t id, uint8_t * buffer, size_t length) + { + sta::lock_guard lock(*mutex_); + + Debug->println(id); + Debug->read(buffer, length); + } + + void request(uint8_t id, float * buffer, size_t length) + { + sta::lock_guard lock(*mutex_); + + Debug->println(id); + Debug->read(buffer, length); + } + + void request(uint8_t id, double * buffer, size_t length) + { + sta::lock_guard lock(*mutex_); + + Debug->println(id); + Debug->read(buffer, length); + } + } // namespace spatz +} // namespace sta + +#endif // STA_SPATZ_ENABLED \ No newline at end of file From 27407f36bb83d3ccc7aec9c2934b044735c651b1 Mon Sep 17 00:00:00 2001 From: dario Date: Sat, 29 Jun 2024 17:30:11 +0200 Subject: [PATCH 6/6] Updated spatz implementation --- include/sta/debug/printing/printable.hpp | 9 ----- include/sta/debug/spatz.hpp | 14 ++++++-- src/debug/spatz.cpp | 43 ++++++++++++++++++++---- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/include/sta/debug/printing/printable.hpp b/include/sta/debug/printing/printable.hpp index c913bff..d453702 100644 --- a/include/sta/debug/printing/printable.hpp +++ b/include/sta/debug/printing/printable.hpp @@ -201,15 +201,6 @@ namespace sta * @param length Buffer length */ void read(double * buffer, size_t length); - public: - virtual void request(uint8_t * txBuffer, size_t txLength, uint8_t * rxBuffer, size_t rxLength) = 0; - - void request(uint8_t * txBuffer, size_t txLength, float * rxBuffer, size_t rxLength); - - void request(uint8_t * txBuffer, size_t txLength, double * rxBuffer, size_t rxLenght); - - - private: /** * @brief Print unsigned integer in selected base. diff --git a/include/sta/debug/spatz.hpp b/include/sta/debug/spatz.hpp index 5f6f54f..7b69f4c 100644 --- a/include/sta/debug/spatz.hpp +++ b/include/sta/debug/spatz.hpp @@ -1,6 +1,8 @@ #ifndef STA_CORE_SPATZ_HPP #define STA_CORE_SPATZ_HPP +#include + #include #ifdef STA_SPATZ_ENABLED @@ -9,8 +11,6 @@ # error "Cannot use SPATZ without debugging being enabled." #endif // STA_DEBUGGING_ENABLED -#include - namespace sta { namespace spatz @@ -51,6 +51,16 @@ namespace sta } // namespace spatz } // namespace sta +#else + +namespace sta +{ + namespace spatz + { + void init(Mutex * mutex); + } // namespace spatz +} // namespace sta + #endif // STA_SPATZ_ENABLED #endif // STA_CORE_SPATZ_HPP diff --git a/src/debug/spatz.cpp b/src/debug/spatz.cpp index 0e3082a..2f22c0b 100644 --- a/src/debug/spatz.cpp +++ b/src/debug/spatz.cpp @@ -2,6 +2,10 @@ #ifdef STA_SPATZ_ENABLED +#ifndef STA_INIT_SPATZ_ID +# error "The ID used to communicate SPATZ initialization was not defined." +#endif // STA_INIT_SPATZ_ID + namespace sta { namespace spatz @@ -11,32 +15,57 @@ namespace sta void init(sta::Mutex * mutex) { mutex_ = mutex; + uint8_t msg = 0xFF; + + sta::lock_guard lock(*mutex_); + + // Wait until SPATZ sends the init byte. + while (msg != STA_INIT_SPATZ_ID) + { + Debug->read(&msg, 1); + } } void request(uint8_t id, uint8_t * buffer, size_t length) { - sta::lock_guard lock(*mutex_); + sta::lock_guard lock(*mutex_); - Debug->println(id); + Debug->println("%d", id); Debug->read(buffer, length); } void request(uint8_t id, float * buffer, size_t length) { - sta::lock_guard lock(*mutex_); + sta::lock_guard lock(*mutex_); - Debug->println(id); + // Debug->println(id, IntegerBase::DEC); + + Debug->printf("%d", id); Debug->read(buffer, length); } void request(uint8_t id, double * buffer, size_t length) { - sta::lock_guard lock(*mutex_); + sta::lock_guard lock(*mutex_); - Debug->println(id); + Debug->println("%d", id); Debug->read(buffer, length); } } // namespace spatz } // namespace sta -#endif // STA_SPATZ_ENABLED \ No newline at end of file +#else + +namespace sta +{ + namespace spatz + { + void init(sta::Mutex * mutex) + { + // Nothing to do here. + } + } // namespace spatz +} // namespace sta + + +#endif // STA_SPATZ_ENABLED