#include #include #include #include #include #include "encoding.h" #include "platform.h" #include "plic/plic_driver.h" #include "eos.h" #include "msgq.h" #include "interrupt.h" #include "net.h" #include "spi.h" #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) #define SPI_BUFQ_IDX_MASK(IDX) ((IDX) & (SPI_SIZE_BUFQ - 1)) static EOSMsgQ spi_sndq; static EOSMsgItem spi_sndq_array[SPI_SIZE_BUFQ]; static eos_evt_fptr_t evt_handler[EOS_NET_MAX_CMD]; static uint16_t evt_handler_wrapper_acq = 0; static uint16_t evt_handler_wrapper_en = 0; static SPIBufQ spi_bufq; static unsigned char spi_bufq_array[SPI_SIZE_BUFQ][SPI_SIZE_BUF]; static int spi_bufq_push(unsigned char *buffer) { spi_bufq.array[SPI_BUFQ_IDX_MASK(spi_bufq.idx_w++)] = buffer; return EOS_OK; } static unsigned char *spi_bufq_pop(void) { if (spi_bufq.idx_r == spi_bufq.idx_w) return NULL; return spi_bufq.array[SPI_BUFQ_IDX_MASK(spi_bufq.idx_r++)]; } static SPIState spi_state; static void spi_reset(void) { int i; spi_state.flags &= ~SPI_FLAG_CTS; spi_state.flags |= SPI_FLAG_RST; // before starting a transaction, set SPI peripheral to desired mode SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; while (SPI1_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL); SPI1_REG(SPI_REG_TXFIFO) = 0; SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(0); SPI1_REG(SPI_REG_IE) = SPI_IP_RXWM; } static void spi_xchg(unsigned char cmd, unsigned char *buffer, uint16_t len) { spi_state.flags &= ~SPI_FLAG_CTS; spi_state.flags |= SPI_FLAG_INIT; if (spi_state.next_buf == NULL) { if (cmd & EOS_NET_CMD_FLAG_ONEW) { spi_state.flags |= SPI_FLAG_ONEW; } else if (spi_state.next_cnt) { cmd |= EOS_NET_CMD_FLAG_ONEW; } } else if (cmd & EOS_NET_CMD_FLAG_ONEW) { cmd &= ~EOS_NET_CMD_FLAG_ONEW; } spi_state.cmd = cmd; spi_state.buf = buffer; spi_state.len_tx = len; spi_state.len_rx = 0; spi_state.idx_tx = 0; spi_state.idx_rx = 0; // before starting a transaction, set SPI peripheral to desired mode SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; while (SPI1_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL); SPI1_REG(SPI_REG_TXFIFO) = ((cmd << 3) | (len >> 8)) & 0xFF; while (SPI1_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL); SPI1_REG(SPI_REG_TXFIFO) = len & 0xFF; SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(1); SPI1_REG(SPI_REG_IE) = SPI_IP_RXWM; } static int spi_xchg_next(unsigned char *_buffer) { unsigned char cmd; unsigned char *buffer = NULL; uint16_t len; eos_msgq_pop(&spi_sndq, &cmd, &buffer, &len); if (cmd) { spi_xchg(cmd, buffer, len); } else if (spi_state.flags & SPI_FLAG_RTS) { if (_buffer == NULL) _buffer = spi_bufq_pop(); if (_buffer) { spi_xchg(0, _buffer, 0); return 0; } } return 1; } static void spi_xchg_handler(void) { volatile uint32_t r1, r2; int i; if (spi_state.flags & SPI_FLAG_RST) { while ((r1 = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY); SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO; SPI1_REG(SPI_REG_IE) = 0x0; spi_state.flags &= ~SPI_FLAG_RST; return; } else if (spi_state.flags & SPI_FLAG_INIT) { while ((r1 = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY); while ((r2 = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY); if (spi_state.cmd & EOS_NET_CMD_FLAG_ONEW) { r1 = 0; r2 = 0; } spi_state.cmd = ((r1 & 0xFF) >> 3); spi_state.len_rx = ((r1 & 0x07) << 8); spi_state.len_rx |= (r2 & 0xFF); spi_state.len = MAX(spi_state.len_tx, spi_state.len_rx); // Work around esp32 bug if (spi_state.len < 6) { spi_state.len = 6; } else if ((spi_state.len + 2) % 4 != 0) { spi_state.len = ((spi_state.len + 2)/4 + 1) * 4 - 2; } SPI1_REG(SPI_REG_TXCTRL) = SPI_TXWM(SPI_SIZE_CHUNK/2); SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(SPI_SIZE_CHUNK - 1); SPI1_REG(SPI_REG_IE) = SPI_IP_TXWM | SPI_IP_RXWM; spi_state.flags &= ~SPI_FLAG_INIT; } if (SPI1_REG(SPI_REG_IP) & SPI_IP_TXWM) { uint16_t sz_chunk = MIN(spi_state.len - spi_state.idx_tx, SPI_SIZE_CHUNK); for (i=0; i EOS_NET_MAX_CMD) { eos_evtq_bad_handler(cmd, buffer, len); } else { unsigned char idx = (cmd & ~EOS_EVT_MASK) - 1; uint16_t flag = (uint16_t)1 << idx; if (flag & evt_handler_wrapper_en) { eos_evtq_handler_wrapper(cmd, buffer, len, &evt_handler_wrapper_acq, flag, evt_handler[idx]); } else { evt_handler[idx](cmd, buffer, len); } } } void eos_net_init(void) { int i; spi_bufq.idx_r = 0; spi_bufq.idx_w = 0; for (i=0; i