I should point out that the code below is far from peak efficiency - the right way to push a known string ("Ready") is to use a word define in assembly, but as I
said, I was starting from what I knew, and I knew how to send one character at a time, so that's the way I began.
; Set Divisor Latch Enable
Setup:
; Bring up OUT2 GPIO pin first, so we know things are starting like they're supposed to
IN A,($04)
OR 00001000b
OUT ($04), A
; Set Divisor Latch Enable
LD A,10000000b ; Set Div Latch Enable to 1
OUT ($03),A ; Write LCR
; Set divisor to 104 (16mHz / 104 / 16 = 9615bps)
LD A,$68
OUT ($00),A ; DLL 0x68 (#104)
LD A,$00
OUT ($01),A ; DLM 0x00
LD A,00000011b ; Set DLE to 0, Break to 0, No parity, 1 stop bit, 8 bytes
OUT ($03),A ; Write now configured LCR
LD C,$00 ; Write output UART port to reg C for use later
LD SP,$ff00 ; Initialise the stack pointer to $ff00 (it will grow DOWN in RAM)
Alert: ; Print a prompt so we know it's up
LD B,'R' ; Load first character to print into reg B
CALL Output
LD B,'e'
CALL Output
LD B,'a'
CALL Output
LD B, 'd'
CALL Output
LD B, 'y'
CALL Output
LD B, ':'
CALL Output
LD B, ' '
CALL Output
Main: ; Main read/write loop
CALL Input ; Read a byte from serial terminal
CALL Output ; Echo it straight back out
LD B, ' ' ; Print a space between echo
CALL Output
JP Main
;; Take a character in register B and output to the UART, toggling the GPIO LED
Output:
IN A,($04) ; Toggle OUT1 GPIO LED
XOR 00000100b
OUT ($04), A
OUT (C),B ; Send character to UART
LoopOut: ; Ensure the byte was transmitted
IN A,($05) ; Read LSR
BIT 6,A ; Check bit 6 (THR empty, line idle)
JP Z,LoopOut
RET
;; Read a character from the UART and place in register B
Input:
LoopIn:
IN A,($05) ; Read LSR
BIT 0,A ; Check bit 0 (RHR byte ready)
JP Z,LoopIn
IN B,(C) ; Place ready character into B
RET
What's it doing?
The initialisation of the UART works in the same manner as it does in the UART test code, the only addition is setting the stack pointer. I then, character by character, print
a banner - 'Ready: ' - to let me know it's working. The code then works as a very simple echo, by: