;This code tests UART1 of the MCF5307 

;

;The code first prints a message out of UART1 to a terminal

; such as WIN95 Hyperterm.  The message is "Send text file 

; now....".  The message is sent out one character at a time

; until the number "0" is recognized at the end of the string.  

; Then the code enters the receiver section.  This piece of 

; code receives characters from a terminal.  It polls the 

; RxRDY for a character and stores it into memory.  After 

; 26 characters are received, the code goes into a forever 

; loop.  You must send 26 characters for the code to exit 

; the receive section.  The best way to do this is probably

; to type the alphabet into a text file and send it when

; prompted.

  

    XDEF    _main 



status:   .skip 1

databyte: .skip 1



    XDEF    _main

_main:

    ;SIMR reset values are correct

    ;SYPCR reset values (watchdog disabled) are correct

    ;watchdog is disabled so no need to set up SWIVR and SWSR

    move.l  #0x10000001,d0       ;set up MBAR

    movec.l d0,MBAR

    move.l  #0,a6			;set up VBR - table is at 0

    movec.l a6,VBR			

    move.l #0x1000c040,d0		;set up ACR's and CACR

    movec.l d0,ACR0

    move.l #0x80008000,d0

    movec.l d0,CACR

    move.l  #_UARTInterrupt,d0

    move.l  d0,0x0100		;set Vector #64 to _UART1 addr(first user intr)



    clr.l	d0



    move.b #0x20,d0              ;reset reciever

    move.b d0,0x100001c8	;UCR



    move.b #0x30,d0              ;reset xmt

    move.b d0,0x100001c8	;UCR



    move.b #0x10,d0              ;reset mode reg pointer

    move.b d0,0x100001c8	;UCR, now points to UMR



    move.b #0x13,d0

    move.b d0,0x100001c0	;UMR1



    move.b #0x07,d0              ;

    move.b d0,0x100001c0	;UMR2



    move.b #0xDD,d0              ;internal clock

    move.b d0,0x100001c4	;UCSR



    move.b #64,d0                ;use interrupt 64 for interrupt

    move.b d0,0x100001f0	;UIVR



    move.b #0x00,d0              ;baud rate 19200 for sys clk 45Mhz

    move.b d0,0x100001d8	;UBG1 MSByte

    move.b #0x49,d0

    move.b d0,0x100001dC	;UBG2 LSByte



test_transmit:



    lea.l  message,a1		;set address pointer to message



    move.b #0x04,d0              ;enable transmit don't enable recieve

    move.b d0,0x100001c8	;UCR



loop1:

	clr.l 	d0

	move.b	(a1)+,d0	;load character into d0

	cmp.l	#0x30,d0	;check for EOL

	beq	finished

	move.b	d0,0x100001cc	;load UTB which begins transmission



wait:	

	move.b 	0x100001c4,d1	;load USR

	btst	#0x03,d1	;test for transmitter empty

	beq	wait	

	jmp	loop1



finished:

	bra 	recieve

recieve:

	lea.l	received,a1	;set the address pointer to the

				;beginning of the string

	clr.l	d0

	clr.l 	d1

	clr.l 	d2



	move.b  #0x09,d0	

	move.b 	d0,0x100001c8	;enable reciever and disable transmitter



wait2:	

	move.b 	0x100001c4,d1	;

	btst	#0x0,d1		;has a character been recieved?

	beq	wait2		;if no, wait



	move.b	0x100001cc,d0	;read URB

	move.b	d0,(a1)+	;store character in string

	

	addi.l	#1,d2		;count to 26

	cmp.l	#25,d2

	beq	t_finished

	jmp	wait2



t_finished:

	nop	

	



;    move.w #0x2400,sr		;lower IPL to allow UART1 rcv intr.



l1: 

    nop                         ;go into forever loop

    nop                         ;go into forever loop

    nop                         ;go into forever loop

    nop                         ;go into forever loop

    nop                         ;go into forever loop

    nop                         ;go into forever loop

    jmp l1

    dc.l    0x55555555

    dc.l    0x55555555

    dc.l    0x55555555

    dc.l    0x55555555



    XDEF    _UnexpectedInterrupt

_UnexpectedInterrupt:

    rte



    XDEF    _UARTInterrupt

_UARTInterrupt:

    move.l  d0,-(a7)

    clr.l   d0

    move.b  0x100001c4,d0       ;move the USR into d0

    move.b  d0,status           ;put it in dram

    

    btst    #0,d0               ;is it a receive interrupt?

    beq     trans               ;if equal, looks for zero bit set - that means

                                ;it wasn't a receive interrupt, must have been

                                ;transmit

    move.b  0x100001cC,d0       ;URB1, get received data

    move.b  d0,databyte         ;put it in dram 

;don't xmt    move.b  d0,0x100001cC       ;now turn around and transmit it

trans:

    move.l  (a7)+,d0

    rte



message:	

	.ascii	"Send text file now...0"

received:

	ds.b	26	;define 26 bytes of storage



.end



