portmacro.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifndef PORTMACRO_H
  29. #define PORTMACRO_H
  30. /* *INDENT-OFF* */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* *INDENT-ON* */
  35. /*-----------------------------------------------------------
  36. * Port specific definitions.
  37. *
  38. * The settings in this file configure FreeRTOS correctly for the
  39. * given hardware and compiler.
  40. *
  41. * These settings should not be altered.
  42. *-----------------------------------------------------------
  43. */
  44. /* Type definitions. */
  45. #define portCHAR char
  46. #define portFLOAT float
  47. #define portDOUBLE double
  48. #define portLONG long
  49. #define portSHORT short
  50. #define portSTACK_TYPE uint32_t
  51. #define portBASE_TYPE portLONG
  52. typedef portSTACK_TYPE StackType_t;
  53. typedef long BaseType_t;
  54. typedef unsigned long UBaseType_t;
  55. #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
  56. typedef uint16_t TickType_t;
  57. #define portMAX_DELAY ( TickType_t ) 0xffff
  58. #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
  59. typedef uint32_t TickType_t;
  60. #define portMAX_DELAY ( TickType_t ) ( 0xFFFFFFFFUL )
  61. #else
  62. #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
  63. #endif
  64. /*-----------------------------------------------------------*/
  65. /* Architecture specifics. */
  66. #define portSTACK_GROWTH ( -1 )
  67. #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
  68. #define portBYTE_ALIGNMENT 8
  69. #define portNOP() __asm volatile ( "NOP" );
  70. /*-----------------------------------------------------------*/
  71. /* Scheduler utilities. */
  72. /*
  73. * portRESTORE_CONTEXT, portRESTORE_CONTEXT, portENTER_SWITCHING_ISR
  74. * and portEXIT_SWITCHING_ISR can only be called from ARM mode, but
  75. * are included here for efficiency. An attempt to call one from
  76. * THUMB mode code will result in a compile time error.
  77. */
  78. #define portRESTORE_CONTEXT() \
  79. { \
  80. extern volatile void * volatile pxCurrentTCB; \
  81. extern volatile uint32_t ulCriticalNesting; \
  82. \
  83. /* Set the LR to the task stack. */ \
  84. __asm volatile ( \
  85. "LDR R0, =pxCurrentTCB \n\t" \
  86. "LDR R0, [R0] \n\t" \
  87. "LDR LR, [R0] \n\t" \
  88. \
  89. /* The critical nesting depth is the first item on the stack. */ \
  90. /* Load it into the ulCriticalNesting variable. */ \
  91. "LDR R0, =ulCriticalNesting \n\t" \
  92. "LDMFD LR!, {R1} \n\t" \
  93. "STR R1, [R0] \n\t" \
  94. \
  95. /* Get the SPSR from the stack. */ \
  96. "LDMFD LR!, {R0} \n\t" \
  97. "MSR SPSR, R0 \n\t" \
  98. \
  99. /* Restore all system mode registers for the task. */ \
  100. "LDMFD LR, {R0-R14}^ \n\t" \
  101. "NOP \n\t" \
  102. \
  103. /* Restore the return address. */ \
  104. "LDR LR, [LR, #+60] \n\t" \
  105. \
  106. /* And return - correcting the offset in the LR to obtain the */ \
  107. /* correct address. */ \
  108. "SUBS PC, LR, #4 \n\t" \
  109. ); \
  110. ( void ) ulCriticalNesting; \
  111. ( void ) pxCurrentTCB; \
  112. }
  113. /*-----------------------------------------------------------*/
  114. #define portSAVE_CONTEXT() \
  115. { \
  116. extern volatile void * volatile pxCurrentTCB; \
  117. extern volatile uint32_t ulCriticalNesting; \
  118. \
  119. /* Push R0 as we are going to use the register. */ \
  120. __asm volatile ( \
  121. "STMDB SP!, {R0} \n\t" \
  122. \
  123. /* Set R0 to point to the task stack pointer. */ \
  124. "STMDB SP,{SP}^ \n\t" \
  125. "NOP \n\t" \
  126. "SUB SP, SP, #4 \n\t" \
  127. "LDMIA SP!,{R0} \n\t" \
  128. \
  129. /* Push the return address onto the stack. */ \
  130. "STMDB R0!, {LR} \n\t" \
  131. \
  132. /* Now we have saved LR we can use it instead of R0. */ \
  133. "MOV LR, R0 \n\t" \
  134. \
  135. /* Pop R0 so we can save it onto the system mode stack. */ \
  136. "LDMIA SP!, {R0} \n\t" \
  137. \
  138. /* Push all the system mode registers onto the task stack. */ \
  139. "STMDB LR,{R0-LR}^ \n\t" \
  140. "NOP \n\t" \
  141. "SUB LR, LR, #60 \n\t" \
  142. \
  143. /* Push the SPSR onto the task stack. */ \
  144. "MRS R0, SPSR \n\t" \
  145. "STMDB LR!, {R0} \n\t" \
  146. \
  147. "LDR R0, =ulCriticalNesting \n\t" \
  148. "LDR R0, [R0] \n\t" \
  149. "STMDB LR!, {R0} \n\t" \
  150. \
  151. /* Store the new top of stack for the task. */ \
  152. "LDR R0, =pxCurrentTCB \n\t" \
  153. "LDR R0, [R0] \n\t" \
  154. "STR LR, [R0] \n\t" \
  155. ); \
  156. ( void ) ulCriticalNesting; \
  157. ( void ) pxCurrentTCB; \
  158. }
  159. extern void vTaskSwitchContext( void );
  160. #define portYIELD_FROM_ISR() vTaskSwitchContext()
  161. #define portYIELD() __asm volatile ( "SWI 0" )
  162. /*-----------------------------------------------------------*/
  163. /* Critical section management. */
  164. /*
  165. * The interrupt management utilities can only be called from ARM mode. When
  166. * THUMB_INTERWORK is defined the utilities are defined as functions in
  167. * portISR.c to ensure a switch to ARM mode. When THUMB_INTERWORK is not
  168. * defined then the utilities are defined as macros here - as per other ports.
  169. */
  170. #ifdef THUMB_INTERWORK
  171. extern void vPortDisableInterruptsFromThumb( void ) __attribute__( ( naked ) );
  172. extern void vPortEnableInterruptsFromThumb( void ) __attribute__( ( naked ) );
  173. #define portDISABLE_INTERRUPTS() vPortDisableInterruptsFromThumb()
  174. #define portENABLE_INTERRUPTS() vPortEnableInterruptsFromThumb()
  175. #else
  176. #define portDISABLE_INTERRUPTS() \
  177. __asm volatile ( \
  178. "STMDB SP!, {R0} \n\t" /* Push R0. */ \
  179. "MRS R0, CPSR \n\t" /* Get CPSR. */ \
  180. "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */ \
  181. "MSR CPSR, R0 \n\t" /* Write back modified value. */ \
  182. "LDMIA SP!, {R0} " ) /* Pop R0. */
  183. #define portENABLE_INTERRUPTS() \
  184. __asm volatile ( \
  185. "STMDB SP!, {R0} \n\t" /* Push R0. */ \
  186. "MRS R0, CPSR \n\t" /* Get CPSR. */ \
  187. "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */ \
  188. "MSR CPSR, R0 \n\t" /* Write back modified value. */ \
  189. "LDMIA SP!, {R0} " ) /* Pop R0. */
  190. #endif /* THUMB_INTERWORK */
  191. extern void vPortEnterCritical( void );
  192. extern void vPortExitCritical( void );
  193. #define portENTER_CRITICAL() vPortEnterCritical();
  194. #define portEXIT_CRITICAL() vPortExitCritical();
  195. /*-----------------------------------------------------------*/
  196. /* Task function macros as described on the FreeRTOS.org WEB site. */
  197. #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void * pvParameters )
  198. #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void * pvParameters )
  199. /* *INDENT-OFF* */
  200. #ifdef __cplusplus
  201. }
  202. #endif
  203. /* *INDENT-ON* */
  204. #endif /* PORTMACRO_H */