portmacro.h 8.4 KB

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