1/***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12/**************************************************************************/
13/**************************************************************************/
14/**                                                                       */
15/** ThreadX Component                                                     */
16/**                                                                       */
17/**   Initialize                                                          */
18/**                                                                       */
19/**************************************************************************/
20/**************************************************************************/
21
22
23/* #define TX_SOURCE_CODE  */
24
25
26/* Include necessary system files.  */
27
28/*  #include "tx_api.h"
29    #include "tx_initialize.h"
30    #include "tx_thread.h"
31    #include "tx_timer.h"  */
32
33    EXTERN      _tx_thread_system_stack_ptr
34    EXTERN      _tx_initialize_unused_memory
35    EXTERN      _tx_thread_context_save
36    EXTERN      _tx_thread_context_restore
37    EXTERN      _tx_timer_interrupt
38
39    RSEG    FREE_MEM:DATA
40    PUBLIC  __tx_free_memory_start
41__tx_free_memory_start:
42    DS32    4
43
44
45    SECTION `.text`:CODE:REORDER:NOROOT(2)
46    CODE
47/**************************************************************************/
48/*                                                                        */
49/*  FUNCTION                                               RELEASE        */
50/*                                                                        */
51/*    _tx_initialize_low_level                           RISC-V32/IAR     */
52/*                                                           6.1          */
53/*  AUTHOR                                                                */
54/*                                                                        */
55/*    William E. Lamie, Microsoft Corporation                             */
56/*    Tom van Leeuwen, Technolution B.V.                                  */
57/*                                                                        */
58/*  DESCRIPTION                                                           */
59/*                                                                        */
60/*    This function is responsible for any low-level processor            */
61/*    initialization, including setting up interrupt vectors, setting     */
62/*    up a periodic timer interrupt source, saving the system stack       */
63/*    pointer for use in ISR processing later, and finding the first      */
64/*    available RAM memory address for tx_application_define.             */
65/*                                                                        */
66/*  INPUT                                                                 */
67/*                                                                        */
68/*    None                                                                */
69/*                                                                        */
70/*  OUTPUT                                                                */
71/*                                                                        */
72/*    None                                                                */
73/*                                                                        */
74/*  CALLS                                                                 */
75/*                                                                        */
76/*    None                                                                */
77/*                                                                        */
78/*  CALLED BY                                                             */
79/*                                                                        */
80/*    _tx_initialize_kernel_enter           ThreadX entry function        */
81/*                                                                        */
82/*  RELEASE HISTORY                                                       */
83/*                                                                        */
84/*    DATE              NAME                      DESCRIPTION             */
85/*                                                                        */
86/*  09-30-2020     William E. Lamie         Initial Version 6.1           */
87/*                                                                        */
88/**************************************************************************/
89/* VOID   _tx_initialize_low_level(VOID)
90{  */
91    PUBLIC  _tx_initialize_low_level
92_tx_initialize_low_level:
93    sw      sp, _tx_thread_system_stack_ptr, t0     ; Save system stack pointer
94
95    la      t0, __tx_free_memory_start              ; Pickup first free address
96    sw      t0, _tx_initialize_unused_memory, t1    ; Save unused memory address
97
98    ret
99
100
101    /* Define the actual timer interrupt/exception handler.  */
102
103    PUBLIC  _tx_timer_interrupt_handler
104    PUBLIC  __minterrupt_000007
105    EXTWEAK __require_minterrupt_vector_table
106_tx_timer_interrupt_handler:
107__minterrupt_000007:
108    REQUIRE __require_minterrupt_vector_table
109
110
111    /* Before calling _tx_thread_context_save, we have to allocate an interrupt
112       stack frame and save the current value of x1 (ra). */
113#if __iar_riscv_base_isa == rv32e
114    addi    sp, sp, -260                            ; Allocate space for all registers - with floating point enabled
115#else
116    addi    sp, sp, -128                            ; Allocate space for all registers - without floating point enabled
117#endif
118    sw      x1, 0x70(sp)                            ; Store RA
119    call    _tx_thread_context_save                 ; Call ThreadX context save
120
121    /* Call the ThreadX timer routine.  */
122    call    _tx_timer_interrupt                     ; Call timer interrupt handler
123
124    /* Timer interrupt processing is done, jump to ThreadX context restore.  */
125    j       _tx_thread_context_restore              ; Jump to ThreadX context restore function. Note: this does not return!
126
127
128    END
129