summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/dev/spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/fe310/eos/dev/spi.c')
-rw-r--r--fw/fe310/eos/dev/spi.c68
1 files changed, 40 insertions, 28 deletions
diff --git a/fw/fe310/eos/dev/spi.c b/fw/fe310/eos/dev/spi.c
index fef00e1..319816d 100644
--- a/fw/fe310/eos/dev/spi.c
+++ b/fw/fe310/eos/dev/spi.c
@@ -5,6 +5,7 @@
#include "platform.h"
#include "eos.h"
+#include "log.h"
#include "msgq.h"
#include "event.h"
@@ -13,33 +14,36 @@
#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"
-#ifdef EOS_DEBUG
-#include <stdio.h>
-#endif
-
static unsigned char spi_dstack[EOS_SPI_MAX_DSTACK];
static unsigned char spi_dstack_len;
-static uint16_t spi_div[EOS_SPI_MAX_DEV];
+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 void spi_stop(unsigned char dev) {
+static int spi_stop(unsigned char dev) {
if (dev == EOS_SPI_DEV_NET) {
eos_net_stop();
- } else if (spi_cfg[dev].flags & SPI_DEV_FLAG_9BIT) {
- eos_spi_enable();
} else {
- eos_spi_stop();
+ 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) {
@@ -48,6 +52,7 @@ static void spi_start(unsigned char dev) {
} 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);
}
@@ -56,7 +61,8 @@ static void spi_start(unsigned char dev) {
int eos_spi_dev_init(void) {
int i;
- for (i=0; i<EOS_SPI_MAX_DEV; i++) {
+ /* dev modules are responsibile for configuring cs gpio */
+ for (i=0; i<SPI_MAX_DEV; i++) {
spi_div[i] = spi_cfg[i].div;
}
@@ -71,14 +77,10 @@ int eos_spi_select(unsigned char dev) {
int rv;
int dsel;
- if (eos_spi_cs_get()) rv = EOS_ERR_BUSY;
- if (!rv && (spi_dstack_len == EOS_SPI_MAX_DSTACK)) rv = EOS_ERR_FULL;
-
- if (rv) {
-#ifdef EOS_DEBUG
- printf("SPI SELECT DEV:%d ERR:%d\n", dev, rv);
-#endif
- return rv;
+ rv = EOS_OK;
+ if (spi_dstack_len == EOS_SPI_MAX_DSTACK) {
+ rv = EOS_ERR_FULL;
+ goto spi_select_fin;
}
dsel = 1;
@@ -87,13 +89,21 @@ int eos_spi_select(unsigned char dev) {
dsel = 0;
}
- if (dsel) spi_stop(spi_dev());
+ 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;
}
@@ -101,23 +111,25 @@ void eos_spi_deselect(void) {
int rv;
int dsel;
- if (eos_spi_cs_get()) rv = EOS_ERR_BUSY;
- if (!rv && (spi_dstack_len == 0)) rv = EOS_ERR_EMPTY;
-
- if (rv) {
-#ifdef EOS_DEBUG
- printf("SPI DESELECT ERR:%d\n", rv);
-#endif
- return;
+ 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) spi_stop(spi_dev());
+ 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) {