summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/dev/spi.c
blob: 12549fc2aa14754e74a7268350c62b61e8edb12c (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdlib.h>
#include <stdint.h>

#include "encoding.h"
#include "platform.h"

#include "eos.h"
#include "msgq.h"
#include "event.h"

#include "board.h"

#include "soc/interrupt.h"
#include "soc/spi.h"

#include "net.h"

#include "spi.h"
#include "spi_cfg.h"

static uint8_t spi_dev;
static uint8_t spi_lock;
static uint16_t spi_div[EOS_SPI_MAX_DEV];

int eos_spi_dev_init(uint8_t wakeup_cause) {
    int i;

    for (i=0; i<EOS_SPI_MAX_DEV; i++) {
        spi_div[i] = spi_cfg[i].div;
        if (!(spi_cfg[i].flags & SPI_DEV_FLAG_CSFLOAT) && (spi_cfg[i].cspin != -1)) {
            GPIO_REG(GPIO_OUTPUT_VAL)   |=  (1 << spi_cfg[i].cspin);
            GPIO_REG(GPIO_OUTPUT_EN)    |=  (1 << spi_cfg[i].cspin);
        }
    }

    return EOS_OK;
}

int eos_spi_select(unsigned char dev) {
    if (spi_lock) return EOS_ERR_BUSY;

    if (spi_cfg[spi_dev].flags & SPI_DEV_FLAG_9BIT) {
        eos_spi_enable();
    } else {
        if (spi_dev == EOS_SPI_DEV_NET) {
            eos_net_stop();
        } else {
            eos_spi_stop();
        }
    }

    spi_dev = dev;
    if (spi_cfg[dev].flags & SPI_DEV_FLAG_9BIT) {
        eos_spi_configure(spi_div[dev], spi_cfg[dev].csid, spi_cfg[dev].cspin, spi_cfg[dev].evt);
        eos_spi_disable();
    } else {
        if (dev == EOS_SPI_DEV_NET) {
            eos_net_start();
        } else {
            eos_spi_start(spi_div[dev], spi_cfg[dev].csid, spi_cfg[dev].cspin, spi_cfg[dev].evt);
        }
    }

    return EOS_OK;
}

int eos_spi_deselect(void) {
    if (spi_lock) return EOS_ERR_BUSY;
    if (spi_dev == EOS_SPI_DEV_NET) return EOS_ERR;

    if (spi_cfg[spi_dev].flags & SPI_DEV_FLAG_9BIT) {
        eos_spi_enable();
    } else {
        eos_spi_stop();
    }

    spi_dev = EOS_SPI_DEV_NET;
    eos_net_start();

    return EOS_OK;
}

void eos_spi_dev_configure(unsigned char dev) {
    eos_spi_configure(spi_div[dev], spi_cfg[dev].csid, spi_cfg[dev].cspin, spi_cfg[dev].evt);
}

uint8_t eos_spi_dev(void) {
    return spi_dev;
}

uint16_t eos_spi_div(unsigned char dev) {
    return spi_div[dev];
}

uint8_t eos_spi_csid(unsigned char dev) {
    return spi_cfg[dev].csid;
}

uint8_t eos_spi_cspin(unsigned char dev) {
    return spi_cfg[dev].cspin;
}

void eos_spi_lock(void) {
    spi_lock = 1;
}

void eos_spi_unlock(void) {
    spi_lock = 0;
}

void eos_spi_set_div(unsigned char dev, uint16_t div) {
    spi_div[dev] = div;
}