SEGGER_SYSVIEW_FreeRTOS.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2017 SEGGER Microcontroller GmbH & Co. KG
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /*********************************************************************
  7. * SEGGER Microcontroller GmbH & Co. KG *
  8. * The Embedded Experts *
  9. **********************************************************************
  10. * *
  11. * (c) 2015 - 2017 SEGGER Microcontroller GmbH & Co. KG *
  12. * *
  13. * www.segger.com Support: support@segger.com *
  14. * *
  15. **********************************************************************
  16. * *
  17. * SEGGER SystemView * Real-time application analysis *
  18. * *
  19. **********************************************************************
  20. * *
  21. * All rights reserved. *
  22. * *
  23. * SEGGER strongly recommends to not make any changes *
  24. * to or modify the source code of this software in order to stay *
  25. * compatible with the RTT protocol and J-Link. *
  26. * *
  27. * Redistribution and use in source and binary forms, with or *
  28. * without modification, are permitted provided that the following *
  29. * conditions are met: *
  30. * *
  31. * o Redistributions of source code must retain the above copyright *
  32. * notice, this list of conditions and the following disclaimer. *
  33. * *
  34. * o Redistributions in binary form must reproduce the above *
  35. * copyright notice, this list of conditions and the following *
  36. * disclaimer in the documentation and/or other materials provided *
  37. * with the distribution. *
  38. * *
  39. * o Neither the name of SEGGER Microcontroller GmbH & Co. KG *
  40. * nor the names of its contributors may be used to endorse or *
  41. * promote products derived from this software without specific *
  42. * prior written permission. *
  43. * *
  44. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  45. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
  46. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
  47. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
  48. * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
  49. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
  50. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
  51. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
  52. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  53. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
  54. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
  55. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
  56. * DAMAGE. *
  57. * *
  58. **********************************************************************
  59. * *
  60. * SystemView version: V2.42 *
  61. * *
  62. **********************************************************************
  63. -------------------------- END-OF-HEADER -----------------------------
  64. File : SEGGER_SYSVIEW_FreeRTOS.c
  65. Purpose : Interface between FreeRTOS and SystemView.
  66. Revision: $Rev: 3734 $
  67. */
  68. #include "freertos/FreeRTOS.h"
  69. #include "freertos/task.h"
  70. #include "SEGGER_SYSVIEW.h"
  71. #include "SEGGER_SYSVIEW_FreeRTOS.h"
  72. #include "string.h" // Required for memset
  73. typedef struct SYSVIEW_FREERTOS_TASK_STATUS SYSVIEW_FREERTOS_TASK_STATUS;
  74. struct SYSVIEW_FREERTOS_TASK_STATUS {
  75. U32 xHandle;
  76. const char* pcTaskName;
  77. unsigned uxCurrentPriority;
  78. U32 pxStack;
  79. unsigned uStackHighWaterMark;
  80. };
  81. static SYSVIEW_FREERTOS_TASK_STATUS _aTasks[SYSVIEW_FREERTOS_MAX_NOF_TASKS];
  82. /*********************************************************************
  83. *
  84. * _cbSendTaskList()
  85. *
  86. * Function description
  87. * This function is part of the link between FreeRTOS and SYSVIEW.
  88. * Called from SystemView when asked by the host, it uses SYSVIEW
  89. * functions to send the entire task list to the host.
  90. */
  91. static void _cbSendTaskList(void) {
  92. unsigned n;
  93. for (n = 0; n < SYSVIEW_FREERTOS_MAX_NOF_TASKS; n++) {
  94. if (_aTasks[n].xHandle) {
  95. #if INCLUDE_uxTaskGetStackHighWaterMark // Report Task Stack High Watermark
  96. _aTasks[n].uStackHighWaterMark = uxTaskGetStackHighWaterMark((TaskHandle_t)_aTasks[n].xHandle);
  97. #endif
  98. SYSVIEW_SendTaskInfo((U32)_aTasks[n].xHandle, _aTasks[n].pcTaskName, (unsigned)_aTasks[n].uxCurrentPriority, (U32)_aTasks[n].pxStack, (unsigned)_aTasks[n].uStackHighWaterMark);
  99. }
  100. }
  101. }
  102. /*********************************************************************
  103. *
  104. * _cbGetTime()
  105. *
  106. * Function description
  107. * This function is part of the link between FreeRTOS and SYSVIEW.
  108. * Called from SystemView when asked by the host, returns the
  109. * current system time in micro seconds.
  110. */
  111. static U64 _cbGetTime(void) {
  112. U64 Time;
  113. Time = xTaskGetTickCountFromISR();
  114. Time *= portTICK_PERIOD_MS;
  115. Time *= 1000;
  116. return Time;
  117. }
  118. /*********************************************************************
  119. *
  120. * Global functions
  121. *
  122. **********************************************************************
  123. */
  124. /*********************************************************************
  125. *
  126. * SYSVIEW_AddTask()
  127. *
  128. * Function description
  129. * Add a task to the internal list and record its information.
  130. */
  131. void SYSVIEW_AddTask(U32 xHandle, const char* pcTaskName, unsigned uxCurrentPriority, U32 pxStack, unsigned uStackHighWaterMark) {
  132. unsigned n;
  133. if (memcmp(pcTaskName, "IDLE", 5) == 0) {
  134. return;
  135. }
  136. for (n = 0; n < SYSVIEW_FREERTOS_MAX_NOF_TASKS; n++) {
  137. if (_aTasks[n].xHandle == 0) {
  138. break;
  139. }
  140. }
  141. if (n == SYSVIEW_FREERTOS_MAX_NOF_TASKS) {
  142. SEGGER_SYSVIEW_Warn("SYSTEMVIEW: Could not record task information. Maximum number of tasks reached.");
  143. return;
  144. }
  145. _aTasks[n].xHandle = xHandle;
  146. _aTasks[n].pcTaskName = pcTaskName;
  147. _aTasks[n].uxCurrentPriority = uxCurrentPriority;
  148. _aTasks[n].pxStack = pxStack;
  149. _aTasks[n].uStackHighWaterMark = uStackHighWaterMark;
  150. SYSVIEW_SendTaskInfo(xHandle, pcTaskName,uxCurrentPriority, pxStack, uStackHighWaterMark);
  151. }
  152. /*********************************************************************
  153. *
  154. * SYSVIEW_UpdateTask()
  155. *
  156. * Function description
  157. * Update a task in the internal list and record its information.
  158. */
  159. void SYSVIEW_UpdateTask(U32 xHandle, const char* pcTaskName, unsigned uxCurrentPriority, U32 pxStack, unsigned uStackHighWaterMark) {
  160. unsigned n;
  161. if (memcmp(pcTaskName, "IDLE", 5) == 0) {
  162. return;
  163. }
  164. for (n = 0; n < SYSVIEW_FREERTOS_MAX_NOF_TASKS; n++) {
  165. if (_aTasks[n].xHandle == xHandle) {
  166. break;
  167. }
  168. }
  169. if (n < SYSVIEW_FREERTOS_MAX_NOF_TASKS) {
  170. _aTasks[n].pcTaskName = pcTaskName;
  171. _aTasks[n].uxCurrentPriority = uxCurrentPriority;
  172. _aTasks[n].pxStack = pxStack;
  173. _aTasks[n].uStackHighWaterMark = uStackHighWaterMark;
  174. SYSVIEW_SendTaskInfo(xHandle, pcTaskName, uxCurrentPriority, pxStack, uStackHighWaterMark);
  175. } else {
  176. SYSVIEW_AddTask(xHandle, pcTaskName, uxCurrentPriority, pxStack, uStackHighWaterMark);
  177. }
  178. }
  179. /*********************************************************************
  180. *
  181. * SYSVIEW_DeleteTask()
  182. *
  183. * Function description
  184. * Delete a task from the internal list.
  185. */
  186. void SYSVIEW_DeleteTask(U32 xHandle) {
  187. unsigned n;
  188. for (n = 0; n < SYSVIEW_FREERTOS_MAX_NOF_TASKS; n++) {
  189. if (_aTasks[n].xHandle == xHandle) {
  190. break;
  191. }
  192. }
  193. if (n == SYSVIEW_FREERTOS_MAX_NOF_TASKS) {
  194. SEGGER_SYSVIEW_Warn("SYSTEMVIEW: Could not find task information. Cannot delete task.");
  195. return;
  196. }
  197. _aTasks[n].xHandle = 0;
  198. }
  199. /*********************************************************************
  200. *
  201. * SYSVIEW_SendTaskInfo()
  202. *
  203. * Function description
  204. * Record task information.
  205. */
  206. void SYSVIEW_SendTaskInfo(U32 TaskID, const char* sName, unsigned Prio, U32 StackBase, unsigned StackSize) {
  207. SEGGER_SYSVIEW_TASKINFO TaskInfo;
  208. memset(&TaskInfo, 0, sizeof(TaskInfo)); // Fill all elements with 0 to allow extending the structure in future version without breaking the code
  209. TaskInfo.TaskID = TaskID;
  210. TaskInfo.sName = sName;
  211. TaskInfo.Prio = Prio;
  212. TaskInfo.StackBase = StackBase;
  213. TaskInfo.StackSize = StackSize;
  214. SEGGER_SYSVIEW_SendTaskInfo(&TaskInfo);
  215. }
  216. /*********************************************************************
  217. *
  218. * SYSVIEW_RecordU32x4()
  219. *
  220. * Function description
  221. * Record an event with 4 parameters
  222. */
  223. void SYSVIEW_RecordU32x4(unsigned Id, U32 Para0, U32 Para1, U32 Para2, U32 Para3) {
  224. U8 aPacket[SEGGER_SYSVIEW_INFO_SIZE + 4 * SEGGER_SYSVIEW_QUANTA_U32];
  225. U8* pPayload;
  226. //
  227. pPayload = SEGGER_SYSVIEW_PREPARE_PACKET(aPacket); // Prepare the packet for SystemView
  228. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para0); // Add the first parameter to the packet
  229. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para1); // Add the second parameter to the packet
  230. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para2); // Add the third parameter to the packet
  231. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para3); // Add the fourth parameter to the packet
  232. //
  233. SEGGER_SYSVIEW_SendPacket(&aPacket[0], pPayload, Id); // Send the packet
  234. }
  235. /*********************************************************************
  236. *
  237. * SYSVIEW_RecordU32x5()
  238. *
  239. * Function description
  240. * Record an event with 5 parameters
  241. */
  242. void SYSVIEW_RecordU32x5(unsigned Id, U32 Para0, U32 Para1, U32 Para2, U32 Para3, U32 Para4) {
  243. U8 aPacket[SEGGER_SYSVIEW_INFO_SIZE + 5 * SEGGER_SYSVIEW_QUANTA_U32];
  244. U8* pPayload;
  245. //
  246. pPayload = SEGGER_SYSVIEW_PREPARE_PACKET(aPacket); // Prepare the packet for SystemView
  247. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para0); // Add the first parameter to the packet
  248. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para1); // Add the second parameter to the packet
  249. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para2); // Add the third parameter to the packet
  250. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para3); // Add the fourth parameter to the packet
  251. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para4); // Add the fifth parameter to the packet
  252. //
  253. SEGGER_SYSVIEW_SendPacket(&aPacket[0], pPayload, Id); // Send the packet
  254. }
  255. /*********************************************************************
  256. *
  257. * Public API structures
  258. *
  259. **********************************************************************
  260. */
  261. // Callbacks provided to SYSTEMVIEW by FreeRTOS
  262. const SEGGER_SYSVIEW_OS_API SYSVIEW_X_OS_TraceAPI = {
  263. _cbGetTime,
  264. _cbSendTaskList,
  265. };
  266. /*************************** End of file ****************************/