summaryrefslogtreecommitdiff
path: root/ports/xtensa/xcc/src/tx_timer_interrupt.S
blob: d09baddeec1570875e577f05d473cdd69a4ba1ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/**************************************************************************/
/*   Copyright (c) Cadence Design Systems, Inc.                           */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** ThreadX Component                                                     */
/**                                                                       */
/**   Timer                                                               */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


#include "xtensa_rtos.h"
#include "tx_api_asm.h"

#ifndef TX_NO_TIMER

    .text

/**************************************************************************/
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function processes the hardware timer interrupt.  This         */
/*    processing includes incrementing the system clock and checking for  */
/*    time slice and/or timer expiration.  If either is found, the        */
/*    interrupt context save/restore functions are called along with the  */
/*    expiration functions.                                               */
/*                                                                        */
/**************************************************************************/

//  VOID   _tx_timer_interrupt(VOID)
//  {
    .globl  _tx_timer_interrupt
    .type   _tx_timer_interrupt,@function
    .align  4
_tx_timer_interrupt:

    #ifdef __XTENSA_CALL0_ABI__
    /* Define local variable spill offsets in stack frame for Call0 ABI. */
    #define __tx_timer_interrupt_a0     0   /* ENTRY()/RET() saves/restores */
    #define __tx_timer_interrupt_a2     4   /* preserve a2 */
    #define __tx_timer_interrupt_a3     8   /* preserve a3 */
    #endif

    ENTRY(16)

    .globl  tx_timer_user_isr
    .weak   tx_timer_user_isr
    movi    a2, tx_timer_user_isr
    beqz    a2, 1f
    #ifdef __XTENSA_CALL0_ABI__
    callx0  a2
    #else
    callx8  a2
    #endif
1:

    /*
    Xtensa timers work by comparing a cycle counter with a preset value.
    Once the match occurs an interrupt is generated, and the handler has
    to set a new cycle count into the comparator. To avoid clock drift
    due to interrupt latency, the new cycle count is computed from the old,
    not the time the interrupt was serviced. However if a timer interrupt
    is ever serviced more than one tick late, it is necessary to process
    multiple ticks until the new cycle count is in the future, otherwise
    the next timer interrupt would not occur until after the cycle counter
    had wrapped (2^32 cycles later).

    do {
        ticks++;
        old_ccompare = read_ccompare_i();
        write_ccompare_i( old_ccompare + divisor );
        service one tick;
        diff = read_ccount() - old_ccompare;
    } while ( diff > divisor );
    */

.L_tx_timer_catchup:

    /* Increment the system clock.  */
    //  _tx_timer_system_clock++;
    movi    a2, _tx_timer_system_clock  /* a2 = &_tx_timer_system_clock */
    l32i    a3, a2, 0                   /* a3 =  _tx_timer_system_clock++ */
    addi    a3, a3, 1
    s32i    a3, a2, 0

    /* Update the timer comparator for the next tick. */
    #ifdef XT_CLOCK_FREQ
    movi    a2, XT_TICK_DIVISOR         /* a2 = comparator increment */
    #else
    movi    a3, xt_tick_divisor
    l32i    a2, a3, 0                   /* a2 = comparator increment */
    #endif
    rsr     a3, XT_CCOMPARE             /* a3 = old comparator value */
    add     a4, a3, a2                  /* a4 = new comparator value */
    wsr     a4, XT_CCOMPARE             /* update comp. and clear interrupt */
    esync

    /* Test for time-slice expiration.  */
    //  if (_tx_timer_time_slice)
    //  {
    movi    a4, _tx_timer_time_slice    /* a4 = &_tx_timer_time_slice */
    l32i    a5, a4, 0                   /* a5 =  _tx_timer_time_slice */
    beqz    a5, .L_tx_timer_no_time_slice

    /* Decrement the time_slice.  */
    //      _tx_timer_time_slice--;
    addi    a5, a5, -1
    s32i    a5, a4, 0

    /* Check for expiration.  */
    //      if (_tx_timer_time_slice == 0)
    bnez    a5, .L_tx_timer_no_time_slice

    /* Set the time-slice expired flag.  */
    //          _tx_timer_expired_time_slice =  TX_TRUE;
    movi    a4, _tx_timer_expired_time_slice
    movi    a5, TX_TRUE
    s32i    a5, a4, 0

    //  }

