;File name: vlfterm2313.asm	Initial code for VLF terminal program;Copyright 2004 Richard Cappels, projects@cappels.org   www.project.cappels.org;4 MHz AT90S2313;Copyright 2004 Richard Cappels, projects@cappels.org.include "2313def.inc"		;Include file in same directory as this file..;	UART BAUD RATE CALCULATIONS.equ     clock 		= 4000000	;Clock frequency.equ     baudrate 	= 9600          ;Choose a baudrate.equ     baudconstant 	= (clock/(16*baudrate))-1.equ	RFSigPort	= PORTB		;Port output signal is to appear on..equ	RFSigDDR	= DDRB		;Data Direction Register for signal output.equ	RFSigPin	= 0		;Pin output signal is to appear on..equ	LEDOutPort	= PORTB		;Indicator LED.equ	LEDOutDDR	= DDRB.equ	LEDOutPin	= 3.equ	CompPlusPort	= PORTB		;Comparitor noninverting input (input 0).equ	CompPlusDDR	= DDRB.equ	CompPlusPin	= 0.equ	BridgePowerPort	= PORTB		;Power for bridging resistors.equ	BridgePowerDDR	= DDRB.equ	BridgePowerPin	= 2;	used		= r0		;Used for loading indirect from memory..def	RFChar		= r16		;RF character I/O buffer.def	flagreg		= r17		;Flags for firmware control;	used		= ZL		;	used		= ZL;Assignment of flagreg bits;0	if set, indicates that local echo is on.;1;2;3;4;5;6;7;definition of I/O;B0	+ comparitor input - Antenna Signal;B1	- comparitor input - Antenna reference;B2	Bridge power		;B3	LED (high to turn LED on)	.	;B4	Receive/Xmit application (for test purposes)	;B5	(not assigned - configure as INPUT with weak pullup)	;B6	(not assigned - configure as INPUT with weak pullup)	;B7	(not assigned - configure as INPUT with weak pullup)	;D0	Reserved FOR UART RECEIVE - input has weak pullup;D1	Reserved FOR UART TRANSMIT - output.;D2	(not assigned - configure as INPUT with weak pullup);D3	(not assigned - configure as INPUT with weak pullup);D4	(not assigned - configure as INPUT with weak pullup);D5	(not assigned - configure as INPUT with weak pullup);D6	(not assigned - configure as INPUT with weak pullup);D7	(not assigned - configure as INPUT with weak pullup).cseg	.ORG $0000				;Initializaton code		rjmp start.ORG $0006 		rjmp timer0service	;Timer/counter compare interrupt for RF xmit/rcv..ORG $000A		rjmp comparitorservice	;Service comparitor interupt for RF receive..include	"vlfcw2313.inc"		;RF communications include file, in same directory as this file.start:   	ldi	RFChar,RAMEND		;Initialize Stack Pointer (AT90S2313 has an 8 bit stack pointer).	out	spl,RFChar			ldi     RFChar,baudconstant     	out     ubrr,RFChar		;Load baud rate into UART register.							;Set PORTD.	ldi	RFChar,0b00000010			out	DDRD,RFChar	ldi	RFChar,0b10111111	out	PORTD,RFChar						;Set PORTB.	ldi	RFChar,0b00000000			out	DDRB,RFChar	ldi	RFChar,0b11110100	out	PORTB,RFChar					sbi	ucr,rxen		;Enable UART receive.		sbi     ucr,txen 		;Enmable USRT transmit.		clr	flagreg			;Clear firmware flags.		wdr				;Reset the watchdog timer	ldi	RFChar,0b00001111	out	wdtcr,RFChar		;Enable the watchdog.		sbi	BridgePowerDDR,BridgePowerPin	;Turn on bridge power	sbi	LEDOutDDR,LEDOutPin	;Make LED pin an output pin.	sei				;ENABLE THE INTERRUPTS.	rcall	SayHello		;Send greeting to terminal.Terminal:				;This routine is for a terminal connection.	sbis	usr,rxc			;Check for data in UART receiver.	rjmp    norsdata		;If no data in UART, skip down.	in	RFChar,udr		;Read data from UART.	sbrc	flagreg,0		;Don't echo char if local echo is off	rcall	EchoIt			;Echo the character to terminal	cpi	RFChar,$1B		;Is this an escape character?	breq	escapechar		;If escape character, branch to treat as special.SB1:	rcall	SendRFByte		;Send data via RF.	rcall	PostXmitDelay		;Wait for circuit to recover from xmit.	norsdata:			rcall	ReceiveRFByte		;Check for data from RF port	wdr				;Reset the watchdog timer.	brcc	noemit			;If no data, don't emit the char, but skip down.	rcall	EmitUart	noemit:rjmp	Terminal		EchoIt:					;Echo character to terminal. If return, also send linefeed.	cpi	RFChar,$0D	breq	SendCRLF	rcall	EmitUart		;Send char via UART.	ret	SendCRLF:	rjmp	CRLF			;CRLF executes an "ret" instruction when complete (why exercize the stack pointer?).escapechar:	rcall	SendESCMessageW4DATA:	wdr				;Keep watchdog from timing out.	sbis	usr,rxc			;Wait for data in UART receiver.	rjmp    W4DATA				in	RFChar,udr		;Read data from UART.	rcall	EmitUart		;Echo the character 	rcall	CRLF			;Drop down one line.	cpi	RFChar,$1B		;If escape key this time, send via RF link.	breq	SB100				andi	RFChar,$5F		;Make upper-case character	cpi	RFChar,'E'		;If an "E", then toggle local echo enable bit.	breq	ToggleEcho	rjmp	norsdata		;If not special to this program, drop it.	SB100:	rcall	crlf	rjmp	SB1			;Send by RF link then continue to service terminal.	ToggleEcho:	sbrc	flagreg,0		;Test local echo enable flag bit.	rjmp	echoset	ori	flagreg,1		;Set local echo flag.	rcall	EchoOnMsg		;Announce that local echo is on.	rjmp	TEDone			;Jump to toggle echo done.echoset:	andi	flagreg,0b11111110	;Clear local echo flag	rcall	EchoOffMsg		;Announce that local echo is off.TEDone:	rjmp	Terminal		;Go back to servicing the terminal/RF link/EchoOnMsg:	ldi     ZH,high(2*EOM)		;Load high part of byte address into ZH.	ldi     ZL,low(2*EOM)		;Load low part of byte address into ZL.	rcall 	sendstring		;Send greeting.		retEOM:					;Text if Local Echo ON message.	.db     "Local Echo ON."	.db     $0A,$0D	.db     00,00EchoOffMsg:	ldi     ZH,high(2*EOFFM)	;Load high part of byte address into ZH.	ldi     ZL,low(2*EOFFM)		;Load low part of byte address into ZL.	rcall 	sendstring		;Send greeting.		retEOFFM:					;ext if Local Echo OFF message.	.db     "Local Echo OFF. "	.db     $0A,$0D	.db     00,00EmitUart:				;Send RFChar via UART, preserves content of RFChar	sbis    usr,udre		;Wait for data to arrive.	rjmp    EmitUart     	out     udr,RFChar		;Send byte.	ret			SendESCMessage:				;Send message indicating that a first escape character was received.	ldi     ZH,high(2*ESCMsg)	;Load high part of byte address into ZH.	ldi     ZL,low(2*ESCMsg)	;Load low part of byte address into ZL.	rcall 	sendstring		;Send greeting.		retESCMsg:					;Text of greeting.	.db     "ESC-"	.db     00,00SayHello:				;Power on/reset greeting routine.	ldi     ZH,high(2*Greeting)	;Load high part of byte address into ZH.	ldi     ZL,low(2*Greeting)	;Load low part of byte address into ZL.	rcall 	sendstring		;Send greeting.		ret	sendstring:          			;Flash string send routine. Call with location of string in Z.	lpm                    		;Load byte from program memory into r0.	tst	r0			;Check if we've reached the end of the message	breq	finisheds		;If so, return	mov	RFChar,r0	rcall	EmitUart		;Send the character via UART.	adiw	ZL,1			;Increment Z registers	rjmp	sendstring		;Keep going until a $00 is reached.finisheds:	retGreeting:				;Text of power on/reset greeting.	.db     $0A,$0D	.db	"VLF Terminal interface program vlfterm040715A "	.db     $0A,$0D	.db	"www.projects.cappels.org" 	.db     $0A,$0D	.db	"Local echo is off, to toggle, type escape-e." 	.db     $0A,$0D	.db     $0A,$0D	.db     00,00crlf:					;Send a carriage return and linefeed.	push	RFChar	ldi	RFChar,$0A	rcall	EmitUart	ldi	RFChar,$0D	rcall	EmitUart	pop	RFChar	ret	.exit				;No assembly below this line.
