summaryrefslogtreecommitdiffstats
path: root/sw-avr/rom-loader/usart.c
blob: 5637d4fe12afbf2a39cfd54a12177db05ac762d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "usart.h"

#include <avr/io.h>
#include <util/setbaud.h>

void usart_init(void)
{
    // automated by util/setbaud.h
    UBRR0H = UBRRH_VALUE;
    UBRR0L = UBRRL_VALUE;

#if USE_2X
    UCSR0A |= _BV(U2X0);
#else
    UCSR0A &= ~(_BV(U2X0));
#endif

    // 8 bit data
    UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);

    // enable TX and RX pins
    UCSR0B = _BV(RXEN0) | _BV(TXEN0);
}

void usart_send(uint8_t c)
{
    UDR0 = c;    
    // wait until transmission ready
    loop_until_bit_is_set(UCSR0A, TXC0); 
}

uint8_t usart_recv(void)
{
    // wait until data exists
    loop_until_bit_is_set(UCSR0A, RXC0);
    return UDR0;
}