]> cloudbase.mooo.com Git - avrcpm.git/blame - avr/init.asm
* Set macro I2C=0 as default.
[avrcpm.git] / avr / init.asm
CommitLineData
6efd6291 1; Hardware initialisation, disk, mmc, timer, DRAM test
9c15f366
L
2;
3; Copyright (C) 2010 Sprite_tm
4; Copyright (C) 2010 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
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_l:
83 st z+,_0
84 cpi zl,low(ramtop)
85 cpc zh,temp2
86 brne clr_l
87
9c15f366
L
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)=312 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
d8fa6a36
L
122#if I2C
123 rcall i2c_init ; Init I2C master
124 rcall rtc_get
125#endif
126
9c15f366
L
127
128.if BOOTWAIT
129 ldi temp,10
b741422e 130 rcall delay_ms
9c15f366
L
131
132.endif
133
134 rcall printstr
135 .db 13,13,"CPM on an AVR, v"
136 db_version VMAJOR, VMINOR
fa9059af 137 printstring " r" SVN_REVSTR
9c15f366
L
138
139.if MEMTEST
140 printnewline
141 printstring "Testing RAM: fill..."
142
143;Fill RAM
144 ldiw x,0
145ramtestw:
146 mov temp,xh
147 eor temp,xl
4675c141 148 rcall dram_write_pp
9c15f366
L
149 brcc ramtestw
150 printstring "wait..."
151
152 ldi temp2,8
153ramtestwl:
154 ldi temp,255
b741422e 155 rcall delay_ms
9c15f366
L
156 dec temp2
157 brne ramtestwl
158
159 printstring "reread..."
160
161;re-read RAM
162 ldiw x,0
fa9059af 163 clr temp3 ;Error counter
9c15f366
L
164ramtestr:
165 mem_read
fa9059af
L
166
167; ori temp,0x04 ;simulate error
168; andi temp,0xF7
169
9c15f366
L
170 mov temp2,xh
171 eor temp2,xl
172 cp temp,temp2
173 breq ramtestrok
fa9059af
L
174 tst temp3
175 brne ramtestr1
176 printnewline
177 printstring "Addr xx yy "
178ramtestr1:
179 printnewline
180 mov temp4,temp
181 movw temp,x
182 rcall printhexw
183 rcall printspace
9c15f366
L
184 mov temp,xh
185 eor temp,xl
fa9059af 186 mov temp2,temp
9c15f366 187 rcall printhex
fa9059af
L
188 rcall printspace
189 mov temp,temp4
190 rcall printhex
191 rcall printspace
192 mov temp,temp2
193 eor temp,temp4
194 and temp,temp2
195 rcall printxbits
196 rcall printspace
197 mov temp,temp2
198 eor temp,temp4
199 com temp2
200 and temp,temp2
201 rcall printxbits
202
203 inc temp3
204 cpi temp3,16 ;
205 brsh ramtestrex
9c15f366
L
206ramtestrok:
207 adiw xl,1
208 brcc ramtestr
fa9059af
L
209ramtestrex:
210 tst temp3 ;any errors?
211 breq ramtestend
212
213 printstring " System halted!"
214halted_loop:
215 rjmp halted_loop ;keep AVR in an endless loop
216
217ramtestend:
9c15f366
L
218
219.endif
220
221.if MEMFILL
222 ldiw x,0
223ramfillw:
224 ldi temp,MEMFILL_VAL
4675c141 225 rcall dram_write_pp
9c15f366
L
226 brcc ramfillw
227.endif
228
229
230;----------------------------------------------------------------------------
231
232boot_again:
233 printnewline
dd7aea8c 234 printstring "Initing mmc..."
64219415 235 printnewline
b741422e 236 lcall mgr_init_partitions
9c15f366
L
237
238 cbr temp,0x80
239 brne boot_ipl2
240 printstring "No bootable CP/M disk found! Please change MMC/SD-Card."
64219415 241 printnewline
9c15f366
L
242 ldi temp2,18
243boot_iplwl:
244 ldi temp,255
245 rcall delay_ms
246 dec temp2
247 brne boot_iplwl
248 rjmp boot_again
249
250
251boot_ipl2:
b741422e 252 lcall mgr_prnt_parttbl
9c15f366
L
253 printnewline
254 printstring "Partinit done."
dd7aea8c 255
dd7aea8c 256 lcall dsk_inval_hostbuf ;init (de)blocking buffer
678fc0b0 257
4675c141 258; Read first sector of first CP/M partition (ipl)
9c15f366 259
92202636
L
260; Disk 0
261 sts seekdsk,_0
262; Track 0
263 sts seektrk,_0
264 sts seektrk+1,_0
265; Sector 0
266 sts seeksec,_0
267
dd7aea8c
L
268; Destination
269 ldiw x,IPLADDR
270 stsw dmaadr,x
271
272 lcall dsk_read
9c15f366 273
dd7aea8c 274; lift off
b741422e 275 ljmp z80_init
9c15f366
L
276
277
fa9059af
L
278printspace:
279 push temp
280 ldi temp,' '
281 rcall uartputc
282 pop temp
283 ret
284
285printxbits:
286 push temp2
287 push temp3
288 mov temp2,temp
289 ldi temp3,8
290prntxb0:
291 ldi temp,'-'
292 lsl temp2
293 brcc prntxb1
294 ldi temp,'X'
295prntxb1:
296 rcall uartPutc
297 dec temp3
298 brne prntxb0
299 pop temp3
300 pop temp2
301 ret
302
303
304
305