
/*
 * uart.S - uart code for the h8
 */
	
#include "board.h"

	SELECT_CPU
	.text
		
#define BOARD_BRR BOARD_BRR_9600
	.global uart_init	
uart_init:	

	ENABLE_SCI
	
	/* disable the serial port */

	mov.b #0, r0l      
	mov.b r0l, @SCR

	/* setup the serial controller as uart n81 and clock/1 */
	
	mov.b #0, r0l      
	mov.b r0l, @SMR

	/* setup the clock generator to the desired bps */
	
	mov.b #BOARD_BRR, r0l      
	mov.b r0l, @BRR

	/* enable transmit and receive, no interrupts */
	
	mov.b #0x30, r0l
	mov.b r0l, @SCR

	rts

	.global uart_tx
uart_tx:			
	/* send a character passed in r0l*/
	
uart_tx_drain:	
	btst #7, @SSR
	beq uart_tx_drain
	
	mov.b r0l, @TDR
	bclr #7, @SSR

uart_tx_finish:	
	btst #2, @SSR
	beq uart_tx_finish

	rts	

	.global uart_rx
uart_rx:
	/* receive a character and return in r0l */

uart_rx_wait:	
	btst #6, @SSR
	beq uart_rx_wait

	mov.b @RDR, r0l
	bclr #6, @SSR
	
	rts

	.global uart_rx_check
uart_rx_check:
	/* check if data is available - z-flag is cleared if available */
	btst #6, @SSR
	rts
