portmacro.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 uint8_t
  51. #define portBASE_TYPE char
  52. typedef portSTACK_TYPE StackType_t;
  53. typedef signed char BaseType_t;
  54. typedef unsigned char 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. /* Hardware specifics. */
  66. #define portBYTE_ALIGNMENT 1
  67. #define portSTACK_GROWTH ( -1 )
  68. #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
  69. #define portYIELD() __asm( "swi" );
  70. /*-----------------------------------------------------------*/
  71. /* Critical section handling. */
  72. #define portENABLE_INTERRUPTS() __asm( "cli" )
  73. #define portDISABLE_INTERRUPTS() __asm( "sei" )
  74. /*
  75. * Disable interrupts before incrementing the count of critical section nesting.
  76. * The nesting count is maintained so we know when interrupts should be
  77. * re-enabled. Once interrupts are disabled the nesting count can be accessed
  78. * directly. Each task maintains its own nesting count.
  79. */
  80. #define portENTER_CRITICAL() \
  81. { \
  82. extern volatile UBaseType_t uxCriticalNesting; \
  83. \
  84. portDISABLE_INTERRUPTS(); \
  85. uxCriticalNesting++; \
  86. }
  87. /*
  88. * Interrupts are disabled so we can access the nesting count directly. If the
  89. * nesting is found to be 0 (no nesting) then we are leaving the critical
  90. * section and interrupts can be re-enabled.
  91. */
  92. #define portEXIT_CRITICAL() \
  93. { \
  94. extern volatile UBaseType_t uxCriticalNesting; \
  95. \
  96. uxCriticalNesting--; \
  97. if( uxCriticalNesting == 0 ) \
  98. { \
  99. portENABLE_INTERRUPTS(); \
  100. } \
  101. }
  102. /*-----------------------------------------------------------*/
  103. /* Task utilities. */
  104. /*
  105. * These macros are very simple as the processor automatically saves and
  106. * restores its registers as interrupts are entered and exited. In
  107. * addition to the (automatically stacked) registers we also stack the
  108. * critical nesting count. Each task maintains its own critical nesting
  109. * count as it is legitimate for a task to yield from within a critical
  110. * section. If the banked memory model is being used then the PPAGE
  111. * register is also stored as part of the tasks context.
  112. */
  113. #ifdef BANKED_MODEL
  114. /*
  115. * Load the stack pointer for the task, then pull the critical nesting
  116. * count and PPAGE register from the stack. The remains of the
  117. * context are restored by the RTI instruction.
  118. */
  119. #define portRESTORE_CONTEXT() \
  120. { \
  121. __asm( " \n\
  122. .globl pxCurrentTCB ; void * \n\
  123. .globl uxCriticalNesting ; char \n\
  124. \n\
  125. ldx pxCurrentTCB \n\
  126. lds 0,x ; Stack \n\
  127. \n\
  128. movb 1,sp+,uxCriticalNesting \n\
  129. movb 1,sp+,0x30 ; PPAGE \n\
  130. " ); \
  131. }
  132. /*
  133. * By the time this macro is called the processor has already stacked the
  134. * registers. Simply stack the nesting count and PPAGE value, then save
  135. * the task stack pointer.
  136. */
  137. #define portSAVE_CONTEXT() \
  138. { \
  139. __asm( " \n\
  140. .globl pxCurrentTCB ; void * \n\
  141. .globl uxCriticalNesting ; char \n\
  142. \n\
  143. movb 0x30, 1,-sp ; PPAGE \n\
  144. movb uxCriticalNesting, 1,-sp \n\
  145. \n\
  146. ldx pxCurrentTCB \n\
  147. sts 0,x ; Stack \n\
  148. " ); \
  149. }
  150. #else /* ifdef BANKED_MODEL */
  151. /*
  152. * These macros are as per the BANKED versions above, but without saving
  153. * and restoring the PPAGE register.
  154. */
  155. #define portRESTORE_CONTEXT() \
  156. { \
  157. __asm( " \n\
  158. .globl pxCurrentTCB ; void * \n\
  159. .globl uxCriticalNesting ; char \n\
  160. \n\
  161. ldx pxCurrentTCB \n\
  162. lds 0,x ; Stack \n\
  163. \n\
  164. movb 1,sp+,uxCriticalNesting \n\
  165. " ); \
  166. }
  167. #define portSAVE_CONTEXT() \
  168. { \
  169. __asm( " \n\
  170. .globl pxCurrentTCB ; void * \n\
  171. .globl uxCriticalNesting ; char \n\
  172. \n\
  173. movb uxCriticalNesting, 1,-sp \n\
  174. \n\
  175. ldx pxCurrentTCB \n\
  176. sts 0,x ; Stack \n\
  177. " ); \
  178. }
  179. #endif /* ifdef BANKED_MODEL */
  180. /*
  181. * Utility macros to save/restore correct software registers for GCC. This is
  182. * useful when GCC does not generate appropriate ISR head/tail code.
  183. */
  184. #define portISR_HEAD() \
  185. { \
  186. __asm( " \n\
  187. movw _.frame, 2,-sp \n\
  188. movw _.tmp, 2,-sp \n\
  189. movw _.z, 2,-sp \n\
  190. movw _.xy, 2,-sp \n\
  191. ;movw _.d2, 2,-sp \n\
  192. ;movw _.d1, 2,-sp \n\
  193. " ); \
  194. }
  195. #define portISR_TAIL() \
  196. { \
  197. __asm( " \n\
  198. movw 2,sp+, _.xy \n\
  199. movw 2,sp+, _.z \n\
  200. movw 2,sp+, _.tmp \n\
  201. movw 2,sp+, _.frame \n\
  202. ;movw 2,sp+, _.d1 \n\
  203. ;movw 2,sp+, _.d2 \n\
  204. rti \n\
  205. " ); \
  206. }
  207. /*
  208. * Utility macro to call macros above in correct order in order to perform a
  209. * task switch from within a standard ISR. This macro can only be used if
  210. * the ISR does not use any local (stack) variables. If the ISR uses stack
  211. * variables portYIELD() should be used in it's place.
  212. */
  213. #define portTASK_SWITCH_FROM_ISR() \
  214. portSAVE_CONTEXT(); \
  215. vTaskSwitchContext(); \
  216. portRESTORE_CONTEXT();
  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 */