SEGGER_SYSVIEW_FreeRTOS.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*********************************************************************
  2. * SEGGER Microcontroller GmbH *
  3. * The Embedded Experts *
  4. **********************************************************************
  5. * *
  6. * (c) 1995 - 2021 SEGGER Microcontroller GmbH *
  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. * SEGGER strongly recommends to not make any changes *
  19. * to or modify the source code of this software in order to stay *
  20. * compatible with the SystemView and RTT protocol, and J-Link. *
  21. * *
  22. * Redistribution and use in source and binary forms, with or *
  23. * without modification, are permitted provided that the following *
  24. * condition is met: *
  25. * *
  26. * o Redistributions of source code must retain the above copyright *
  27. * notice, this condition and the following disclaimer. *
  28. * *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
  32. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
  33. * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
  34. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
  36. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
  37. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  38. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
  40. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
  41. * DAMAGE. *
  42. * *
  43. **********************************************************************
  44. * *
  45. * SystemView version: 3.32 *
  46. * *
  47. **********************************************************************
  48. -------------------------- END-OF-HEADER -----------------------------
  49. File : SEGGER_SYSVIEW_FreeRTOS.c
  50. Purpose : Interface between FreeRTOS and SystemView.
  51. Revision: $Rev: 7947 $
  52. */
  53. #include "FreeRTOS.h"
  54. #include "task.h"
  55. #include "SEGGER_SYSVIEW.h"
  56. #include "SEGGER_SYSVIEW_FreeRTOS.h"
  57. #include "string.h" // Required for memset
  58. typedef struct SYSVIEW_FREERTOS_TASK_STATUS SYSVIEW_FREERTOS_TASK_STATUS;
  59. struct SYSVIEW_FREERTOS_TASK_STATUS {
  60. U32 xHandle;
  61. const char* pcTaskName;
  62. unsigned uxCurrentPriority;
  63. U32 pxStack;
  64. unsigned uStackHighWaterMark;
  65. };
  66. static SYSVIEW_FREERTOS_TASK_STATUS _aTasks[SYSVIEW_FREERTOS_MAX_NOF_TASKS];
  67. static unsigned _NumTasks;
  68. /*********************************************************************
  69. *
  70. * _cbSendTaskList()
  71. *
  72. * Function description
  73. * This function is part of the link between FreeRTOS and SYSVIEW.
  74. * Called from SystemView when asked by the host, it uses SYSVIEW
  75. * functions to send the entire task list to the host.
  76. */
  77. static void _cbSendTaskList(void) {
  78. unsigned n;
  79. for (n = 0; n < _NumTasks; n++) {
  80. #if INCLUDE_uxTaskGetStackHighWaterMark // Report Task Stack High Watermark
  81. _aTasks[n].uStackHighWaterMark = uxTaskGetStackHighWaterMark((TaskHandle_t)_aTasks[n].xHandle);
  82. #endif
  83. SYSVIEW_SendTaskInfo((U32)_aTasks[n].xHandle, _aTasks[n].pcTaskName, (unsigned)_aTasks[n].uxCurrentPriority, (U32)_aTasks[n].pxStack, (unsigned)_aTasks[n].uStackHighWaterMark);
  84. }
  85. }
  86. /*********************************************************************
  87. *
  88. * _cbGetTime()
  89. *
  90. * Function description
  91. * This function is part of the link between FreeRTOS and SYSVIEW.
  92. * Called from SystemView when asked by the host, returns the
  93. * current system time in micro seconds.
  94. */
  95. static U64 _cbGetTime(void) {
  96. U64 Time;
  97. Time = xTaskGetTickCountFromISR();
  98. Time *= portTICK_PERIOD_MS;
  99. Time *= 1000;
  100. return Time;
  101. }
  102. /*********************************************************************
  103. *
  104. * Global functions
  105. *
  106. **********************************************************************
  107. */
  108. /*********************************************************************
  109. *
  110. * SYSVIEW_AddTask()
  111. *
  112. * Function description
  113. * Add a task to the internal list and record its information.
  114. */
  115. void SYSVIEW_AddTask(U32 xHandle, const char* pcTaskName, unsigned uxCurrentPriority, U32 pxStack, unsigned uStackHighWaterMark) {
  116. if (memcmp(pcTaskName, "IDLE", 5) == 0) {
  117. return;
  118. }
  119. if (_NumTasks >= SYSVIEW_FREERTOS_MAX_NOF_TASKS) {
  120. SEGGER_SYSVIEW_Warn("SYSTEMVIEW: Could not record task information. Maximum number of tasks reached.");
  121. return;
  122. }
  123. _aTasks[_NumTasks].xHandle = xHandle;
  124. _aTasks[_NumTasks].pcTaskName = pcTaskName;
  125. _aTasks[_NumTasks].uxCurrentPriority = uxCurrentPriority;
  126. _aTasks[_NumTasks].pxStack = pxStack;
  127. _aTasks[_NumTasks].uStackHighWaterMark = uStackHighWaterMark;
  128. _NumTasks++;
  129. SYSVIEW_SendTaskInfo(xHandle, pcTaskName,uxCurrentPriority, pxStack, uStackHighWaterMark);
  130. }
  131. /*********************************************************************
  132. *
  133. * SYSVIEW_UpdateTask()
  134. *
  135. * Function description
  136. * Update a task in the internal list and record its information.
  137. */
  138. void SYSVIEW_UpdateTask(U32 xHandle, const char* pcTaskName, unsigned uxCurrentPriority, U32 pxStack, unsigned uStackHighWaterMark) {
  139. unsigned n;
  140. if (memcmp(pcTaskName, "IDLE", 5) == 0) {
  141. return;
  142. }
  143. for (n = 0; n < _NumTasks; n++) {
  144. if (_aTasks[n].xHandle == xHandle) {
  145. break;
  146. }
  147. }
  148. if (n < _NumTasks) {
  149. _aTasks[n].pcTaskName = pcTaskName;
  150. _aTasks[n].uxCurrentPriority = uxCurrentPriority;
  151. _aTasks[n].pxStack = pxStack;
  152. _aTasks[n].uStackHighWaterMark = uStackHighWaterMark;
  153. SYSVIEW_SendTaskInfo(xHandle, pcTaskName, uxCurrentPriority, pxStack, uStackHighWaterMark);
  154. } else {
  155. SYSVIEW_AddTask(xHandle, pcTaskName, uxCurrentPriority, pxStack, uStackHighWaterMark);
  156. }
  157. }
  158. /*********************************************************************
  159. *
  160. * SYSVIEW_DeleteTask()
  161. *
  162. * Function description
  163. * Delete a task from the internal list.
  164. */
  165. void SYSVIEW_DeleteTask(U32 xHandle) {
  166. unsigned n;
  167. if (_NumTasks == 0) {
  168. return; // Early out
  169. }
  170. for (n = 0; n < _NumTasks; n++) {
  171. if (_aTasks[n].xHandle == xHandle) {
  172. break;
  173. }
  174. }
  175. if (n == (_NumTasks - 1)) {
  176. //
  177. // Task is last item in list.
  178. // Simply zero the item and decrement number of tasks.
  179. //
  180. memset(&_aTasks[n], 0, sizeof(_aTasks[n]));
  181. _NumTasks--;
  182. } else if (n < _NumTasks) {
  183. //
  184. // Task is in the middle of the list.
  185. // Move last item to current position and decrement number of tasks.
  186. // Order of tasks does not really matter, so no need to move all following items.
  187. //
  188. _aTasks[n].xHandle = _aTasks[_NumTasks - 1].xHandle;
  189. _aTasks[n].pcTaskName = _aTasks[_NumTasks - 1].pcTaskName;
  190. _aTasks[n].uxCurrentPriority = _aTasks[_NumTasks - 1].uxCurrentPriority;
  191. _aTasks[n].pxStack = _aTasks[_NumTasks - 1].pxStack;
  192. _aTasks[n].uStackHighWaterMark = _aTasks[_NumTasks - 1].uStackHighWaterMark;
  193. memset(&_aTasks[_NumTasks - 1], 0, sizeof(_aTasks[_NumTasks - 1]));
  194. _NumTasks--;
  195. }
  196. }
  197. /*********************************************************************
  198. *
  199. * SYSVIEW_SendTaskInfo()
  200. *
  201. * Function description
  202. * Record task information.
  203. */
  204. void SYSVIEW_SendTaskInfo(U32 TaskID, const char* sName, unsigned Prio, U32 StackBase, unsigned StackSize) {
  205. SEGGER_SYSVIEW_TASKINFO TaskInfo;
  206. memset(&TaskInfo, 0, sizeof(TaskInfo)); // Fill all elements with 0 to allow extending the structure in future version without breaking the code
  207. TaskInfo.TaskID = TaskID;
  208. TaskInfo.sName = sName;
  209. TaskInfo.Prio = Prio;
  210. TaskInfo.StackBase = StackBase;
  211. TaskInfo.StackSize = StackSize;
  212. SEGGER_SYSVIEW_SendTaskInfo(&TaskInfo);
  213. }
  214. /*********************************************************************
  215. *
  216. * Public API structures
  217. *
  218. **********************************************************************
  219. */
  220. // Callbacks provided to SYSTEMVIEW by FreeRTOS
  221. const SEGGER_SYSVIEW_OS_API SYSVIEW_X_OS_TraceAPI = {
  222. _cbGetTime,
  223. _cbSendTaskList,
  224. };
  225. /*************************** End of file ****************************/