/
button.s
288 lines (220 loc) · 8.27 KB
/
button.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
;;; button - serial input dripver for CTP
;;;
;;; Copyright (c) 2009 Openmoko Inc.
;;;
;;; Authors Christopher Hall <hsw@openmoko.com>
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions are
;;; met:
;;;
;;; 1. Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; 2. Redistributions in binary form must reproduce the above copyright
;;; notice, this list of conditions and the following disclaimer in
;;; the documentation and/or other materials provided with the
;;; distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY
;;; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.include "regs.inc"
;;; register usage
;;; r0 .. r3 must be preserved
;;; r4 result low
;;; r5 result high
;;; r6 .. r9 arguments 1..4
;;; r10 ..r14 reserved
;;; r15 __dp value
.section .bss
;;; buffer size = 2^n is conveient for modulo operation
BufferSize = 256
;;; buffer is full if: (write + 1) mod size == read
;;; buffer is full if: write == read
RxBuffer:
.space BufferSize
RxRead:
.long 0
RxWrite:
.long 0
ButtonMask = 0x07 ; only 3 bits used
PowerButton = 0x10 ; above ButtonMask
.section .text
;;; see if input is available
;;; input:
;;; output:
;;; r4 = 0 => not ready
;;; 1 => ready
;;; r5 = offset of byte to read
;;; r6 = address of RxRead
;;; temporary:
.global Button_available
Button_available:
xld.w %r6, RxRead
xld.w %r4, RxWrite
ld.w %r5, [%r6] ; read
ld.w %r4, [%r4] ; write
cmp %r5, %r4 ; read == write ?
jreq Button_available_buffer_empty
ld.w %r4, 1 ; TRUE => buffer is not empty
ret
Button_available_buffer_empty:
ld.w %r4, 0 ; FALSE => buffer is empty
ret
;;; read a button
;;; input:
;;; output:
;;; r4 = button
.global Button_get
Button_get_wait:
xcall suspend ; suspend until more input
Button_get:
call Button_available
or %r4, %r4
jreq Button_get_wait
ld.w %r7, %r5
xld.w %r4, RxBuffer
add %r5, %r4
xadd %r7, 1
xand %r7, (BufferSize - 1)
ld.ub %r4, [%r5]+ ; value
ld.w [%r6], %r7 ; update RxRead
ret
;;; poll the buttons
;;; input:
;;; output:
;;; r4 = button
.global Button_poll
Button_poll:
xld.w %r4, R8_P6_P6D ; address of buttond port
ld.ub %r4, [%r4] ; read buttond
xand %r4, ButtonMask ; mask unused bits
ret
;;; flush the buffer
;;; input:
;;; output:
.global Button_flush
Button_flush:
xld.w %r4, RxRead
xld.w %r5, RxWrite
ld.w %r6, 0
DISABLE_INTERRUPTS
ld.w [%r4], %r6
ld.w [%r5], %r6
ENABLE_INTERRUPTS
ret
;;; initialisation
;;; input:
;;; output:
.global Button_initialise
Button_initialise:
xld.w %r6, Vector_Key_input_interrupt_0
xld.w %r7, Button_RxInterrupt
xcall Vector_set
xld.w %r6, Vector_Key_input_interrupt_1
xld.w %r7, Button_RxInterrupt
xcall Vector_set
xld.w %r6, Vector_Port_input_interrupt_3
xld.w %r7, Button_P0Interrupt
xcall Vector_set
ld.w %r5, 0
xld.w %r4, RxRead
ld.w [%r4], %r5
xld.w %r4, RxWrite
ld.w [%r4], %r5
DISABLE_INTERRUPTS
xld.w %r4, R8_INT_PK01L ; key priority
xld.w %r5, 0x66
ld.b [%r4], %r5
xld.w %r4, R8_INT_FK01_FP03 ; clear key interrupts
xld.w %r5, FK1 | FK0 | FP3 | FP2 | FP1 | FP0
ld.b [%r4], %r5
xld.w %r4, R8_KINTCOMP_SCPK0 ; comparison = all buttons off
xld.w %r5, 0x00
ld.b [%r4], %r5
xld.w %r4, R8_KINTCOMP_SMPK0 ; enable 3 buttons
xld.w %r5, 0x07
ld.b [%r4], %r5
xld.w %r4, R8_KINTSEL_SPPK01 ; use P60..P62
xld.w %r5, 0x04
ld.b [%r4], %r5
xld.w %r4, R8_INT_PP01L ; P0 prority
xld.w %r5, 0x06
ld.b [%r4], %r5
xld.w %r4, R8_PINTSEL_SPT03 ; select P03 interrupt
xld.w %r5, 0x00
ld.b [%r4], %r5
xld.w %r4, R8_PINTPOL_SPP07 ; P03 active high
xld.w %r5, SPPT3
ld.b [%r4], %r5
xld.w %r4, R8_PINTEL_SEPT07 ; P03 edge trigered
xld.w %r5, SEPT3
ld.b [%r4], %r5
xld.w %r4, R8_INT_EK01_EP03 ; enable KINT0 and Port0
xld.w %r5, EK0 | EP3
ld.b [%r4], %r5
ENABLE_INTERRUPTS
ret
;;; receive new button value
;;; input:
;;; output:
.global Button_RxInterrupt
Button_RxInterrupt:
pushn %r14
xld.w %r4, R8_INT_FK01_FP03
xld.w %r5, FK1 | FK0
ld.b [%r4], %r5
xld.w %r0, R8_P6_P6D
xld.w %r1, R8_KINTCOMP_SCPK0
xld.w %r2, RxRead
xld.w %r3, RxWrite
ld.ub %r10, [%r0] ; button
xand %r10, ButtonMask ; mask unused bits
ld.b [%r1], %r10 ; update change of state
ld.w %r9, [%r3] ; RxWrite
xld.w %r11, RxBuffer ; buffer start
add %r11, %r9 ; + offset
ld.b [%r11]+, %r10 ; store button
ld.w %r11, [%r2] ; RxRead
xadd %r9, 1 ; next position
xand %r9, (BufferSize - 1) ; mod buffer size
cmp %r11, %r9 ; read == write?
jreq Button_RxInterrupt_buffer_full ; ...yes => buffer overrun, value lost
ld.w [%r3], %r9 ; update RxWrite
Button_RxInterrupt_buffer_full:
popn %r14
reti
;;; receive power button short pulse
;;; input:
;;; output:
.global Button_P0Interrupt
Button_P0Interrupt:
pushn %r14
xld.w %r4, R8_INT_FK01_FP03
xld.w %r5, FP3
ld.b [%r4], %r5
xld.w %r2, RxRead
xld.w %r3, RxWrite
xld.w %r10, PowerButton
ld.w %r9, [%r3] ; RxWrite
xld.w %r11, RxBuffer ; buffer start
add %r11, %r9 ; + offset
ld.b [%r11]+, %r10 ; store button
ld.w %r11, [%r2] ; RxRead
xadd %r9, 1 ; next position
xand %r9, (BufferSize - 1) ; mod buffer size
cmp %r11, %r9 ; read == write?
jreq Button_P0Interrupt_buffer_full ; ...yes => buffer overrun, value lost
ld.w [%r3], %r9 ; update RxWrite
Button_P0Interrupt_buffer_full:
popn %r14
reti