aboutsummaryrefslogtreecommitdiff
path: root/src/usi_spi.c
blob: 01c37f74b3765b81ac221e4f389fa3ce973c3950 (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
#include "../usi_spi.h"
#include "../mcu/mcu_def.h"

inline void usi_spi_init(enum spiMode mode) {
    if (mode == USI_SPI_MODE_MASTER) {
        // Set USCK and DO as output
        DDR_USI |= _BV(DD_USCK) | _BV(DD_DO);
        // Set DI pull up resistor
        PORT_USI |= _BV(PORT_DI);

        USICR |= _BV(USIWM0) | _BV(USICS1) | _BV(USICLK);
    } else {
        // Set DO as output
        DDR_USI |= _BV(DD_DO);
        // Set USCK and DI pull up resistor
        PORT_USI |= _BV(PORT_USCK) | _BV(PORT_DI);

        USICR |= _BV(USIWM0) | _BV(USICS1) | _BV(USIOIE);
    }
}

inline int8_t usi_spi_busy(void) {
    if (interface == SPI_INTERFACE_USI)
        return USISR & 0x0F;
}

inline void usi_spi_join(void) {
    while (spi_busy(interface));
}

inline uint8_t usi_spi_send(uint8_t data) {
    while (spi_busy(interface));
    return spi_transfer(interface, data);
}

inline uint8_t usi_spi_transfer(uint8_t data) {
    // TODO rewrite and this for noninterupt
    if (interface == SPI_INTERFACE_USI) {
        _spi_usi_busy = 1;
        USISR |= _BV(USIOIF);
        USIDR = d;
        do {
            USICR |= _BV(USITC);
        } while (!(USISR & _BV(USIOIF)));
        return USIDR;
    }
}

inline void usi_spi_expose(uint8_t data) {
    if (interface == SPI_INTERFACE_USI)
        USIDR = data;
}

////// Interrupts ////////////////////////////////