-
Notifications
You must be signed in to change notification settings - Fork 0
/
BM_BBG.s
executable file
·471 lines (401 loc) · 9.32 KB
/
BM_BBG.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//Registers
.equ CM_PER_GPIO1_CLKCTRL, 0x44e000AC
.equ GPIO1_OE, 0x4804C134
.equ GPIO1_SETDATAOUT, 0x4804C194
.equ GPIO1_CLEARDATAOUT, 0x4804C190
.equ WDT_BASE, 0x44E35000
.equ UART0_BASE, 0x44E09000
.equ RTC_BASE, 0x44E3E000
.equ CM_RTC_RTC_CLKCTRL, 0x44E00800
.equ CM_RTC_CLKSTCTRL, 0x44E00804
_start:
/* init */
mrs r0, cpsr
bic r0, r0, #0x1F @ clear mode bits
orr r0, r0, #0x13 @ set SVC mode
orr r0, r0, #0xC0 @ disable FIQ and IRQ
msr cpsr, r0
bl disable_wdt
bl gpio_setup
bl rtc_setup
/********************************************************
Escreve a hora nos registradores da hora
********************************************************/
write_time:
bl print_string_hora
bl recebe_digito
lsl r1, r0, #4 //DIGITO 1
ldr r0, =RTC_BASE
mov r3, #0x8
add r3, r0, r3 //HORA
str r1, [r3]
bl recebe_digito
mov r2, r0 //DIGITO 2
orr r1, r1, r2
str r1, [r3]
ldr r0,=':'
bl uart_putc
bl recebe_digito
lsl r1, r0, #4 //DIGITO 1
ldr r0, =RTC_BASE
mov r3, #0x4
add r3, r0, r3 //MINUTOS
str r1, [r3]
bl recebe_digito
mov r2, r0 //DIGITO 2
orr r1, r1, r2
str r1, [r3]
ldr r0,=':'
bl uart_putc
bl recebe_digito
lsl r1, r0, #4 //DIGITO 1
ldr r0, =RTC_BASE
mov r3, #0x0
add r3, r0, r3 //SEGUNDOS
str r1, [r3]
bl recebe_digito
mov r2, r0 //DIGITO 2
orr r1, r1, r2
str r1, [r3]
/********************************************************
Escreve a data nos registradores da data
********************************************************/
write_data:
ldr r0,=' '
bl uart_putc
bl print_string_data
bl recebe_digito
lsl r1, r0, #4 //DIGITO 1
ldr r0, =RTC_BASE
mov r3, #0xC
add r3, r0, r3 //DIA
//str r1, [r3]
bl recebe_digito
mov r2, r0 //DIGITO 2
orr r1, r1, r2
str r1, [r3]
ldr r0,='/'
bl uart_putc
bl recebe_digito
lsl r1, r0, #4 //DIGITO 1
ldr r0, =RTC_BASE
mov r3, #0x10
add r3, r0, r3 //MES
str r1, [r3]
bl recebe_digito
mov r2, r0 //DIGITO 2
orr r1, r1, r2
str r1, [r3]
ldr r0,='/'
bl uart_putc
bl recebe_digito
lsl r1, r0, #4 //DIGITO 1
ldr r0, =RTC_BASE
mov r3, #0x14
add r3, r0, r3 //DIA
str r1, [r3]
bl recebe_digito
mov r2, r0 //DIGITO 2
orr r1, r1, r2
str r1, [r3]
bl print_quebra
/********************************************************
Ler os registradores da hora e imprime na tela
********************************************************/
read_time:
bl print_string_hora
ldr r0, =RTC_BASE
ldr r1, [r0, #0x8] //Hora
mov r0, r1, lsr #0x4
add r0, r0, #0x30
bl uart_putc
and r0, r1, #0xF
add r0, r0, #0x30
bl uart_putc
ldr r0,=':'
bl uart_putc
ldr r0, =RTC_BASE
ldr r1, [r0, #0x4] //Minutes
mov r0, r1, lsr #0x4
add r0, r0, #0x30
bl uart_putc
and r0, r1, #0xF
add r0, r0, #0x30
bl uart_putc
ldr r0,=':'
bl uart_putc
ldr r0, =RTC_BASE
ldr r1, [r0, #0x0] //Seconds
mov r0, r1, lsr #0x4
add r0, r0, #0x30
bl uart_putc
and r0, r1, #0xF
add r0, r0, #0x30
bl uart_putc
cmp r1, r2
blne blink
mov r2, r1
/********************************************************
Ler os registradores da data e imprime na tela
********************************************************/
read_data:
mov r0, #0x9 //Tab
bl uart_putc
bl print_string_data
ldr r0, =RTC_BASE
ldr r1, [r0, #0xC] //Dia
mov r0, r1, lsr #0x4
add r0, r0, #0x30
bl uart_putc
and r0, r1, #0xF
add r0, r0, #0x30
bl uart_putc
ldr r0,='/'
bl uart_putc
ldr r0, =RTC_BASE
ldr r1, [r0, #0x10] //Mes
mov r0, r1, lsr #0x4
add r0, r0, #0x30
bl uart_putc
and r0, r1, #0xF
add r0, r0, #0x30
bl uart_putc
ldr r0,='/'
bl uart_putc
ldr r0, =RTC_BASE
ldr r1, [r0, #0x14] //Ano
mov r0, r1, lsr #0x4
add r0, r0, #0x30
bl uart_putc
and r0, r1, #0xF
add r0, r0, #0x30
bl uart_putc
ldr r0, ='\r'
bl uart_putc //Sobrescreve
b read_time
/********************************************************
Configura o RTC
********************************************************/
rtc_setup:
ldr r0, =CM_RTC_CLKSTCTRL
ldr r1, =0x2
str r1, [r0]
ldr r0, =CM_RTC_RTC_CLKCTRL
str r1, [r0]
/*Disable write protection*/
ldr r0, =RTC_BASE
ldr r1, =0x83E70B13
str r1, [r0, #0x6c]
ldr r1, =0x95A4F1E0
str r1, [r0, #0x70]
/* Select external clock*/
ldr r1, =0x48
str r1, [r0, #0x54]
ldr r1, =0x4 /* interrupt every second */
//ldr r1, =0x5 /* interrupt every minute */
str r1, [r0, #0x48]
/* Enable RTC */
ldr r0, =RTC_BASE
ldr r1, =0x01
str r1, [r0, #0x40]
bx lr
/********************************************************
Recebe um dígito do teclado
********************************************************/
recebe_digito:
stmfd sp!, {lr}
bl uart_getc
cmp r0, #0
beq recebe_digito
bl uart_putc
sub r0, r0, #0x30
ldmfd sp!, {pc}
/********************************************************
Imprime r0
********************************************************/
uart_putc:
stmfd sp!, {r0-r2,lr}
ldr r1, =UART0_BASE
wait_tx_fifo_empty:
ldr r2, [r1, #0x14]
and r2, r2, #(1<<5)
cmp r2, #0
beq wait_tx_fifo_empty
strb r0, [r1]
ldmfd sp!, {r0-r2,pc}
/********************************************************
Recebe e salva em r0
********************************************************/
uart_getc:
stmfd sp!,{r1-r2,lr}
ldr r1, =UART0_BASE
wait_rx_fifo:
ldr r2, [r1, #0x14]
and r2, r2, #(1<<0)
cmp r2, #0
beq wait_rx_fifo
ldrb r0, [r1]
ldmfd sp!,{r1-r2,pc}
/********************************************************
Print r3 in screen
********************************************************/
print_number:
stmfd sp!,{r0-r2,lr}
mov r2, #100
print_loop:
mov r0, r2
mov r1, #10
bl div
mov r2, r0
mov r0, r3
mov r1, r2
bl div
add r0, r0, #0x30
bl uart_putc
mov r3, r1
cmp r2, #1
bne print_loop
ldmfd sp!,{r0-r2,pc}
/********************************************************
Print string in r3 in screen
********************************************************/
print_string:
stmfd sp!,{r0-r2,lr}
print:
ldrb r0,[r3],#1
and r0, r0, #0xff
cmp r0, #0
beq end_print
bl uart_putc
b print
end_print:
ldmfd sp!,{r0-r2,pc}
/********************************************************
Divisão
r0 = r0 / r1
r1 = resto
********************************************************/
div:
stmfd sp!,{r2-r3,lr}
mov r2, r0 //num
mov r3, r1 //den
mov r0, #0
div_loop:
cmp r2, r3
blt fim_div
sub r2, r2, r3
add r0, r0, #1
b div_loop
fim_div:
mov r1, r2
ldmfd sp!,{r2-r3,pc}
/********************************************************
Blink Leds
********************************************************/
blink:
stmfd sp!, {lr}
bl set_gpio
bl delay
bl clear_gpio
bl delay
ldmfd sp!, {pc}
/********************************************************
Delay not precise
********************************************************/
delay:
stmfd sp!, {r1,lr}
ldr r1, =0x2FFFFFF
wait:
sub r1, r1, #0x1
cmp r1, #0
bne wait
ldmfd sp!, {r1,pc}
/********************************************************
Imprime mensagens especificas
********************************************************/
print_string_hora:
stmfd sp!, {lr}
adr r3, hora
bl print_string
ldmfd sp!, {pc}
print_string_data:
stmfd sp!, {lr}
adr r3, data
bl print_string
ldmfd sp!, {pc}
print_quebra:
stmfd sp!, {lr}
adr r3, quebra_linha
bl print_string
ldmfd sp!, {pc}
/********************************************************
Set gpio value
********************************************************/
set_gpio:
stmfd sp!, {r0,r1,lr}
ldr r0, =GPIO1_SETDATAOUT
ldr r1, =15<<21
str r1, [r0]
bl delay
ldmfd sp!, {r0,r1,pc}
/********************************************************
Clear gpio value
********************************************************/
clear_gpio:
stmfd sp!, {r0,r1,lr}
ldr r0, =GPIO1_CLEARDATAOUT
ldr r1, =15<<21
str r1, [r0]
bl delay
ldmfd sp!, {r0,r1,pc}
/********************************************************
Registers (see TRM 20.4.4.1):
WDT_BASE -> 0x44E35000
WDT_WSPR -> 0x44E35048
WDT_WWPS -> 0x44E35034
********************************************************/
gpio_setup:
/* set clock for GPIO1, TRM 8.1.12.1.31 */
ldr r0, =CM_PER_GPIO1_CLKCTRL
ldr r1, =0x40002
str r1, [r0]
/* set pin 21-24 for output, led USR0, TRM 25.3.4.3 */
ldr r0, =GPIO1_OE
ldr r1, [r0]
bic r1, r1, #(0xF<<21)
str r1, [r0]
bx lr
/********************************************************
WDT disable sequence (see TRM 20.4.3.8):
1- Write XXXX AAAAh in WDT_WSPR.
2- Poll for posted write to complete using WDT_WWPS.W_PEND_WSPR. (bit 4)
3- Write XXXX 5555h in WDT_WSPR.
4- Poll for posted write to complete using WDT_WWPS.W_PEND_WSPR. (bit 4)
Registers (see TRM 20.4.4.1):
WDT_BASE -> 0x44E35000
WDT_WSPR -> 0x44E35048
WDT_WWPS -> 0x44E35034
********************************************************/
disable_wdt:
stmfd sp!,{r0-r1,lr}
ldr r0, =WDT_BASE
ldr r1, =0xAAAA
str r1, [r0, #0x48]
bl poll_wdt_write
ldr r1, =0x5555
str r1, [r0, #0x48]
bl poll_wdt_write
ldmfd sp!,{r0-r1,pc}
poll_wdt_write:
ldr r1, [r0, #0x34]
and r1, r1, #(1<<4)
cmp r1, #0
bne poll_wdt_write
bx lr
/********************************************************/
//Strings
data: .asciz "Data: "
hora: .asciz "Hora: "
minutos: .asciz "Minutos: "
segundos: .asciz "Segundos: "
quebra_linha: .asciz "\n\r"