portable.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*-----------------------------------------------------------
  29. * Portable layer API. Each function must be defined for each port.
  30. *----------------------------------------------------------*/
  31. #ifndef PORTABLE_H
  32. #define PORTABLE_H
  33. /* Each FreeRTOS port has a unique portmacro.h header file. Originally a
  34. * pre-processor definition was used to ensure the pre-processor found the correct
  35. * portmacro.h file for the port being used. That scheme was deprecated in favour
  36. * of setting the compiler's include path such that it found the correct
  37. * portmacro.h file - removing the need for the constant and allowing the
  38. * portmacro.h file to be located anywhere in relation to the port being used.
  39. * Purely for reasons of backward compatibility the old method is still valid, but
  40. * to make it clear that new projects should not use it, support for the port
  41. * specific constants has been moved into the deprecated_definitions.h header
  42. * file. */
  43. #include "deprecated_definitions.h"
  44. /* If portENTER_CRITICAL is not defined then including deprecated_definitions.h
  45. * did not result in a portmacro.h header file being included - and it should be
  46. * included here. In this case the path to the correct portmacro.h header file
  47. * must be set in the compiler's include path. */
  48. #ifndef portENTER_CRITICAL
  49. #include "portmacro.h"
  50. #endif
  51. #if portBYTE_ALIGNMENT == 32
  52. #define portBYTE_ALIGNMENT_MASK ( 0x001f )
  53. #elif portBYTE_ALIGNMENT == 16
  54. #define portBYTE_ALIGNMENT_MASK ( 0x000f )
  55. #elif portBYTE_ALIGNMENT == 8
  56. #define portBYTE_ALIGNMENT_MASK ( 0x0007 )
  57. #elif portBYTE_ALIGNMENT == 4
  58. #define portBYTE_ALIGNMENT_MASK ( 0x0003 )
  59. #elif portBYTE_ALIGNMENT == 2
  60. #define portBYTE_ALIGNMENT_MASK ( 0x0001 )
  61. #elif portBYTE_ALIGNMENT == 1
  62. #define portBYTE_ALIGNMENT_MASK ( 0x0000 )
  63. #else /* if portBYTE_ALIGNMENT == 32 */
  64. #error "Invalid portBYTE_ALIGNMENT definition"
  65. #endif /* if portBYTE_ALIGNMENT == 32 */
  66. #ifndef portUSING_MPU_WRAPPERS
  67. #define portUSING_MPU_WRAPPERS 0
  68. #endif
  69. #ifndef portNUM_CONFIGURABLE_REGIONS
  70. #define portNUM_CONFIGURABLE_REGIONS 1
  71. #endif
  72. #ifndef portHAS_STACK_OVERFLOW_CHECKING
  73. #define portHAS_STACK_OVERFLOW_CHECKING 0
  74. #endif
  75. #ifndef portARCH_NAME
  76. #define portARCH_NAME NULL
  77. #endif
  78. #ifndef portBASE_TYPE_ENTER_CRITICAL
  79. #define portBASE_TYPE_ENTER_CRITICAL() taskENTER_CRITICAL()
  80. #endif
  81. #ifndef portBASE_TYPE_EXIT_CRITICAL
  82. #define portBASE_TYPE_EXIT_CRITICAL() taskEXIT_CRITICAL()
  83. #endif
  84. #ifndef configSTACK_DEPTH_TYPE
  85. #define configSTACK_DEPTH_TYPE StackType_t
  86. #endif
  87. #ifndef configSTACK_ALLOCATION_FROM_SEPARATE_HEAP
  88. /* Defaults to 0 for backward compatibility. */
  89. #define configSTACK_ALLOCATION_FROM_SEPARATE_HEAP 0
  90. #endif
  91. #include "mpu_wrappers.h"
  92. /* *INDENT-OFF* */
  93. #ifdef __cplusplus
  94. extern "C" {
  95. #endif
  96. /* *INDENT-ON* */
  97. /*
  98. * Setup the stack of a new task so it is ready to be placed under the
  99. * scheduler control. The registers have to be placed on the stack in
  100. * the order that the port expects to find them.
  101. *
  102. */
  103. #if ( portUSING_MPU_WRAPPERS == 1 )
  104. #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
  105. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  106. StackType_t * pxEndOfStack,
  107. TaskFunction_t pxCode,
  108. void * pvParameters,
  109. BaseType_t xRunPrivileged,
  110. xMPU_SETTINGS * xMPUSettings ) PRIVILEGED_FUNCTION;
  111. #else
  112. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  113. TaskFunction_t pxCode,
  114. void * pvParameters,
  115. BaseType_t xRunPrivileged,
  116. xMPU_SETTINGS * xMPUSettings ) PRIVILEGED_FUNCTION;
  117. #endif /* if ( portHAS_STACK_OVERFLOW_CHECKING == 1 ) */
  118. #else /* if ( portUSING_MPU_WRAPPERS == 1 ) */
  119. #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
  120. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  121. StackType_t * pxEndOfStack,
  122. TaskFunction_t pxCode,
  123. void * pvParameters ) PRIVILEGED_FUNCTION;
  124. #else
  125. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  126. TaskFunction_t pxCode,
  127. void * pvParameters ) PRIVILEGED_FUNCTION;
  128. #endif
  129. #endif /* if ( portUSING_MPU_WRAPPERS == 1 ) */
  130. /* Used by heap_5.c to define the start address and size of each memory region
  131. * that together comprise the total FreeRTOS heap space. */
  132. typedef struct HeapRegion
  133. {
  134. uint8_t * pucStartAddress;
  135. size_t xSizeInBytes;
  136. } HeapRegion_t;
  137. /* Used to pass information about the heap out of vPortGetHeapStats(). */
  138. typedef struct xHeapStats
  139. {
  140. size_t xAvailableHeapSpaceInBytes; /* The total heap size currently available - this is the sum of all the free blocks, not the largest block that can be allocated. */
  141. size_t xSizeOfLargestFreeBlockInBytes; /* The maximum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */
  142. size_t xSizeOfSmallestFreeBlockInBytes; /* The minimum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */
  143. size_t xNumberOfFreeBlocks; /* The number of free memory blocks within the heap at the time vPortGetHeapStats() is called. */
  144. size_t xMinimumEverFreeBytesRemaining; /* The minimum amount of total free memory (sum of all free blocks) there has been in the heap since the system booted. */
  145. size_t xNumberOfSuccessfulAllocations; /* The number of calls to pvPortMalloc() that have returned a valid memory block. */
  146. size_t xNumberOfSuccessfulFrees; /* The number of calls to vPortFree() that has successfully freed a block of memory. */
  147. } HeapStats_t;
  148. /*
  149. * Used to define multiple heap regions for use by heap_5.c. This function
  150. * must be called before any calls to pvPortMalloc() - not creating a task,
  151. * queue, semaphore, mutex, software timer, event group, etc. will result in
  152. * pvPortMalloc being called.
  153. *
  154. * pxHeapRegions passes in an array of HeapRegion_t structures - each of which
  155. * defines a region of memory that can be used as the heap. The array is
  156. * terminated by a HeapRegions_t structure that has a size of 0. The region
  157. * with the lowest start address must appear first in the array.
  158. */
  159. void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION;
  160. /*
  161. * Returns a HeapStats_t structure filled with information about the current
  162. * heap state.
  163. */
  164. void vPortGetHeapStats( HeapStats_t * pxHeapStats );
  165. /*
  166. * Map to the memory management routines required for the port.
  167. */
  168. void * pvPortMalloc( size_t xWantedSize ) PRIVILEGED_FUNCTION;
  169. void * pvPortCalloc( size_t xNum,
  170. size_t xSize ) PRIVILEGED_FUNCTION;
  171. void vPortFree( void * pv ) PRIVILEGED_FUNCTION;
  172. void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION;
  173. size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION;
  174. size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION;
  175. void xPortResetHeapMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION;
  176. #if ( configSTACK_ALLOCATION_FROM_SEPARATE_HEAP == 1 )
  177. void * pvPortMallocStack( size_t xSize ) PRIVILEGED_FUNCTION;
  178. void vPortFreeStack( void * pv ) PRIVILEGED_FUNCTION;
  179. #else
  180. #define pvPortMallocStack pvPortMalloc
  181. #define vPortFreeStack vPortFree
  182. #endif
  183. /*
  184. * This function resets the internal state of the heap module. It must be called
  185. * by the application before restarting the scheduler.
  186. */
  187. void vPortHeapResetState( void ) PRIVILEGED_FUNCTION;
  188. #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
  189. /**
  190. * task.h
  191. * @code{c}
  192. * void vApplicationMallocFailedHook( void )
  193. * @endcode
  194. *
  195. * This hook function is called when allocation failed.
  196. */
  197. void vApplicationMallocFailedHook( void );
  198. #endif
  199. /*
  200. * Setup the hardware ready for the scheduler to take control. This generally
  201. * sets up a tick interrupt and sets timers for the correct tick frequency.
  202. */
  203. BaseType_t xPortStartScheduler( void ) PRIVILEGED_FUNCTION;
  204. /*
  205. * Undo any hardware/ISR setup that was performed by xPortStartScheduler() so
  206. * the hardware is left in its original condition after the scheduler stops
  207. * executing.
  208. */
  209. void vPortEndScheduler( void ) PRIVILEGED_FUNCTION;
  210. /*
  211. * The structures and methods of manipulating the MPU are contained within the
  212. * port layer.
  213. *
  214. * Fills the xMPUSettings structure with the memory region information
  215. * contained in xRegions.
  216. */
  217. #if ( portUSING_MPU_WRAPPERS == 1 )
  218. struct xMEMORY_REGION;
  219. void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
  220. const struct xMEMORY_REGION * const xRegions,
  221. StackType_t * pxBottomOfStack,
  222. configSTACK_DEPTH_TYPE uxStackDepth ) PRIVILEGED_FUNCTION;
  223. #endif
  224. /**
  225. * @brief Checks if the calling task is authorized to access the given buffer.
  226. *
  227. * @param pvBuffer The buffer which the calling task wants to access.
  228. * @param ulBufferLength The length of the pvBuffer.
  229. * @param ulAccessRequested The permissions that the calling task wants.
  230. *
  231. * @return pdTRUE if the calling task is authorized to access the buffer,
  232. * pdFALSE otherwise.
  233. */
  234. #if ( portUSING_MPU_WRAPPERS == 1 )
  235. BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
  236. uint32_t ulBufferLength,
  237. uint32_t ulAccessRequested ) PRIVILEGED_FUNCTION;
  238. #endif
  239. /**
  240. * @brief Checks if the calling task is authorized to access the given kernel object.
  241. *
  242. * @param lInternalIndexOfKernelObject The index of the kernel object in the kernel
  243. * object handle pool.
  244. *
  245. * @return pdTRUE if the calling task is authorized to access the kernel object,
  246. * pdFALSE otherwise.
  247. */
  248. #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
  249. BaseType_t xPortIsAuthorizedToAccessKernelObject( int32_t lInternalIndexOfKernelObject ) PRIVILEGED_FUNCTION;
  250. #endif
  251. /* *INDENT-OFF* */
  252. #ifdef __cplusplus
  253. }
  254. #endif
  255. /* *INDENT-ON* */
  256. #endif /* PORTABLE_H */