]> cloudbase.mooo.com Git - avrcpm.git/blame_incremental - avr/init.asm
* New config option: SRAM_FILL
[avrcpm.git] / avr / init.asm
... / ...
CommitLineData
1; Hardware initialisation, disk, mmc, timer, DRAM test
2;
3; Copyright (C) 2010 Sprite_tm
4; Copyright (C) 2010-2013 Leo C.
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;
21; $Id$
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:
37 ldi temp,low(RAMEND) ; top of memory
38 out SPL,temp ; init stack pointer
39 ldi temp,high(RAMEND) ; top of memory
40 out SPH,temp ; init stack pointer
41
42; - Load some registers with constant values
43
44 ldiw z,regval_tab*2
45 ldiw y,0
46cp_l: lpm xh,z+
47 st y+,xh
48 cpi zl,low(regval_tab_e*2)
49 brne cp_l
50
51; - Kill wdt
52
53 wdr
54 out MCUSR,_0
55
56 ldi temp,(1<<WDCE) | (1<<WDE)
57 outm8 WDTCSR,temp
58 ldi temp,(1<<WDCE)
59 outm8 WDTCSR,temp
60
61; - Setup Ports
62
63; ldi temp,(1<<PUD) ;disable pullups
64; outm8 P_PUD,temp
65 out PORTD,_255 ;all pins high (enables pullup on input ports)
66 out PORTB,_255
67 out PORTC,_255
68 out DDRD,_255 ; all outputs
69 out DDRB,_255
70 out DDRC,_255
71 cbi P_RXD-1,RXD ; RXD pin is input
72
73 outm8 TIMSK1,_0
74 outm8 TIMSK2,_0
75 outm8 TCCR2A,_0
76 outm8 TCCR2B,_0
77
78; - Clear RAM
79
80 ldiw z,SRAM_START
81 ldi temp2,high(ramtop)
82clr_loop:
83 st z+,_0
84 cpi zl,low(ramtop)
85 cpc zh,temp2
86 brne clr_loop
87
88; - Fill unused RAM (stack)
89
90 ldi temp2,high(RAMEND+1)
91 ldi temp,SRAMFILL_VAL
92fill_loop:
93 st z+,temp
94 cpi zl,low(RAMEND+1)
95 cpc zh,temp2
96 brne fill_loop
97
98; Init clock/timer system
99
100; Init timer 1 as 1 ms system clock tick.
101
102 ldi temp, low (F_CPU/1000)
103 ldi temp2,high(F_CPU/1000)
104 outm8 OCR1BH,temp2
105 outm8 OCR1BL,temp
106 ldi temp,(1<<ICNC1)|(1<<CS10) ;Noise cancel, fall. edge, Normal Mode, clk/1
107 outm8 TCCR1B,temp
108 inm8 temp,TIMSK1
109 ori temp,(1<<OCIE1B) ;Enable 1ms int.
110 outm8 TIMSK1,temp
111
112; - Init serial port
113
114 rcall uart_init
115
116;Init timer2. Refresh-call should happen every (8ms/512) cycles.
117
118 ldi temp,REFR_CNT*2 ; 2 cycles per int
119 outm8 OCR2A,temp
120 inm8 temp,TCCR2A
121 ori temp,(1<<WGM21) ;CTC mode
122 outm8 TCCR2A,temp
123 inm8 temp,TCCR2B
124 ori temp,REFR_CS ;clk/REFR_PRE
125 outm8 TCCR2B,temp
126 inm8 temp,TIMSK2
127 ori temp, (1<<OCIE2A)
128 outm8 TIMSK2,temp
129
130 sei
131
132#if I2C_SUPPORT
133 rcall i2c_init ; Init I2C master
134 rcall rtc_get
135#endif
136
137
138.if BOOTWAIT
139 ldi temp,10
140 rcall delay_ms
141
142.endif
143
144 rcall printstr
145 .db 13,13,"CPM on an AVR, v"
146 db_version VMAJOR, VMINOR
147 printstring " r" SVN_REVSTR " " TESTSTR
148
149.if MEMTEST
150 printnewline
151 printstring "Testing RAM: fill..."
152
153;Fill RAM
154 ldiw x,0
155ramtestw:
156 mov temp,xh
157 eor temp,xl
158 rcall dram_write_pp
159 brcc ramtestw
160 printstring "wait..."
161
162 ldi temp2,8
163ramtestwl:
164 ldi temp,255
165 rcall delay_ms
166 dec temp2
167 brne ramtestwl
168
169 printstring "reread..."
170
171;re-read RAM
172 ldiw x,0
173 clr temp3 ;Error counter
174ramtestr:
175 mem_read
176
177; ori temp,0x04 ;simulate error
178; andi temp,0xF7
179
180 mov temp2,xh
181 eor temp2,xl
182 cp temp,temp2
183 breq ramtestrok
184 tst temp3
185 brne ramtestr1
186 printnewline
187 printstring "Addr xx yy "
188ramtestr1:
189 printnewline
190 mov zl,temp
191 movw temp,x
192 rcall printhexw
193 rcall printspace
194 mov temp,xh
195 eor temp,xl
196 mov temp2,temp
197 rcall printhex
198 rcall printspace
199 mov temp,zl
200 rcall printhex
201 rcall printspace
202 mov temp,temp2
203 eor temp,zl
204 and temp,temp2
205 rcall printxbits
206 rcall printspace
207 mov temp,temp2
208 eor temp,zl
209 com temp2
210 and temp,temp2
211 rcall printxbits
212
213 inc temp3
214 cpi temp3,16 ;
215 brsh ramtestrex
216ramtestrok:
217 adiw xl,1
218 brcc ramtestr
219ramtestrex:
220 tst temp3 ;any errors?
221 breq ramtestend
222
223 printstring " System halted!"
224halted_loop:
225 rjmp halted_loop ;keep AVR in an endless loop
226
227ramtestend:
228
229.endif
230
231.if MEMFILL
232 ldiw x,0
233 ldi temp,MEMFILL_VAL
234ramfillw:
235 rcall dram_write_pp
236 brcc ramfillw
237.endif
238
239
240;----------------------------------------------------------------------------
241
242boot_again:
243 printnewline
244 printstring "Initing mmc..."
245 printnewline
246 lcall mgr_init_partitions
247
248 cbr temp,0x80
249 brne boot_ipl2
250 printstring "No bootable CP/M disk found! Please change MMC/SD-Card."
251 printnewline
252 ldi temp2,18
253boot_iplwl:
254 ldi temp,255
255 rcall delay_ms
256 dec temp2
257 brne boot_iplwl
258 rjmp boot_again
259
260
261boot_ipl2:
262 lcall mgr_prnt_parttbl
263 printnewline
264 printstring "Partinit done."
265
266; Init (de)blocking buffer
267
268 lcall dsk_inval_hostbuf
269
270; Read first sector of first CP/M partition (ipl)
271
272; Disk 0
273 sts seekdsk,_0
274; Track 0
275 sts seektrk,_0
276 sts seektrk+1,_0
277; Sector 0
278 sts seeksec,_0
279
280; Destination
281 ldiw x,IPLADDR
282 stsw dmaadr,x
283
284 ldi temp,1<<READ_FUNC
285 lcall dskDoIt
286
287; lift off
288 ljmp z80_init
289
290
291printspace:
292 push temp
293 ldi temp,' '
294 rcall uartputc
295 pop temp
296 ret
297
298printxbits:
299 push temp2
300 push temp3
301 mov temp2,temp
302 ldi temp3,8
303prntxb0:
304 ldi temp,'-'
305 lsl temp2
306 brcc prntxb1
307 ldi temp,'X'
308prntxb1:
309 rcall uartPutc
310 dec temp3
311 brne prntxb0
312 pop temp3
313 pop temp2
314 ret
315
316
317
318