#include <avr/io.h>

	.global __INIT_UART_9600_8N1
	.global __wait_tx
	.global __wait_rx

__INIT_UART_9600_8N1:
	push	R16
	push	R17
	ldi		R16, 0
	ldi		R17, 12
	sts		UBRR0H, R16
	out		_SFR_IO_ADDR(UBRR0L), R17	; load divider for 9600 ($000C)

 	ldi		R16, 0x02 ; INITIALIZE UART0
	out		_SFR_IO_ADDR(UCSR0A), R16

	ldi		R16, 0b00011000
	out		_SFR_IO_ADDR(UCSR0B), R16

	ldi		R16, 0b00000110
	sts		_SFR_IO_ADDR(UCSR0C), R16
	pop		R17
	pop		R16
	
	ret
	
__wait_tx:
	sbis	_SFR_IO_ADDR(UCSR0A), UDRE0
	rjmp	__wait_tx
	ret
	
__wait_rx:
	sbis	_SFR_IO_ADDR(UCSR0A), RXC0
	rjmp	__wait_rx
	ret
