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;/**************************************************************************/
15;/**                                                                       */
16;/** ThreadX Component                                                     */
17;/**                                                                       */
18;/**   Thread                                                              */
19;/**                                                                       */
20;/**************************************************************************/
21;/**************************************************************************/
22;
23;
24;#define TX_SOURCE_CODE
25;
26;
27;/* Include necessary system files.  */
28;
29;#include "tx_api.h"
30;#include "tx_thread.h"
31;#include "tx_timer.h"
32;
33;
34    .global     __tx_thread_execute_ptr
35    .global     __tx_thread_current_ptr
36    .global     __tx_timer_time_slice
37#if (TX_LOW_POWER == 1)
38    .global     _tx_low_power_enter
39    .global     _tx_low_power_exit
40    .global     __tx_thread_preempt_disable
41#endif
42;
43    .text
44
45;/**************************************************************************/
46;/*                                                                        */
47;/*  FUNCTION                                               RELEASE        */
48;/*                                                                        */
49;/*    _tx_thread_schedule                                  RXv2/GNURX     */
50;/*                                                           6.1.11       */
51;/*  AUTHOR                                                                */
52;/*                                                                        */
53;/*    William E. Lamie, Microsoft Corporation                             */
54;/*                                                                        */
55;/*  DESCRIPTION                                                           */
56;/*                                                                        */
57;/*    This function waits for a thread control block pointer to appear in */
58;/*    the _tx_thread_execute_ptr variable.  Once a thread pointer appears */
59;/*    in the variable, the corresponding thread is resumed.               */
60;/*                                                                        */
61;/*  INPUT                                                                 */
62;/*                                                                        */
63;/*    None                                                                */
64;/*                                                                        */
65;/*  OUTPUT                                                                */
66;/*                                                                        */
67;/*    None                                                                */
68;/*                                                                        */
69;/*  CALLS                                                                 */
70;/*                                                                        */
71;/*    None                                                                */
72;/*                                                                        */
73;/*  CALLED BY                                                             */
74;/*                                                                        */
75;/*    _tx_initialize_kernel_enter          ThreadX entry function         */
76;/*    _tx_thread_system_return             Return to system from thread   */
77;/*    _tx_thread_context_restore           Restore thread's context       */
78;/*                                                                        */
79;/*  RELEASE HISTORY                                                       */
80;/*                                                                        */
81;/*    DATE              NAME                      DESCRIPTION             */
82;/*                                                                        */
83;/*  12-30-2020     William E. Lamie         Initial Version 6.1.3         */
84;/*  10-15-2021     William E. Lamie         Modified comment(s), and      */
85;/*                                            removed unnecessary stack   */
86;/*                                            type checking,              */
87;/*                                            resulting in version 6.1.9  */
88;/*  01-31-2022     William E. Lamie         Modified comment(s),          */
89;/*                                            resulting in version 6.1.10 */
90;/*  04-25-2022     William E. Lamie         Modified comment(s), and      */
91;/*                                            added low power support,    */
92;/*                                            resulting in version 6.1.11 */
93;/*                                                                        */
94;/**************************************************************************/
95;VOID   _tx_thread_schedule(VOID)
96;{
97    .global __tx_thread_schedule
98__tx_thread_schedule:
99;
100;
101;    /* Wait for a thread to execute.  */
102;    do
103;    {
104    MOV.L    #__tx_thread_execute_ptr, R1        ; Address of thread to executer ptr
105__tx_thread_schedule_loop:
106    SETPSW I                                     ; Enable interrupts
107    CLRPSW I                                     ; Disable interrupts
108    MOV.L    [R1],R2                             ; Pickup next thread to execute
109    CMP      #0,R2                               ; Is it NULL?
110    BNE      __tx_thread_thread_ready            ; Not NULL, schedule the thread
111                                                 ; Idle system - no thread is ready
112#if (TX_LOW_POWER == 1)
113    MOV.L    #__tx_thread_preempt_disable, R1    ; Load prempt disable flag.
114    MOV.L    [R1], R2
115    ADD	     #1, R2                              ; Disable preemption while enter/exit
116    MOV.L    R2, [R1]
117    BSR      _tx_low_power_enter                 ; Possibly enter low power mode
118#endif
119
120#if (TX_ENABLE_WAIT == 1)
121    WAIT                                         ; Wait for interrupt
122#endif
123
124#if (TX_LOW_POWER == 1)
125    CLRPSW I                                     ; Disable interrupts (because WAIT enables interrupts)
126    BSR      _tx_low_power_exit                  ; Possibly exit low power mode
127    MOV.L    #__tx_thread_preempt_disable, R1    ; Load prempt disable flag.
128    MOV.L    [R1], R2
129    SUB	     #1, R2				 ; Enable preemption
130    MOV.L    R2, [R1]
131    MOV.L    #__tx_thread_execute_ptr, R1        ; Address of thread to executer ptr
132#endif
133
134    BRA      __tx_thread_schedule_loop           ; Idle system, keep checking
135
136__tx_thread_thread_ready:
137;
138;    }
139;    while(_tx_thread_execute_ptr == TX_NULL);
140;
141;    /* Yes! We have a thread to execute. Note that interrupts are locked out at this point.  */
142;
143;    /* Setup the current thread pointer.  */
144;    _tx_thread_current_ptr =  _tx_thread_execute_ptr;
145;
146    MOV.L    #__tx_thread_current_ptr, R3
147    MOV.L    R2,[R3]                             ; Setup current thread pointer
148;
149;    /* Increment the run count for this thread.  */
150;    _tx_thread_current_ptr -> tx_thread_run_count++;
151;
152    MOV.L    4[R2],R3                            ; Pickup run count
153    ADD      #1,R3                               ; Increment run counter
154    MOV.L    R3,4[R2]                            ; Store it back in control block
155;
156;    /* Setup time-slice, if present.  */
157;    _tx_timer_time_slice =  _tx_thread_current_ptr -> tx_thread_time_slice;
158;
159    MOV.L    24[R2],R3                           ; Pickup thread time-slice
160    MOV.L    #__tx_timer_time_slice,R4           ; Pickup pointer to time-slice
161    MOV.L    R3, [R4]                            ; Setup time-slice
162;
163;    /* Switch to the thread's stack.  */
164;    SP =  _tx_thread_execute_ptr -> tx_thread_stack_ptr;
165    SETPSW U                                     ; User stack mode
166    MOV.L   8[R2],R0                             ; Pickup stack pointer
167
168    POPM    R1-R3                                ; Restore accumulators.
169    MVTACLO R3, A0
170    MVTACHI R2, A0
171    MVTACGU R1, A0
172    POPM    R1-R3
173    MVTACLO R3, A1
174    MVTACHI R2, A1
175    MVTACGU R1, A1
176
177    POPM   R6-R13                                ; Recover interrupt stack frame
178    POPC   FPSW
179    POPM   R14-R15
180    POPM   R3-R5
181    POPM   R1-R2
182    RTE                                          ; Return to point of interrupt, this restores PC and PSW
183
184;}
185
186
187.global __tx_thread_context_save
188.global __tx_thread_context_restore
189
190
191; Software triggered interrupt used to perform context switches.
192; The priority of this interrupt is set to the lowest priority within
193; tx_initialize_low_level() and triggered by ThreadX when calling
194; _tx_thread_system_return().
195.global $tableentry$27$.rvectors
196$tableentry$27$.rvectors:
197
198    PUSHM R1-R2
199
200    BSR __tx_thread_context_save
201
202    BRA __tx_thread_context_restore
203
204    .end
205