aboutsummaryrefslogtreecommitdiff
path: root/src/avr25/spi_usi.c
blob: e63721abc5a553318de0c2e7b3525cfa30e18e36 (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
#include "../../spi_usi.h"

#if __AVR_ARCH__ == 25

#ifdef IOE_SPI_USI_MASTER

// TODO counter settings with interups
inline void ioe_spi_usi_init(void) {
    USI_DIR_REG |= _BV(USI_USCK_PIN) | _BV(USI_DO_PIN);
    USI_OUT_REG |= _BV(USI_DI_PIN);

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

inline int8_t ioe_spi_usi_transfer(int8_t d) {
    USISR |= _BV(USIOIF);
    USIDR = d;
    do {
        USICR |= _BV(USITC);
    } while (!(USISR & _BV(USIOIF)));
    return USIDR;
}

#else /* IOE_SPI_USI_MASTER */

inline void ioe_spi_usi_init(void) {
    USI_DIR_REG |= _BV(USI_DO_PIN);
    USI_OUT_REG |= _BV(USI_USCK_PIN) | _BV(USI_DI_PIN);

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

inline void ioe_spi_usi_expose(int8_t data) {
    USIDR = data;
}

inline void ioe_spi_usi_expect(void) {
    USISR |= _BV(USIOIF);
}

inline int ioe_spi_usi_busy(void) {
    return USISR & 0x0F;
}

inline void ioe_spi_usi_join(void) {
    while (ioe_spi_usi_busy()) {
    }
}

SIGNAL(USI_OVF_vect) {
    ioe_spi_usi_retrieve(USIDR);
}

#endif /* IOE_SPI_USI_MASTER */

#endif /* __AVR_ARCH__ == 25 */