From d9f461f0f17b2b3cfe26184a92e4e6c7e1add9b7 Mon Sep 17 00:00:00 2001 From: victhor Date: Thu, 25 Dec 2025 12:41:24 +0100 Subject: [PATCH 1/2] 1. Only include string.h when SEGGER_RTT_MEMCPY is not defined 2. Fix memcpy being used in a couple places instead of SEGGER_RTT_MEMCPY 3. Remove stdlib include from SEGGER_RTT.h since it's not necessary (unused) --- RTT/SEGGER_RTT.c | 11 +++++------ RTT/SEGGER_RTT.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/RTT/SEGGER_RTT.c b/RTT/SEGGER_RTT.c index 63d4fbb..4b8d9cc 100644 --- a/RTT/SEGGER_RTT.c +++ b/RTT/SEGGER_RTT.c @@ -76,8 +76,6 @@ Additional information: #include "SEGGER_RTT.h" -#include // for memcpy - /********************************************************************* * * Configuration, default values @@ -158,6 +156,7 @@ Additional information: #ifdef MEMCPY #define SEGGER_RTT_MEMCPY(pDest, pSrc, NumBytes) MEMCPY((pDest), (pSrc), (NumBytes)) #else + #include // for memcpy #define SEGGER_RTT_MEMCPY(pDest, pSrc, NumBytes) memcpy((pDest), (pSrc), (NumBytes)) #endif #endif @@ -951,7 +950,7 @@ unsigned SEGGER_RTT_WriteSkipNoLock(unsigned BufferIndex, const void* pBuffer, u if (RdOff <= WrOff) { // Case 1), 2) or 3) Avail = pRing->SizeOfBuffer - WrOff - 1u; // Space until wrap-around (assume 1 byte not usable for case that RdOff == 0) if (Avail >= NumBytes) { // Case 1)? - memcpy((void*)pDst, pData, NumBytes); + SEGGER_RTT_MEMCPY((void*)pDst, pData, NumBytes); RTT__DMB(); // Force data write to be complete before writing the , in case CPU is allowed to change the order of memory accesses pRing->WrOff = WrOff + NumBytes; return 1; @@ -959,7 +958,7 @@ unsigned SEGGER_RTT_WriteSkipNoLock(unsigned BufferIndex, const void* pBuffer, u Avail += RdOff; // Space incl. wrap-around if (Avail >= NumBytes) { // Case 2? => If not, we have case 3) (does not fit) Rem = pRing->SizeOfBuffer - WrOff; // Space until end of buffer - memcpy((void*)pDst, pData, Rem); // Copy 1st chunk + SEGGER_RTT_MEMCPY((void*)pDst, pData, Rem); // Copy 1st chunk NumBytes -= Rem; // // Special case: First check that assumed RdOff == 0 calculated that last element before wrap-around could not be used @@ -969,7 +968,7 @@ unsigned SEGGER_RTT_WriteSkipNoLock(unsigned BufferIndex, const void* pBuffer, u // if (NumBytes) { pDst = pRing->pBuffer + SEGGER_RTT_UNCACHED_OFF; - memcpy((void*)pDst, pData + Rem, NumBytes); + SEGGER_RTT_MEMCPY((void*)pDst, pData + Rem, NumBytes); } RTT__DMB(); // Force data write to be complete before writing the , in case CPU is allowed to change the order of memory accesses pRing->WrOff = NumBytes; @@ -978,7 +977,7 @@ unsigned SEGGER_RTT_WriteSkipNoLock(unsigned BufferIndex, const void* pBuffer, u } else { // Potential case 4) Avail = RdOff - WrOff - 1u; if (Avail >= NumBytes) { // Case 4)? => If not, we have case 5) (does not fit) - memcpy((void*)pDst, pData, NumBytes); + SEGGER_RTT_MEMCPY((void*)pDst, pData, NumBytes); RTT__DMB(); // Force data write to be complete before writing the , in case CPU is allowed to change the order of memory accesses pRing->WrOff = WrOff + NumBytes; return 1; diff --git a/RTT/SEGGER_RTT.h b/RTT/SEGGER_RTT.h index e8679b4..6dde645 100644 --- a/RTT/SEGGER_RTT.h +++ b/RTT/SEGGER_RTT.h @@ -284,7 +284,6 @@ Revision: $Rev: 25842 $ #endif #ifndef SEGGER_RTT_ASM // defined when SEGGER_RTT.h is included from assembly file -#include #include #include From 1adf953aacab6a02b5cea3ef34fcbec051eb96e4 Mon Sep 17 00:00:00 2001 From: victhor Date: Thu, 25 Dec 2025 12:45:07 +0100 Subject: [PATCH 2/2] 4. Add a new macro: SEGGER_RTT_PRINTF_FORMAT to let the compiler know SEGGER_RTT_printf is a printf function (it will check for errors in printf calls) --- RTT/SEGGER_RTT.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/RTT/SEGGER_RTT.h b/RTT/SEGGER_RTT.h index 6dde645..435ce80 100644 --- a/RTT/SEGGER_RTT.h +++ b/RTT/SEGGER_RTT.h @@ -309,6 +309,17 @@ Revision: $Rev: 25842 $ #define SEGGER_RTT__CB_SIZE (16 + 4 + 4 + (SEGGER_RTT_MAX_NUM_UP_BUFFERS * 24) + (SEGGER_RTT_MAX_NUM_DOWN_BUFFERS * 24)) #define SEGGER_RTT__CB_PADDING (SEGGER_RTT__ROUND_UP_2_CACHE_LINE_SIZE(SEGGER_RTT__CB_SIZE) - SEGGER_RTT__CB_SIZE) +#if defined(__GNUC__) || defined(__clang__) +// https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html +# ifdef __MINGW_PRINTF_FORMAT +# define SEGGER_RTT_PRINTF_FORMAT(STRING_INDEX, FIRST_TO_CHECK) __attribute__ ((format (__MINGW_PRINTF_FORMAT, STRING_INDEX, FIRST_TO_CHECK))) +# else +# define SEGGER_RTT_PRINTF_FORMAT(STRING_INDEX, FIRST_TO_CHECK) __attribute__ ((format (printf, STRING_INDEX, FIRST_TO_CHECK))) +# endif // __MINGW_PRINTF_FORMAT +#else +# define SEGGER_RTT_PRINTF_FORMAT(STRING_INDEX, FIRST_TO_CHECK) +#endif + /********************************************************************* * * Types @@ -439,7 +450,7 @@ int SEGGER_RTT_TerminalOut (unsigned char TerminalId, const char* s); * ********************************************************************** */ -int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...); +int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...) SEGGER_RTT_PRINTF_FORMAT(2, 3); int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList); #ifdef __cplusplus