perf_os_patch_freertos.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /****************************************************************************
  2. * Copyright 2022 Gorgon Meducer (Email:embedded_zhuoran@hotmail.com) *
  3. * *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); *
  5. * you may not use this file except in compliance with the License. *
  6. * You may obtain a copy of the License at *
  7. * *
  8. * http://www.apache.org/licenses/LICENSE-2.0 *
  9. * *
  10. * Unless required by applicable law or agreed to in writing, software *
  11. * distributed under the License is distributed on an "AS IS" BASIS, *
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  13. * See the License for the specific language governing permissions and *
  14. * limitations under the License. *
  15. * *
  16. ****************************************************************************/
  17. /*============================ INCLUDES ======================================*/
  18. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  19. all the API functions to use the MPU wrappers. That should only be done when
  20. task.h is included from an application file. */
  21. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  22. /* FreeRTOS includes. */
  23. #include "FreeRTOS.h"
  24. #include "task.h"
  25. #include "timers.h"
  26. #include "stack_macros.h"
  27. /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
  28. because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  29. for the header files above, but not in this file, in order to generate the
  30. correct privileged Vs unprivileged linkage and placement. */
  31. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
  32. #include "perf_counter.h"
  33. #include "cmsis_compiler.h"
  34. /*============================ MACROS ========================================*/
  35. #undef __WRAP_FUNC
  36. #undef WRAP_FUNC
  37. #if defined(__IS_COMPILER_ARM_COMPILER__) && __IS_COMPILER_ARM_COMPILER__
  38. # define __WRAP_FUNC(__NAME) $Sub$$##__NAME
  39. # define __ORIG_FUNC(__NAME) $Super$$##__NAME
  40. #elif (defined(__IS_COMPILER_LLVM__) && __IS_COMPILER_LLVM__) \
  41. || (defined(__IS_COMPILER_GCC__) && __IS_COMPILER_GCC__)
  42. # define __WRAP_FUNC(__NAME) __wrap_##__NAME
  43. # define __ORIG_FUNC(__NAME) __real_##__NAME
  44. #endif
  45. #define WRAP_FUNC(__NAME) __WRAP_FUNC(__NAME)
  46. #define ORIG_FUNC(__NAME) __ORIG_FUNC(__NAME)
  47. struct __task_cycle_info_t {
  48. task_cycle_info_t tInfo;
  49. int64_t lLastTimeStamp;
  50. task_cycle_info_agent_t tList;
  51. uint32_t wMagicWord;
  52. } ;
  53. /*============================ TYPES =========================================*/
  54. /*
  55. * Task control block. A task control block (TCB) is allocated for each task,
  56. * and stores task state information, including a pointer to the task's context
  57. * (the task's run time environment, including register values)
  58. */
  59. typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
  60. {
  61. volatile StackType_t *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
  62. #if ( portUSING_MPU_WRAPPERS == 1 )
  63. xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
  64. #endif
  65. ListItem_t xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
  66. ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
  67. UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
  68. StackType_t *pxStack; /*< Points to the start of the stack. */
  69. char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  70. #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
  71. StackType_t *pxEndOfStack; /*< Points to the highest valid address for the stack. */
  72. #endif
  73. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  74. UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
  75. #endif
  76. #if ( configUSE_TRACE_FACILITY == 1 )
  77. UBaseType_t uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
  78. UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
  79. #endif
  80. #if ( configUSE_MUTEXES == 1 )
  81. UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
  82. UBaseType_t uxMutexesHeld;
  83. #endif
  84. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  85. TaskHookFunction_t pxTaskTag;
  86. #endif
  87. #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
  88. void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
  89. #endif
  90. #if( configGENERATE_RUN_TIME_STATS == 1 )
  91. uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
  92. #endif
  93. #if ( configUSE_NEWLIB_REENTRANT == 1 )
  94. /* Allocate a Newlib reent structure that is specific to this task.
  95. Note Newlib support has been included by popular demand, but is not
  96. used by the FreeRTOS maintainers themselves. FreeRTOS is not
  97. responsible for resulting newlib operation. User must be familiar with
  98. newlib and must provide system-wide implementations of the necessary
  99. stubs. Be warned that (at the time of writing) the current newlib design
  100. implements a system-wide malloc() that must be provided with locks.
  101. See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
  102. for additional information. */
  103. struct _reent xNewLib_reent;
  104. #endif
  105. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  106. volatile uint32_t ulNotifiedValue;
  107. volatile uint8_t ucNotifyState;
  108. #endif
  109. /* See the comments in FreeRTOS.h with the definition of
  110. tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
  111. #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
  112. uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
  113. #endif
  114. #if( INCLUDE_xTaskAbortDelay == 1 )
  115. uint8_t ucDelayAborted;
  116. #endif
  117. #if( configUSE_POSIX_ERRNO == 1 )
  118. int iTaskErrno;
  119. #endif
  120. } tskTCB;
  121. /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
  122. below to enable the use of older kernel aware debuggers. */
  123. typedef tskTCB TCB_t;
  124. /*lint -save -e956 A manual analysis and inspection has been used to determine
  125. which static variables must be declared volatile. */
  126. PRIVILEGED_DATA
  127. extern TCB_t * volatile pxCurrentTCB;
  128. /*! \note if you aren't using perf_counter inside KEIL with RTE, please create
  129. *! a header file called "Pre_Include_Global.h", copy the following
  130. *! content into the header file and and put following option
  131. *! to your command line (supposing you are using arm compiler 6):
  132. *! -include "Pre_Include_Global.h"
  133. */
  134. /*
  135. //! \brief Enable RTOS Patch for perf_counter
  136. #define __PERF_CNT_USE_RTOS__
  137. #define traceTASK_SWITCHED_OUT_DISABLE
  138. #define traceTASK_SWITCHED_IN_DISABLE
  139. extern void __freertos_evr_on_task_switched_out (void *ptTCB);
  140. extern void __freertos_evr_on_task_switched_in(void *ptTCB, unsigned int uxTopPriority) ;
  141. # define traceTASK_SWITCHED_OUT() \
  142. __freertos_evr_on_task_switched_out(pxCurrentTCB)
  143. # define traceTASK_SWITCHED_IN() \
  144. __freertos_evr_on_task_switched_in(pxCurrentTCB, uxTopReadyPriority)
  145. */
  146. /*============================ GLOBAL VARIABLES ==============================*/
  147. /*============================ LOCAL VARIABLES ===============================*/
  148. /*============================ PROTOTYPES ====================================*/
  149. extern void __on_context_switch_in(uint32_t *pwStack);
  150. extern void __on_context_switch_out(uint32_t *pwStack);
  151. /*============================ IMPLEMENTATION ================================*/
  152. #if defined(RTE_Compiler_EventRecorder)
  153. # include "EventRecorder.h"
  154. #endif
  155. #define EvtFreeRTOSTasksNo (0xF0U)
  156. #define EvtFreeRTOSTasks_TaskSwitchedOut \
  157. EventID(EventLevelOp, EvtFreeRTOSTasksNo, 0x0BU)
  158. #define EvtFreeRTOSTasks_TaskSwitchedIn \
  159. EventID(EventLevelOp, EvtFreeRTOSTasksNo, 0x0CU)
  160. void __freertos_evr_on_task_switched_out (void *ptTCB) {
  161. #if defined(RTE_Compiler_EventRecorder)
  162. EventRecord2(EvtFreeRTOSTasks_TaskSwitchedOut, (uint32_t)ptTCB, 0U);
  163. #else
  164. (void)pxCurrentTCB;
  165. #endif
  166. __on_context_switch_out(((TCB_t *)ptTCB)->pxStack);
  167. }
  168. void __freertos_evr_on_task_switched_in(void *ptTCB, uint32_t uxTopPriority) {
  169. #if defined(RTE_Compiler_EventRecorder)
  170. EventRecord2(EvtFreeRTOSTasks_TaskSwitchedIn, (uint32_t)ptTCB, uxTopPriority);
  171. #else
  172. (void)pxCurrentTCB;
  173. (void)uxTopPriority;
  174. #endif
  175. __on_context_switch_in(((TCB_t *)ptTCB)->pxStack);
  176. }
  177. task_cycle_info_t * get_rtos_task_cycle_info(void)
  178. {
  179. return &(((struct __task_cycle_info_t *)pxCurrentTCB->pxStack)->tInfo);
  180. }