portmacro.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * FreeRTOS Kernel V10.4.3 LTS Patch 2
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */
  26. #ifndef PORTMACRO_H
  27. #define PORTMACRO_H
  28. /* *INDENT-OFF* */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* *INDENT-ON* */
  33. /*-----------------------------------------------------------
  34. * Port specific definitions.
  35. *
  36. * The settings in this file configure FreeRTOS correctly for the
  37. * given hardware and compiler.
  38. *
  39. * These settings should not be altered.
  40. *-----------------------------------------------------------
  41. */
  42. /* Type definitions. */
  43. #define portCHAR char
  44. #define portFLOAT float
  45. #define portDOUBLE double
  46. #define portLONG long
  47. #define portSHORT short
  48. #define portSTACK_TYPE uint32_t
  49. #define portBASE_TYPE long
  50. typedef portSTACK_TYPE StackType_t;
  51. typedef long BaseType_t;
  52. typedef unsigned long UBaseType_t;
  53. #if ( configUSE_16_BIT_TICKS == 1 )
  54. typedef uint16_t TickType_t;
  55. #define portMAX_DELAY ( TickType_t ) 0xffff
  56. #else
  57. typedef uint32_t TickType_t;
  58. #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
  59. /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
  60. * not need to be guarded with a critical section. */
  61. #define portTICK_TYPE_IS_ATOMIC 1
  62. #endif
  63. /*-----------------------------------------------------------*/
  64. /* MPU specific constants. */
  65. #define portUSING_MPU_WRAPPERS 1
  66. #define portPRIVILEGE_BIT ( 0x80000000UL )
  67. #define portMPU_REGION_READ_WRITE ( 0x03UL << 24UL )
  68. #define portMPU_REGION_PRIVILEGED_READ_ONLY ( 0x05UL << 24UL )
  69. #define portMPU_REGION_READ_ONLY ( 0x06UL << 24UL )
  70. #define portMPU_REGION_PRIVILEGED_READ_WRITE ( 0x01UL << 24UL )
  71. #define portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ( 0x02UL << 24UL )
  72. #define portMPU_REGION_CACHEABLE_BUFFERABLE ( 0x07UL << 16UL )
  73. #define portMPU_REGION_EXECUTE_NEVER ( 0x01UL << 28UL )
  74. /* Location of the TEX,S,C,B bits in the MPU Region Attribute and Size
  75. * Register (RASR). */
  76. #define portMPU_RASR_TEX_S_C_B_LOCATION ( 16UL )
  77. #define portMPU_RASR_TEX_S_C_B_MASK ( 0x3FUL )
  78. /* MPU settings that can be overriden in FreeRTOSConfig.h. */
  79. #ifndef configTOTAL_MPU_REGIONS
  80. /* Define to 8 for backward compatibility. */
  81. #define configTOTAL_MPU_REGIONS ( 8UL )
  82. #endif
  83. /*
  84. * The TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits define the
  85. * memory type, and where necessary the cacheable and shareable properties
  86. * of the memory region.
  87. *
  88. * The TEX, C, and B bits together indicate the memory type of the region,
  89. * and:
  90. * - For Normal memory, the cacheable properties of the region.
  91. * - For Device memory, whether the region is shareable.
  92. *
  93. * For Normal memory regions, the S bit indicates whether the region is
  94. * shareable. For Strongly-ordered and Device memory, the S bit is ignored.
  95. *
  96. * See the following two tables for setting TEX, S, C and B bits for
  97. * unprivileged flash, privileged flash and privileged RAM regions.
  98. *
  99. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  100. | TEX | C | B | Memory type | Description or Normal region cacheability | Shareable? |
  101. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  102. | 000 | 0 | 0 | Strongly-ordered | Strongly ordered | Shareable |
  103. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  104. | 000 | 0 | 1 | Device | Shared device | Shareable |
  105. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  106. | 000 | 1 | 0 | Normal | Outer and inner write-through; no write allocate | S bit |
  107. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  108. | 000 | 1 | 1 | Normal | Outer and inner write-back; no write allocate | S bit |
  109. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  110. | 001 | 0 | 0 | Normal | Outer and inner Non-cacheable | S bit |
  111. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  112. | 001 | 0 | 1 | Reserved | Reserved | Reserved |
  113. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  114. | 001 | 1 | 0 | IMPLEMENTATION DEFINED | IMPLEMENTATION DEFINED | IMPLEMENTATION DEFINED |
  115. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  116. | 001 | 1 | 1 | Normal | Outer and inner write-back; write and read allocate | S bit |
  117. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  118. | 010 | 0 | 0 | Device | Non-shared device | Not shareable |
  119. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  120. | 010 | 0 | 1 | Reserved | Reserved | Reserved |
  121. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  122. | 010 | 1 | X | Reserved | Reserved | Reserved |
  123. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  124. | 011 | X | X | Reserved | Reserved | Reserved |
  125. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  126. | 1BB | A | A | Normal | Cached memory, with AA and BB indicating the inner and | Reserved |
  127. | | | | | outer cacheability rules that must be exported on the | |
  128. | | | | | bus. See the table below for the cacheability policy | |
  129. | | | | | encoding. memory, BB=Outer policy, AA=Inner policy. | |
  130. +-----+---+---+------------------------+--------------------------------------------------------+-------------------------+
  131. |
  132. +-----------------------------------------+----------------------------------------+
  133. | AA or BB subfield of {TEX,C,B} encoding | Cacheability policy |
  134. +-----------------------------------------+----------------------------------------+
  135. | 00 | Non-cacheable |
  136. +-----------------------------------------+----------------------------------------+
  137. | 01 | Write-back, write and read allocate |
  138. +-----------------------------------------+----------------------------------------+
  139. | 10 | Write-through, no write allocate |
  140. +-----------------------------------------+----------------------------------------+
  141. | 11 | Write-back, no write allocate |
  142. +-----------------------------------------+----------------------------------------+
  143. */
  144. /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for flash
  145. * region. */
  146. #ifndef configTEX_S_C_B_FLASH
  147. /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */
  148. #define configTEX_S_C_B_FLASH ( 0x07UL )
  149. #endif
  150. /* TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits for RAM
  151. * region. */
  152. #ifndef configTEX_S_C_B_SRAM
  153. /* Default to TEX=000, S=1, C=1, B=1 for backward compatibility. */
  154. #define configTEX_S_C_B_SRAM ( 0x07UL )
  155. #endif
  156. #define portUNPRIVILEGED_FLASH_REGION ( 0UL )
  157. #define portPRIVILEGED_FLASH_REGION ( 1UL )
  158. #define portPRIVILEGED_RAM_REGION ( 2UL )
  159. #define portGENERAL_PERIPHERALS_REGION ( 3UL )
  160. #define portSTACK_REGION ( 4UL )
  161. #define portFIRST_CONFIGURABLE_REGION ( 5UL )
  162. #define portTOTAL_NUM_REGIONS ( configTOTAL_MPU_REGIONS )
  163. #define portNUM_CONFIGURABLE_REGIONS ( portTOTAL_NUM_REGIONS - portFIRST_CONFIGURABLE_REGION )
  164. #define portLAST_CONFIGURABLE_REGION ( portTOTAL_NUM_REGIONS - 1 )
  165. #define portSWITCH_TO_USER_MODE() __asm volatile ( " mrs r0, control \n orr r0, #1 \n msr control, r0 " ::: "r0", "memory" )
  166. typedef struct MPU_REGION_REGISTERS
  167. {
  168. uint32_t ulRegionBaseAddress;
  169. uint32_t ulRegionAttribute;
  170. } xMPU_REGION_REGISTERS;
  171. /* Plus 1 to create space for the stack region. */
  172. typedef struct MPU_SETTINGS
  173. {
  174. xMPU_REGION_REGISTERS xRegion[ portTOTAL_NUM_REGIONS ];
  175. } xMPU_SETTINGS;
  176. /* Architecture specifics. */
  177. #define portSTACK_GROWTH ( -1 )
  178. #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
  179. #define portBYTE_ALIGNMENT 8
  180. #define portDONT_DISCARD __attribute__( ( used ) )
  181. /*-----------------------------------------------------------*/
  182. /* SVC numbers for various services. */
  183. #define portSVC_START_SCHEDULER 0
  184. #define portSVC_YIELD 1
  185. #define portSVC_RAISE_PRIVILEGE 2
  186. /* Scheduler utilities. */
  187. #define portYIELD() __asm volatile ( " SVC %0 \n"::"i" ( portSVC_YIELD ) : "memory" )
  188. #define portYIELD_WITHIN_API() \
  189. { \
  190. /* Set a PendSV to request a context switch. */ \
  191. portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \
  192. \
  193. /* Barriers are normally not required but do ensure the code is completely \
  194. * within the specified behaviour for the architecture. */ \
  195. __asm volatile ( "dsb" ::: "memory" ); \
  196. __asm volatile ( "isb" ); \
  197. }
  198. #define portNVIC_INT_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
  199. #define portNVIC_PENDSVSET_BIT ( 1UL << 28UL )
  200. #define portEND_SWITCHING_ISR( xSwitchRequired ) if( xSwitchRequired ) portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT
  201. #define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
  202. /*-----------------------------------------------------------*/
  203. /* Critical section management. */
  204. extern void vPortEnterCritical( void );
  205. extern void vPortExitCritical( void );
  206. #define portSET_INTERRUPT_MASK_FROM_ISR() ulPortRaiseBASEPRI()
  207. #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) vPortSetBASEPRI( x )
  208. #define portDISABLE_INTERRUPTS() vPortRaiseBASEPRI()
  209. #define portENABLE_INTERRUPTS() vPortSetBASEPRI( 0 )
  210. #define portENTER_CRITICAL() vPortEnterCritical()
  211. #define portEXIT_CRITICAL() vPortExitCritical()
  212. /*-----------------------------------------------------------*/
  213. /* Task function macros as described on the FreeRTOS.org WEB site. These are
  214. * not necessary for to use this port. They are defined so the common demo files
  215. * (which build with all the ports) will build. */
  216. #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void * pvParameters )
  217. #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void * pvParameters )
  218. /*-----------------------------------------------------------*/
  219. /* Architecture specific optimisations. */
  220. #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
  221. #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
  222. #endif
  223. #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
  224. /* Generic helper function. */
  225. __attribute__( ( always_inline ) ) static inline uint8_t ucPortCountLeadingZeros( uint32_t ulBitmap )
  226. {
  227. uint8_t ucReturn;
  228. __asm volatile ( "clz %0, %1" : "=r" ( ucReturn ) : "r" ( ulBitmap ) : "memory" );
  229. return ucReturn;
  230. }
  231. /* Check the configuration. */
  232. #if ( configMAX_PRIORITIES > 32 )
  233. #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
  234. #endif
  235. /* Store/clear the ready priorities in a bit map. */
  236. #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
  237. #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
  238. /*-----------------------------------------------------------*/
  239. #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) uxTopPriority = ( 31UL - ( uint32_t ) ucPortCountLeadingZeros( ( uxReadyPriorities ) ) )
  240. #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  241. /*-----------------------------------------------------------*/
  242. #ifdef configASSERT
  243. void vPortValidateInterruptPriority( void );
  244. #define portASSERT_IF_INTERRUPT_PRIORITY_INVALID() vPortValidateInterruptPriority()
  245. #endif
  246. /* portNOP() is not required by this port. */
  247. #define portNOP()
  248. #define portINLINE __inline
  249. #ifndef portFORCE_INLINE
  250. #define portFORCE_INLINE inline __attribute__( ( always_inline ) )
  251. #endif
  252. /*-----------------------------------------------------------*/
  253. extern BaseType_t xIsPrivileged( void );
  254. extern void vResetPrivilege( void );
  255. /**
  256. * @brief Checks whether or not the processor is privileged.
  257. *
  258. * @return 1 if the processor is already privileged, 0 otherwise.
  259. */
  260. #define portIS_PRIVILEGED() xIsPrivileged()
  261. /**
  262. * @brief Raise an SVC request to raise privilege.
  263. */
  264. #define portRAISE_PRIVILEGE() __asm volatile ( "svc %0 \n" ::"i" ( portSVC_RAISE_PRIVILEGE ) : "memory" );
  265. /**
  266. * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
  267. * register.
  268. */
  269. #define portRESET_PRIVILEGE() vResetPrivilege()
  270. /*-----------------------------------------------------------*/
  271. portFORCE_INLINE static BaseType_t xPortIsInsideInterrupt( void )
  272. {
  273. uint32_t ulCurrentInterrupt;
  274. BaseType_t xReturn;
  275. /* Obtain the number of the currently executing interrupt. */
  276. __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
  277. if( ulCurrentInterrupt == 0 )
  278. {
  279. xReturn = pdFALSE;
  280. }
  281. else
  282. {
  283. xReturn = pdTRUE;
  284. }
  285. return xReturn;
  286. }
  287. /*-----------------------------------------------------------*/
  288. portFORCE_INLINE static void vPortRaiseBASEPRI( void )
  289. {
  290. uint32_t ulNewBASEPRI;
  291. __asm volatile
  292. (
  293. " mov %0, %1 \n"\
  294. " msr basepri, %0 \n"\
  295. " isb \n"\
  296. " dsb \n"\
  297. : "=r" ( ulNewBASEPRI ) : "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory"
  298. );
  299. }
  300. /*-----------------------------------------------------------*/
  301. portFORCE_INLINE static uint32_t ulPortRaiseBASEPRI( void )
  302. {
  303. uint32_t ulOriginalBASEPRI, ulNewBASEPRI;
  304. __asm volatile
  305. (
  306. " mrs %0, basepri \n"\
  307. " mov %1, %2 \n"\
  308. " msr basepri, %1 \n"\
  309. " isb \n"\
  310. " dsb \n"\
  311. : "=r" ( ulOriginalBASEPRI ), "=r" ( ulNewBASEPRI ) : "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory"
  312. );
  313. /* This return will not be reached but is necessary to prevent compiler
  314. * warnings. */
  315. return ulOriginalBASEPRI;
  316. }
  317. /*-----------------------------------------------------------*/
  318. portFORCE_INLINE static void vPortSetBASEPRI( uint32_t ulNewMaskValue )
  319. {
  320. __asm volatile
  321. (
  322. " msr basepri, %0 "::"r" ( ulNewMaskValue ) : "memory"
  323. );
  324. }
  325. /*-----------------------------------------------------------*/
  326. #define portMEMORY_BARRIER() __asm volatile ( "" ::: "memory" )
  327. #ifndef configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY
  328. #warning "configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY is not defined. We recommend defining it to 1 in FreeRTOSConfig.h for better security. https://www.FreeRTOS.org/FreeRTOS-V10.3.x.html"
  329. #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY 0
  330. #endif
  331. /*-----------------------------------------------------------*/
  332. /* *INDENT-OFF* */
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. /* *INDENT-ON* */
  337. #endif /* PORTMACRO_H */