]> cloudbase.mooo.com Git - avrcpm.git/blame - avr/init.asm
SVN --> GIT
[avrcpm.git] / avr / init.asm
CommitLineData
6efd6291 1; Hardware initialisation, disk, mmc, timer, DRAM test
9c15f366
L
2;
3; Copyright (C) 2010 Sprite_tm
dc705dc0 4; Copyright (C) 2010-2013 Leo C.
9c15f366
L
5;
6; This file is part of avrcpm.
7;
8; avrcpm is free software: you can redistribute it and/or modify it
9; under the terms of the GNU General Public License as published by
10; the Free Software Foundation, either version 3 of the License, or
11; (at your option) any later version.
12;
13; avrcpm is distributed in the hope that it will be useful,
14; but WITHOUT ANY WARRANTY; without even the implied warranty of
15; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16; GNU General Public License for more details.
17;
18; You should have received a copy of the GNU General Public License
19; along with avrcpm. If not, see <http://www.gnu.org/licenses/>.
20;
12a27f27 21; $Id: init.asm 241 2015-12-10 09:38:25Z rapid $
9c15f366
L
22;
23
24#define REFR_PRE 8 /* timer prescale factor 1/8 */
25#define REFR_CS 0x02 /* timer clock select for 1/8 */
26#define REFR_CNT F_CPU / REFR_RATE / REFR_PRE
27
28 .cseg
29regval_tab:
30 .db 0,0
31 .db 0xFE,0xFC ; _RAS0 _CAS0
32 .db 0xF8,0xF4 ; _OE _WE
33 .db 255,0 ; _255 _0
34regval_tab_e:
35
36start:
e8384f88
L
37 ldi temp,low(RAMEND) ; top of memory; vim:set ts=8 noet nowrap
38
9c15f366
L
39 out SPL,temp ; init stack pointer
40 ldi temp,high(RAMEND) ; top of memory
41 out SPH,temp ; init stack pointer
42
43; - Load some registers with constant values
44
45 ldiw z,regval_tab*2
46 ldiw y,0
47cp_l: lpm xh,z+
48 st y+,xh
49 cpi zl,low(regval_tab_e*2)
50 brne cp_l
51
52; - Kill wdt
53
54 wdr
55 out MCUSR,_0
56
57 ldi temp,(1<<WDCE) | (1<<WDE)
58 outm8 WDTCSR,temp
59 ldi temp,(1<<WDCE)
60 outm8 WDTCSR,temp
4bd49b80 61
9c15f366
L
62; - Setup Ports
63
825ecc9d
L
64; ldi temp,(1<<PUD) ;disable pullups
65; outm8 P_PUD,temp
66 out PORTD,_255 ;all pins high (enables pullup on input ports)
9c15f366
L
67 out PORTB,_255
68 out PORTC,_255
12a27f27
L
69 out DDRD,_255 ; PD all outputs
70#if I2C_SUPPORT
71 ldi temp,~((1<<SCL)|(1<<SDA))
72 out DDRC,temp
73#endif
74#if DRAM_8BIT
75 ldi temp,~(1<<RXD)
76 out DDRB,temp
77#endif
9c15f366
L
78
79 outm8 TIMSK1,_0
80 outm8 TIMSK2,_0
81 outm8 TCCR2A,_0
82 outm8 TCCR2B,_0
83
84; - Clear RAM
85
86 ldiw z,SRAM_START
87 ldi temp2,high(ramtop)
98979541 88clr_loop:
9c15f366
L
89 st z+,_0
90 cpi zl,low(ramtop)
91 cpc zh,temp2
98979541
L
92 brne clr_loop
93
94; - Fill unused RAM (stack)
95
96 ldi temp2,high(RAMEND+1)
97 ldi temp,SRAMFILL_VAL
98fill_loop:
99 st z+,temp
100 cpi zl,low(RAMEND+1)
101 cpc zh,temp2
102 brne fill_loop
9c15f366 103
9c15f366
L
104; Init clock/timer system
105
106; Init timer 1 as 1 ms system clock tick.
107
12a27f27
L
108 ldi temp, low (TC_1MS)
109 ldi temp2,high(TC_1MS)
9c15f366
L
110 outm8 OCR1BH,temp2
111 outm8 OCR1BL,temp
112 ldi temp,(1<<ICNC1)|(1<<CS10) ;Noise cancel, fall. edge, Normal Mode, clk/1
113 outm8 TCCR1B,temp
114 inm8 temp,TIMSK1
115 ori temp,(1<<OCIE1B) ;Enable 1ms int.
116 outm8 TIMSK1,temp
117
118; - Init serial port
119
120 rcall uart_init
4bd49b80 121
825ecc9d 122;Init timer2. Refresh-call should happen every (8ms/512) cycles.
9c15f366
L
123
124 ldi temp,REFR_CNT*2 ; 2 cycles per int
125 outm8 OCR2A,temp
126 inm8 temp,TCCR2A
127 ori temp,(1<<WGM21) ;CTC mode
128 outm8 TCCR2A,temp
129 inm8 temp,TCCR2B
130 ori temp,REFR_CS ;clk/REFR_PRE
131 outm8 TCCR2B,temp
132 inm8 temp,TIMSK2
133 ori temp, (1<<OCIE2A)
134 outm8 TIMSK2,temp
135
136 sei
137
dc705dc0 138#if I2C_SUPPORT
d8fa6a36
L
139 rcall i2c_init ; Init I2C master
140 rcall rtc_get
141#endif
142
9c15f366
L
143
144.if BOOTWAIT
145 ldi temp,10
b741422e 146 rcall delay_ms
9c15f366
L
147
148.endif
149
150 rcall printstr
12a27f27
L
151 .db '\r', '\r'
152version_string:
153 makestring "CPM on an AVR, v" VERS_STR " r" SVN_REVSTR TESTSTR
4bd49b80 154
9c15f366
L
155.if MEMTEST
156 printnewline
157 printstring "Testing RAM: fill..."
158
159;Fill RAM
160 ldiw x,0
161ramtestw:
162 mov temp,xh
163 eor temp,xl
4675c141 164 rcall dram_write_pp
9c15f366
L
165 brcc ramtestw
166 printstring "wait..."
167
168 ldi temp2,8
169ramtestwl:
170 ldi temp,255
b741422e 171 rcall delay_ms
9c15f366
L
172 dec temp2
173 brne ramtestwl
174
175 printstring "reread..."
176
177;re-read RAM
178 ldiw x,0
fa9059af 179 clr temp3 ;Error counter
9c15f366 180ramtestr:
bca3519f 181 rcall dram_read
fa9059af
L
182
183; ori temp,0x04 ;simulate error
12a27f27 184; andi temp,0xF7 ;another error
fa9059af 185
9c15f366
L
186 mov temp2,xh
187 eor temp2,xl
188 cp temp,temp2
189 breq ramtestrok
fa9059af
L
190 tst temp3
191 brne ramtestr1
192 printnewline
193 printstring "Addr xx yy "
194ramtestr1:
195 printnewline
825ecc9d 196 mov zl,temp
fa9059af
L
197 movw temp,x
198 rcall printhexw
199 rcall printspace
9c15f366
L
200 mov temp,xh
201 eor temp,xl
fa9059af 202 mov temp2,temp
9c15f366 203 rcall printhex
fa9059af 204 rcall printspace
825ecc9d 205 mov temp,zl
fa9059af
L
206 rcall printhex
207 rcall printspace
208 mov temp,temp2
825ecc9d 209 eor temp,zl
fa9059af
L
210 and temp,temp2
211 rcall printxbits
212 rcall printspace
213 mov temp,temp2
825ecc9d 214 eor temp,zl
fa9059af
L
215 com temp2
216 and temp,temp2
217 rcall printxbits
218
219 inc temp3
220 cpi temp3,16 ;
221 brsh ramtestrex
9c15f366
L
222ramtestrok:
223 adiw xl,1
224 brcc ramtestr
fa9059af
L
225ramtestrex:
226 tst temp3 ;any errors?
4bd49b80 227 breq ramtestend
fa9059af
L
228
229 printstring " System halted!"
230halted_loop:
231 rjmp halted_loop ;keep AVR in an endless loop
232
e8384f88
L
233printxbits:
234 push temp2
235 push temp3
236 mov temp2,temp
237 ldi temp3,8
238prntxb0:
239 ldi temp,'-'
240 lsl temp2
241 brcc prntxb1
242 ldi temp,'X'
243prntxb1:
244 rcall uartPutc
245 dec temp3
246 brne prntxb0
247 pop temp3
248 pop temp2
249 ret
250
fa9059af 251ramtestend:
9c15f366
L
252
253.endif
254
255.if MEMFILL
256 ldiw x,0
02d57479 257 ldi temp,MEMFILL_VAL
9c15f366 258ramfillw:
4675c141 259 rcall dram_write_pp
9c15f366
L
260 brcc ramfillw
261.endif
262
263
264;----------------------------------------------------------------------------
265
266boot_again:
267 printnewline
dd7aea8c 268 printstring "Initing mmc..."
64219415 269 printnewline
b741422e 270 lcall mgr_init_partitions
9c15f366
L
271
272 cbr temp,0x80
273 brne boot_ipl2
274 printstring "No bootable CP/M disk found! Please change MMC/SD-Card."
64219415 275 printnewline
9c15f366
L
276 ldi temp2,18
277boot_iplwl:
278 ldi temp,255
279 rcall delay_ms
280 dec temp2
281 brne boot_iplwl
282 rjmp boot_again
4bd49b80 283
9c15f366
L
284
285boot_ipl2:
b741422e 286 lcall mgr_prnt_parttbl
9c15f366
L
287 printnewline
288 printstring "Partinit done."
4bd49b80 289
2ccaac16
L
290; Init (de)blocking buffer
291
292 lcall dsk_inval_hostbuf
678fc0b0 293
4675c141 294; Read first sector of first CP/M partition (ipl)
9c15f366 295
e8384f88
L
296 ldiw y,fsys_vars
297
92202636 298; Disk 0
e8384f88 299 std y+o_seekdsk,_0
92202636 300; Track 0
e8384f88
L
301 std y+o_seektrk, _0
302 std y+o_seektrk+1,_0
92202636 303; Sector 0
e8384f88 304 std y+o_seeksec,_0
92202636 305
dd7aea8c
L
306; Destination
307 ldiw x,IPLADDR
e8384f88
L
308 std y+o_dmaadr+0,xl
309 std y+o_dmaadr+1,xh
4bd49b80 310
2ccaac16
L
311 ldi temp,1<<READ_FUNC
312 lcall dskDoIt
9c15f366 313
dd7aea8c 314; lift off
b741422e 315 ljmp z80_init
9c15f366
L
316
317
e8384f88 318; vim:set ts=8 noet nowrap