Debian Codegraph @

test/sample.asm

raw
1
2 ; FATROM.ASM
3 ;
4 ;-----------------------------------------------------
5 ; MEMORY MAP -
6 ;-----------------------------------------------------
7 ;
8 ; We throw away some internal ram (96 bytes) in order
9 ; to use direct addressing for the majority of memory
10 ; accesses by mapping the registers at 0.
11 ;
12 ; MC68HC11F1 port and bit assignments
13 ;
14 ; We setup a very fast OC1 interrupt.. around 208uS, to
15 ; handle main system timing. This also allows us to have
16 ; a WAI instruction in our serial poll loop to lower power
17 ; consumption.
18
19 RAM equ $0000 ; 1K byte of RAM
20 LORAMBEG equ $0060 ; 96 unusable due to ctl regs
21 ifconst INEEPROM
22 HIRAMBEG equ $0300 ; hardwired for RTU software
23 else
24 HIRAMBEG equ $0320
25 eif
26 REGS equ $0000 ; 96 control regs (after INIT)
27 EEPROM equ $FE00 ; 512 bytes EEPROM
28
29 PORTA equ REGS+$00 ; i/o port A
30 PORTA_24V equ $80 ; 24V power switch
31
32 DDRA equ REGS+$01 ;
33
34 PORTG equ REGS+$02
35 CSPROG equ $80
36 CSGEN equ $40
37 CS101 equ $20
38 CS102 equ $10
39 PORTG_RESERVED7 equ $80
40 PORTG_MUXSEL3 equ $40
41 PORTG_MUXSEL2 equ $20
42 PORTG_MUXSEL1 equ $10
43 PORTG_MUXSEL0 equ $08
44 PORTG_ADSTROBHI equ $04
45
46 DDRG equ REGS+$03
47
48 PORTB equ REGS+$04 ; i/o port B
49
50 PORTF equ REGS+$05 ; PORTF l.e.d's and digout 1 & 2
51 PORTF_DIGOUT2 equ $80
52 PORTF_DIGOUT1 equ $40
53 PORTF_GRN equ $20
54 PORTF_RED equ $10
55
56 PORTC equ REGS+$06
57
58 DDRC equ REGS+$07
59
60 PORTD equ REGS+$08
61
62 DDRD equ REGS+$09
63
64 PORTE equ REGS+$0A
65
66 CFORC equ REGS+$0B
67 CFORC_FOC1 equ $80 ; FORCE OUTPUT COMPARE 1-4
68 CFORC_FOC2 equ $40 ; ....OR'D WITH TOC'S
69 CFORC_FOC3 equ $20 ;
70 CFORC_FOC4 equ $10 ;
71 CFORC_FOC5 equ $08 ;
72
73 OC1M equ REGS+$0C ; OUTPUT COMPARE MASK
74 OC1_M7 equ $80 ; ALLOWS COUNTER COMPARE OUTPUTS ESTABLISHED BY
75 OC1_M6 equ $40 ; OC1D6-D3 ON PA
76 OC1_M5 equ $20 ; EXCEPT FOR OC1M7 WHICH ALLOWS EITHER
77 OC1_M4 equ $10 ; INTERNAL DATA (0) OR OC1D7 DURING WRITES TO
78 OC1_M3 equ $08 ; PA7
79
80 OC1D equ REGS+$0D
81 OC1_d7 equ $80 ; STATES WHICH WILL BE PRESENT ON PA6-PA3
82 OC1_d6 equ $40 ; WHEN PERMITTED BY OC1_M6-M3
83 OC1_d5 equ $20 ;
84 OC1_d4 equ $10 ;
85 OC1_d3 equ $08 ;
86
87 TCNT equ REGS+$0E
88
89 TIC1 equ REGS+$10
90
91 TIC2 equ REGS+$12
92
93 TIC3 equ REGS+$14
94
95 TOC1 equ REGS+$16
96
97 TOC2 equ REGS+$18
98
99 TOC3 equ REGS+$1A
100
101 TOC4 equ REGS+$1C
102
103 TOC5 equ REGS+$1E
104
105 TCTL1 equ REGS+$20 ;ACTION TAKEN CODES M L
106 TCTL1_OM2 equ $80 ; 0 0 -DISABLE
107 TCTL1_OL2 equ $40 ; 0 1 -TOGGLE
108 TCTL1_OM3 equ $20 ; 1 0 -CLEAR
109 TCTL1_OL3 equ $10 ; 1 1 -SET
110 TCTL1_OM4 equ $08 ;
111 TCTL1_OL4 equ $04 ; OR, WHAT 1 OF 4 THINGS HAPPEN TO OUT.CMP
112 TCTL1_OM5 equ $02 ; FLAGS IN TFLG1 2-5 (OCF'S 2-5)
113 TCTL1_OL5 equ $01 ;
114
115 TCTL2 equ REGS+$21 ; B A
116 TCTL2_EDG1B equ $20 ; 0 0 CAPTURE DISABLED
117 TCTL2_EDG1A equ $10 ; 0 1 '' ON RISING
118 TCTL2_EDG2B equ $08 ; 1 0 '' ON FALLING
119 TCTL2_EDG2A equ $04 ; 1 1 '' ON ANY
120 TCTL2_EDG3B equ $02
121 TCTL2_EDG3A equ $01
122
123 TMSK1 equ REGS+$22
124 TMSK1_OC1I equ $80 ;
125 TMSK1_OC2I equ $40 ; ENABLE OUTPUT COMPARE INTERRUPT WHEN SET
126 TMSK1_OC3I equ $20 ;
127 TMSK1_OC4I equ $10 ;
128 TMSK1_OC5I equ $08 ;
129 TMSK1_IC1I equ $04 ; ENABLE INTERUPTS ON CAPTURE FLAG WHEN SET
130 TMSK1_IC2I equ $02 ;
131 TMSK1_IC3I equ $01 ;
132
133 TFLG1 equ REGS+$23
134 TFLG1_OC1F equ $80 ; CORRESPONDING FLAGS TO THE ABOVE
135 TFLG1_OC2F equ $40 ;
136 TFLG1_OC3F equ $20 ;
137 TFLG1_OC4F equ $10 ;
138 TFLG1_OC5F equ $08 ;
139 TFLG1_IC1F equ $04 ;
140 TFLG1_IC2F equ $02 ;
141 TFLG1_IC3F equ $01 ;
142
143 ;NOTE: PR1 & PR0 MUST BE WRITTEN IMMEDIATELY AFTER RESET!!
144
145 TMSK2 equ REGS+$24
146 TMSK2_TOI equ $80 ; Timer overflow interrupt enable
147 TMSK2_RTII equ $40 ; Real time interrupt
148 TMSK2_PAOVI equ $20 ; Pulse accumulator overflow
149 TMSK2_PAII equ $10 ; Pulse accumulator count
150 TMSK2_PR1 equ $02 ; Timer prescale select
151 TMSK2_PR0 equ $01 ; 0,0=/1 0,1=/4 1,0=/8 1,1=/16
152
153
154
155 TFLG2 equ REGS+$25
156 TFLG2_TOF equ $80 ; FLAGS FOR THE ABOVE 1ST 4 INTERRUPTS
157 TFLG2_RTIF equ $40 ;
158 TFLG2_PAOVF equ $20 ;
159 TFLG2_PAIF equ $10 ;
160
161
162 PACTL equ REGS+$26
163 PACTL_DDRA7 equ $80 ; 0=INPUT ONLY, 1=OUTPUT
164 PACTL_PAEN equ $40 ; ENABLE PA
165 PACTL_PAMOD equ $20 ; 0=COUNT EXTERNAL EVENTS,1=GATED TIME ACCUM.
166 PACTL_PEDGE equ $10 ; 0=INC ON FALLING OR INHIBITS CNT;1=OPPOSITE
167 PACTL_RTR1 equ $02 ; Divider for real time interrupt
168 PACTL_RTR0 equ $01 ; Divide by 2^13, 2^14, 2^15, or 2^16
169
170
171 PACNT equ REGS+$27
172
173 SPCR equ REGS+$28
174 SPCR_SPIE equ $80 ; serial PERIPH INTERRUPT ENABLE
175 SPCR_SPE equ $40 ; serial periph system enable
176 SPCR_DWOM equ $20 ; PORT D WIRED-OR 0=TOTEM,1=OPEN DRAIN
177 SPCR_MSTR equ $10 ; 0=SLAVE, 1=MASTER
178 SPCR_CPOL equ $08 ; 0=ACT. HIGH CLKS;SCK IDLES LOW--1 IS OPPOSITE
179 SPCR_CPHA equ $04 ; CLOCK PHASE
180 SPCR_SPR1 equ $02 ; E CLOCK / 2 4 16 32
181 SPCR_SPR0 equ $01 ; 00 01 10 11
182
183 SPSR equ REGS+$29
184 SPSR_SPIF equ $80 ; TXFR COMPLETE
185 SPSR_WCOL equ $40 ; WRITE COLLISION
186 SPSR_MODF equ $10 ; MODE-FAULT ERROR FLAG
187
188 SPDR equ REGS+$2A
189
190 BAUD equ REGS+$2B
191 BAUD_TCLR equ $80 ; 0
192 BAUD_SCP1 equ $20 ; divide e by 1 3 4 13
193 BAUD_SCP0 equ $10 ; 00 01 10 11
194 BAUD_RCKB equ $08 ; 0
195 BAUD_SCR2 equ $04 ; divide prescaler out by 1, 2 4,.....128
196 BAUD_SCR1 equ $02 ;
197 BAUD_SCR0 equ $01 ; scr0,1,2 = 000 001 010.....111
198 BAUD_4800 equ BAUD_SCP1|BAUD_SCR1
199 BAUD_9600 equ BAUD_SCR0|BAUD_SCR1
200 BAUD_19200 equ BAUD_SCR1
201
202
203 SCCR1 equ REGS+$2C
204 SCCR1_R8 equ $80 ; get the ninth bit here, if 'M' bit is set
205 SCCR1_T8 equ $40 ; set this bit for the 9th tx bit if 'M' is set
206 SCCR1_M equ $10 ; 'M'
207 SCCR1_WAKE equ $08 ; 0=idle line, 1=address mark
208
209 SCCR2 equ REGS+$2D
210 SCCR2_TIE equ $80 ; Transmit interrupt enable
211 SCCR2_TCIE equ $40 ; Transmit Complete interr en
212 SCCR2_RIE equ $20 ; Receiver Interrupt enable
213 SCCR2_ILIE equ $10 ; Idle Line '' ''
214 SCCR2_TE equ $08 ; tx enable
215 SCCR2_RE equ $04 ; rx """
216 SCCR2_RWU equ $02 ; wake up when set
217 SCCR2_SBK equ $01 ; send break
218
219
220 SCSR equ REGS+$2E
221 SCSR_TDRE equ $80 ; Transmit Data Register Empty
222 SCSR_TC equ $40 ; Transmit Complete
223 SCSR_RDRF equ $20 ; Receiver Data Ready
224 SCSR_IDLE equ $10 ; Idle Line Detect
225 SCSR_OR equ $08 ; overrun error
226 SCSR_NF equ $04 ; noise flag
227 SCSR_FE equ $02 ; framing error
228
229
230 SCDR equ REGS+$2F ; sc data reg
231
232 ADCTL equ REGS+$30
233 ADCTL_CCF equ $80 ; Conversion complete, cleared on ADCTL write
234 ADCTL_SCAN equ $20 ; Continuous scan mode, else single scan
235 ADCTL_MULT equ $10 ; Scan all four channels or just one channel
236 ADCTL_CD equ $08 ;
237 ADCTL_CC equ $04 ;
238 ADCTL_CB equ $02 ;
239 ADCTL_CA equ $01 ;
240
241 ADR1 equ REGS+$31 ; ADC register 1
242 ADR2 equ REGS+$32
243 ADR3 equ REGS+$33
244 ADR4 equ REGS+$34
245
246
247 BPROT equ REGS+$35
248 BPROT_PTCON equ $10
249 BPROT_BPRT3 equ $08
250 BPROT_BPRT2 equ $04
251 BPROT_BPRT1 equ $02
252 BPROT_BPRT0 equ $01
253
254 OPT2 equ REGS+$38 ; config options 2 register
255 OPT2_GWOM equ $80
256 OPT2_CWOM equ $40
257 OPT2_CLK4X equ $20
258
259 OPTION equ REGS+$39
260 OPTION_ADPU equ $80 ; A/D power
261 OPTION_CSEL equ $40
262 OPTION_IRQE equ $20 ; IRQ edge sensitive enable, else level sens.
263 OPTION_DLY equ $10 ; delay 4000 E clks after stop, else 4
264 OPTION_CME equ $08
265 OPTION_CR1 equ $02 ; cop timer rates of E/2^15/(1,4,16,64)
266 OPTION_CR0 equ $01 ; for 00,01,10,11
267
268
269 COPRST equ REGS+$3A
270
271 ; EEPROM programming register.
272 ;
273 ; BYTE ROW (only valid when erase bit is set)
274 ; 0 0 bulk erase
275 ; 0 1 row erase
276 ; 1 0 byte erase
277 ; 1 1 byte erase
278 ;
279 ; erase sequence is:
280 ; +BYTE+ERASE+EELAT, write eeprom, +PGM, delay 10 ms,
281 ; -PGM-ERASE-EELAT
282 ; program sequence is:
283 ; +EELAT, write eeprom, +PGM, delay 10 ms, -PGM-EELAT
284 ;
285 ; you do not need to program to zero before erasing to 1's.
286
287 PPROG equ REGS+$3B
288 PPROG_ODD equ $80 ; Factory reserved
289 PPROG_EVEN equ $40 ; Factory reserved
290 PPROG_BYTE equ $10 ; Byte erase mode
291 PPROG_ROW equ $08 ; Row erase mode
292 PPROG_ERASE equ $04 ; Erase enable
293 PPROG_EELAT equ $02 ; arm eeprom write (then write to eeprom)
294 PPROG_EEPGM equ $01 ; apply programming voltage
295
296
297 HPRIO equ REGS+$3C
298 HPRIO_RBOOT equ $80 ; Set in special test mode
299 HPRIO_SMOD equ $40 ; ~MODB pin latched at reset
300 HPRIO_MDA equ $20 ; MODB pin latched at reset, can be clrd in
301 ; special tes/tbootstrap mode ?
302 HPRIO_IRV equ $10 ; internal read visibility
303 HPRIO_PSEL3 equ $08 ; Select highest priority interrupt, only
304 HPRIO_PSEL2 equ $04 ; writable when interrupts are masked.
305 HPRIO_PSEL1 equ $02 ;
306 HPRIO_PSEL0 equ $01 ;
307
308
309 INIT equ REGS+$3D
310 INIT_RAM3 equ $80 ; Internal ram base address in 4K chunks,
311 INIT_RAM2 equ $40 ; default $0000 (0000)
312 INIT_RAM1 equ $20 ;
313 INIT_RAM0 equ $10 ;
314 INIT_REG3 equ $08 ; Control register base address in 4K chunks,
315 INIT_REG2 equ $04 ; default $1000 (0001)
316 INIT_REG1 equ $02 ;
317 INIT_REG0 equ $01 ;
318
319 TEST1 equ REGS+$3E
320 TEST1_TILOP equ $80 ; FACTORY TEST STUFF ONLY WHEN SMOD=1
321 TEST1_OCCR equ $20
322 TEST1_CBYP equ $10
323 TEST1_DISR equ $08
324 TEST1_FCM equ $04
325 TEST1_FCOP equ $02
326 TEST1_TCON equ $01
327
328
329 CONFIG equ REGS+$3F
330 CONFIG_NOSEC equ $08 ; no eeprom security
331 CONFIG_NOCOP equ $04 ; no hardware watchdog
332 CONFIG_ROMON equ $02 ; enable rom
333 CONFIG_EEON equ $01 ; enable eeprom
334
335 ; In bootstrap mode vectors go through ram
336 ;
337
338 BsSCIJmp equ $C4
339 BsSPIJmp equ $C7
340 BsPAIJmp equ $CA
341 BsPAOVJmp equ $CD
342 BsTOVJmp equ $D0
343 BsOC5Jmp equ $D3
344 BsOC4Jmp equ $D6
345 BsOC3Jmp equ $D9
346 BsOC2Jmp equ $DC
347 BsOC1Jmp equ $DF
348 BsIC3Jmp equ $E2
349 BsIC2Jmp equ $E5
350 BsIC1Jmp equ $E8
351 BOOTSTRAPORG equ $EB ; bootstrap origin
352 BsRTIJmp equ $EB
353 BsIRQJmp equ $EE
354 BsXIRQJmp equ $F1
355 BsSWIJmp equ $F4
356 BsILLJmp equ $F7
357 BsCOPJmp equ $FA
358 BsCMJmp equ $FD
359
360 processor 68hc11
361
362 ;-----------------------------------------------------
363 ; RAM VARS -
364 ;-----------------------------------------------------
365 ;
366 ; most/all ram variables can be accessed with direct
367 ; addressing for the EEPROM version.
368
369 seg.u bss
370 org LORAMBEG
371
372 RTUConfig ds.b 1 ; RTU Flags reg, cleared on reset
373 BoardAddr ds.b 1 ; 1 byte board address
374 Counter1 ds.w 1 ; 2 bytes counter input
375 Counter2 ds.w 1 ; 2 bytes counter input
376 RTIRedCnt ds.b 1 ; real time interrupt red l.e.d. countdown
377 RTIGrnCnt ds.b 1 ; real time interrupt grn l.e.d. countdown
378 RTIWDog ds.w 1 ; 2 byte wdog ctr, reset by packet reception
379 RTIAvrCnt ds.b 1 ; 1 16 sample countdown
380
381 ; note: packet size for ram-dl version must be smaller so
382 ; program fits in ram
383
384 ifconst INEEPROM
385 PacketBuf ds.b 256+8 ; maximum packet size plus slop
386 else
387 PacketBuf ds.b 32+8 ; maximum packet size plus slop in bootstrap
388 eif
389
390 ; note: MaxScanTable location hardwired by ioscan for normal
391 ; operation
392 ;
393
394 org HIRAMBEG
395
396 MaxScanTable ds.b 96 ; 16 channels x 3 (holdingregx1, accumulatorx2)
397 Stack ds.b 64 ; Reserve 64 bytes for the stack
398 StackEnd ds.b 0
399
400 ;-----------------------------------------------------
401 ; CODE SEGMENT -
402 ;-----------------------------------------------------
403 ;
404 ; For EEPROM version code starts at $FE00. For RAM bootstrap
405 ; version code starts at zero from which we immediately jump
406 ; forward past all registers and ram variables
407 ;
408
409 seg code
410
411 ifconst INEEPROM
412 org $FE00
413 else
414 org $0000
415 jmp Reset
416 org BOOTSTRAPORG
417 eif
418
419 ;-----------------------------------------------------
420 ; ROM VARS -
421 ;-----------------------------------------------------
422 ;
423 ; Calibration constants are at the beginning of the rom,
424 ; consisting of two 16 bit base constants for analog channel
425 ; 1 and two 8 bit constants for each of analog channels
426 ; 2 through 7, two 8 bit constants for the two temp.
427 ; sensors, two 8 bit constants for the board temp. sensor.
428 ;
429 ; The RTU will interpret these constants
430
431 dc.w $FFFF,$FFFF ; Mux channel 0, 4 and 20mA AI01
432 dc.b $FF,$FF ; Mux channel 1 (delta) AI02
433 dc.b $FF,$FF ; Mux channel 2 (delta) AI03
434 dc.b $FF,$FF ; Mux channel 3 (delta) AI04
435 dc.b $FF,$FF ; Mux channel 4 (delta) (unused)
436 dc.b $FF,$FF ; Mux channel 5 (delta) AI05
437 dc.b $FF,$FF ; Mux channel 6 (delta) AI06
438 dc.b $FF,$FF ; Mux channel 7 (delta) AI07
439 dc.b $FF,$FF ; External temp. sensor 1
440 dc.b $FF,$FF ; External temp. sensor 2
441 dc.b $FF,$FF ; On-Board temp. sensor
442
443 ;-----------------------------------------------------
444 ; RESET -
445 ;-----------------------------------------------------
446 ;
447 ; Platform reset. Interrupts are disabled and the stack
448 ; pointer is/becomes invalid.
449
450 Reset subroutine
451 ResetCmd
452 BadInt
453 ; disable interrupts, setup protected control registers
454 ; INIT - relocate ram and hardware regs to 0
455 ; OPT2 - disable clk4x output (save power)
456 ; OPTION - enable ADC
457 ;
458 ; note: due to ram/regs overlap, first 96 bytes of ram
459 ; is unaccessable. For bootstrapped programs, the
460 ; jmp to Reset becomes unaccessable after it is executed.
461
462 sei
463 clra ; .A = 0
464 staa INIT-REGS+$1000 ; map ram & regs to base address 0
465 staa OPT2 ; disable clk4x & C,G wire-or modes
466 staa BPROT ; allow eeprom to be written
467 ldab #OPTION_ADPU ; enable ADC
468 stab OPTION
469
470 ; SETUP PORTS AND DDRS
471 ;
472 ; DDR DATA
473 ; PORTA 10000000 0xxxxxxx (+24V power switch)
474 ; PORTB <OUTPUT> 11111111 (LED's)
475 ; PORTC 00000000 xxxxxxxx
476 ; PORTD xx110000 xx00xxxx (~ss, sck)
477 ; PORTE <INPUT> xxxxxxxx (addr h, l, brdid, temp)
478 ; PORTF <OUTPUT> 00111111 (digout, LED's)
479 ; PORTG 11111100 000000xx (filter, mux sel, startconv,
480 ; digin)
481 ; note: PD5 must be an output so as not to interfere with
482 ; SPI ~SS hardware.
483 ;
484 ; NOTE: PORTA_24V is off (0) by default. This is to prevent
485 ; the RTU's isolated power supply from getting jerked around
486 ; when several boards are in the system. Analog and digital
487 ; inputs will not be valid until the RTU turns on this line.
488 ;
489 ; NOTE: PORTD ~SS output must be set to 1 before configuring
490 ; the SPI as a master (???)
491 ;
492 ; .A is still 0
493
494 lds #DDRD
495 ldab #%00111000 ; DDRD = 00111000
496 pshb
497 pshb ; PORTD= 00111000
498 psha ; DDRC = 00000000
499 des ; RESERVED06
500 ldab #$3F ; PORTF= 00111111
501 pshb
502 ldab #$FF ; PORTB= 11111111
503 pshb
504 ldab #%11111100 ; DDRG = 11111100
505 pshb
506 psha ; PORTG= 0
507 ldab #PORTA_24V ; DDRA = PORTA_24V
508 pshb
509 psha ; PORTA= 0
510
511 ; Setup remaining hardware, .A still 0
512 ;
513 ; ADC setup for continuous scan channels 4-7
514 ; SCI 9600bps 8N1, IDLE wake detection
515 ; SPI Setup clock phase, etc..
516
517 ldab #ADCTL_MULT|ADCTL_SCAN|ADCTL_CC
518 stab ADCTL
519
520 staa SCCR1
521 ldab #SCCR2_TE|SCCR2_RE
522 stab SCCR2
523 ldab #BAUD_9600
524 stab BAUD
525
526 ; SPI, .A still 0
527 ;
528 ; CPOL=0, CPHA=0, clkrate= E/2 (614KHz)
529
530 ldab #SPCR_MSTR ; make master, disable
531 stab SPCR
532 bset SPCR,SPCR_SPE ; enable
533
534 ; ICR - Input capture registers
535 ;
536 ; Setup input capture registers for counter inputs and setup
537 ; OC1 to handle system timing.
538 ;
539
540 bset TCTL2,TCTL2_EDG1A|TCTL2_EDG2A|TCTL2_EDG3A
541 bset TMSK1,TMSK1_IC1I|TMSK1_IC2I|TMSK1_OC1I
542
543 ; bootstrap interrupt vectors (.A still 0)
544 ;
545 ; When bootstrapping to ram, the 68HC11F1 implements pseudo
546 ; vectors and we have to create a jump table for the vectors
547 ; we use.
548
549 ifnconst INEEPROM
550 ldab #$7E ; JMP extended instruction
551 stab BsOC1Jmp
552 stab BsIC1Jmp
553 stab BsIC2Jmp
554 ldx #OC1Int ; address of OC1 interrupt
555 stx BsOC1Jmp+1
556 ldx #IC1Int
557 stx BsIC1Jmp+1
558 ldx #IC2Int
559 stx BsIC2Jmp+1
560 eif
561
562 ; Setup stack and various ram variables, .A still 0
563 ;
564 ; We do not have to clear RTIWDog since it is reset by packet
565 ; reception and the relays are off anyway before then. We
566 ; do not have to clear the red/grn count either because the
567 ; l.e.d's are already off.
568 ;
569
570 lds #StackEnd-1 ; reset stack pointer
571 staa RTUConfig
572 staa RTIAvrCnt
573 cli ; enable ints (note: stop disble is 1)
574
575 ;-----------------------------------------------------
576 ; MAIN PACKET WAIT LOOP -
577 ;-----------------------------------------------------
578 ;
579 ; Wait for an incomming packet
580 ;
581 ; The RED led is lit when an error is detected
582 ;
583 ; The GRN led is lit whenever we receive a valid packet
584 ; whos address is for us.
585
586 PKT_LEN equ 0
587 PKT_ADR equ 1
588 PKT_CMD equ 2
589 PKT_DATA equ 3
590
591 subroutine
592
593 WaitPacketRed
594 ldaa #56 ; 3 seconds
595 staa RTIRedCnt
596 bclr PORTF,PORTF_RED
597
598 ; Wait for a packet. We keep a running checksum while
599 ; bringing in the packet and setup the loop such that
600 ; .X will point to the CHK byte when done rather then
601 ; one past the CHK byte.
602 ;
603 ; AA
604 ; LEN \
605 ; ADR |
606 ; CMD | LEN bytes
607 ; DATA[LEN-4] |
608 ; CHK /
609 ; 55
610 ;
611 ; CHK does not include AA or 55. CHK is formulated such
612 ; that it all adds to zero.
613
614 WaitPacket
615 bsr GetB ; get protocol/synch
616 cmpb #$AA ; ignore if not $AA packet
617 bne WaitPacketRed
618 clra ; checksum accumulation
619 ldx #PacketBuf-1
620 .1 bsr GetB ; get byte (first byte is length)
621 inx
622 stab 0,x ; store
623 aba ; accumulate checksum
624 dec PacketBuf
625 bne .1
626 tsta
627 bne WaitPacketRed
628 bsr GetB ; obtain endsynch and check
629 cmpb #$55
630 bne WaitPacketRed
631 ; check address
632 ldaa PacketBuf+PKT_ADR
633 bmi .10
634 cmpa BoardAddr
635 bne WaitPacket
636 .10
637 ; Now have a valid packet, setup for return and
638 ; execute command
639 ;
640
641 ldab PacketBuf+PKT_CMD
642 ldaa #22 ; aprox set wdog 5 min (sto msb)
643 staa RTIWDog
644
645 ldx #CommandTable ; lookup the command
646 andb #$0E ; force command to be valid
647 abx
648 ldx 0,x ; get pointer to command
649
650 ; setup part of response
651 ldaa #$20
652 staa PacketBuf+PKT_ADR
653 ldaa RTUConfig
654 staa PacketBuf+PKT_CMD
655
656 jsr 0,x ; Execute the command
657 bsr TransmitPacket
658 bra WaitPacket
659
660 ; Upon completion transmit back .B bytes of data.. that is,
661 ; a packet:
662 ; AA LEN ADR+$20 FLG DATA[.B] CHK $55 (LEN=.B+4)
663
664 TransmitPacket
665 ldaa #9 ; set green led 0.5 sec
666 staa RTIGrnCnt
667 bclr PORTF,PORTF_GRN
668
669 addb #4
670 stab PacketBuf+PKT_LEN
671 ldx #PacketBuf
672 clra ; checksum
673 bclr SCCR2,SCCR2_RE ; disable receiver
674 ldab #$AA ; sync
675 .110 bsr SendB ; send loop, everything except checksum
676 ldab 0,x
677 inx
678 sba
679 dec PacketBuf+PKT_LEN
680 bne .110
681 aba
682 tab ; send checksum
683 bsr SendB
684 ldab #$55
685 bsr SendB
686 .120 brclr SCSR,SCSR_TC,.120
687 bset SCCR2,SCCR2_RE ; reenable receiver
688 rts
689
690 ; GETB - receive a character from the serial port
691 ;
692
693 subroutine
694 GetBLoop wai
695 GetB brclr SCSR,SCSR_RDRF,GetBLoop
696 ldab SCDR
697 rts
698
699 ; SENDB - transmit a character to the serial port
700 ;
701
702 subroutine
703 SendBLoop wai
704 SendB brclr SCSR,SCSR_TDRE,SendBLoop
705 stab SCDR
706 rts
707
708 ;-----------------------------------------------------
709 ; COMMANDS
710 ;-----------------------------------------------------
711 ;
712 ; A command must return the response length in .B, 0
713 ; if no data (returns a packet with no data)
714 ;
715 ; 00 IDENTIFY
716 ; 02 READ BYTES
717 ; 04 WRITE BYTES
718 ; 06 PROGRAM EEPROM BYTE
719 ; 08 RESET
720 ; 0A CONFIGURE
721 ; 0C BAD CMD
722 ; 0E BAD CMD
723
724 CommandTable
725 dc.w IdentifyCmd
726 dc.w ReadBytesCmd
727 dc.w WriteBytesCmd
728 ifconst INEEPROM
729 dc.w BadCmd
730 else
731 dc.w ProgramByteCmd
732 eif
733 dc.w ResetCmd
734 dc.w ConfigureCmd
735 dc.w BadCmd
736 dc.w BadCmd
737
738 ; BADCMD
739 ;
740 ; Bad command, simply ignore... return an empty packet
741
742 BadCmd subroutine
743 clrb
744 rts
745
746 ; CONFIGURECMD
747 ;
748 ; Configure an I/O board, copy data to RTUConfig and PORTA
749
750 ConfigureCmd subroutine
751 ldd PacketBuf+PKT_DATA
752 staa RTUConfig
753 stab PORTA
754 clrb
755 rts
756
757 ; IDENTIFY - identify board
758 ;
759 ; returns board type, board address, board temp, board id
760 ;
761 ; IdentifyUncCmd only identifies unconfigured boards
762
763 subroutine
764 IdentifyCmd
765 ldaa #1
766 staa PacketBuf+PKT_DATA
767 ldaa BoardAddr
768 staa PacketBuf+PKT_DATA+1
769 ldd ADR1
770 std PacketBuf+PKT_DATA+2
771 ldab #4
772 rts
773
774 ; READ BYTES - read data bytes
775 ;
776 ;
777
778 ReadBytesCmd subroutine
779 ldx PacketBuf+PKT_DATA ; source address
780 ldy #PacketBuf+PKT_DATA+3 ; destination address
781 memCpy clrb
782 memCpyLoop ldaa 0,x
783 staa 0,y
784 inx
785 iny
786 incb
787 cmpb PacketBuf+PKT_DATA+2
788 bne memCpyLoop
789 addb #3
790 rts
791
792 ; WRITE BYTES - write data bytes, written data returned
793 ;
794
795 WriteBytesCmd subroutine
796 ldx #PacketBuf+PKT_DATA+3 ; source address
797 ldy PacketBuf+PKT_DATA ; destination address
798 bra memCpy
799
800 ifnconst INEEPROM
801 ; PROGRAM BYTE - write one byte of data to eeprom
802 ;
803 ; Command: addr cmd ADRH ADRL LEN DATA[]
804 ; Response: 40+X err
805
806 ProgramByteCmd subroutine
807 ldx PacketBuf+PKT_DATA ; destination address
808 ldab PacketBuf+PKT_DATA+2 ; # of bytes to write
809 ldy #PacketBuf+PKT_DATA+3 ; source address
810 pbloop pshb
811 ldab 0,y
812 bsr eepromWrite
813 pulb
814 inx
815 iny
816 decb
817 bne pbloop
818 ; clrb
819 rts
820
821 ; eepromWrite - write data in .B to eeprom address .X
822 ;
823 ; .B, .X left alone
824 ;
825
826 eepromWrite subroutine
827 ldaa #PPROG_BYTE|PPROG_ERASE|PPROG_EELAT
828 bsr .10
829 ldaa #PPROG_EELAT
830 .10 staa PPROG
831 stab 0,x
832 bset PPROG,PPROG_EEPGM
833
834 pshb
835 ldd TCNT
836 addd #12228
837 .11 cpd TCNT ; D-TCNT
838 bpl .11
839 pulb
840
841 clra
842 staa PPROG
843 rts
844
845 eif
846
847
848 ;-----------------------------------------------------
849 ; COUNTER INTERRUPTS
850 ;-----------------------------------------------------
851 ;
852 ; IC1 - digital input counter 2
853 ; IC2 - digital input counter 1
854
855 IC1Int
856 ldx Counter2
857 inx
858 stx Counter2
859 bclr TFLG1,<~TFLG1_IC1F
860 rti
861
862 IC2Int
863 ldx Counter1
864 inx
865 stx Counter1
866 bclr TFLG1,<~TFLG1_IC2F
867 rti
868
869 ;-----------------------------------------------------
870 ; GET BOARD ADDRESS -
871 ;-----------------------------------------------------
872 ;
873 ; GetBoardAddr - calculate board network address 00-0F
874 ; and return in .B
875 ;
876 ; ADR4 (msb) and ADR3 (lsb) contain the address.
877 ; Each A/D is valued as shown below. We conver the
878 ; A/D value to a two bit address simply by multiplying
879 ; it by x3 and extracting the top two bits. The
880 ; result is also flexible enough to allow variations in
881 ; ADC values.
882 ;
883 ; **xxxxxx
884 ;
885 ; 00 x 3 = 00 00000000
886 ; 1E x 3 = 5A 01011010
887 ; 37 x 3 = A5 10100101
888 ; 4B x 3 = E1 11100001
889
890 GetBoardAddr subroutine
891 clrb ; address accumulator
892 ldaa ADR4
893 bsr oneOfFour
894 ldaa ADR3
895 oneOfFour
896 pshb
897 tab
898 asla
899 aba
900 pulb
901
902 asla
903 rolb
904 asla
905 rolb
906 rts
907
908 ;-----------------------------------------------------
909 ; OC1 INTERRUPT
910 ;-----------------------------------------------------
911 ;
912 ; The OC1 interrupt is set to occur at 3840Hz (~260uS). Each
913 ; interrupt switches channel so each individual channel is
914 ; effectively sampled at 240Hz. 256 samples are accumulated
915 ; yielding a 20 bit sum which is normalized to 16 bits and
916 ; transfered to another ram address for reading by the RTU.
917 ;
918 ; Working the math, you will also notice that 256 samples at
919 ; 240 Hz winds up being an integral number of 60Hz cycles,
920 ; therefore any 60Hz hum (and most harmonics of 60Hz) are
921 ; automatically removed.
922 ;
923 ; The OC1 interrupt also serves the following functions:
924 ; * Break WAI instruction in main loop for serial poll
925 ; * Handle LEDs and watchdog countdown
926 ; * Ensure processor runs long enough for ADC to convert
927
928 OC1Int subroutine
929
930 ; 1.2288MHzx320 (3840Hz) Max176 scan
931 ;
932
933
934 ldab PORTG
935 lsrb
936 lsrb
937 ldx #MaxScanTable ; +0 primary, +32 acclsb, +64 accmsb
938 abx
939 bsr ScanMax16 ; fmt ssdddddd.ddddddxx
940 andb #$FC
941 aslb
942 rola
943 aslb
944 rola
945 bcc .4 ; if negative, msb is all 1's
946 dec 64,x
947 .4 addd 32,x ; fmt dddddddd.dddd0000
948 bcc .5 ; carry
949 inc 64,x
950 .5 tst RTIAvrCnt
951 bne .10 ; fmt 0ddddddd.dddddddd.dddd0000
952 aslb ; cvt to dddddddd.dddddddd
953 tab
954 rolb
955 ldaa 64,x
956 rola
957 std 0,x
958 clra ; setup to clear msb, lsb
959 clrb
960 staa 64,x
961 .10 std 32,x
962 ldab PORTG
963 addb #$08
964 bpl .20
965 ldaa RTIAvrCnt
966 adda #$02 ; $10=16 samp/avr, $02=128 samp avr
967 staa RTIAvrCnt
968 clrb
969 .20 stab PORTG
970
971 ; Setup for next interrupt
972 ;
973 ;
974
975 bclr TFLG1,<~TFLG1_OC1F
976
977 ldd TOC1
978 addd #320
979 std TOC1
980 bcc .200
981
982 ; x65536 (18.75 Hz) code
983 ;
984
985 dec RTIRedCnt
986 bne .100
987 bset PORTF,PORTF_RED
988 .100
989 dec RTIGrnCnt
990 bne .101
991 bset PORTF,PORTF_GRN
992 .101
993 ldx RTIWDog
994 dex
995 stx RTIWDog
996 beq .102r
997 .102
998 bsr GetBoardAddr ; obtain board address
999 cmpb BoardAddr
1000 beq .103
1001 stab BoardAddr
1002 .102r
1003 clr RTUConfig
1004 bclr PORTF,PORTF_DIGOUT1|PORTF_DIGOUT2
1005 .103
1006
1007 ;
1008 ;
1009
1010 .200
1011 rti
1012
1013 ; SCANMAX16 - scan Max176
1014 ;
1015 ; This routine may not be interrupted due to sample-and-hold
1016 ; droop in the Max176. We use the SPI to generate 16 clocks
1017 ; to the Max176 after the rising edge of strobe. The falling
1018 ; edge of strobe must occur before the 12th bit and can occur
1019 ; as soon as the second bit.
1020
1021 ScanMax16 subroutine
1022 bset PORTG,PORTG_ADSTROBHI
1023 bsr Get8B ; 13 from store
1024 bsr Get8A ; 16 to load, 13 from store
1025 Get8A bclr PORTG,PORTG_ADSTROBHI ; 6
1026 tba ; 2
1027 Get8B tst SPSR ; 3
1028 ldab SPDR ; 3
1029 stab SPDR ; 3
1030 asla ; 2
1031 asla ; 2
1032 asra ; 2
1033 asra ; 2
1034 rts ; 5
1035
1036 ;-----------------------------------------------------
1037 ; INTERRUPT VECTORS -
1038 ;-----------------------------------------------------
1039 ;
1040 ; Interrupt Vectors
1041 ;
1042 ; note: if we need the space we can use some of this space
1043 ; for code (for those interrupt vectors we do not use)
1044
1045 ifconst INEEPROM
1046 org $FFD6+9*2
1047
1048 ;;dc.w BadInt ; SCI serial
1049 ;;dc.w BadInt ; SPIE
1050 ;;dc.w BadInt ; PAII Pulse Accumulator
1051 ;;dc.w BadInt ; PAOVI Pulse Accumulator Overflow
1052 ;;dc.w BadInt ; TOI Timer overflow
1053 ;;dc.w BadInt ; OC5
1054 ;;dc.w BadInt ; OC4
1055 ;;dc.w BadInt ; OC3
1056 ;;dc.w BadInt ; OC2
1057 dc.w OC1Int ; OC1
1058 dc.w BadInt ; IC3
1059 dc.w IC2Int ; IC2
1060 dc.w IC1Int ; IC1
1061
1062 dc.w BadInt ; RTI Realtime interrupt
1063 dc.w BadInt ; IRQ
1064 dc.w BadInt ; XIRQ
1065 dc.w BadInt ; SWI
1066 dc.w Reset ; Illegal OpCode Trap
1067 dc.w Reset ; COP failure (reset)
1068 dc.w Reset ; Clock Monitor Failure
1069 dc.w Reset ; RESET
1070
1071 eif
1072