This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.s
374 lines (318 loc) · 7.56 KB
/
main.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
;******************** (C) Yifeng ZHU *******************************************
; @file main.s
; @author Yifeng Zhu
; @date May-17-2015
; @note
; This code is for the book "Embedded Systems with ARM Cortex-M
; Microcontrollers in Assembly Language and C, Yifeng Zhu,
; ISBN-13: 978-0982692639, ISBN-10: 0982692633
; @attension
; This code is provided for education purpose. The author shall not be
; held liable for any direct, indirect or consequential damages, for any
; reason whatever. More information can be found from book website:
; http:;www.eece.maine.edu/~zhu/book
;*******************************************************************************
INCLUDE core_cm4_constants.s ; Load Constant Definitions
INCLUDE stm32l476xx_constants.s
IMPORT EXTI_Init
IMPORT System_Clock_Init
IMPORT UART2_Init
IMPORT USART2_Write
IMPORT MoveInit
IMPORT MoveUp
IMPORT MoveDown
IMPORT KeypadInit
IMPORT doorOpen
IMPORT doorClose
IMPORT SpeakerInit
IMPORT Speaker
AREA main, CODE, READONLY
EXPORT __main ; make __main visible to linker
ENTRY
__main PROC
BL System_Clock_Init
BL UART2_Init
BL EXTI_Init ; necessary for interrupts
;LDR r0, =str ; First argument
;MOV r1, #11 ; Second argument
;BL USART2_Write
;set a8, a9, a11, a12, a14, a15 as output
LDR r0, =RCC_BASE ; enable clock of GPIOA/B/C
LDR r1, [r0, #RCC_AHB2ENR]
ORR r1, #0x7
STR r1, [r0, #RCC_AHB2ENR]
LDR r0, =RCC_BASE
LDR r1, [r0, #RCC_AHB2ENR]
ORR r1, #0x00000001 ;set
STR r1, [r0, #RCC_AHB2ENR]
LDR r0, =GPIOA_BASE
LDR r1, [r0, #GPIO_MODER]
BIC r1, #0x0000000C
BIC r1, #0x03000000
BIC r1, #0x00C00000
BIC r1, #0x000F0000
BIC r1, #0x0000FF00
ORR r1, #0x00000004
ORR r1, #0x01000000
ORR r1, #0x00400000
ORR r1, #0x00050000
ORR r1, #0x0000D000
ORR r1, #0x00000500 ;set
STR r1, [r0, #GPIO_MODER]
LDR r0, =GPIOB_BASE
LDR r1, [r0, #GPIO_MODER]
BIC r1, #0x000000C0
BIC r1, #0xC0000000
BIC r1, #0x00300000
BIC r1, #0x0000F000
BIC r1, #0x00000C00
ORR r1, #0x00000040
ORR r1, #0x40000000
ORR r1, #0x00100000
ORR r1, #0x00005000
ORR r1, #0x00000400
STR r1, [r0, #GPIO_MODER]
;initializes the b pins for call lights (5, 6, 10, 15)
;initialize move process
BL MoveInit
BL SpeakerInit
;
;Variables used by the rest of the program
MOV r5, #0x00000001
MOV r6, #0x00000000
MOV r7, #0x1
;;;;;;;;;;;; YOUR CODE GOES HERE ;;;;;;;;;;;;;;;;;;;
BL KeypadInit
BL MoveInit
B while
;Update LEDs and character display here
;get floor and move onto the appropriate pins A8, A9, A11, A12
;current floor is stored on r7
seven
LDR r0, =GPIOA_BASE ;hardcoded output values
LDR r1, [r0, #GPIO_ODR]
BIC r1, #0x1B00
CMP r7, #0x8
ORREQ r1, #0x800
CMP r7, #0x4
ORREQ r1, #0x300
CMP r7, #0x2
ORREQ r1, #0x200
CMP r7, #0x1
ORREQ r1, #0x100
STR r1, [r0, #GPIO_ODR]
BX LR
direction
;direction lights are a14, a15
;r8 0x0 is down, r8 0x1 is up
LDR r0, =GPIOA_BASE
LDR r1, [r0, #GPIO_ODR]
BIC r1, #0x12
CMP r8, #0x0
ORREQ r1, #0x2
CMP r8, #0x1
ORREQ r1, #0x10
STR r1, [r0, #GPIO_ODR]
BX LR
downFlags
AND r9, #0x00000000
AND r9, r6, #0xF0
LSR r9, #0x4
B midWhile
upFlags
AND r9, #0x00000000
AND r9, r6, #0xF
B midWhile
while
; check if the current floor is a destination
; or if it is called in the direction
BL seven
AND r8, r5, #0x00000001
CMP r8, #0x1
BNE downFlags
B upFlags
midWhile
AND r9, r9, r7
CMP r9, r7
BEQ openSesame
AND r9, r5, #0x0000F000 ;checks main call buttons
LSR r9, #0xC
AND r9, r7
CMP r9, r7
BEQ openSesame
AND r9, r6, #0x100 ;checks panic button
CMP r9, #0x0
BHI openSesame
AND r9, r5, #0x10 ;checks open door button
CMP r9, #0x0
BHI openSesame
LDR r0, =GPIOA_BASE ;reset direction leds if still
LDR r1, [r0, #GPIO_ODR]
BIC r1, #0x12
STR r1, [r0, #GPIO_ODR]
CMP r8, #0x1
BEQ checkAbove
B checkBelow
openSesame ;elevator determines door needs open
BL seven
BL callLights
BL Speaker
BL doorOpen
BL callLights
BL doorClose
;update floor call lights
BL clearFloor
BL callLights
B while ;back to start
downFloor
BL callLights
BL direction ;display direction
BL MoveDown
BIC r5, #0x10 ;clear open door
;updates current floor
LSR r7, #0x1
B while
upFloor
BL callLights
BL direction ;display direction
BL MoveUp
BIC r5, #0x10 ;clear open door flag
;updates current floor
LSL r7, #0x1
B while
;turn on next floor
;turn off previous destination and previous calls
clearFloor
AND r2, r7, #0x0000000F ;isolate for just location bits
CMP r2, #0x0000008
BEQ clear4
CMP r2, #0x0000004
BEQ clear3
CMP r2, #0x0000002
BEQ clear2
CMP r2, #0x0000001
BEQ clear1
BX LR
clear4
BIC r5, #0x8000 ;clears internal call
BIC r6, #0x80 ;clears down call
BX LR
clear3
BIC r5, #0x4000 ;clear internal call
CMP r8, #0x1
BICEQ r6, #0x4 ;clear up call
BICNE r6, #0x40 ;clear down call
BX LR
clear2
BIC r5, #0x2000
CMP r8, #0x1
BICEQ r6, #0x2 ;clear up call
BICNE r6, #0x20 ;clear down call
BX LR
clear1
BIC r5, #0x1000
CMP r8, #0x1
BICEQ r6, #0x1 ;clear up call
BX LR
checkAbove
MOV r9, r5 ;load flags
CMP r7, #0x8 ;if floor four change directions
EORHS r5, #0x1
BXHS LR
LSR r9, #0xC ;shift to be LSBs
AND r9, #0xF ;mask floor flags
SUB r9, r9, r7 ;filter out lower floors
SUBS r9, r9, r7
BPL upFloor ;if higher floors called up, move up
MOV r9, r6 ;load up calls
AND r9, #0x7 ;mask up calls
CMP r9, #0x1 ;if any greater floor calls up
BHI upFloor
MOV r9, r6
LSR r9, #0x4
AND r9, #0xE
SUB r9, r7
SUBS r9, r7
BPL upFloor
EOR r5, #0x1 ;if no call above flip direcions
B while
checkBelow
MOV r9, r5 ;load flags
CMP r7, #0x1 ;if floor one change direction and return
EORLS r5, #0x1
BXLS LR
LSR r9, #0xC ;shift internal calls to be LSBs
AND r9, #0xF ;mask flags
BIC r9, r7 ;clear calls to higher floors
BIC r9, r7, LSL #1
BIC r9, r7, LSL #2
CMP r9, #0x0 ;if any calls, go down
BHI downFloor
MOV r9, r6 ;check if any down flags below
LSR r9, #0x4
AND r9, #0xE
BIC r9, r7 ;ignore higher floor calls down
BIC r9, r7, LSL #1
BIC r9, r7, LSL #2
CMP r9, #0x0
BHI downFloor
MOV r9, r6 ;look for up calls below
AND r9, #0x7
BIC r9, r7 ;ignore higher floor calls up
BIC r9, r7, LSL #1
CMP r9, #0x0
BHI downFloor
EOR r5, #0x1 ;if no calls, change direction
B while
callLights
LDR r0, =GPIOB_BASE
LDR r1, [r0, #GPIO_ODR]
BIC r1, #0x8000 ;should clear the bits for odr
BIC r1, #0xE0
;checks floor 4
AND r3, r5, #0x8000 ;internal flag
ORR r1, r3
AND r3, r6, #0x80 ;external flag
LSL r3, #0x8
ORR r1, r3
;checks floor 3
AND r3, r5, #0x4000
LSR r3, #0x7
ORR r1, r3
AND r3, r6, #0x40
LSL r3, #0x1
ORR r1, r3
AND r3, r6, #0x4
LSL r3, #0x5
ORR r1, r3
;checks floor 2
AND r3, r5, #0x2000
LSR r3, #0x7
ORR r1, r3
AND r3, r6, #0x20
LSL r3, #0x1
ORR r1, r3
AND r3, r6, #0x2
LSL r3, #0x5
ORR r1, r3
;checks floor 1
AND r3, r5, #0x1000
LSR r3, #0x7
ORR r1, r3
AND r3, r6, #0x1
LSL r3, #0x5
ORR r1, r3
STR r1, [r0, #GPIO_ODR] ;turn them on
BX LR
ENDP
ALIGN
AREA myData, DATA, READWRITE
ALIGN
str DCB "Main",0
END
ENDP
ALIGN
AREA myData, DATA, READWRITE
ALIGN
;str DCB "Levine\r\n", 0
END