فهرست منبع

[Fix] Enable DWT for correct SystemView timestamp via serial; fix thread info error (#13)

KK 9 ماه پیش
والد
کامیت
b63eb3c74a
2فایلهای تغییر یافته به همراه30 افزوده شده و 26 حذف شده
  1. 23 14
      SystemView_Src/Config/SEGGER_SYSVIEW_Config_RTThread.c
  2. 7 12
      SystemView_Src/Config/SEGGER_SYSVIEW_RTThread.c

+ 23 - 14
SystemView_Src/Config/SEGGER_SYSVIEW_Config_RTThread.c

@@ -155,15 +155,28 @@ extern unsigned int SystemCoreClock;
 
 
 
-/*********************************************************************
-*
-*       Defines, fixed
-*
-**********************************************************************
-*/
-#define DWT_CTRL                  (*(volatile rt_uint32_t*) (0xE0001000uL))  // DWT Control Register
-#define NOCYCCNT_BIT              (1uL << 25)                           // Cycle counter support bit
-#define CYCCNTENA_BIT             (1uL << 0)                            // Cycle counter enable bit
+//==================== DWT & CoreDebug 地址定义 ====================//
+
+#define DWT_CTRL        (*(volatile unsigned int *)0xE0001000u)  // DWT Control Register
+#define DWT_CYCCNT      (*(volatile unsigned int *)0xE0001004u)  // DWT Cycle Counter Register
+#define DEMCR           (*(volatile unsigned int *)0xE000EDFCu)  // Debug Exception and Monitor Control Register
+
+//==================== 位定义 ====================//
+
+#define TRCENA_BIT      (1u << 24)   // DEMCR[24] = TRCENA: Trace enable
+#define CYCCNTENA_BIT   (1u << 0)    // DWT_CTRL[0] = CYCCNTENA: Cycle Counter Enable
+#define NOCYCCNT_BIT    (1u << 25)   // DWT_CTRL[25] = NOCYCCNT: Cycle Counter not present
+
+//==================== 启用 DWT Cycle Counter 的宏 ====================//
+
+#define ENABLE_DWT_CYCLE_COUNTER()             \
+    do {                                       \
+        DEMCR |= TRCENA_BIT;                   /* Enable trace */           \
+        if ((DWT_CTRL & NOCYCCNT_BIT) == 0) {  /* If CYCCNT is supported */ \
+            DWT_CYCCNT = 0;                    /* Clear counter */          \
+            DWT_CTRL |= CYCCNTENA_BIT;         /* Enable counter */         \
+        }                                      \
+    } while (0)
 
 /*********************************************************************
 *
@@ -198,11 +211,7 @@ void SEGGER_SYSVIEW_Conf(void) {
   //  The cycle counter must be activated in order
   //  to use time related functions.
   //
-  if ((DWT_CTRL & NOCYCCNT_BIT) == 0) {       // Cycle counter supported?
-    if ((DWT_CTRL & CYCCNTENA_BIT) == 0) {    // Cycle counter not enabled?
-      DWT_CTRL |= CYCCNTENA_BIT;              // Enable Cycle counter
-    }
-  }
+  ENABLE_DWT_CYCLE_COUNTER();  
 #endif
   SEGGER_SYSVIEW_Init(SYSVIEW_TIMESTAMP_FREQ, SYSVIEW_CPU_FREQ,
                       &SYSVIEW_X_OS_TraceAPI, _cbSendSystemDesc);

+ 7 - 12
SystemView_Src/Config/SEGGER_SYSVIEW_RTThread.c

@@ -98,9 +98,9 @@ static void _cbSendTaskInfo(const rt_thread_t thread)
 
 static void _cbSendTaskList(void)
 {
-    struct rt_thread *thread;
-    struct rt_list_node *node;
-    struct rt_list_node *list;
+    struct rt_thread* thread;
+    struct rt_list_node* node;
+    struct rt_list_node* list;
     struct rt_object_information *info;
 
     info = rt_object_get_information(RT_Object_Class_Thread);
@@ -109,21 +109,16 @@ static void _cbSendTaskList(void)
     tidle = rt_thread_idle_gethandler();
 
     rt_enter_critical();
-    for (node = list->next; node != list; node = node->next)
+    for(node = list->next; node != list; node = node->next)
     {
-#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0))
-        thread = RT_THREAD_LIST_NODE_ENTRY(node);
-#elif defined(RT_VERSION_CHECK) &&(RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1))
-        thread = rt_list_entry(node, struct rt_thread, tlist);
+#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1))
+        thread = (struct rt_thread *)rt_list_entry(node, struct rt_object, list);
 #else
         thread = rt_list_entry(node, struct rt_thread, list);
 #endif
         /* skip idle thread */
-#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0))
-            _cbSendTaskInfo((int)(thread + 0x20));  //ref SEGGER_SystemView/issues/9
-#else
+        if(thread != tidle)
             _cbSendTaskInfo(thread);
-#endif
     }
     rt_exit_critical();
 }