portmacro.h 13 KB

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