Subversion Repositories svn.mios

Rev

Rev 52 | Rev 379 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
53 tk 1
; $Id: mios_startup.inc 53 2008-01-30 22:52:41Z tk $
1 tk 2
;
3
; MIOS system startup routine
4
;
5
; ==========================================================================
6
;
7
;  Copyright 1998-2006 Thorsten Klose (tk@midibox.org)
8
;  Licensed for personal non-commercial use only.
9
;  All other rights reserved.
10
;
11
; ==========================================================================
12
 
13
;; --------------------------------------------------------------------------
14
;;  FUNCTION: USER_Init
15
;;  C_DECLARATION: void Init(void)
16
;;  DESCRIPTION: This function is called by MIOS after startup to initialize the
17
;;  application
18
;;  IN:	  -
19
;;  C_IN:  -
20
;;  OUT:  -
21
;;  C_OUT:  -
22
;;  ISR: no
23
;; --------------------------------------------------------------------------
24
 
25
;; --------------------------------------------------------------------------
26
;;  FUNCTION: USER_Tick
27
;;  C_DECLARATION: void Tick(void)
28
;;  DESCRIPTION: This function is called by MIOS in the mainloop when
29
;;  nothing else is to do
30
;;  IN:	  -
31
;;  C_IN:  -
32
;;  OUT:  -
33
;;  C_OUT:  -
34
;;  ISR: no
35
;; --------------------------------------------------------------------------
36
 
37
MIOS_STARTUP
38
	clrwdt
39
 
40
	;; ------------------------------------------------------------------
41
	;;  initialize the SRAM
42
	;; ------------------------------------------------------------------
43
	lfsr	FSR0, 0x000
44
MIOS_STARTUP_ClearRAMLoop
45
	clrf	POSTINC0
46
	movlw	LOW(PIC_DERIVATIVE_RAM_SIZE)
47
	cpfseq	FSR0L, ACCESS
48
	rgoto MIOS_STARTUP_ClearRAMLoop
49
	movlw	HIGH(PIC_DERIVATIVE_RAM_SIZE)
50
	cpfseq	FSR0H, ACCESS
51
	rgoto MIOS_STARTUP_ClearRAMLoop
52
 
53
	;; ------------------------------------------------------------------
54
	;;  initialize the Ports
55
	;; ------------------------------------------------------------------
56
	bcf	INTCON2, NOT_RBPU	; Enable Pull-Ups of PortB
57
 
58
	clrf	PORTA			; Reset PortA
59
	clrf	PORTB			; Reset PortB
60
	movlw	0x40			; Reset PortC (TX Pin = 1)
61
	movwf	PORTC
62
	clrf	PORTD			; Reset PortD
63
	clrf	PORTE			; Reset PortE
64
 
65
	movlw	b'11101111'		; all PORTA pins beside if RA4 are tristate
66
	movwf	TRISA
67
#if PIC_DERIVATIVE_SET_LCD_4BIT	; for CAN derivatives, where Tx/Rx is located at RB2/RB3
68
	movlw	0x0f
69
	movwf	TRISB
70
#else
71
	clrf	TRISB			; PORTB driver enabled by default (display data port)
72
#endif
73
	movlw	b'10000000'		; Rx Pin tristate, all other outputs
74
	movwf	TRISC
75
	movlw	b'00000010'		; DIN pin tristate, all other outputs
76
	movwf	TRISD
77
	movlw	b'00000111'		; all PORTE pins are tristate
78
	movwf	TRISE
79
 
80
#if PIC_DERIVATIVE_NEW_ADC	; PIC18F4620, PIC18F4520, ...
81
	movlw	0x07					; PortA and E: All Pins are analog pins
82
	movwf	ADCON1
83
	movlw	(1 << ADFM) | (0 << 3) | (6 << 0)	; right justified results, ACQT=20 AD, ADCS=FOSC/64
84
	movwf	ADCON2
85
#else				; PIC18F452
86
	movlw	(1 << ADFM) | (1 << ADCS2) | 0x00 	; PortA and E: All Pins are analog pins
87
	movwf	ADCON1
88
#endif
89
 
90
#if PIC_DERIVATIVE_CMCON_INIT	; PIC18F4620, PIC18F4520, ...
91
	movlw	0x07		; turn off comparators
