]> cloudbase.mooo.com Git - avrcpm.git/blob - avr/init.asm
* avr/dsk_fsys.asm
[avrcpm.git] / avr / init.asm
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
29 regval_tab:
30 .db 0,0
31 .db 0xFE,0xFC ; _RAS0 _CAS0
32 .db 0xF8,0xF4 ; _OE _WE
33 .db 255,0 ; _255 _0
34 regval_tab_e:
35
36 start:
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
46 cp_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)
82 clr_l:
83 st z+,_0
84 cpi zl,low(ramtop)
85 cpc zh,temp2
86 brne clr_l
87
88 ; Init clock/timer system
89
90 ; Init timer 1 as 1 ms system clock tick.
91
92 ldi temp, low (F_CPU/1000)
93 ldi temp2,high(F_CPU/1000)
94 outm8 OCR1BH,temp2
95 outm8 OCR1BL,temp
96 ldi temp,(1<<ICNC1)|(1<<CS10) ;Noise cancel, fall. edge, Normal Mode, clk/1
97 outm8 TCCR1B,temp
98 inm8 temp,TIMSK1
99 ori temp,(1<<OCIE1B) ;Enable 1ms int.
100 outm8 TIMSK1,temp
101
102 ; - Init serial port
103
104 rcall uart_init
105
106 ;Init timer2. Refresh-call should happen every (8ms/512) cycles.
107
108 ldi temp,REFR_CNT*2 ; 2 cycles per int
109 outm8 OCR2A,temp
110 inm8 temp,TCCR2A
111 ori temp,(1<<WGM21) ;CTC mode
112 outm8 TCCR2A,temp
113 inm8 temp,TCCR2B
114 ori temp,REFR_CS ;clk/REFR_PRE
115 outm8 TCCR2B,temp
116 inm8 temp,TIMSK2
117 ori temp, (1<<OCIE2A)
118 outm8 TIMSK2,temp
119
120 sei
121
122 #if I2C_SUPPORT
123 rcall i2c_init ; Init I2C master
124 rcall rtc_get
125 #endif
126
127
128 .if BOOTWAIT
129 ldi temp,10
130 rcall delay_ms
131
132 .endif
133
134 rcall printstr
135 .db 13,13,"CPM on an AVR, v"
136 db_version VMAJOR, VMINOR
137 printstring " r" SVN_REVSTR " " TESTSTR
138
139 .if MEMTEST
140 printnewline
141 printstring "Testing RAM: fill..."
142
143 ;Fill RAM
144 ldiw x,0
145 ramtestw:
146 mov temp,xh
147 eor temp,xl
148 rcall dram_write_pp
149 brcc ramtestw
150 printstring "wait..."
151
152 ldi temp2,8
153 ramtestwl:
154 ldi temp,255
155 rcall delay_ms
156 dec temp2
157 brne ramtestwl
158
159 printstring "reread..."
160
161 ;re-read RAM
162 ldiw x,0
163 clr temp3 ;Error counter
164 ramtestr:
165 mem_read
166
167 ; ori temp,0x04 ;simulate error
168 ; andi temp,0xF7
169
170 mov temp2,xh
171 eor temp2,xl
172 cp temp,temp2
173 breq ramtestrok
174 tst temp3
175 brne ramtestr1
176 printnewline
177 printstring "Addr xx yy "
178 ramtestr1:
179 printnewline
180 mov zl,temp
181 movw temp,x
182 rcall printhexw
183 rcall printspace
184 mov temp,xh
185 eor temp,xl
186 mov temp2,temp
187 rcall printhex
188 rcall printspace
189 mov temp,zl
190 rcall printhex
191 rcall printspace
192 mov temp,temp2
193 eor temp,zl
194 and temp,temp2
195 rcall printxbits
196 rcall printspace
197 mov temp,temp2
198 eor temp,zl
199 com temp2
200 and temp,temp2
201 rcall printxbits
202
203 inc temp3
204 cpi temp3,16 ;
205 brsh ramtestrex
206 ramtestrok:
207 adiw xl,1
208 brcc ramtestr
209 ramtestrex:
210 tst temp3 ;any errors?
211 breq ramtestend
212
213 printstring " System halted!"
214 halted_loop:
215 rjmp halted_loop ;keep AVR in an endless loop
216
217 ramtestend:
218
219 .endif
220
221 .if MEMFILL
222 ldiw x,0
223 ramfillw:
224 ldi temp,MEMFILL_VAL
225 rcall dram_write_pp
226 brcc ramfillw
227 .endif
228
229
230 ;----------------------------------------------------------------------------
231
232 boot_again:
233 printnewline
234 printstring "Initing mmc..."
235 printnewline
236 lcall mgr_init_partitions
237
238 cbr temp,0x80
239 brne boot_ipl2
240 printstring "No bootable CP/M disk found! Please change MMC/SD-Card."
241 printnewline
242 ldi temp2,18
243 boot_iplwl:
244 ldi temp,255
245 rcall delay_ms
246 dec temp2
247 brne boot_iplwl
248 rjmp boot_again
249
250
251 boot_ipl2:
252 lcall mgr_prnt_parttbl
253 printnewline
254 printstring "Partinit done."
255
256 ; Init (de)blocking buffer
257
258 lcall dsk_inval_hostbuf
259
260 ; Read first sector of first CP/M partition (ipl)
261
262 ; Disk 0
263 sts seekdsk,_0
264 ; Track 0
265 sts seektrk,_0
266 sts seektrk+1,_0
267 ; Sector 0
268 sts seeksec,_0
269
270 ; Destination
271 ldiw x,IPLADDR
272 stsw dmaadr,x
273
274 ldi temp,1<<READ_FUNC
275 lcall dskDoIt
276
277 ; lift off
278 ljmp z80_init
279
280
281 printspace:
282 push temp
283 ldi temp,' '
284 rcall uartputc
285 pop temp
286 ret
287
288 printxbits:
289 push temp2
290 push temp3
291 mov temp2,temp
292 ldi temp3,8
293 prntxb0:
294 ldi temp,'-'
295 lsl temp2
296 brcc prntxb1
297 ldi temp,'X'
298 prntxb1:
299 rcall uartPutc
300 dec temp3
301 brne prntxb0
302 pop temp3
303 pop temp2
304 ret
305
306
307
308