11/***************************************************************************
2- * Copyright (c) 2024 Microsoft Corporation
3- *
2+ * Copyright (c) 2025 10xEngineers
3+ *
44 * This program and the accompanying materials are made available under the
55 * terms of the MIT License which is available at
66 * https://opensource.org/licenses/MIT.
7- *
7+ *
88 * SPDX-License-Identifier: MIT
99 **************************************************************************/
1010
1111
1212/**************************************************************************/
1313/**************************************************************************/
14- /** */
14+ /** */
1515/** ThreadX Component */
1616/** */
1717/** Port Specific */
2020/**************************************************************************/
2121
2222
23- /**************************************************************************/
24- /* */
25- /* PORT SPECIFIC C INFORMATION RELEASE */
26- /* */
23+ /**************************************************************************/
24+ /* */
25+ /* PORT SPECIFIC C INFORMATION RELEASE */
26+ /* */
2727/* tx_port.h RISC-V32/GNU */
28- /* 6.x */
28+ /* 6.4.x */
29+ /* */
30+ /* AUTHOR */
31+ /* */
32+ /* Akif Ejaz, 10xEngineers */
33+ /* */
34+ /* DESCRIPTION */
35+ /* */
36+ /* This file contains data type definitions that make the ThreadX */
37+ /* real-time kernel function identically on a variety of different */
38+ /* processor architectures. For example, the size or number of bits */
39+ /* in an "int" data type vary between microprocessor architectures and */
40+ /* even C compilers for the same microprocessor. ThreadX does not */
41+ /* directly use native C data types. Instead, ThreadX creates its */
42+ /* own special types that can be mapped to actual data types by this */
43+ /* file to guarantee consistency in the interface and functionality. */
44+ /* */
45+ /* RELEASE HISTORY */
2946/* */
30- /* AUTHOR */
31- /* */
32- /* Wei-Chen Lai, National Cheng Kung University, Taiwan */
33- /* */
34- /* DESCRIPTION */
35- /* */
36- /* This file contains data type definitions that make the ThreadX */
37- /* real-time kernel function identically on a variety of different */
38- /* processor architectures. For example, the size or number of bits */
39- /* in an "int" data type vary between microprocessor architectures and */
40- /* even C compilers for the same microprocessor. ThreadX does not */
41- /* directly use native C data types. Instead, ThreadX creates its */
42- /* own special types that can be mapped to actual data types by this */
43- /* file to guarantee consistency in the interface and functionality. */
44- /* */
45- /* */
46- /* RELEASE HISTORY */
47- /* */
48- /* DATE NAME DESCRIPTION */
49- /* */
50- /* xx-xx-xxxx Wei-Chen Lai Initial Version 6.x */
51- /* */
52- /**************************************************************************/
47+ /* DATE NAME DESCRIPTION */
48+ /* */
49+ /* 23-12-2025 Akif Ejaz Initial Version 6.4.x */
50+ /* */
51+ /**************************************************************************/
5352
5453#ifndef TX_PORT_H
5554#define TX_PORT_H
5655
56+ #ifdef __ASSEMBLER__
5757
58- /* Include prototypes for memset. */
5958
59+ #if __riscv_xlen == 64
60+ # define SLL32 sllw
61+ # define STORE sd
62+ # define LOAD ld
63+ # define LWU lwu
64+ # define LOG_REGBYTES 3
65+ #else
66+ # define SLL32 sll
67+ # define STORE sw
68+ # define LOAD lw
69+ # define LWU lw
70+ # define LOG_REGBYTES 2
71+ #endif
72+ #define REGBYTES (1 << LOG_REGBYTES)
73+
74+ #else /*not __ASSEMBLER__ */
75+
76+ /* Include for memset. */
6077#include <string.h>
6178
6279
6582#ifdef TX_INCLUDE_USER_DEFINE_FILE
6683
6784
68- /* Yes, include the user defines in tx_user.h. The defines in this file may
85+ /* Yes, include the user defines in tx_user.h. The defines in this file may
6986 alternately be defined on the command line. */
7087
7188#include "tx_user.h"
7592/* Define compiler library include files. */
7693
7794
78- /* Define ThreadX basic types for this port. */
95+ /* Define ThreadX basic types for this port. */
7996
8097#define VOID void
8198typedef char CHAR ;
@@ -84,8 +101,14 @@ typedef int INT;
84101typedef unsigned int UINT ;
85102typedef long LONG ;
86103typedef unsigned long ULONG ;
104+ typedef unsigned long long ULONG64 ;
87105typedef short SHORT ;
88106typedef unsigned short USHORT ;
107+ #define ULONG64_DEFINED
108+ #define ALIGN_TYPE_DEFINED
109+ #define ALIGN_TYPE ULONG64
110+
111+
89112
90113
91114/* Define the priority levels for ThreadX. Legal values range
@@ -100,7 +123,7 @@ typedef unsigned short USHORT;
100123 thread creation is less than this value, the thread create call will return an error. */
101124
102125#ifndef TX_MINIMUM_STACK
103- #define TX_MINIMUM_STACK 512 /* Minimum stack size for this port */
126+ #define TX_MINIMUM_STACK 1024 /* Minimum stack size for this port */
104127#endif
105128
106129
@@ -111,19 +134,19 @@ typedef unsigned short USHORT;
111134#define TX_TIMER_THREAD_STACK_SIZE 1024 /* Default timer thread stack size */
112135#endif
113136
114- #ifndef TX_TIMER_THREAD_PRIORITY
115- #define TX_TIMER_THREAD_PRIORITY 0 /* Default timer thread priority */
137+ #ifndef TX_TIMER_THREAD_PRIORITY
138+ #define TX_TIMER_THREAD_PRIORITY 0 /* Default timer thread priority */
116139#endif
117140
118141
119- /* Define various constants for the ThreadX RISC-V port. */
142+ /* Define various constants for the ThreadX RISC-V port. */
120143
121144#define TX_INT_DISABLE 0x00000000 /* Disable interrupts value */
122145#define TX_INT_ENABLE 0x00000008 /* Enable interrupt value */
123146
124147
125- /* Define the clock source for trace event entry time stamp. The following two item are port specific.
126- For example, if the time source is at the address 0x0a800024 and is 16-bits in size, the clock
148+ /* Define the clock source for trace event entry time stamp. The following two item are port specific.
149+ For example, if the time source is at the address 0x0a800024 and is 16-bits in size, the clock
127150 source constants would be:
128151
129152#define TX_TRACE_TIME_SOURCE *((ULONG *) 0x0a800024)
@@ -152,7 +175,7 @@ typedef unsigned short USHORT;
152175#define TX_INLINE_INITIALIZATION
153176
154177
155- /* Determine whether or not stack checking is enabled. By default, ThreadX stack checking is
178+ /* Determine whether or not stack checking is enabled. By default, ThreadX stack checking is
156179 disabled. When the following is defined, ThreadX thread stack checking is enabled. If stack
157180 checking is enabled (TX_ENABLE_STACK_CHECKING is defined), the TX_DISABLE_STACK_FILLING
158181 define is negated, thereby forcing the stack fill which is necessary for the stack checking
@@ -164,13 +187,13 @@ typedef unsigned short USHORT;
164187
165188
166189/* Define the TX_THREAD control block extensions for this port. The main reason
167- for the multiple macros is so that backward compatibility can be maintained with
190+ for the multiple macros is so that backward compatibility can be maintained with
168191 existing ThreadX kernel awareness modules. */
169192
170- #define TX_THREAD_EXTENSION_0
171- #define TX_THREAD_EXTENSION_1
172- #define TX_THREAD_EXTENSION_2
173- #define TX_THREAD_EXTENSION_3
193+ #define TX_THREAD_EXTENSION_0
194+ #define TX_THREAD_EXTENSION_1
195+ #define TX_THREAD_EXTENSION_2
196+ #define TX_THREAD_EXTENSION_3
174197
175198
176199/* Define the port extensions of the remaining ThreadX objects. */
@@ -184,11 +207,11 @@ typedef unsigned short USHORT;
184207#define TX_TIMER_EXTENSION
185208
186209
187- /* Define the user extension field of the thread control block. Nothing
210+ /* Define the user extension field of the thread control block. Nothing
188211 additional is needed for this port so it is defined as white space. */
189212
190213#ifndef TX_THREAD_USER_EXTENSION
191- #define TX_THREAD_USER_EXTENSION
214+ #define TX_THREAD_USER_EXTENSION
192215#endif
193216
194217
@@ -223,42 +246,30 @@ typedef unsigned short USHORT;
223246#define TX_TIMER_DELETE_EXTENSION (timer_ptr )
224247
225248
226- /* Define ThreadX interrupt lockout and restore macros for protection on
227- access of critical kernel information. The restore interrupt macro must
228- restore the interrupt posture of the running thread prior to the value
249+ /* Define ThreadX interrupt lockout and restore macros for protection on
250+ access of critical kernel information. The restore interrupt macro must
251+ restore the interrupt posture of the running thread prior to the value
229252 present prior to the disable macro. In most cases, the save area macro
230253 is used to define a local function save area for the disable and restore
231254 macros. */
232255
233256#ifdef TX_DISABLE_INLINE
234257
235- unsigned int _tx_thread_interrupt_control (unsigned int new_posture );
258+ ULONG64 _tx_thread_interrupt_control (unsigned int new_posture );
236259
237- #define TX_INTERRUPT_SAVE_AREA register INT interrupt_save;
260+ #define TX_INTERRUPT_SAVE_AREA register ULONG64 interrupt_save;
238261
239262#define TX_DISABLE interrupt_save = _tx_thread_interrupt_control(TX_INT_DISABLE);
240263#define TX_RESTORE _tx_thread_interrupt_control(interrupt_save);
241264
242265#else
243266
244- #define TX_INTERRUPT_SAVE_AREA ULONG interrupt_save;
245-
246- #define TX_DISABLE \
247- __asm__ volatile ( \
248- "csrr %0, mstatus\n\t" \
249- "csrci mstatus, 8" \
250- : "=r" (interrupt_save) \
251- : \
252- : "memory" \
253- );
254-
255- #define TX_RESTORE \
256- __asm__ volatile ( \
257- "csrw mstatus, %0\n\t" \
258- : \
259- : "r" (interrupt_save) \
260- : "memory" \
261- );
267+ #define TX_INTERRUPT_SAVE_AREA ULONG64 interrupt_save;
268+ /* Atomically read mstatus into interrupt_save and clear bit 3 of mstatus. */
269+ #define TX_DISABLE {__asm__ ("csrrci %0, mstatus, 0x08" : "=r" (interrupt_save) : );};
270+ /* We only care about mstatus.mie (bit 3), so mask interrupt_save and write to mstatus. */
271+ #define TX_RESTORE {register ULONG64 __tempmask = interrupt_save & 0x08; \
272+ __asm__ ("csrrs x0, mstatus, %0 \n\t" : : "r" (__tempmask) : );};
262273
263274#endif
264275
@@ -276,10 +287,11 @@ unsigned int _tx_thread_interrupt_control(uns
276287/* Define the version ID of ThreadX. This may be utilized by the application. */
277288
278289#ifdef TX_THREAD_INIT
279- CHAR _tx_version_id [] =
280- "Copyright (c) 2024 Microsoft Corporation. * ThreadX RISC-V32/GNU Version G6 .4.2 *" ;
290+ CHAR _tx_version_id [] =
291+ "Copyright (c) 2024 Microsoft Corporation. * ThreadX RISC-V32/GNU Version 6 .4.2 *" ;
281292#else
282293extern CHAR _tx_version_id [];
283294#endif
284295
296+ #endif /*not __ASSEMBLER__ */
285297#endif
0 commit comments