Skip to content

Commit

Permalink
All 4 buffer types are working wit direct RTT32 and ring&double buffe…
Browse files Browse the repository at this point in the history
…r work deferred parallel over UART
  • Loading branch information
rokath committed May 3, 2023
1 parent aaafbac commit 98be03f
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 114 deletions.
8 changes: 4 additions & 4 deletions src/box/triceDiagnostics.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ void showTriceDepth( void ){
{
uint32_t triceDepth = TriceDepth();
triceDepthMax = TriceDepthMax();
TRice( iD( 3668), "MSG: ?? STOP TriceDepth = %d, TriceDepthMax = %d + %4u of %d\n", triceDepth, TRICE_DATA_OFFSET, triceDepthMax-TRICE_DATA_OFFSET, TRICE_HALF_BUFFER_SIZE ); //lint !e585
TRice( iD( 3668), "MSG: ?? STOP TriceDepth = %d, TriceDepthMax = %d + %4u of %d\n", triceDepth, TRICE_DATA_OFFSET, triceDepthMax-TRICE_DATA_OFFSET, TRICE_DEFERRED_BUFFER_SIZE/2 ); //lint !e585
}
#endif
}

void TriceLogDepthMax( void ){
#if TRICE_BUFFER == TRICE_DOUBLE_BUFFER
size_t tdm = TriceDepthMax();
if( tdm <= TRICE_HALF_BUFFER_SIZE ){
TRice16( iD( 6149), "diag:TriceDepthMax =%4u of %d\n", tdm, TRICE_HALF_BUFFER_SIZE );
if( tdm <= TRICE_DEFERRED_BUFFER_SIZE/2 ){
TRice16( iD( 6149), "diag:TriceDepthMax =%4u of %d\n", tdm, TRICE_DEFERRED_BUFFER_SIZE/2 );
}else{
TRice16( iD( 6885), "err:TriceDepthMax =%4u of %d (overflow!)\n", tdm, TRICE_HALF_BUFFER_SIZE );
TRice16( iD( 6885), "err:TriceDepthMax =%4u of %d (overflow!)\n", tdm, TRICE_DEFERRED_BUFFER_SIZE/2 );
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/box/triceDoubleBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
static void TriceOut( uint32_t* tb, size_t tLen );

//! triceBuffer is a double buffer for better write speed.
static uint32_t triceBuffer[2][TRICE_HALF_BUFFER_SIZE>>2] = {0};
static uint32_t triceBuffer[2][TRICE_DEFERRED_BUFFER_SIZE/8] = {0};

//! triceSwap is the index of the active write buffer. !triceSwap is the active read buffer index.
static int triceSwap = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/box/triceRingBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ uint32_t* const triceRingBufferLimit = &TriceRingBuffer[TRICE_DEFERRED_BUFFER_SI
//! singleTricesRingCount holds the readable trices count inside TriceRingBuffer.
int singleTricesRingCount = 0;

#pragma push
#pragma diag_suppress=170 //warning: #170-D: pointer points outside of underlying object
//! TriceRingBufferReadPosition points to a valid trice message when singleTricesRingCount > 0.
//! This is first the TRICE_DATA_OFFSET byte space followedy the trice data.
//! Initally this value is set to TriceRingBuffer minus TRICE_DATA_OFFSET byte space
//! to ga correct value for the very first call of triceNextRingBufferRead
uint32_t* TriceRingBufferReadPosition = TriceRingBuffer - (TRICE_DATA_OFFSET>>2);
#pragma pop

//! triceNextRingBufferRead returns a single trice data buffer address. The trice are data starting at byte offset TRICE_DATA_OFFSET.
//! Implicit assumed is singleTricesRingCount > 0.
Expand Down
149 changes: 76 additions & 73 deletions src/trice.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,56 +147,6 @@ extern uint32_t TriceRingBuffer[];
extern uint32_t* TriceBufferWritePosition;
#endif

// check configuration:

#if TRICE_BUFFER == TRICE_DOUBLE_BUFFER

//! TRICE_HALF_BUFFER_SIZE is the size of each of both buffers. Must be able to hold the max TRICE burst count within TRICE_TRANSFER_INTERVAL_MS or even more,
//! if the write out speed is small. Must not exceed SEGGER BUFFER_SIZE_UP
#define TRICE_HALF_BUFFER_SIZE (TRICE_DEFERRED_BUFFER_SIZE/2)

#if TRICE_HALF_BUFFER_SIZE < TRICE_BUFFER_SIZE
#error configuration error
#endif

#endif // #if TRICE_BUFFER == TRICE_DOUBLE_BUFFER

#if defined(TRICE_RTT0) && (TRICE_BUFFER_SIZE > BUFFER_SIZE_UP)
#error wrong configuration
#endif

#if defined( TRICE_UARTA ) && ( TRICE_BUFFER != TRICE_RING_BUFFER) && ( TRICE_BUFFER != TRICE_DOUBLE_BUFFER)
#error wrong configuration
#endif

#if defined( TRICE_UARTB ) && ( TRICE_BUFFER != TRICE_RING_BUFFER) && ( TRICE_BUFFER != TRICE_DOUBLE_BUFFER)
#error wrong configuration
#endif

#if ( TRICE_BUFFER == TRICE_STACK_BUFFER) && (TRICE_DIRECT_OUTPUT == 0)
#error wrong configuration
#endif

#if ( TRICE_BUFFER == TRICE_STATIC_BUFFER) && (TRICE_DIRECT_OUTPUT == 0)
#error wrong configuration
#endif

#if defined(TRICE_RTT0) && (TRICE_SEGGER_RTT_32BIT_WRITE == 1) && (TRICE_DIRECT_OUT_FRAMING != TRICE_FRAMING_NONE)
#error wrong configuration
#endif

#if TRICE_DATA_OFFSET & 3
#error All size values must be a multiple of 4!
#endif

#if TRICE_SINGLE_MAX_SIZE & 3
#error All size values must be a multiple of 4!
#endif

#if TRICE_DEFERRED_BUFFER_SIZE & 3
#error All size values must be a multiple of 4!
#endif

// defaults:

//! TRICE_BUFFER_SIZE is
Expand Down Expand Up @@ -247,10 +197,11 @@ extern uint32_t* TriceBufferWritePosition;

#endif

#ifndef TRICE_TRANSFER_MODE
#if (TRICE_BUFFER == TRICE_DOUBLE_BUFFER) && !defined(TRICE_TRANSFER_MODE)

//! TRICE_TRANSFER_MODE is the selected trice transfer method. Options: TRICE_SAFE_SINGLE_MODE (recommended), TRICE_PACK_MULTI_MODE.
//! TRICE_PACK_MULTI_MODE is usable only in TRICE_BUFFER TRICE_DOUBLE_BUFFER. It packs several trice messages before adding a 0-delimiter byte.
//! TRICE_TRANSFER_MODE is the selected deferred trice transfer method for (TRICE_BUFFER == TRICE_DOUBLE_BUFFER). Options:
//! - TRICE_SAFE_SINGLE_MODE: Each package is followed by a 0-delimiter byte (recommended).
//! - TRICE_PACK_MULTI_MODE packs several trice messages before adding a 0-delimiter byte.
#define TRICE_TRANSFER_MODE TRICE_SAFE_SINGLE_MODE

#endif
Expand All @@ -262,36 +213,69 @@ extern uint32_t* TriceBufferWritePosition;

#endif

#ifndef TRICE_DATA_OFFSET
#if TRICE_CYCLE_COUNTER == 1

//! TRICE_DATA_OFFSET is the space in front of trice data for in-buffer (T)COBS encoding. It must be be a multiple of sizeof(uint32_t).
//! If not defined, it is determined here automatically in a safe way (worst case consideration).
#define TRICE_CYCLE TriceCycle++ //! TRICE_CYCLE is the trice cycle counter as 8 bit count 0-255.

#if defined(TRICE_HALF_BUFFER_SIZE) && (TRICE_TRANSFER_MODE == TRICE_PACK_MULTI_MODE)
#else // #if TRICE_CYCLE_COUNTER == 1

#define TRICE_DATA_OFFSET (((TRICE_HALF_BUFFER_SIZE/3)+4)&~3) // In worst case the buffer gets filled to the end only with 4-byte trices and each gets an additional sigil and a 0, so 33% are safe.
#define TRICE_CYCLE 0xC0 //! TRICE_CYCLE is no trice cycle counter, just a static value.

#elif defined(TRICE_BUFFER_SIZE) // todo: make difference clear
#endif // #else // #if TRICE_CYCLE_COUNTER == 1

#define TRICE_DATA_OFFSET ((TRICE_BUFFER_SIZE/31+5)&~3) // For single trices the worst case is +1 for each 31 plus terminating 0 at the end
// check configuration:

#else
#if (TRICE_BUFFER == TRICE_DOUBLE_BUFFER) && (TRICE_DEFERRED_BUFFER_SIZE/2 < TRICE_BUFFER_SIZE)
#error configuration error
#endif

#define TRICE_DATA_OFFSET ((TRICE_SINGLE_MAX_SIZE/31+5)&~3) // For single trices the worst case is +1 for each 31 plus terminating 0 at the end. Must be a multiple of 4.
#if (TRICE_BUFFER == TRICE_RING_BUFFER) && (TRICE_DEFERRED_BUFFER_SIZE < TRICE_BUFFER_SIZE)
#error configuration error
#endif

#if defined(TRICE_RTT0) && (TRICE_BUFFER_SIZE > BUFFER_SIZE_UP)
#error wrong configuration
#endif

#endif // #ifndef TRICE_DATA_OFFSET
#if defined( TRICE_UARTA ) && ( TRICE_BUFFER != TRICE_RING_BUFFER) && ( TRICE_BUFFER != TRICE_DOUBLE_BUFFER)
#error wrong configuration
#endif

#if TRICE_CYCLE_COUNTER == 1
#if defined( TRICE_UARTB ) && ( TRICE_BUFFER != TRICE_RING_BUFFER) && ( TRICE_BUFFER != TRICE_DOUBLE_BUFFER)
#error wrong configuration
#endif

#define TRICE_CYCLE TriceCycle++ //! TRICE_CYCLE is the trice cycle counter as 8 bit count 0-255.
#if ( TRICE_BUFFER == TRICE_STACK_BUFFER) && (TRICE_DIRECT_OUTPUT == 0)
#error wrong configuration
#endif

#else // #if TRICE_CYCLE_COUNTER == 1
#if ( TRICE_BUFFER == TRICE_STATIC_BUFFER) && (TRICE_DIRECT_OUTPUT == 0)
#error wrong configuration
#endif

#define TRICE_CYCLE 0xC0 //! TRICE_CYCLE is no trice cycle counter, just a static value.
#if defined(TRICE_RTT0) && (TRICE_SEGGER_RTT_32BIT_WRITE == 1) && (TRICE_DIRECT_OUT_FRAMING != TRICE_FRAMING_NONE)
#error wrong configuration
#endif

#endif // #else // #if TRICE_CYCLE_COUNTER == 1
#if (TRICE_BUFFER == TRICE_DOUBLE_BUFFER) && (TRICE_DIRECT_OUTPUT == 0) && !defined(TRICE_UARTA) && !defined(TRICE_UARTB) && !defined(TRICE_AUXILIARY)
#error wrong configuration
#endif

#if (TRICE_BUFFER == TRICE_RING_BUFFER) && (TRICE_DIRECT_OUTPUT == 0) && !defined(TRICE_UARTA) && !defined(TRICE_UARTB) && !defined(TRICE_AUXILIARY)
#error wrong configuration
#endif

#if TRICE_DATA_OFFSET & 3
#error All size values must be a multiple of 4!
#endif

#if TRICE_SINGLE_MAX_SIZE & 3
#error All size values must be a multiple of 4!
#endif

#if TRICE_DEFERRED_BUFFER_SIZE & 3
#error All size values must be a multiple of 4!
#endif

#include "./box/trice8.h"
#include "./box/trice16.h"
Expand Down Expand Up @@ -354,16 +338,24 @@ extern uint32_t* TriceBufferWritePosition;

#endif // #if TRICE_BUFFER == TRICE_STATIC_BUFFER

#if TRICE_BUFFER == TRICE_DOUBLE_BUFFER
#if (TRICE_BUFFER == TRICE_DOUBLE_BUFFER) && (TRICE_DIRECT_OUTPUT == 1)

//! TRICE_ENTER is the start of TRICE macro.
#define TRICE_ENTER \
TRICE_ENTER_CRITICAL_SECTION { \
uint32_t* const triceSingleBufferStartWritePosition = TriceBufferWritePosition; \
uint32_t* const triceSingleBufferStartWritePosition = TriceBufferWritePosition;

#endif // #if TRICE_BUFFER == TRICE_DOUBLE_BUFFER
#endif // #if (TRICE_BUFFER == TRICE_DOUBLE_BUFFER) && (TRICE_DIRECT_OUTPUT == 1)

#if TRICE_BUFFER == TRICE_RING_BUFFER
#if (TRICE_BUFFER == TRICE_DOUBLE_BUFFER) && (TRICE_DIRECT_OUTPUT == 0)

//! TRICE_ENTER is the start of TRICE macro.
#define TRICE_ENTER \
TRICE_ENTER_CRITICAL_SECTION {

#endif // #if (TRICE_BUFFER == TRICE_DOUBLE_BUFFER) && (TRICE_DIRECT_OUTPUT == 0)

#if (TRICE_BUFFER == TRICE_RING_BUFFER) && (TRICE_DIRECT_OUTPUT == 1)

//! TRICE_ENTER is the start of TRICE macro.
#define TRICE_ENTER \
Expand All @@ -373,7 +365,18 @@ extern uint32_t* TriceBufferWritePosition;
uint32_t* const triceSingleBufferStartWritePosition = TriceBufferWritePosition; \
singleTricesRingCount++; // Because TRICE macros are an atomic instruction normally, this can be done here.

#endif // #if TRICE_BUFFER == TRICE_RING_BUFFER
#endif // #if TRICE_BUFFER == TRICE_RING_BUFFER && (TRICE_DIRECT_OUTPUT == 1)

#if (TRICE_BUFFER == TRICE_RING_BUFFER) && (TRICE_DIRECT_OUTPUT == 0)

//! TRICE_ENTER is the start of TRICE macro.
#define TRICE_ENTER \
TRICE_ENTER_CRITICAL_SECTION { \
TriceBufferWritePosition = (TriceBufferWritePosition + (TRICE_BUFFER_SIZE>>2)) <= triceRingBufferLimit ? TriceBufferWritePosition : TriceRingBuffer; \
TriceBufferWritePosition += (TRICE_DATA_OFFSET>>2); /* space for in buffer encoding */ \
singleTricesRingCount++; // Because TRICE macros are an atomic instruction normally, this can be done here.

#endif // #if TRICE_BUFFER == TRICE_RING_BUFFER && (TRICE_DIRECT_OUTPUT == 0)

#if TRICE_DIRECT_OUTPUT

Expand Down
6 changes: 3 additions & 3 deletions test/MDK-ARM_STM32F030R8/Core/Inc/triceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ extern "C" {
//! This is the fastest execution option for TRICE macros but needs more RAM. Used for deferred output and optional additional direct output.
//! - TRICE_RING_BUFFER: TRICE macros write direct into a ring buffer without any additional management action.
//! This is not the fastest execution option for TRICE macros but needs less RAM. Used for deferred output and optional additional direct output.
//! If unsure select TRICE_RING_BUFFER.
#define TRICE_BUFFER TRICE_DOUBLE_BUFFER
//! If unsure select TRICE_DOUBLE_BUFFER. The TRICE_RING_BUFFER option works, but is experimental.
#define TRICE_BUFFER TRICE_RING_BUFFER

//! TRICE_DIRECT_OUTPUT == 0: only deferred output, usually UART output only
//! TRICE_DIRECT_OUTPUT == 1: with direct output, usually RTT
Expand Down Expand Up @@ -99,7 +99,7 @@ extern "C" {
#define TRICE_UARTB_MAX_ID ((1<<14)-1) //!< TRICE_UARTB_MAX_ID is the biggest ID transferred to UARTB. Define with TRICE_UARTB_MIN_ID if you want select trice output here.

//! Enable option for an additional interface, you can define by your own.
#define TRICE_AUXILIARY // comment out, if you do not use TRICE_AUXILIARY
//#define TRICE_AUXILIARY // comment out, if you do not use TRICE_AUXILIARY
#define TRICE_AUXILIARY_MIN_ID 1 //!< TRICE_AUXILIARY_MIN_ID is the smallest ID transferred to AUXILIARY. Define with TRICE_AUXILIARY_MAX_ID if you want select trice output here.
#define TRICE_AUXILIARY_MAX_ID ((1<<14)-1) //!< TRICE_AUXILIARY_MAX_ID is the biggest ID transferred to AUXILIARY. Define with TRICE_AUXILIARY_MIN_ID if you want select trice output here.

Expand Down
20 changes: 8 additions & 12 deletions test/MDK-ARM_STM32F030R8/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ uint32_t milliSecond( void );

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int beginTriceCheck = 20;
int limitTriceCheck = 400;

#if TRICE_CHECK_CODE
int beginTriceCheck = 20;
int limitTriceCheck = 400;
#endif
/* USER CODE END 0 */

/**
Expand Down Expand Up @@ -142,19 +143,14 @@ int main(void)

uint32_t ms = milliSecond();

#if ( TRICE_BUFFER == TRICE_RING_BUFFER) || ( TRICE_BUFFER == TRICE_DOUBLE_BUFFER)
// Serve trice transfer every few ms.
// With an RTOS put this in a separate task.
//static unsigned lastMs = 0;
//if( ms >= lastMs + 10 ){
// lastMs = ms;
TriceTransfer(); // serve deferred output
//}
#if (TRICE_BUFFER == TRICE_RING_BUFFER) || (TRICE_BUFFER == TRICE_DOUBLE_BUFFER)
// Serve deferred trice transfer every few ms. With an RTOS put this in a separate task.
TriceTransfer(); // serve deferred output
#endif // #if ( TRICE_BUFFER == TRICE_RING_BUFFER) || ( TRICE_BUFFER == TRICE_DOUBLE_BUFFER)

// generate some trices every few ms
static unsigned lastTricesTime = 0;
const unsigned msInterval = 5; // change this value to change trice generation speed
const unsigned msInterval = 2; // change this value to change trice generation speed
if( ms >= lastTricesTime + msInterval ){
lastTricesTime = ms;
#if TRICE_CHECK_CODE // with or without triceCheck.c
Expand Down
22 changes: 11 additions & 11 deletions test/MDK-ARM_STM32F030R8/Core/Src/stm32f0xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ static uint64_t us_1 = 0;
} \
us_1 = us + correction; /* keep for next call */

//! Us64 reads the 1us tick.
//! \retval us count since last reset
static inline uint64_t Us64( void ){
US_DUTY
return us_1;
}
// //! Us64 reads the 1us tick.
// //! \retval us count since last reset
// static inline uint64_t Us64( void ){
// US_DUTY
// return us_1;
// }

//! Us32 reads the 1us tick. A 32-bit us stamp, wraps after 71,58 seconds.
//! \retval us count since last reset
Expand All @@ -99,11 +99,11 @@ uint32_t Us32( void ){
return us_1;
}

//! Us16 or Us64 needs be called in < 1ms intervals.
static inline uint16_t Us16( void ){
US_DUTY
return us16 + usOffset + correction; // max 9000 + max 999 + max 1000 = max 10999 us;
}
// //! Us16 or Us64 needs be called in < 1ms intervals.
// static inline uint16_t Us16( void ){
// US_DUTY
// return us16 + usOffset + correction; // max 9000 + max 999 + max 1000 = max 10999 us;
// }

// UsDuty is usable in wait loops, waiting for a hardware flag for example.
void UsDuty( void ){
Expand Down
Loading

0 comments on commit 98be03f

Please sign in to comment.