From 5c4ad4969514b82285ef37922f7acf61600c70de Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Thu, 8 Apr 2021 04:03:03 +0200 Subject: reintrodiced spi_dev --- fw/fe310/eos/Makefile | 2 +- fw/fe310/eos/board.h | 2 + fw/fe310/eos/eos.c | 2 + fw/fe310/eos/eve/eve_platform.h | 1 + fw/fe310/eos/net.c | 1 + fw/fe310/eos/power.c | 2 + fw/fe310/eos/spi.c | 92 +++++++++++------------------------------ fw/fe310/eos/spi.h | 25 ++++------- fw/fe310/eos/spi_cfg.h | 35 ++++++++++++++++ fw/fe310/eos/spi_dev.c | 89 +++++++++++++++++++++++++++++++++++++++ fw/fe310/eos/spi_dev.h | 21 ++++++++++ fw/fe310/eos/spi_priv.h | 44 ++------------------ 12 files changed, 189 insertions(+), 127 deletions(-) create mode 100644 fw/fe310/eos/spi_cfg.h create mode 100644 fw/fe310/eos/spi_dev.c create mode 100644 fw/fe310/eos/spi_dev.h (limited to 'fw') diff --git a/fw/fe310/eos/Makefile b/fw/fe310/eos/Makefile index 366e69a..0b3d6fe 100644 --- a/fw/fe310/eos/Makefile +++ b/fw/fe310/eos/Makefile @@ -2,7 +2,7 @@ include ../common.mk CFLAGS += -I. -I../bsp/include -I../bsp/drivers -obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o power.o i2s.o i2c.o uart.o spi.o net.o wifi.o cell.o sock.o unicode.o +obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o power.o i2s.o i2c.o uart.o spi.o spi_dev.o net.o wifi.o cell.o sock.o unicode.o %.o: %.c %.h diff --git a/fw/fe310/eos/board.h b/fw/fe310/eos/board.h index 98741da..e663f62 100644 --- a/fw/fe310/eos/board.h +++ b/fw/fe310/eos/board.h @@ -13,6 +13,8 @@ #define SPI_CSPIN_SDC SPI_CSPIN_NONE #define SPI_CSPIN_CAM 23 +#define SPI_IOF_MASK_CS (((uint32_t)1 << IOF_SPI1_SS0) | ((uint32_t)1 << IOF_SPI1_SS2) | ((uint32_t)1 << IOF_SPI1_SS3)) + #define NET_PIN_RTS 20 #define NET_PIN_CTS 22 diff --git a/fw/fe310/eos/eos.c b/fw/fe310/eos/eos.c index 490e68b..aa374a0 100644 --- a/fw/fe310/eos/eos.c +++ b/fw/fe310/eos/eos.c @@ -7,6 +7,7 @@ #include "i2s.h" #include "uart.h" #include "spi.h" +#include "spi_dev.h" #include "net.h" #include "wifi.h" #include "cell.h" @@ -28,6 +29,7 @@ void eos_init(void) { eos_i2s_init(); eos_uart_init(); eos_spi_init(); + eos_spi_dev_init(); eos_net_init(); eos_power_init(); eos_wifi_init(); diff --git a/fw/fe310/eos/eve/eve_platform.h b/fw/fe310/eos/eve/eve_platform.h index 48ea00b..7975fbc 100644 --- a/fw/fe310/eos/eve/eve_platform.h +++ b/fw/fe310/eos/eve/eve_platform.h @@ -1,6 +1,7 @@ #include #include "spi.h" +#include "spi_dev.h" #include "timer.h" #define EVE_ETYPE_INTR 1 diff --git a/fw/fe310/eos/net.c b/fw/fe310/eos/net.c index c9bbc63..5edb013 100644 --- a/fw/fe310/eos/net.c +++ b/fw/fe310/eos/net.c @@ -15,6 +15,7 @@ #include "spi.h" #include "spi_priv.h" +#include "spi_dev.h" #include "net.h" diff --git a/fw/fe310/eos/power.c b/fw/fe310/eos/power.c index 6f292b8..5f44a94 100644 --- a/fw/fe310/eos/power.c +++ b/fw/fe310/eos/power.c @@ -7,6 +7,8 @@ #include "eos.h" #include "event.h" #include "timer.h" +#include "spi.h" +#include "spi_dev.h" #include "net.h" #include "eve/eve.h" diff --git a/fw/fe310/eos/spi.c b/fw/fe310/eos/spi.c index 932c5f1..2966ab9 100644 --- a/fw/fe310/eos/spi.c +++ b/fw/fe310/eos/spi.c @@ -11,18 +11,24 @@ #include "board.h" -#include "net.h" #include "spi.h" #include "spi_priv.h" +#define SPI_MODE0 0x00 +#define SPI_MODE1 0x01 +#define SPI_MODE2 0x02 +#define SPI_MODE3 0x03 + +#define SPI_FLAG_XCHG 0x10 + +#define SPI_IOF_MASK (((uint32_t)1 << IOF_SPI1_SCK) | ((uint32_t)1 << IOF_SPI1_MOSI) | ((uint32_t)1 << IOF_SPI1_MISO)) | SPI_IOF_MASK_CS + #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) -static uint8_t spi_dev; static uint8_t spi_cspin; -static uint8_t spi_lock; -static uint16_t spi_div[EOS_SPI_MAX_DEV]; static volatile uint8_t spi_state_flags; +static unsigned char spi_evt; static unsigned char spi_in_xchg; static uint32_t spi_state_len = 0; @@ -30,11 +36,11 @@ static uint32_t spi_state_idx_tx = 0; static uint32_t spi_state_idx_rx = 0; static unsigned char *spi_state_buf = NULL; -static eos_evt_handler_t evt_handler[EOS_SPI_MAX_DEV]; +static eos_evt_handler_t evt_handler[EOS_SPI_MAX_EVT]; static void spi_handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) { unsigned char idx = (type & ~EOS_EVT_MASK) - 1; - if (idx < EOS_SPI_MAX_DEV) { + if (idx < EOS_SPI_MAX_EVT) { evt_handler[idx](type, buffer, len); } else { eos_evtq_bad_handler(type, buffer, len); @@ -44,7 +50,7 @@ static void spi_handle_evt(unsigned char type, unsigned char *buffer, uint16_t l void eos_spi_init(void) { int i; - for (i=0; i #include "event.h" -#define EOS_SPI_DEV_NET 0 -#define EOS_SPI_DEV_EVE 1 -#define EOS_SPI_DEV_SDC 2 -#define EOS_SPI_DEV_CAM 3 - -#define EOS_SPI_MAX_DEV 4 - #define EOS_SPI_FLAG_TX 0x01 #define EOS_SPI_FLAG_MORE 0x02 #define EOS_SPI_FLAG_BSWAP 0x04 -void eos_spi_init(void); -int eos_spi_select(unsigned char dev); -int eos_spi_deselect(void); +#define EOS_SPI_EVT_SDC 1 +#define EOS_SPI_EVT_CAM 2 -uint8_t eos_spi_dev(void); -uint16_t eos_spi_div(unsigned char dev); -uint16_t eos_spi_csid(unsigned char dev); -uint16_t eos_spi_cspin(unsigned char dev); +#define EOS_SPI_MAX_EVT 2 -void eos_spi_lock(void); -void eos_spi_unlock(void); -void eos_spi_set_div(unsigned char dev, uint16_t div); -void eos_spi_set_handler(unsigned char dev, eos_evt_handler_t handler); +void eos_spi_init(void); +void eos_spi_start(uint8_t div, uint8_t csid, uint8_t cspin, unsigned char evt); +void eos_spi_stop(void); +void eos_spi_set_handler(unsigned char evt, eos_evt_handler_t handler); void _eos_spi_xchg_init(unsigned char *buffer, uint16_t len, uint8_t flags); void eos_spi_xchg(unsigned char *buffer, uint16_t len, uint8_t flags); diff --git a/fw/fe310/eos/spi_cfg.h b/fw/fe310/eos/spi_cfg.h new file mode 100644 index 0000000..43b0763 --- /dev/null +++ b/fw/fe310/eos/spi_cfg.h @@ -0,0 +1,35 @@ +#include + +typedef struct { + uint16_t div; + uint8_t csid; + uint8_t cspin; + unsigned char evt; +} SPIConfig; + +static const SPIConfig spi_cfg[] = { + { // DEV_NET + .div = SPI_DIV_NET, + .csid = SPI_CSID_NET, + .cspin = SPI_CSPIN_NET, + .evt = 0, // Not SPI event + }, + { // DEV_EVE + .div = SPI_DIV_EVE, + .csid = SPI_CSID_EVE, + .cspin = SPI_CSPIN_EVE, + .evt = 0, + }, + { // DEV_SDC + .div = SPI_DIV_SDC, + .csid = SPI_CSID_SDC, + .cspin = SPI_CSPIN_SDC, + .evt = EOS_SPI_EVT_SDC, + }, + { // DEV_CAM + .div = SPI_DIV_CAM, + .csid = SPI_CSID_CAM, + .cspin = SPI_CSPIN_CAM, + .evt = EOS_SPI_EVT_CAM, + }, +}; diff --git a/fw/fe310/eos/spi_dev.c b/fw/fe310/eos/spi_dev.c new file mode 100644 index 0000000..d279faf --- /dev/null +++ b/fw/fe310/eos/spi_dev.c @@ -0,0 +1,89 @@ +#include +#include + +#include "encoding.h" +#include "platform.h" + +#include "eos.h" +#include "msgq.h" +#include "interrupt.h" +#include "event.h" + +#include "board.h" + +#include "net.h" +#include "spi.h" +#include "spi_priv.h" +#include "spi_cfg.h" +#include "spi_dev.h" + +static uint8_t spi_dev; +static uint8_t spi_lock; +static uint16_t spi_div[EOS_SPI_MAX_DEV]; + +void eos_spi_dev_init(void) { + int i; + + for (i=0; i + +#define EOS_SPI_DEV_NET 0 +#define EOS_SPI_DEV_EVE 1 +#define EOS_SPI_DEV_SDC 2 +#define EOS_SPI_DEV_CAM 3 + +#define EOS_SPI_MAX_DEV 4 + +void eos_spi_dev_init(void); +int eos_spi_select(unsigned char dev); +int eos_spi_deselect(void); + +uint8_t eos_spi_dev(void); +uint16_t eos_spi_div(unsigned char dev); +uint8_t eos_spi_csid(unsigned char dev); +uint8_t eos_spi_cspin(unsigned char dev); + +void eos_spi_lock(void); +void eos_spi_unlock(void); +void eos_spi_set_div(unsigned char dev, uint16_t div); diff --git a/fw/fe310/eos/spi_priv.h b/fw/fe310/eos/spi_priv.h index a0344ff..72c2dae 100644 --- a/fw/fe310/eos/spi_priv.h +++ b/fw/fe310/eos/spi_priv.h @@ -1,44 +1,8 @@ -#define SPI_MODE0 0x00 -#define SPI_MODE1 0x01 -#define SPI_MODE2 0x02 -#define SPI_MODE3 0x03 - -/* DO NOT TOUCH THEESE */ -#define SPI_SIZE_CHUNK 4 -#define SPI_SIZE_WM 2 - -#define SPI_FLAG_XCHG 0x10 +#include #define SPI_CSID_NONE 1 #define SPI_CSPIN_NONE 0xff -#define SPI_IOF_MASK (((uint32_t)1 << IOF_SPI1_SCK) | ((uint32_t)1 << IOF_SPI1_MOSI) | ((uint32_t)1 << IOF_SPI1_MISO)) | ((uint32_t)1 << IOF_SPI1_SS0) | ((uint32_t)1 << IOF_SPI1_SS2) | ((uint32_t)1 << IOF_SPI1_SS3) - -typedef struct { - uint8_t div; - uint8_t csid; - uint8_t cspin; -} SPIConfig; - -static const SPIConfig spi_cfg[] = { - { // DEV_NET - .div = SPI_DIV_NET, - .csid = SPI_CSID_NET, - .cspin = SPI_CSPIN_NET, - }, - { // DEV_EVE - .div = SPI_DIV_EVE, - .csid = SPI_CSID_EVE, - .cspin = SPI_CSPIN_EVE, - }, - { // DEV_SDC - .div = SPI_DIV_SDC, - .csid = SPI_CSID_SDC, - .cspin = SPI_CSPIN_SDC, - }, - { // DEV_CAM - .div = SPI_DIV_CAM, - .csid = SPI_CSID_CAM, - .cspin = SPI_CSPIN_CAM, - }, -}; \ No newline at end of file +/* DO NOT TOUCH THEESE */ +#define SPI_SIZE_CHUNK 4 +#define SPI_SIZE_WM 2 -- cgit v1.2.3