SEGGER_SYSVIEW_FreeRTOS.c 12 KB

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