1 /*
2  * Trace Recorder for Tracealyzer v4.9.2
3  * Copyright 2023 Percepio AB
4  * www.percepio.com
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * The interface definitions for trace streaming ("stream ports").
9  * This "stream port" sets up the recorder to use SEGGER RTT as streaming channel.
10  *
11  * Note that this stream port is more complex than the typical case, since
12  * the J-Link interface uses a separate RAM buffer in SEGGER_RTT.c, instead
13  * of the default buffer included in the recorder core. The other stream ports
14  * offer more typical examples of how to define a custom streaming interface.
15  */
16 
17 #ifndef TRC_STREAM_PORT_H
18 #define TRC_STREAM_PORT_H
19 
20 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
21 
22 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include <trcDefines.h>
29 #include <trcTypes.h>
30 #include <trcStreamPortConfig.h>
31 
32 #include <SEGGER_RTT_Conf.h>
33 #include <SEGGER_RTT.h>
34 
35 #define TRC_USE_INTERNAL_BUFFER (TRC_CFG_STREAM_PORT_USE_INTERNAL_BUFFER)
36 
37 #define TRC_INTERNAL_EVENT_BUFFER_WRITE_MODE (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_WRITE_MODE)
38 
39 #define TRC_INTERNAL_EVENT_BUFFER_TRANSFER_MODE (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_TRANSFER_MODE)
40 
41 #define TRC_INTERNAL_BUFFER_CHUNK_SIZE (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_CHUNK_SIZE)
42 
43 #define TRC_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_SIZE_LIMIT (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_SIZE_LIMIT)
44 
45 #define TRC_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_COUNT_LIMIT (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_COUNT_LIMIT)
46 
47 /* Aligned */
48 #define TRC_STREAM_PORT_INTERNAL_BUFFER_SIZE ((((TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_SIZE) + sizeof(TraceUnsignedBaseType_t) - 1) / sizeof(TraceUnsignedBaseType_t)) * sizeof(TraceUnsignedBaseType_t))
49 
50 /* Aligned */
51 #define TRC_STREAM_PORT_RTT_UP_BUFFER_SIZE ((((TRC_CFG_STREAM_PORT_RTT_UP_BUFFER_SIZE) + sizeof(TraceUnsignedBaseType_t) - 1) / sizeof(TraceUnsignedBaseType_t)) * sizeof(TraceUnsignedBaseType_t))
52 
53 /* Aligned */
54 #define TRC_STREAM_PORT_RTT_DOWN_BUFFER_SIZE ((((TRC_CFG_STREAM_PORT_RTT_DOWN_BUFFER_SIZE) + sizeof(TraceUnsignedBaseType_t) - 1) / sizeof(TraceUnsignedBaseType_t)) * sizeof(TraceUnsignedBaseType_t))
55 
56 
57 /**
58  * @brief A structure representing the trace stream port buffer.
59  */
60 typedef struct TraceStreamPortBuffer	/* Aligned */
61 {
62 #if (TRC_USE_INTERNAL_BUFFER == 1)
63 	uint8_t bufferInternal[TRC_STREAM_PORT_INTERNAL_BUFFER_SIZE];
64 #endif
65 	uint8_t bufferUp[TRC_STREAM_PORT_RTT_UP_BUFFER_SIZE];
66 	uint8_t bufferDown[TRC_STREAM_PORT_RTT_DOWN_BUFFER_SIZE];
67 } TraceStreamPortBuffer_t;
68 
69 /**
70  * @internal Stream port initialize callback.
71  *
72  * This function is called by the recorder as part of its initialization phase.
73  *
74  * @param[in] pxBuffer Buffer
75  *
76  * @retval TRC_FAIL Initialization failed
77  * @retval TRC_SUCCESS Success
78  */
79 traceResult xTraceStreamPortInitialize(TraceStreamPortBuffer_t* pxBuffer);
80 
81 /**
82  * @brief Allocates data from the stream port.
83  *
84  * @param[in] uiSize Allocation size
85  * @param[out] ppvData Allocation data pointer
86  *
87  * @retval TRC_FAIL Allocate failed
88  * @retval TRC_SUCCESS Success
89  */
90 #if (TRC_USE_INTERNAL_BUFFER == 1)
91 	#if (TRC_INTERNAL_EVENT_BUFFER_WRITE_MODE == TRC_INTERNAL_EVENT_BUFFER_OPTION_WRITE_MODE_COPY)
92 		#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceStaticBufferGet(ppvData))
93 	#else
94 		#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceInternalEventBufferAlloc(uiSize, ppvData))
95 	#endif
96 #else
97 	#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceStaticBufferGet(ppvData))
98 #endif
99 
100 /**
101  * @brief Commits data to the stream port, depending on the implementation/configuration of the
102  * stream port this data might be directly written to the stream port interface, buffered, or
103  * something else.
104  *
105  * @param[in] pvData Data to commit
106  * @param[in] uiSize Data to commit size
107  * @param[out] piBytesCommitted Bytes committed
108  *
109  * @retval TRC_FAIL Commit failed
110  * @retval TRC_SUCCESS Success
111  */
112 #if (TRC_USE_INTERNAL_BUFFER == 1)
113 	#if (TRC_INTERNAL_EVENT_BUFFER_WRITE_MODE == TRC_INTERNAL_EVENT_BUFFER_OPTION_WRITE_MODE_COPY)
114 		#define xTraceStreamPortCommit xTraceInternalEventBufferPush
115 	#else
116 		#define xTraceStreamPortCommit xTraceInternalEventBufferAllocCommit
117 	#endif
118 #else
119 	#define xTraceStreamPortCommit xTraceStreamPortWriteData
120 #endif
121 
122 /**
123  * @brief Writes data through the stream port interface.
124  *
125  * @param[in] pvData Data to write
126  * @param[in] uiSize Data to write size
127  * @param[out] piBytesWritten Bytes written
128  *
129  * @retval TRC_FAIL Write failed
130  * @retval TRC_SUCCESS Success
131  */
132 #if (defined(TRC_CFG_STREAM_PORT_RTT_NO_LOCK_WRITE) && TRC_CFG_STREAM_PORT_RTT_NO_LOCK_WRITE == 1)
133 #define xTraceStreamPortWriteData(pvData, uiSize, piBytesWritten) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(piBytesWritten) = (int32_t)SEGGER_RTT_WriteNoLock((TRC_CFG_STREAM_PORT_RTT_UP_BUFFER_INDEX), (const char*)pvData, uiSize), TRC_SUCCESS)
134 #else
135 #define xTraceStreamPortWriteData(pvData, uiSize, piBytesWritten) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(piBytesWritten) = (int32_t)SEGGER_RTT_Write((TRC_CFG_STREAM_PORT_RTT_UP_BUFFER_INDEX), (const char*)pvData, uiSize), TRC_SUCCESS)
136 #endif
137 
138 /**
139  * @brief Reads data through the stream port interface.
140  *
141  * @param[in] pvData Destination data buffer
142  * @param[in] uiSize Destination data buffer size
143  * @param[out] piBytesRead Bytes read
144  *
145  * @retval TRC_FAIL Read failed
146  * @retval TRC_SUCCESS Success
147  */
148 #define xTraceStreamPortReadData(pvData, uiSize, piBytesRead) ((SEGGER_RTT_HASDATA(TRC_CFG_STREAM_PORT_RTT_DOWN_BUFFER_INDEX)) ? (*(piBytesRead) = (int32_t)SEGGER_RTT_Read((TRC_CFG_STREAM_PORT_RTT_DOWN_BUFFER_INDEX), (char*)(pvData), uiSize), TRC_SUCCESS) : TRC_SUCCESS)
149 
150 traceResult xTraceStreamPortOnEnable(uint32_t uiStartOption);
151 
152 #define xTraceStreamPortOnDisable() (void)(TRC_SUCCESS)
153 
154 #define xTraceStreamPortOnTraceBegin() (void)(TRC_SUCCESS)
155 
156 #define xTraceStreamPortOnTraceEnd() (void)(TRC_SUCCESS)
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 
162 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/
163 
164 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/
165 
166 #endif /* TRC_STREAM_PORT_H */
167