v0.1 - Some receiving works with the new MCU using HW SPI, yay!
[wj-unilink2.git] / wjuni2.asm
1                 title   "PIC16F876A Unilink Interface by Werner Johansson (c) 2004-2005"\r
2                 subtitl "Definitions"\r
3                 list    c=132,P=16F876a,R=DEC,F=inhx8m\r
4         include "p16f876a.inc"            ; Standard equates & Macros\r
5         ERRORLEVEL 1,-302               ; Get rid of those annoying 302 msgs!\r
6 \r
7 ;----------------------------------------------------------------\r
8 ;  The Configuration Word\r
9       __CONFIG _HS_OSC&_WDT_OFF&_PWRTE_OFF&_BODEN_ON&_LVP_OFF&_CPD_OFF&_WRT_OFF&_DEBUG_OFF&_CP_OFF\r
10 \r
11 ;----------------------------------------------------------------\r
12 ;       HISTORY\r
13 ;----------------------------------------------------------------\r
14 ;  Version\r
15 ;\r
16 ;  0.0  Very first "Fucking No Work!" version\r
17 ;  0.1  Some receiving works with the new MCU using HW SPI, yay!\r
18 ;\r
19 ;----------------------------------------------------------------\r
20 \r
21 ; Unilink BUSON IN (blue) connected to RA4C2/T0CKI\r
22 ; Unilink DATA (green) connected to RC4(SDI), RC5(SDO) via open collector driver (HW USART)\r
23 ; Unilink BUSON OUT (blue) connected to RA5 via 100R resistor (this is for daisy-chaining)\r
24 ; Unilink CLK (yellow) connected to RC3(SCK) (USART CLK)\r
25 ; Unilink RST (lilac) connected to RB0(INT)\r
26 ; RS-232 TX from computer connected to RC7/RX\r
27 ; RS-232 RX to computer connected to RC6/TX\r
28 ; RS-232 RTS from computer connected to RC2\r
29 ; RS-232 RI and CTS to computer connected to RC1\r
30 ; RS-232 INVALID signal from MAX connected to RC0\r
31 \r
32 ; Pin mapping as follows:\r
33 \r
34 ; RA0 Voltage sense (A/D in 0)\r
35 ; RA1 Amp sense 1 (A/D in 1)\r
36 ; RA2 Early-Startup sense/Blue LED drive (active low) (RA2 and RA3 have been reversed from the PCB layout!)\r
37 ; RA3 Amp sense 2 (A/D in 3)\r
38 ; RA4/T0CKI BUSON IN from Head Unit (can trigger interrupt on clocking?)\r
39 ; RA5 BUSON OUT to cascade\r
40 \r
41 ; RB0 UNI RST in (interrupt triggered)\r
42 ; RB1 Enable-PWR output to SEPIC converter\r
43 ; RB2 Disable-AMP output\r
44 ; RB3 Enable-AUX output to trigger analog switch relay\r
45 ; RB4 Sign output from Amp sense 1/Red LED drive, active low\r
46 ; RB5 Sign output from Amp sense 2/Green LED drive, active low\r
47 ; RB6 ICSP/ICD reserved\r
48 ; RB7 ICSP/ICD reserved\r
49 \r
50 ; RC0 INVALID input from MAX3235\r
51 ; RC1 CTS and RI output\r
52 ; RC2 RTS input\r
53 ; RC3 Unilink clock input\r
54 ; RC4 Unilink data input\r
55 ; RC5 Unilink data output (drives inverted!)\r
56 ; RC6 TX output\r
57 ; RC7 RX input\r
58 \r
59 ;----------------------------------------------------------------\r
60 ; File register usage\r
61 \r
62 ; Memory from 20h to  7fh (BANK 0 96 bytes)\r
63 ;             a0h to  efh (BANK 1 80 bytes)\r
64 ;            110h to 16fh (BANK 2 96 bytes)\r
65 ;            190h to 1efh (BANK 3 96 bytes)\r
66 ; 70-7fh, f0-ffh, 170-17fh and 1f0-1ffh share the same 16 bytes, for ISR!)\r
67 \r
68 Dcount  equ     20h\r
69 e_LEN   equ     21h\r
70 Icount  equ   2Dh   ; Offset of string to print\r
71 TxTemp  equ   2Eh   ; blahblah\r
72 TxTemp2 equ   2Fh   ; Blahblah2\r
73 \r
74                 org     0\r
75                 nop                                             ; Leave the first location, just in case\r
76 ;               call    Bootstrap               ; Call Flash Load routine\r
77 ;               call    LCDInit                 ; Initialize LCD I/F\r
78 ;               call    IRQInit                 ; Set up and start the IRQ handler\r
79                 goto    Main                    ; Run the main program loop (skip the IRQ handler)\r
80 \r
81                 subtitl "IRQ Handler"\r
82 ;----------------------------------------------------------------\r
83 ; Interrupt handler always starts at addr 4.\r
84 ; Let the entire handler stay here instead of branching away\r
85 \r
86                 org     4                                       ; Must be on Address 4!\r
87                 retfie                  ; Interrupt return\r
88 \r
89 \r
90 \r
91                 subtitl "Main loop"\r
92                 page\r
93 \r
94                 org     100h\r
95 Main\r
96         clrf    PORTA\r
97         clrf    PORTB\r
98         clrf    PORTC\r
99         bsf     STATUS,RP0      ; Hi Bank\r
100         movlw   04h             ; AD mode 4h, 3 ad inputs AN0, 1 and 3, not 2 (!) fix pcb.. :(\r
101         movwf   ADCON1\r
102        movlw   0dfh            ; RA5 should be outputs...\r
103 ;        movlw   0dbh            ; RA2 & RA5 should be outputs...\r
104 ;        movlw   0fbh            ; RA2 should be output...\r
105         movwf   TRISA           ; Yep.\r
106         movlw   0c1h            ; RB0, 6 & 7 are inputs.\r
107         movwf   TRISB           ; Yep\r
108                 movlw   9dh                             ; RC0, 2, 3, 4, 7 are inputs\r
109 ;               movlw   0bdh                            ; RC0, 2, 3, 4, 7 are inputs\r
110                 movwf   TRISC\r
111         bcf     OPTION_REG,NOT_RBPU     ; Turn on port B pull-up\r
112         bcf     STATUS,RP0      ; Restore Lo Bank\r
113 \r
114         bsf     PORTB,1                 ; Turn on SEPIC\r
115 ;       bsf PORTA,5                     ; test ss shit\r
116 \r
117         bsf     STATUS,RP0              ; Access bank 1\r
118         bsf     TXSTA,TXEN              ; Enable UART TX\r
119         movlw   31                      ; Divisor for 9k6 @ 20MHz Fosc\r
120         movwf   SPBRG                   ; Store\r
121         bcf     STATUS,RP0              ; Back to bank 0\r
122 \r
123         bsf     RCSTA,SPEN              ; Enable serial port\r
124         bsf     RCSTA,CREN              ; Enable UART RX\r
125 \r
126 ;               movlw 'W'\r
127 ;               movwf   TXREG   ; Test transmission\r
128 \r
129                 \r
130 ;WaitRSTHigh\r
131 ;               call    UpdateLEDS\r
132 ;               btfss   PORTB,0         ; Test RST bit\r
133 ;               goto    WaitRSTHigh\r
134 \r
135 ;        bsf     STATUS,RP0      ; Hi Bank\r
136 ;               bsf             TRISA,2         ; RA2 back as input (turn off blue led)\r
137 ;        bcf     STATUS,RP0      ; Lo Bank\r
138 \r
139 RestartCrap\r
140                 bcf             SSPCON,SSPEN    ;disable ssp\r
141 \r
142 WaitRSTLow\r
143                 call    UpdateLEDS\r
144 ;b WaitRSTLow\r
145                 btfsc   PORTB,0         ; Test RST bit\r
146                 goto    WaitRSTLow              \r
147 \r
148 WaitBUSONHigh\r
149                 call    UpdateLEDS\r
150                 btfss   PORTA,4         ; Test BUSON bit\r
151                 goto    WaitBUSONHigh           \r
152 \r
153                 bsf             STATUS,RP0\r
154 ;               movlw   40h                     ; Latch on active-to-idle edge (needs ss crap?)\r
155                 movlw   00h                     ; Transfer on idle-to-active edge (and latch on active-to-idle)\r
156                 movwf   SSPSTAT\r
157                 bcf             STATUS,RP0\r
158 \r
159 WaitCLKLow\r
160                 call    UpdateLEDS\r
161                 btfsc   PORTC,3         ; SCK input must be low before enabling SSP according to errata sheet (!)\r
162                 goto    WaitCLKLow\r
163                 movlw   25h\r
164 ;               movlw   34h                     ; fuxx0red ss mode crap\r
165                 movwf   SSPCON          ; Enable SPI slave mode with SCK IDLE low, no _SS control\r
166                 clrw\r
167                 movwf   SSPBUF\r
168 \r
169 OnceMore\r
170 WaitForByte\r
171                 call    UpdateLEDS\r
172 \r
173                 btfsc   PORTB,0         ; Test RST bit\r
174                 goto    RestartCrap\r
175 \r
176                 btfss   PORTA,4         ; Test BUSON bit\r
177                 goto    RestartCrap\r
178 \r
179                 bsf             STATUS,RP0\r
180                 btfss   SSPSTAT,BF      ; Test for buffer complete\r
181                 goto    WaitForByte\r
182                 bcf             STATUS,RP0\r
183                 movf    SSPBUF,w\r
184                 xorlw   0ffh            ; Invert received data\r
185                 movwf   TXREG           ; Send it out to Async serial\r
186                 clrw\r
187                 movwf   SSPBUF\r
188                 goto    OnceMore\r
189 \r
190 meck\r
191         b meck\r
192 \r
193 UpdateLEDS\r
194 ; NOTE!!!! This is wrong, never drive the led outputs high as there are other open-collector drivers on these pins!!!\r
195 ; This is just for debugging, must be removed when hooking things up to the board!!!\r
196                 bcf     STATUS,RP0\r
197 ; IO is inverted logic (active low)\r
198 ;               btfsc   PORTA,4         ; test buson\r
199                 btfss   PORTC,4         ; test IO\r
200                 bcf     PORTB,5         ; turn on green led\r
201 ;               btfss   PORTA,4         ; test buson clear\r
202                 btfsc   PORTC,4         ; test IO\r
203                 bsf     PORTB,5         ; turn off green led\r
204 \r
205 ; CLK is true logic (active high)\r
206 ;               btfsc   PORTB,0         ; Test RST bit\r
207                 btfsc   PORTC,3         ; test clk\r
208                 bcf     PORTB,4         ; turn on red led if RST high\r
209 ;               btfss   PORTB,0         ; Test RST bit\r
210                 btfss   PORTC,3         ; test clk\r
211                 bsf     PORTB,4         ; turn off red led if RST low\r
212 \r
213                 return\r
214 \r
215 ;----------------------------------------------------------------------\r
216 ; EE Data (64 bytes), located at 2100h\r
217 \r
218         org 2100h\r
219 ;        data 0ffh, 0ffh, 0ffh, 0ffh, 001h, 023h, 045h, 067h\r
220 \r
221         END\r