*
* icb file: "compass.asm"
*
* Tom Brusehaver  02/16/96
*
* Run the V2X in master mode, this keeps collecting the "current"
* heading, resulting in having almost real-time, constant input from
* the compass.
*
*       Pin Connections
*     V2X            HC11
*   pin    desc   pin   desc    Notes
*    13     EOC  invert! SS      Sync bit counters between master and slave
*     2     SDO          MOSI    Data coming in
*     3     SDI          MISO    Data going out
*     1     SCLK         CLK     Bit clock
*     5     P/C    2 of U11      enable compass
*
* NOTE: I had to invert the EOC pin.  I used a NPN transistor, Base
*      connected to EOC pin, Collector connected to SS(HC11), and 
*      pulled up with a 390 ohm resistor. Emitter connected to ground.
*      pretty simple really.
*


#include <6811regs.asm>

* motor 2,3 equates  (separate bits from poloroid sonar)
MOTOR_OUTPUT    EQU     $7f00           ; Using motor control lines for output (hb-code.s19)
MOTOR_BYTE      EQU     $0E             ; Using upper half of motor byte (hb-code.s19)
MOTOR_INIT      EQU     %00001000       ; P/C on pin2, motor 3 reverse
MOTOR_INIT_MASK EQU     %11110111       ; P/C on pin2, motor 3 reverse

	ORG		MAIN_START

subroutine_initialize_module:
#include <ldxibase.asm>
* X now has base pointer to interrupt vectors

* get current vector; poke such that when we finish we go there
	LDD		SPIINT,X		        ; SPI Interrupt
	STD		interrupt_code_exit+1   ; fix $0000 at end.

* install ourself as a new vector
	LDD		#interrupt_code_start
	STD		SPIINT,X

*      turn up the P/C bit (turn off the compass)
	LDAA    MOTOR_BYTE
	ORAA	#MOTOR_INIT
	STAA    MOTOR_BYTE
	STAA	MOTOR_OUTPUT
	rts

* variables

variable_temp_word:		FDB 0    ; read each byte and store here
variable_head_word:		FDB 0    ; after reading 2 bytes move them here
variable_byte_count:	FDB 0	 ; read first or second byte?

subroutine_comp_init:
* put SPI in slave mode
	LDAA	#%00000100
	STAA	DDRD
	LDAA	SPDR        ; read it
*	LDAA	#$CC		; SPIE, SPE !DWOM, !MSTR, CPOL, CPHA, E/2
	LDAA	#$C8		; SPIE, SPE !DWOM, !MSTR, !CPOL, !CPHA, E/2
	STAA	SPCR

	LDAA	SPSR		; clear SPIF
	LDAA	SPDR		; 

	LDAB	#$0
	STAB    variable_byte_count

*     pull P/C bit low
	LDAA    MOTOR_BYTE
	ANDA	#MOTOR_INIT_MASK
	STAA	MOTOR_BYTE
	STAA	MOTOR_OUTPUT

	CLI
	RTS

subroutine_comp_disable:
* turn off SPI interrupts for now And turn off P/C
	LDAA	#%00000100
	STAA	DDRD
	LDAA	SPDR        ; read it
	LDAA	#$48		; !SPIE, SPE !DWOM, !MSTR, !CPOL, !CPHA, E/2
	STAA	SPCR

*      turn up the P/C bit
	LDAA    MOTOR_BYTE
	ORAA	#MOTOR_INIT
	STAA    MOTOR_BYTE
	STAA	MOTOR_OUTPUT

	RTS

* interrupt program begins here
interrupt_code_start:
	SEI                           ; no time for interrupts!
	LDAA	SPSR		          ; clear SPIF  VERY necessary
	LDAA	SPDR				  ; get our byte
	LDAB 	variable_byte_count
	CMPB	#0                    ; wouldn't have to
	BNE		byte2_output
	STAA	variable_temp_word    ; just save the current byte away
	INCB						  ; increment byte count
	STAB    variable_byte_count
	BRA		end_out
byte2_output:
	STAA	variable_temp_word+1  ; save the current byte, and move to
	STAA	variable_head_word+1
    LDAA    variable_temp_word    ; valid data holding location
	STAA    variable_head_word    ;
	LDAB	#$0
	STAB    variable_byte_count
end_out:
	CLI

* end of interrupt program.
interrupt_code_exit:
	JMP		$0000	; this gets fixed above

