]> cloudbase.mooo.com Git - avrcpm.git/blob - avr/init.asm
dcc945992e0a75553d25c80689a89832bf49d547
[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: init.asm 241 2015-12-10 09:38:25Z rapid $
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; vim:set ts=8 noet nowrap
38
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
47 cp_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
61
62 ; - Setup Ports
63
64 ; ldi temp,(1<<PUD) ;disable pullups
65 ; outm8 P_PUD,temp
66 out PORTD,_255 ;all pins high (enables pullup on input ports)
67 out PORTB,_255
68 out PORTC,_255
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
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)
88 clr_loop:
89 st z+,_0
90 cpi zl,low(ramtop)
91 cpc zh,temp2
92 brne clr_loop
93
94 ; - Fill unused RAM (stack)
95
96 ldi temp2,high(RAMEND+1)
97 ldi temp,SRAMFILL_VAL
98 fill_loop:
99 st z+,temp
100 cpi zl,low(RAMEND+1)
101 cpc zh,temp2
102 brne fill_loop
103
104 ; Init clock/timer system
105
106 ; Init timer 1 as 1 ms system clock tick.
107
108 ldi temp, low (TC_1MS)
109 ldi temp2,high(TC_1MS)
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
121
122 ;Init timer2. Refresh-call should happen every (8ms/512) cycles.
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
138 #if I2C_SUPPORT
139 rcall i2c_init ; Init I2C master
140 rcall rtc_get
141 #endif
142
143
144 .if BOOTWAIT
145 ldi temp,10
146 rcall delay_ms
147
148 .endif
149
150 rcall printstr
151 .db '\r', '\r'
152 version_string:
153 makestring "CPM on an AVR, v" VERS_STR " r" SVN_REVSTR TESTSTR
154
155 .if MEMTEST
156 printnewline
157 printstring "Testing RAM: fill..."
158
159 ;Fill RAM
160 ldiw x,0
161 ramtestw:
162 mov temp,xh
163 eor temp,xl
164 rcall dram_write_pp
165 brcc ramtestw
166 printstring "wait..."
167
168 ldi temp2,8
169 ramtestwl:
170 ldi temp,255
171 rcall delay_ms
172 dec temp2
173 brne ramtestwl
174
175 printstring "reread..."
176
177 ;re-read RAM
178 ldiw x,0
179 clr temp3 ;Error counter
180 ramtestr:
181 rcall dram_read
182
183 ; ori temp,0x04 ;simulate error
184 ; andi temp,0xF7 ;another error
185
186 mov temp2,xh
187 eor temp2,xl
188 cp temp,temp2
189 breq ramtestrok
190 tst temp3
191 brne ramtestr1
192 printnewline
193 printstring "Addr xx yy "
194 ramtestr1:
195 printnewline
196 mov zl,temp
197 movw temp,x
198 rcall printhexw
199 rcall printspace
200 mov temp,xh
201 eor temp,xl
202 mov temp2,temp
203 rcall printhex
204 rcall printspace
205 mov temp,zl
206 rcall printhex
207 rcall printspace
208 mov temp,temp2
209 eor temp,zl
210 and temp,temp2
211 rcall printxbits
212 rcall printspace
213 mov temp,temp2
214 eor temp,zl
215 com temp2
216 and temp,temp2
217 rcall printxbits
218
219 inc temp3
220 cpi temp3,16 ;
221 brsh ramtestrex
222 ramtestrok:
223 adiw xl,1
224 brcc ramtestr
225 ramtestrex:
226 tst temp3 ;any errors?
227 breq ramtestend
228
229 printstring " System halted!"
230 halted_loop:
231 rjmp halted_loop ;keep AVR in an endless loop
232
233 printxbits:
234 push temp2
235 push temp3
236 mov temp2,temp
237 ldi temp3,8
238 prntxb0:
239 ldi temp,'-'
240 lsl temp2
241 brcc prntxb1
242 ldi temp,'X'
243 prntxb1:
244 rcall uartPutc
245 dec temp3
246 brne prntxb0
247 pop temp3
248 pop temp2
249 ret
250
251 ramtestend:
252
253 .endif
254
255 .if MEMFILL
256 ldiw x,0
257 ldi temp,MEMFILL_VAL
258 ramfillw:
259 rcall dram_write_pp
260 brcc ramfillw
261 .endif
262
263
264 ;----------------------------------------------------------------------------
265
266 boot_again:
267 printnewline
268 printstring "Initing mmc..."
269 printnewline
270 lcall mgr_init_partitions
271
272 cbr temp,0x80
273 brne boot_ipl2
274 printstring "No bootable CP/M disk found! Please change MMC/SD-Card."
275 printnewline
276 ldi temp2,18
277 boot_iplwl:
278 ldi temp,255
279 rcall delay_ms
280 dec temp2
281 brne boot_iplwl
282 rjmp boot_again
283
284
285 boot_ipl2:
286 lcall mgr_prnt_parttbl
287 printnewline
288 printstring "Partinit done."
289
290 ; Init (de)blocking buffer
291
292 lcall dsk_inval_hostbuf
293
294 ; Read first sector of first CP/M partition (ipl)
295
296 ldiw y,fsys_vars
297
298 ; Disk 0
299 std y+o_seekdsk,_0
300 ; Track 0
301 std y+o_seektrk, _0
302 std y+o_seektrk+1,_0
303 ; Sector 0
304 std y+o_seeksec,_0
305
306 ; Destination
307 ldiw x,IPLADDR
308 std y+o_dmaadr+0,xl
309 std y+o_dmaadr+1,xh
310
311 ldi temp,1<<READ_FUNC
312 lcall dskDoIt
313
314 ; lift off
315 ljmp z80_init
316
317
318 ; vim:set ts=8 noet nowrap