.L_tx_timer_no_time_slice:

    /* Test for timer expiration.  */
    //  if (*_tx_timer_current_ptr)
    //  {
    movi    a4, _tx_timer_current_ptr   /* a4 = &_tx_timer_current_ptr */
    l32i    a5, a4, 0                   /* a5 =  _tx_timer_current_ptr */
    l32i    a6, a5, 0                   /* a6 = *_tx_timer_current_ptr */
    beqz    a6, .L_tx_timer_no_timer

    /* Set expiration flag.  */
    //      _tx_timer_expired =  TX_TRUE;
    movi    a6, _tx_timer_expired
    movi    a7, TX_TRUE
    s32i    a7, a6, 0
    j       .L_tx_timer_done

    //  }
    //  else
    //  {

.L_tx_timer_no_timer:

    /* No timer expired, increment the timer pointer.  */
    //      _tx_timer_current_ptr++;

    /* Check for wrap-around.  */
    //      if (_tx_timer_current_ptr == _tx_timer_list_end)
    movi    a6, _tx_timer_list_end
    l32i    a6, a6, 0                   /* a6 =  _tx_timer_list_end */
    addi    a5, a5, 4                   /* a5 = ++_tx_timer_current_ptr */
    bne     a5, a6, .L_tx_timer_skip_wrap

    /* Wrap to beginning of list.  */
    //          _tx_timer_current_ptr =  _tx_timer_list_start;
    movi    a6, _tx_timer_list_start
    l32i    a5, a6, 0                   /* a5 =  _tx_timer_list_start */

.L_tx_timer_skip_wrap:

    s32i    a5, a4, 0                   /* _tx_timer_current_ptr = a5 */
    //  }

.L_tx_timer_done:

    /* See if anything has expired.  */
    //  if ((_tx_timer_expired_time_slice) || (_tx_timer_expired))
    //  {

    #ifdef __XTENSA_CALL0_ABI__
    /* Preserve a2 and a3 across calls. */
    s32i    a2, sp, __tx_timer_interrupt_a2
    s32i    a3, sp, __tx_timer_interrupt_a3
    #endif

    /* Did a timer expire?  */
    //      if (_tx_timer_expired)
    //      {
    movi    a4, _tx_timer_expired
    l32i    a5, a4, 0
    beqz    a5, .L_tx_timer_dont_activate

    /* Call the timer expiration processing.  */
    //          _tx_timer_expiration_process();
    #ifdef __XTENSA_CALL0_ABI__
    call0   _tx_timer_expiration_process
    #else
    call8   _tx_timer_expiration_process
    #endif

    //      }

.L_tx_timer_dont_activate:

    /* Did time slice expire?  */
    //      if (_tx_timer_expired_time_slice)
    //      {
    movi    a4, _tx_timer_expired_time_slice
    l32i    a5, a4, 0
    beqz    a5, .L_tx_timer_not_ts_expiration

    /* Time slice interrupted thread.  */
    //          _tx_thread_time_slice();
    #ifdef __XTENSA_CALL0_ABI__
    call0   _tx_thread_time_slice
    #else
    call8   _tx_thread_time_slice
    #endif

    //      }

.L_tx_timer_not_ts_expiration:

    #ifdef __XTENSA_CALL0_ABI__
    /* Restore a2 and a3. */
    l32i    a2, sp, __tx_timer_interrupt_a2
    l32i    a3, sp, __tx_timer_interrupt_a3
    #endif

    //  }

.Ln_tx_timer_nothing_expired:

    /* Check if we need to process more ticks to catch up. */
    esync                               /* ensure comparator update complete */
    rsr     a4, CCOUNT                  /* a4 = cycle count */
    sub     a4, a4, a3                  /* diff = ccount - old comparator */
    blt     a2, a4, .L_tx_timer_catchup /* repeat while diff > divisor */

    RET(16)

//  }

#endif /* TX_NO_TIMER */