@@ -64,9 +64,6 @@ Revision: $Rev: 3734 $
6464#include "freertos/FreeRTOS.h"
6565#include "SEGGER_SYSVIEW.h"
6666#include "rom/ets_sys.h"
67- #if CONFIG_FREERTOS_UNICORE == 0
68- #include "driver/timer.h"
69- #endif
7067#include "esp_app_trace.h"
7168#include "esp_app_trace_util.h"
7269#include "esp_intr_alloc.h"
@@ -86,10 +83,49 @@ extern const SEGGER_SYSVIEW_OS_API SYSVIEW_X_OS_TraceAPI;
8683// The target device name
8784#define SYSVIEW_DEVICE_NAME "ESP32"
8885
86+ // Determine which timer to use as timestamp source
87+ #if CONFIG_SYSVIEW_TS_SOURCE_CCOUNT
88+ #define TS_USE_CCOUNT 1
89+ #elif CONFIG_SYSVIEW_TS_SOURCE_ESP_TIMER
90+ #define TS_USE_ESP_TIMER 1
91+ #else
92+ #define TS_USE_TIMERGROUP 1
93+ #endif
94+
95+ #if TS_USE_TIMERGROUP
96+ #include "driver/timer.h"
97+
8998// Timer group timer divisor
9099#define SYSVIEW_TIMER_DIV 2
100+
91101// Frequency of the timestamp.
92102#define SYSVIEW_TIMESTAMP_FREQ (esp_clk_apb_freq() / SYSVIEW_TIMER_DIV)
103+
104+ // Timer ID and group ID
105+ #if defined(CONFIG_SYSVIEW_TS_SOURCE_TIMER_00 ) || defined(CONFIG_SYSVIEW_TS_SOURCE_TIMER_01 )
106+ #define TS_TIMER_ID 0
107+ #else
108+ #define TS_TIMER_ID 1
109+ #endif // TIMER_00 || TIMER_01
110+
111+ #if defined(CONFIG_SYSVIEW_TS_SOURCE_TIMER_00 ) || defined(CONFIG_SYSVIEW_TS_SOURCE_TIMER_10 )
112+ #define TS_TIMER_GROUP 0
113+ #else
114+ #define TS_TIMER_GROUP 1
115+ #endif // TIMER_00 || TIMER_10
116+
117+ #endif // TS_USE_TIMERGROUP
118+
119+ #if TS_USE_ESP_TIMER
120+ // esp_timer provides 1us resolution
121+ #define SYSVIEW_TIMESTAMP_FREQ (1000000)
122+ #endif // TS_USE_ESP_TIMER
123+
124+ #if TS_USE_CCOUNT
125+ // CCOUNT is incremented at CPU frequency
126+ #define SYSVIEW_TIMESTAMP_FREQ (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * 1000000)
127+ #endif // TS_USE_CCOUNT
128+
93129// System Frequency.
94130#define SYSVIEW_CPU_FREQ (esp_clk_cpu_freq())
95131
@@ -103,11 +139,8 @@ extern const SEGGER_SYSVIEW_OS_API SYSVIEW_X_OS_TraceAPI;
103139 #define SYSTICK_INTR_ID (ETS_INTERNAL_TIMER1_INTR_SOURCE+ETS_INTERNAL_INTR_SOURCE_OFF)
104140#endif
105141
106- static timer_idx_t s_ts_timer_idx ;
107- static timer_group_t s_ts_timer_group ;
108-
109142// SystemView is single core specific: it implies that SEGGER_SYSVIEW_LOCK()
110- // disables IRQs (disables rescheduling globaly ). So we can not use finite timeouts for locks and return error
143+ // disables IRQs (disables rescheduling globally ). So we can not use finite timeouts for locks and return error
111144// in case of expiration, because error will not be handled and SEGGER's code will go further implying that
112145// everything is fine, so for multi-core env we have to wait on underlying lock forever
113146#define SEGGER_LOCK_WAIT_TMO ESP_APPTRACE_TMO_INFINITE
@@ -213,35 +246,24 @@ static void _cbSendSystemDesc(void) {
213246*/
214247static void SEGGER_SYSVIEW_TS_Init ()
215248{
216- timer_config_t config ;
217-
218- #if CONFIG_SYSVIEW_TS_SOURCE_TIMER_00
219- s_ts_timer_group = TIMER_GROUP_0 ;
220- s_ts_timer_idx = TIMER_0 ;
221- #endif
222- #if CONFIG_SYSVIEW_TS_SOURCE_TIMER_01
223- s_ts_timer_group = TIMER_GROUP_0 ;
224- s_ts_timer_idx = TIMER_1 ;
225- #endif
226- #if CONFIG_SYSVIEW_TS_SOURCE_TIMER_10
227- s_ts_timer_group = TIMER_GROUP_1 ;
228- s_ts_timer_idx = TIMER_0 ;
229- #endif
230- #if CONFIG_SYSVIEW_TS_SOURCE_TIMER_11
231- s_ts_timer_group = TIMER_GROUP_1 ;
232- s_ts_timer_idx = TIMER_1 ;
233- #endif
234- config .alarm_en = 0 ;
235- config .auto_reload = 0 ;
236- config .counter_dir = TIMER_COUNT_UP ;
237- config .divider = SYSVIEW_TIMER_DIV ;
238- config .counter_en = 0 ;
239- /*Configure timer*/
240- timer_init (s_ts_timer_group , s_ts_timer_idx , & config );
241- /*Load counter value */
242- timer_set_counter_value (s_ts_timer_group , s_ts_timer_idx , 0x00000000ULL );
243- /*Enable timer interrupt*/
244- timer_start (s_ts_timer_group , s_ts_timer_idx );
249+ /* We only need to initialize something if we use Timer Group.
250+ * esp_timer and ccount can be used as is.
251+ */
252+ #if TS_USE_TIMERGROUP
253+ timer_config_t config = {
254+ .alarm_en = 0 ,
255+ .auto_reload = 0 ,
256+ .counter_dir = TIMER_COUNT_UP ,
257+ .divider = SYSVIEW_TIMER_DIV ,
258+ .counter_en = 0
259+ };
260+ /* Configure timer */
261+ timer_init (TS_TIMER_GROUP , TS_TIMER_ID , & config );
262+ /* Load counter value */
263+ timer_set_counter_value (TS_TIMER_GROUP , TS_TIMER_ID , 0x00000000ULL );
264+ /* Start counting */
265+ timer_start (TS_TIMER_GROUP , TS_TIMER_ID );
266+ #endif // TS_USE_TIMERGROUP
245267}
246268
247269void SEGGER_SYSVIEW_Conf (void ) {
@@ -296,12 +318,14 @@ void SEGGER_SYSVIEW_Conf(void) {
296318
297319U32 SEGGER_SYSVIEW_X_GetTimestamp ()
298320{
299- #if CONFIG_FREERTOS_UNICORE == 0
321+ #if TS_USE_TIMERGROUP
300322 uint64_t ts = 0 ;
301- timer_get_counter_value (s_ts_timer_group , s_ts_timer_idx , & ts );
302- return (U32 )ts ; // return lower part of counter value
303- #else
323+ timer_get_counter_value (TS_TIMER_GROUP , TS_TIMER_ID , & ts );
324+ return (U32 ) ts ; // return lower part of counter value
325+ #elif TS_USE_CCOUNT
304326 return portGET_RUN_TIME_COUNTER_VALUE ();
327+ #elif TS_USE_ESP_TIMER
328+ return (U32 ) esp_timer_get_time (); // return lower part of counter value
305329#endif
306330}
307331
0 commit comments