tx_port.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**************************************************************************/
  2. /* */
  3. /* Copyright (c) Microsoft Corporation. All rights reserved. */
  4. /* */
  5. /* This software is licensed under the Microsoft Software License */
  6. /* Terms for Microsoft Azure RTOS. Full text of the license can be */
  7. /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
  8. /* and in the root directory of this software. */
  9. /* */
  10. /**************************************************************************/
  11. /**************************************************************************/
  12. /**************************************************************************/
  13. /** */
  14. /** ThreadX Component */
  15. /** */
  16. /** Port Specific */
  17. /** */
  18. /**************************************************************************/
  19. /**************************************************************************/
  20. /**************************************************************************/
  21. /* */
  22. /* PORT SPECIFIC C INFORMATION RELEASE */
  23. /* */
  24. /* tx_port.h RISC-V64/GNU */
  25. /* 6.2.1 */
  26. /* */
  27. /* AUTHOR */
  28. /* */
  29. /* Scott Larson, Microsoft Corporation */
  30. /* */
  31. /* DESCRIPTION */
  32. /* */
  33. /* This file contains data type definitions that make the ThreadX */
  34. /* real-time kernel function identically on a variety of different */
  35. /* processor architectures. For example, the size or number of bits */
  36. /* in an "int" data type vary between microprocessor architectures and */
  37. /* even C compilers for the same microprocessor. ThreadX does not */
  38. /* directly use native C data types. Instead, ThreadX creates its */
  39. /* own special types that can be mapped to actual data types by this */
  40. /* file to guarantee consistency in the interface and functionality. */
  41. /* */
  42. /* RELEASE HISTORY */
  43. /* */
  44. /* DATE NAME DESCRIPTION */
  45. /* */
  46. /* 03-08-2023 Scott Larson Initial Version 6.2.1 */
  47. /* */
  48. /**************************************************************************/
  49. #ifndef TX_PORT_H
  50. #define TX_PORT_H
  51. #include <stdint.h>
  52. #include <string.h>
  53. #ifndef TXM_MODULE
  54. #include <nuclei_sdk_soc.h>
  55. #endif
  56. /* Determine if the optional ThreadX user define file should be used. */
  57. #ifdef TX_INCLUDE_USER_DEFINE_FILE
  58. /* Yes, include the user defines in tx_user.h. The defines in this file may
  59. alternately be defined on the command line. */
  60. #include "tx_user.h"
  61. #endif
  62. /* Define compiler library include files. */
  63. /* Define ThreadX basic types for this port. */
  64. #define VOID void
  65. typedef char CHAR;
  66. typedef unsigned char UCHAR;
  67. typedef int INT;
  68. typedef unsigned int UINT;
  69. typedef long LONG;
  70. typedef unsigned long ULONG;
  71. typedef unsigned long long ULONG64;
  72. typedef short SHORT;
  73. typedef unsigned short USHORT;
  74. #define ULONG64_DEFINED
  75. #define ALIGN_TYPE_DEFINED
  76. #define ALIGN_TYPE ULONG
  77. /* Define the priority levels for ThreadX. Legal values range
  78. from 32 to 1024 and MUST be evenly divisible by 32. */
  79. #ifndef TX_MAX_PRIORITIES
  80. #define TX_MAX_PRIORITIES 32
  81. #endif
  82. /* Define the minimum stack for a ThreadX thread on this processor. If the size supplied during
  83. thread creation is less than this value, the thread create call will return an error. */
  84. #ifndef TX_MINIMUM_STACK
  85. #define TX_MINIMUM_STACK 1024 /* Minimum stack size for this port */
  86. #endif
  87. /* Define the max heap size allocated for ThreadX in bytes, change it to bigger or smaller according to your environment */
  88. // This can be used in your application code as memory pool size
  89. #ifndef TX_HEAP_SIZE
  90. #define TX_HEAP_SIZE 10240 /* Max heap size in bytes allocated for threadx */
  91. #endif
  92. /* Define the system timer thread's default stack size and priority. These are only applicable
  93. if TX_TIMER_PROCESS_IN_ISR is not defined. */
  94. #ifndef TX_TIMER_THREAD_STACK_SIZE
  95. #define TX_TIMER_THREAD_STACK_SIZE 1024 /* Default timer thread stack size */
  96. #endif
  97. #ifndef TX_TIMER_THREAD_PRIORITY
  98. #define TX_TIMER_THREAD_PRIORITY 0 /* Default timer thread priority */
  99. #endif
  100. /* Define various constants for the ThreadX RISC-V port. */
  101. #define TX_INT_DISABLE 0x00000000 /* Disable interrupts value */
  102. #define TX_INT_ENABLE 0x00000008 /* Enable interrupt value */
  103. /* Define the clock source for trace event entry time stamp. The following two item are port specific.
  104. For example, if the time source is at the address 0x0a800024 and is 16-bits in size, the clock
  105. source constants would be:
  106. #define TX_TRACE_TIME_SOURCE *((ULONG *) 0x0a800024)
  107. #define TX_TRACE_TIME_MASK 0x0000FFFFUL
  108. */
  109. #ifndef TX_TRACE_TIME_SOURCE
  110. #define TX_TRACE_TIME_SOURCE ++_tx_trace_simulated_time
  111. #endif
  112. #ifndef TX_TRACE_TIME_MASK
  113. #define TX_TRACE_TIME_MASK 0xFFFFFFFFUL
  114. #endif
  115. /* Define the port specific options for the _tx_build_options variable. This variable indicates
  116. how the ThreadX library was built. */
  117. #define TX_PORT_SPECIFIC_BUILD_OPTIONS 0
  118. /* Define the in-line initialization constant so that modules with in-line
  119. initialization capabilities can prevent their initialization from being
  120. a function call. */
  121. #define TX_INLINE_INITIALIZATION
  122. /* Determine whether or not stack checking is enabled. By default, ThreadX stack checking is
  123. disabled. When the following is defined, ThreadX thread stack checking is enabled. If stack
  124. checking is enabled (TX_ENABLE_STACK_CHECKING is defined), the TX_DISABLE_STACK_FILLING
  125. define is negated, thereby forcing the stack fill which is necessary for the stack checking
  126. logic. */
  127. #ifdef TX_ENABLE_STACK_CHECKING
  128. #undef TX_DISABLE_STACK_FILLING
  129. #endif
  130. /* Define the TX_THREAD control block extensions for this port. The main reason
  131. for the multiple macros is so that backward compatibility can be maintained with
  132. existing ThreadX kernel awareness modules. */
  133. #define TX_THREAD_EXTENSION_0
  134. #define TX_THREAD_EXTENSION_1
  135. #ifdef TX_ENABLE_IAR_LIBRARY_SUPPORT
  136. #define TX_THREAD_EXTENSION_2 VOID *tx_thread_module_instance_ptr; \
  137. VOID *tx_thread_module_entry_info_ptr; \
  138. ULONG tx_thread_module_current_user_mode; \
  139. ULONG tx_thread_module_user_mode; \
  140. ULONG tx_thread_module_saved_lr; \
  141. VOID *tx_thread_module_kernel_stack_start; \
  142. VOID *tx_thread_module_kernel_stack_end; \
  143. ULONG tx_thread_module_kernel_stack_size; \
  144. VOID *tx_thread_module_stack_ptr; \
  145. VOID *tx_thread_module_stack_start; \
  146. VOID *tx_thread_module_stack_end; \
  147. ULONG tx_thread_module_stack_size; \
  148. VOID *tx_thread_module_reserved; \
  149. VOID *tx_thread_iar_tls_pointer;
  150. #else
  151. #define TX_THREAD_EXTENSION_2 VOID *tx_thread_module_instance_ptr; \
  152. VOID *tx_thread_module_entry_info_ptr; \
  153. ULONG tx_thread_module_current_user_mode; \
  154. ULONG tx_thread_module_user_mode; \
  155. ULONG tx_thread_module_saved_lr; \
  156. VOID *tx_thread_module_kernel_stack_start; \
  157. VOID *tx_thread_module_kernel_stack_end; \
  158. ULONG tx_thread_module_kernel_stack_size; \
  159. VOID *tx_thread_module_stack_ptr; \
  160. VOID *tx_thread_module_stack_start; \
  161. VOID *tx_thread_module_stack_end; \
  162. ULONG tx_thread_module_stack_size; \
  163. VOID *tx_thread_module_reserved;
  164. #endif
  165. #ifndef TX_ENABLE_EXECUTION_CHANGE_NOTIFY
  166. #define TX_THREAD_EXTENSION_3
  167. #else
  168. #define TX_THREAD_EXTENSION_3 unsigned long long tx_thread_execution_time_total; \
  169. unsigned long long tx_thread_execution_time_last_start;
  170. #endif
  171. /* Define the port extensions of the remaining ThreadX objects. */
  172. #define TX_BLOCK_POOL_EXTENSION
  173. #define TX_BYTE_POOL_EXTENSION
  174. #define TX_MUTEX_EXTENSION
  175. #define TX_EVENT_FLAGS_GROUP_EXTENSION VOID *tx_event_flags_group_module_instance; \
  176. VOID (*tx_event_flags_group_set_module_notify)(struct TX_EVENT_FLAGS_GROUP_STRUCT *group_ptr);
  177. #define TX_QUEUE_EXTENSION VOID *tx_queue_module_instance; \
  178. VOID (*tx_queue_send_module_notify)(struct TX_QUEUE_STRUCT *queue_ptr);
  179. #define TX_SEMAPHORE_EXTENSION VOID *tx_semaphore_module_instance; \
  180. VOID (*tx_semaphore_put_module_notify)(struct TX_SEMAPHORE_STRUCT *semaphore_ptr);
  181. #define TX_TIMER_EXTENSION VOID *tx_timer_module_instance; \
  182. VOID (*tx_timer_module_expiration_function)(ULONG id);
  183. /* Define the user extension field of the thread control block. Nothing
  184. additional is needed for this port so it is defined as white space. */
  185. #ifndef TX_THREAD_USER_EXTENSION
  186. #define TX_THREAD_USER_EXTENSION
  187. #endif
  188. /* Define the macros for processing extensions in tx_thread_create, tx_thread_delete,
  189. tx_thread_shell_entry, and tx_thread_terminate. */
  190. #define TX_THREAD_CREATE_EXTENSION(thread_ptr)
  191. #define TX_THREAD_DELETE_EXTENSION(thread_ptr)
  192. #define TX_THREAD_COMPLETED_EXTENSION(thread_ptr)
  193. #define TX_THREAD_TERMINATED_EXTENSION(thread_ptr)
  194. /* Define the ThreadX object creation extensions for the remaining objects. */
  195. #define TX_BLOCK_POOL_CREATE_EXTENSION(pool_ptr)
  196. #define TX_BYTE_POOL_CREATE_EXTENSION(pool_ptr)
  197. #define TX_EVENT_FLAGS_GROUP_CREATE_EXTENSION(group_ptr)
  198. #define TX_MUTEX_CREATE_EXTENSION(mutex_ptr)
  199. #define TX_QUEUE_CREATE_EXTENSION(queue_ptr)
  200. #define TX_SEMAPHORE_CREATE_EXTENSION(semaphore_ptr)
  201. #define TX_TIMER_CREATE_EXTENSION(timer_ptr)
  202. /* Define the ThreadX object deletion extensions for the remaining objects. */
  203. #define TX_BLOCK_POOL_DELETE_EXTENSION(pool_ptr)
  204. #define TX_BYTE_POOL_DELETE_EXTENSION(pool_ptr)
  205. #define TX_EVENT_FLAGS_GROUP_DELETE_EXTENSION(group_ptr)
  206. #define TX_MUTEX_DELETE_EXTENSION(mutex_ptr)
  207. #define TX_QUEUE_DELETE_EXTENSION(queue_ptr)
  208. #define TX_SEMAPHORE_DELETE_EXTENSION(semaphore_ptr)
  209. #define TX_TIMER_DELETE_EXTENSION(timer_ptr)
  210. /* Define ThreadX interrupt lockout and restore macros for protection on
  211. access of critical kernel information. The restore interrupt macro must
  212. restore the interrupt posture of the running thread prior to the value
  213. present prior to the disable macro. In most cases, the save area macro
  214. is used to define a local function save area for the disable and restore
  215. macros. */
  216. #ifdef TX_DISABLE_INLINE
  217. #define TX_INTERRUPT_SAVE_AREA register ULONG interrupt_save;
  218. #define TX_DISABLE interrupt_save = _tx_thread_interrupt_control(TX_INT_DISABLE);
  219. #define TX_RESTORE _tx_thread_interrupt_control(interrupt_save);
  220. #else
  221. #define TX_INTERRUPT_SAVE_AREA ULONG interrupt_save;
  222. /* Atomically read mstatus into interrupt_save and clear bit 3 of mstatus. */
  223. #define TX_DISABLE {__asm__ ("csrrci %0, mstatus, 0x08" : "=r" (interrupt_save) : );};
  224. /* We only care about mstatus.mie (bit 3), so mask interrupt_save and write to mstatus. */
  225. #define TX_RESTORE {register ULONG __tempmask = interrupt_save & 0x08; \
  226. __asm__ ("csrrs x0, mstatus, %0 \n\t" : : "r" (__tempmask) : );};
  227. #endif
  228. #ifndef TXM_MODULE
  229. static inline void _tx_thread_system_return(void)
  230. {
  231. /* Set a software interrupt(SWI) request to request a context switch. */
  232. SysTimer_SetSWIRQ();
  233. /* Barriers are normally not required but do ensure the code is completely
  234. within the specified behaviour for the architecture. */
  235. __RWMB();
  236. }
  237. #endif
  238. #define THREAD_INITIAL_MSTATUS (MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
  239. struct thread_stack_frame {
  240. unsigned long epc; /* epc - epc - program counter */
  241. unsigned long ra; /* x1 - ra - return address for jumps */
  242. unsigned long t0; /* x5 - t0 - temporary register 0 */
  243. unsigned long t1; /* x6 - t1 - temporary register 1 */
  244. unsigned long t2; /* x7 - t2 - temporary register 2 */
  245. unsigned long s0_fp; /* x8 - s0/fp - saved register 0 or frame pointer */
  246. unsigned long s1; /* x9 - s1 - saved register 1 */
  247. unsigned long a0; /* x10 - a0 - return value or function argument 0 */
  248. unsigned long a1; /* x11 - a1 - return value or function argument 1 */
  249. unsigned long a2; /* x12 - a2 - function argument 2 */
  250. unsigned long a3; /* x13 - a3 - function argument 3 */
  251. unsigned long a4; /* x14 - a4 - function argument 4 */
  252. unsigned long a5; /* x15 - a5 - function argument 5 */
  253. #ifndef __riscv_32e
  254. unsigned long a6; /* x16 - a6 - function argument 6 */
  255. unsigned long a7; /* x17 - s7 - function argument 7 */
  256. unsigned long s2; /* x18 - s2 - saved register 2 */
  257. unsigned long s3; /* x19 - s3 - saved register 3 */
  258. unsigned long s4; /* x20 - s4 - saved register 4 */
  259. unsigned long s5; /* x21 - s5 - saved register 5 */
  260. unsigned long s6; /* x22 - s6 - saved register 6 */
  261. unsigned long s7; /* x23 - s7 - saved register 7 */
  262. unsigned long s8; /* x24 - s8 - saved register 8 */
  263. unsigned long s9; /* x25 - s9 - saved register 9 */
  264. unsigned long s10; /* x26 - s10 - saved register 10 */
  265. unsigned long s11; /* x27 - s11 - saved register 11 */
  266. unsigned long t3; /* x28 - t3 - temporary register 3 */
  267. unsigned long t4; /* x29 - t4 - temporary register 4 */
  268. unsigned long t5; /* x30 - t5 - temporary register 5 */
  269. unsigned long t6; /* x31 - t6 - temporary register 6 */
  270. unsigned long rsv0; /* reserved 0 - reserved to make space */
  271. unsigned long rsv1; /* reserved 1 - reserved to make space */
  272. #endif
  273. unsigned long mstatus; /* - machine status register */
  274. };
  275. /* Define the interrupt lockout macros for each ThreadX object. */
  276. #define TX_BLOCK_POOL_DISABLE TX_DISABLE
  277. #define TX_BYTE_POOL_DISABLE TX_DISABLE
  278. #define TX_EVENT_FLAGS_GROUP_DISABLE TX_DISABLE
  279. #define TX_MUTEX_DISABLE TX_DISABLE
  280. #define TX_QUEUE_DISABLE TX_DISABLE
  281. #define TX_SEMAPHORE_DISABLE TX_DISABLE
  282. /* Define the version ID of ThreadX. This may be utilized by the application. */
  283. #ifdef TX_THREAD_INIT
  284. CHAR _tx_version_id[] =
  285. "Copyright (c) Microsoft Corporation. All rights reserved. * ThreadX Nuclei RISC-V Version 6.4.0 *";
  286. #else
  287. extern CHAR _tx_version_id[];
  288. #endif
  289. #endif