92
	movwf	CMCON		; (reset value of PIC18F4620/PIC18F4520, but not of PIC18F4685 and PIC18F4682
93
#endif
94
 
95
	;; ------------------------------------------------------------------
96
	;;  initialize the auto settings
97
	;; ------------------------------------------------------------------
98
	call	MIOS_MIDI_DeviceIDAutoSet
99
	call	MIOS_MIDI_InterfaceAutoSet
100
	call	MIOS_LCD_TypeAutoSet
101
 
102
	;; ------------------------------------------------------------------
103
	;;  initialize the MIDI interface
104
	;; ------------------------------------------------------------------
105
	call	MIOS_MIDI_Init
106
 
107
	;; ------------------------------------------------------------------
108
	;;  start the timers
109
	;; ------------------------------------------------------------------
110
	;; switch on timer0 (don't make difference on 20/40 MHz clock due to performance reasons
111
#if PIC_DERIVATIVE_T08BIT_INVERTED	; e.g. PIC18F4620
112
 	movlw	(1 << TMR0ON) | (1 << T016BIT) | 0x01	; internal clock source, 8 bit, prescaler 1:4
113
#else				; all others
114
	movlw	(1 << TMR0ON) | (1 << T08BIT) | 0x01	; internal clock source, 8 bit, prescaler 1:4
115
#endif
116
	movwf	T0CON
117
 
118
	;; switch on timer1
119
	movlw	(1 << TMR1ON)
120
	movwf	T1CON
121
 
122
	;; timer3 configured by user
123
	bcf	T3CON, TMR3ON
124
 
125
	;; ------------------------------------------------------------------
126
	;;  enable the interrupts
127
	;; ------------------------------------------------------------------
128
        movlw   (1 << RCIE) | (1 << TMR1IE)		; enable UART Receive and Timer1 IRQ
129
							;; Tx IRQ will be enabled dynamically
130
        movwf   PIE1
131
	movlw	(1 << TMR3IE)				; enable Timer3 IRQ
132
	movwf	PIE2
133
 
134
	;; make sure that USART Receive Buffer is empty
135
        bcf     RCSTA, CREN
136
        movf    RCREG, W
137
        bsf     RCSTA, CREN
138
 
139
	;; enable maskable IRQs
140
	;; T0IE now enabled seperately by AIN driver - so long not enabled, Timer0 is free as additional resource
141
        movlw   (1 << GIE) | (1 << PEIE)
142
	;;         movlw   (1 << GIE) | (1 << PEIE) | (1 << T0IE)
143
        movwf   INTCON
144
 
145
	;; ------------------------------------------------------------------
146
	;;  configure the default LCD offsets
147
	;; ------------------------------------------------------------------
148
	movlw	0x00
149
	movwf	MIOS_PARAMETER1
150
	movlw	0x40
151
	movwf	MIOS_PARAMETER2
152
	movlw	0x14
153
	movwf	MIOS_PARAMETER3
154
	movlw	0x54
155
	call	MIOS_LCD_YAddressSet
156
 
157
	TABLE_ADDR MIOS_GLCD_FONT	; init font
158
	call	MIOS_GLCD_FontInit
159
 
160
	;; ------------------------------------------------------------------
161
	;;  call the user init routine now!
162
	;; ------------------------------------------------------------------
163
	call	USER_Init
164
 
165
	;; ------------------------------------------------------------------
166
	;;  initialize the LCD
167
	;; ------------------------------------------------------------------
168
	call	MIOS_LCD_Init
169
 
170
	;; ------------------------------------------------------------------
171
	;;  print copyright message
172
	;; ------------------------------------------------------------------
173
	;; clear display
174
	call	MIOS_LCD_Clear
175
 
176
	;; print two lines
177
	TABLE_ADDR MIOS_STARTUP_COPYRIGHT_0
178
	call	MIOS_LCD_PrintString
179
	call	MIOS_LCD_PrintString
180
 
181
	;; wait for 1.5 seconds
182
	movlw	6
183
	movwf	TMP1
184
MIOS_STARTUP_CopyrightWait
185
	clrwdt
186
	movlw	250
187
	call	MIOS_Delay
188
	decfsz	TMP1, F
189
	rgoto	MIOS_STARTUP_CopyrightWait
190
 
191
	call	MIOS_LCD_Clear
192
 
193
	;; ------------------------------------------------------------------
194
	;;  clear CHANGED flags of buttons
195
	;; ------------------------------------------------------------------
196
	movlw	0x10
197
	movwf	TMP1
198
	lfsr	FSR0, MIOS_SR_DIN_CHANGED_0
199
MIOS_STARTUP_ClearDINChngdLoop
200
	clrf	POSTINC0
201
	decfsz	TMP1, F
202
	rgoto	MIOS_STARTUP_ClearDINChngdLoop
203
 
204
	;; ------------------------------------------------------------------
205
	;;  clear the upper DIN state range (mainly used by the encoders)
206
	;; ------------------------------------------------------------------
207
	movlw	0x80
208
	movwf	TMP1
209
	lfsr	FSR0, MIOS_DIN_REG1_00
210
MIOS_STARTUP_ClearDINReg1Loop
211
	clrf	POSTINC0
212
	decfsz	TMP1, F
213
	rgoto	MIOS_STARTUP_ClearDINReg1Loop
214
 
215
	;; ------------------------------------------------------------------
216
	;;  clear CHANGED flags of pots
217
	;; ------------------------------------------------------------------
218
	movlw	0x40		; checked: also ok for MF driver
219
	movwf	TMP1
220
	lfsr	FSR0, MIOS_AIN_RESULT_H_00
221
MIOS_STARTUP_ClearPotValLoop
222
	bcf	POSTINC0, 7
223
	decfsz	TMP1, F
224
	rgoto	MIOS_STARTUP_ClearPotValLoop
225
 
226
	;; ------------------------------------------------------------------
227
	;;  init display counter
228
	;; ------------------------------------------------------------------
229
	movlw	0x01
230
	SET_BSR	MIOS_MESSAGE_CTR
231
	movwf	MIOS_MESSAGE_CTR, BANKED
232
 
233
	;; ------------------------------------------------------------------
234
	;;  finally branch to MIOS mainloop
235
	;; ------------------------------------------------------------------
236
	goto	MIOS_Mainloop
237
 
238
 
239
MIOS_STARTUP_COPYRIGHT_0 MIOS_VERSION_STR ; (defined in main.asm)
240
MIOS_STARTUP_COPYRIGHT_1 STRING 16, 0x40, "(C) 2007 T.Klose"