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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#include <stdlib.h>
#include <stdint.h>
#include "encoding.h"
#include "platform.h"
#include "eos.h"
#include "log.h"
#include "msgq.h"
#include "event.h"
#include "board.h"
#include "soc/interrupt.h"
#include "soc/spi.h"
#include "soc/spi_priv.h"
#include "soc/spi9bit.h"
#include "net.h"
#include "egpio.h"
#include "egpio_priv.h"
#include "spi.h"
#include "spi_cfg.h"
static unsigned char spi_dstack[EOS_SPI_MAX_DSTACK];
static unsigned char spi_dstack_len;
static uint16_t spi_div[SPI_MAX_DEV];
static uint8_t spi_dev(void) {
return spi_dstack_len ? spi_dstack[spi_dstack_len - 1] : EOS_SPI_DEV_NET;
}
static int spi_stop(unsigned char dev) {
if (dev == EOS_SPI_DEV_NET) {
eos_net_stop();
} else {
if (eos_spi_get_cs()) return EOS_ERR_BUSY;
if (spi_cfg[dev].flags & SPI_DEV_FLAG_9BIT) {
eos_spi9bit_stop();
eos_spi_enable();
} else {
eos_spi_stop();
}
}
return EOS_OK;
}
static void spi_start(unsigned char dev) {
if (dev == EOS_SPI_DEV_NET) {
eos_net_start();
} else 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();
eos_spi9bit_start();
} else {
eos_spi_start(spi_div[dev], spi_cfg[dev].csid, spi_cfg[dev].cspin, spi_cfg[dev].evt);
}
}
int eos_spi_dev_init(void) {
int i;
/* dev modules are responsibile for configuring cs gpio */
for (i=0; i<SPI_MAX_DEV; i++) {
spi_div[i] = spi_cfg[i].div;
}
for (i=0; i<EOS_SPI_MAX_DSTACK; i++) {
spi_dstack[i] = 0xff;
}
return EOS_OK;
}
int eos_spi_select(unsigned char dev) {
int rv;
int dsel;
rv = EOS_OK;
if (spi_dstack_len == EOS_SPI_MAX_DSTACK) {
rv = EOS_ERR_FULL;
goto spi_select_fin;
}
dsel = 1;
if (spi_dev() == dev) {
dev |= EOS_SPI_DEV_FLAG_NDSEL;
dsel = 0;
}
if (dsel) {
rv = spi_stop(spi_dev());
if (rv) goto spi_select_fin;
}
spi_dstack[spi_dstack_len] = dev;
spi_dstack_len++;
if (dsel) spi_start(dev);
spi_select_fin:
if (rv) {
EOS_LOG(EOS_LOG_ERR, "SPI SELECT DEV:%d ERR:%d\n", dev, rv);
return rv;
}
return EOS_OK;
}
void eos_spi_deselect(void) {
int rv;
int dsel;
rv = EOS_OK;
if (spi_dstack_len == 0) {
rv = EOS_ERR_EMPTY;
goto spi_deselect_fin;
}
dsel = !(spi_dev() & EOS_SPI_DEV_FLAG_NDSEL);
if (dsel) {
rv = spi_stop(spi_dev());
if (rv) goto spi_deselect_fin;
}
spi_dstack_len--;
spi_dstack[spi_dstack_len] = 0xff;
if (dsel) spi_start(spi_dev());
spi_deselect_fin:
if (rv) EOS_LOG(EOS_LOG_ERR, "SPI DESELECT ERR:%d\n", rv);
}
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_set_div(unsigned char dev, uint16_t div) {
spi_div[dev] = div;
}
|