SEGGER_SYSVIEW_RTThread.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*********************************************************************
  2. * SEGGER MICROCONTROLLER GmbH & Co. KG *
  3. * Solutions for real time microcontroller applications *
  4. **********************************************************************
  5. * *
  6. * (c) 2015 - 2016 SEGGER Microcontroller GmbH & Co. KG *
  7. * *
  8. * www.segger.com Support: support@segger.com *
  9. * *
  10. **********************************************************************
  11. * *
  12. * SEGGER SystemView * Real-time application analysis *
  13. * *
  14. **********************************************************************
  15. * *
  16. * All rights reserved. *
  17. * *
  18. * * This software may in its unmodified form be freely redistributed *
  19. * in source form. *
  20. * * The source code may be modified, provided the source code *
  21. * retains the above copyright notice, this list of conditions and *
  22. * the following disclaimer. *
  23. * * Modified versions of this software in source or linkable form *
  24. * may not be distributed without prior consent of SEGGER. *
  25. * *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND *
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, *
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
  29. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
  30. * SEGGER Microcontroller BE LIABLE FOR ANY DIRECT, INDIRECT, *
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
  32. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, *
  35. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
  38. * *
  39. **********************************************************************
  40. * *
  41. * SystemView version: V2.40 *
  42. * *
  43. **********************************************************************
  44. -------------------------- END-OF-HEADER -----------------------------
  45. File : SEGGER_SYSVIEW_RTThread.c
  46. Purpose : Interface between RT-Thread and System View.
  47. Revision: $Rev: 3745 $
  48. */
  49. #include "rtthread.h"
  50. #include "SEGGER_SYSVIEW.h"
  51. #include "SEGGER_RTT.h"
  52. #ifndef RT_USING_HOOK
  53. #error "SystemView is only works when feature RT_USING_HOOK is enable."
  54. #endif
  55. static rt_thread_t tidle;
  56. static U64 _cbGetTime(void)
  57. {
  58. return (U64)(rt_tick_get() * 1000 / RT_TICK_PER_SECOND);
  59. }
  60. static void _cbSendTaskInfo(const rt_thread_t thread)
  61. {
  62. SEGGER_SYSVIEW_TASKINFO Info;
  63. rt_enter_critical();
  64. rt_memset(&Info, 0, sizeof(Info));
  65. Info.TaskID = (U32)thread;
  66. Info.sName = thread->name;
  67. Info.Prio = thread->current_priority;
  68. Info.StackBase = (U32)thread->stack_addr;
  69. Info.StackSize = thread->stack_size;
  70. SEGGER_SYSVIEW_SendTaskInfo(&Info);
  71. rt_exit_critical();
  72. }
  73. extern struct rt_object_information rt_object_container[];
  74. static void _cbSendTaskList(void)
  75. {
  76. struct rt_thread* thread;
  77. struct rt_list_node* node;
  78. struct rt_list_node* list =
  79. &rt_object_container[RT_Object_Class_Thread].object_list;
  80. tidle = rt_thread_idle_gethandler();
  81. rt_enter_critical();
  82. for(node = list->next; node != list; node = node->next)
  83. {
  84. thread = rt_list_entry(node, struct rt_thread, list);
  85. /* skip idle thread */
  86. if(thread != tidle)
  87. _cbSendTaskInfo(thread);
  88. }
  89. rt_exit_critical();
  90. }
  91. static void _cb_thread_resume(rt_thread_t thread)
  92. {
  93. SEGGER_SYSVIEW_OnTaskStartReady((unsigned)thread);
  94. }
  95. static void _cb_thread_suspend(rt_thread_t thread)
  96. {
  97. SEGGER_SYSVIEW_OnTaskStopReady((unsigned)thread, 0);
  98. }
  99. static void _cb_scheduler(rt_thread_t from, rt_thread_t to)
  100. {
  101. SEGGER_SYSVIEW_OnTaskStopReady((unsigned)from, 0);
  102. if(to == tidle)
  103. SEGGER_SYSVIEW_OnIdle();
  104. else
  105. SEGGER_SYSVIEW_OnTaskStartExec((unsigned)to);
  106. }
  107. static void _cb_irq_enter(void)
  108. {
  109. SEGGER_SYSVIEW_RecordEnterISR();
  110. }
  111. static void _cb_irq_leave(void)
  112. {
  113. rt_thread_t current;
  114. if(rt_interrupt_get_nest())
  115. {
  116. SEGGER_SYSVIEW_RecordExitISR();
  117. return;
  118. }
  119. SEGGER_SYSVIEW_RecordExitISRToScheduler();
  120. current = rt_thread_self();
  121. if(current == tidle)
  122. SEGGER_SYSVIEW_OnIdle();
  123. else
  124. SEGGER_SYSVIEW_OnTaskStartExec((unsigned)current);
  125. }
  126. static void _cb_object_attach(struct rt_object* object)
  127. {
  128. switch(object->type)
  129. {
  130. case RT_Object_Class_Thread:
  131. SEGGER_SYSVIEW_OnTaskCreate((unsigned)object);
  132. _cbSendTaskInfo((rt_thread_t)object);
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. static void _cb_object_detach(struct rt_object* object)
  139. {
  140. switch(object->type)
  141. {
  142. case RT_Object_Class_Thread:
  143. SEGGER_SYSVIEW_OnTaskStopExec();
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. // Services provided to SYSVIEW by RT-Thread
  150. const SEGGER_SYSVIEW_OS_API SYSVIEW_X_OS_TraceAPI = {
  151. _cbGetTime, _cbSendTaskList,
  152. };
  153. // RT-Thread init trace component
  154. static int rt_trace_init(void)
  155. {
  156. tidle = rt_thread_idle_gethandler();
  157. SEGGER_SYSVIEW_Conf();
  158. // register hooks
  159. rt_object_attach_sethook(_cb_object_attach);
  160. rt_object_detach_sethook(_cb_object_detach);
  161. rt_thread_suspend_sethook(_cb_thread_suspend);
  162. rt_thread_resume_sethook(_cb_thread_resume);
  163. rt_scheduler_sethook(_cb_scheduler);
  164. rt_interrupt_enter_sethook(_cb_irq_enter);
  165. rt_interrupt_leave_sethook(_cb_irq_leave);
  166. return 0;
  167. }
  168. INIT_COMPONENT_EXPORT(rt_trace_init);
  169. /*************************** End of file ****************************/