summaryrefslogtreecommitdiffstats
path: root/sw/z80_test/usart.c
blob: 9ec6dbddb927d286890f1d950c102bff12850fc8 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "usart.h"

static struct _usart_device *_usart = (struct _usart_device *) ADDR_DEV_USART;

void usart_set_baudrate(uint16_t baudrate)
{
    // enable latch access
    _usart->LCR.divisor_latch_access = 1;
    _usart->buffer = 0x00FF & baudrate; // LSBs
    memcpy(&_usart->IER, &(baudrate >>8), 1);
    // _usart->IER    = 0x00FF & (baudrate >> 8); // MSBs
    _usart->LCR.divisor_latch_access = 0;
}

void usart_set_parity(int mode)
{
    if (mode == USART_PARITY_EVEN) {
        _usart->LCR.even_parity = 1;
    }
    else if (mode == USART_PARITY_ODD) {
        _usart->LCR.even_parity = 0;
    }

     _usart->LCR.parity = (mode == USART_PARITY_NONE) ? 0 : 1;
}

void usart_set_stop_bits(int count)
{
    _usart->LCR.stop_bits = (count == USART_STOP_BITS_1) ? 0 : 1;
}

void usart_word_length(int length)
{
    _usart->LCR.word_length = length;
}

void usart_set_autoflow(int mode)
{
    _usart->MCR.autoflow = (mode == USART_AUTOFLOW_OFF) ? 0 : 1;
    _usart->MCR.data_terminal_ready = (mode == USART_AUTOFLOW_ALL);
}

inline void usart_init(uint16_t baudrate, int parity, int stop_bits)
{
    usart_set_baudrate(baudrate);
    usart_set_parity(parity);
    usart_set_stop_bits(stop_bits);
    usart_set_autoflow(USART_AUTOFLOW_OFF);
}

void usart_transmit(uint8_t data)
{
    _usart->buffer = data;
    while (_usart->LSR.transmitter_holder_empty == 0); // wait
}

uint8_t usart_receive()
{
    return _usart->buffer;
}

int usart_write(uint8_t *data, size_t size)
{
    uint8_t *dp = data;

    while (size--) {
        _usart->buffer = *(dp++);
        while (_usart->LSR.transmitter_empty);
    }

    // TODO: do something that actually counts for sent bytes 
    return size;
}

int usart_read(uint8_t *buffer, size_t count)
{
    uint8_t *bp = buffer;
    size_t read_count = 0;

    while (count--) {
        *(bp++) = _usart->buffer;
        // check for errors
        if (_usart->LSR.framing_error || _usart->LSR.parity_error) {
            bp--; // delete last byte (?)
        } else {
            read_count++;
        }
    }

    return read_count;
}