-
Notifications
You must be signed in to change notification settings - Fork 6
/
avrx_timequeue_exp.S
211 lines (187 loc) · 6.43 KB
/
avrx_timequeue_exp.S
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
#include "avrx.inc"
/*
Copyright ©1998 - 2002 Larry Barello ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
http://www.gnu.org/copyleft/lgpl.html
REVISION HISTORY
20010403 - Initial update for avrx-ctoasm.inc port
*/
_MODULE(avrx_timequeue)
// _PUBLIC(_TimerHandler)
_EXTERN(AvrXIntSendMessage)
_EXTERN(AvrXIntSetObjectSemaphore)
_EXTERN(AvrXSetObjectSemaphore)
_EXTERN(AvrXIntResetObjectSemaphore)
_EXTERN(IntProlog)
_EXTERN(_Epilog)
_EXTERN(AvrXWaitTimer)
;
; AvrX Time Queue Manager
;
_DATASECTION
_GLOBAL(_TimerQueue, 2) ; Head of the timer queue
_GLOBAL(_TimQLevel, 1) ; Recurse level of timer interrupts
_CODESECTION
/*+
; -----------------------------------------------
; void AvrXDelay(pTcb, unsigned)
;
; Passed: p1h:p1l = TCB
; p2h:p2l = Count
; Returns:
; Uses:
; Stack:
; Notes: See AvrXStartTimer, AvrXWaitTimer
-*/
_FUNCTION(AvrXDelay)
AvrXDelay:
rcall AvrXStartTimer
rjmp AvrXWaitTimer
_ENDFUNC
/*+
; -----------------------------------------------
; void AvrXStartTimer(pTcb, unsigned)
;
; Passed: p1h:p1l = TCB
; p2h:p2l = Count
; Returns:
; Uses:
; Stack:
; Notes: Should check and halt if TCB already queued
; Resets TCB Semaphore If count Zero, just flag
; semaphore and return
-*/
_FUNCTION(AvrXStartTimer)
AvrXStartTimer:
subi p2l, lo8(-0)
sbci p2h, hi8(-0)
brne CountNotZero
rjmp AvrXSetObjectSemaphore
_PUBLIC(CountNotZero)
CountNotZero:
AVRX_Prolog
rcall AvrXIntResetObjectSemaphore
ldi Zl, lo8(_TimerQueue)
ldi Zh, hi8(_TimerQueue)
ast00:
mov Yl, Zl ; Y -> Previous
mov Yh, Zh
ldd Zl, Y+NextL ; Z -> Current
ldd Zh, Y+NextH
adiw Zl, 0
breq ast01 ; End of queue, Wrap up.
ldd tmp0, Z+TcbCount+NextL; tmp = Current count
ldd tmp1, Z+TcbCount+NextH; p2 = our count
sub p2l, tmp0
sbc p2h, tmp1 ; Subtract it from us
brsh ast00 ; Its less than us, continue walking the list
add p2l, tmp0 ; Restore us
adc p2h, tmp1
sub tmp0, p2l ; Subtract us from it
sbc tmp1, p2h
std Z+TcbCount+NextL, tmp0
std Z+TcbCount+NextH, tmp1 ; Put it back out and insert us in front.
;
; Wrap up: Z->Current, P1->Tcb, Y->Prev, P2 = Count
; Insert Tcb into chain
;
ast01:
std Y+NextH, p1h
std Y+NextL, p1l ; Prev.next = NewTCB
mov Yh, p1h
mov Yl, p1l
std Y+NextH, Zh ; NewTCB.next = Current
std Y+NextL, Zl
std Y+TcbCount+NextL, p2l
std Y+TcbCount+NextH, p2h ; NewTCB.Count = count
rjmp _Epilog
_ENDFUNC
/*+
; -----------------------------------------------
; void AvrXTimerHandler(void)
;
; THIS IS INTERRUPT CODE and must be called within an
; _Prolog/_Epilog section.
;
; The register variable _TimQLevel is used to count re-
; entry into the code. It is also a semaphore that prevents
; timer interrupts from hosing the AvrXStartTimer code, above.
; Thus, the entire timer handler chain can be run with interrupts
; enabled.
;
; Since this can be called from C code (avrx 2.5/IAR) gotta preserve everything
; but Z and tmp0-4. System calls within can and do trash the trashable
; registers, hence all the push/pop's
-*/
_FUNCTION(AvrXTimerHandler)
AvrXTimerHandler:
push Yl ; Gotta save these since we do not know
push Yh ; who called us (C code)
push Xl
push Xh
BeginCritical
lds Yh, _TimerQueue+NextH
lds Yl, _TimerQueue+NextL
adiw Yl, 0
breq ati02 ; Empty queue, return
;
; Y points to the first element in the queue.
;
ldd Zh, Y+TcbCount+NextH
ldd Zl, Y+TcbCount+NextL
sbiw Zl, 1 ; Y->Count--
std Y+TcbCount+NextH, Zh
std Y+TcbCount+NextL, Zl
ati01:
or Zl, Zh ; While (Y->Count == 0)
brne ati02 ; {
ldd Xl, Y+NextL
ldd Xh, Y+NextH
sts _TimerQueue+NextH, Xh ; Point queue to (new) first element
sts _TimerQueue+NextL, Xl
std Y+NextH, Zh ; Zero out link
std Y+NextL, Zl
ldd p1l, Y+TcbSemaphore+NextL
ldd p1h, Y+TcbSemaphore+NextH
subi p1l, lo8(TIMERMESSAGE_EV)
sbci p1h, hi8(TIMERMESSAGE_EV)
brne ati04
ldd p1l, Y+TcbQueue+NextL
ldd p1h, Y+TcbQueue+NextH
mov p2h, Yh
mov p2l, Yl
rcall AvrXIntSendMessage
rjmp ati03
ati04:
mov p1l, Yl
mov p1h, Yh
rcall AvrXIntSetObjectSemaphore
ati03:
adiw Xl, 0 ; If Next Tcb == 0 (list empty)
breq ati02 ; break;
; else
mov Yl, Xl
mov Yh, Xh
ldd Zh, Y+TcbCount+NextH ; load up count and loop.
ldd Zl, Y+TcbCount+NextL
rjmp ati01 ; }
ati02:
EndCritical
pop Xh
pop Xl
pop Yh
pop Yl
ret
_ENDFUNC
_END