;***************************************************************************
;* File Name		:spi.asm
;* Title		:SPI Test
;* Author		:C.Kuehnel
;* Date	 		:10/25/97
;* Version 		:1.0
;* Target MCU		:AT90S8515
;* 
;* DESCRIPTION
;* SPI Test w/o interrupt. 
;* Compatible with AVR processors including SPI hardware.
;***************************************************************************	.

;***** Directives
.device	 at90s8515
.nolist
.include "8515def.inc"
.list

; Primary routines:
;------------------
; spi_setup:	Call to setup I/O bits for SPI. Must call prior to using the
;               following routines.
; spi_write:	Call to write SPI device with byte to send in spi_buffer.
; spi_read:	Call to read SPI device, byte returned in spi_buffer.



.equ	EE_addr	= $AA			; Memory location
.equ	EE_data	= $44			; Data to save



; Main Program Register variables
;---------------------------------------------------------------------------
.def	temp		= r16
.def	period		= r17	; Blink frequency
.def	dly		= r18
.def	delcnt		= r19	; Loop Counter
.def 	spi_opcode	= r20	; SPI OpCode
.def	spi_addr	= r21	; SPI Memory Location
.def	spi_data	= r22	; SPI Data



;***** Interrupt vector table

	rjmp	RESET		; Reset handle
	reti			; External Interrupt0 handle
	reti			; External Interrupt1 handle
	reti			; T/C1 Capture Event Interrupt handle
	reti			; T/C1 CompareA Interrupt handle
	reti			; T/C1 CompareB Interrupt handle
	reti			; T/C1 Overflow Interrupt handle
	reti			; T/C0 Overflow Interrupt handle
	reti			; SPI Transfer Complete Interrupt handle
	reti			; UART Rx Complete Interrupt handle
	reti			; UART Data Register Empty Interrupt handle
	reti			; UART Tx Complete Interrupt handle
	reti			; Analog Comparator Interrupt handle

;***** Includes

.include "hw_spi.inc"
.include "hw_uart.inc"

;***** Subroutines

DELAY:	clr	delcnt			; Init Loop Counter
loop1:	dec	delcnt
	nop
	brne	loop1
	dec	dly
	brne	loop1
	ret


;***** Main


RESET:	ldi	temp, LOW(RAMEND)
	out	SPL, temp		; Initialize SPL
	ldi	temp, HIGH(RAMEND)
	out	SPH, temp		; Initialize SPH
	sbi	DDRB, PB0		; PB0 output
	rcall 	uart_init		; Initialize UART
	rcall	spi_setup
	ldi	period, $FF		; Initalize period  (FF ok | 60 for error)
	
	;ldi	spi_data, BL2_PROT	; Block Write Protection from $100 to $1FF
	ldi	spi_data, NO_BL_PROT	; No Block Write Protection
	rcall	write_en
	rcall	block_protection	; Setup Status Register Bits in 25C04
	rcall	wait_for_spi_ready

	
	ldi	spi_addr, EE_addr
	ldi	spi_data, EE_data
	rcall  	write_en
	rcall	spi_write		; Store Byte in 25C04
	rcall	wait_for_spi_ready

	clr	spi_data

	ldi	spi_addr, EE_addr
	rcall	spi_read		; Read back stored byte

	;mov	temp, spi_data
	;rcall	putc			; Send it to terminal for test

	ldi	temp, EE_data		
	cpse	spi_data, temp		; Compare read back byte with byte written
	ldi	period, $60


loop:	sbi	PORTB, PB0
	mov	dly, period		; Initialize delay controlled by period
	rcall	DELAY			
	cbi	PORTB, PB0
	mov	dly, period		; Initialize delay controlled by period
	rcall	DELAY			
	rjmp 	loop			; Repeat forever
	