Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions arch/arm/src/common/stm32/stm32_spi_v1v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,9 @@ static int spi_exchange(struct spi_dev_s *dev, const void *txbuffer,
{
if (nbytes > priv->buflen)
{
nbytes = priv->buflen;
/* Too big for the DMA buffer, fall back to non-DMA */

return spi_exchange_nodma(dev, txbuffer, rxbuffer, nwords);
}

if (txbuffer != NULL)
Expand All @@ -1988,10 +1990,6 @@ static int spi_exchange(struct spi_dev_s *dev, const void *txbuffer,
}

rxbuffer = rxbuffer ? priv->rxbuf : rxbuffer;

/* Rescale nwords to match the (possibly clamped) nbytes. */

nwords = (priv->nbits > 8) ? nbytes >> 1 : nbytes;
}

/* Flush the TX data to memory */
Expand Down
135 changes: 97 additions & 38 deletions arch/arm/src/common/stm32/stm32_spi_v3_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ struct stm32_spidev_s
uint32_t rxccr; /* DMA control register for RX transfers */
bool dmarunning; /* DMA is started */
volatile bool txbusy; /* TX DMA is in progress */
volatile bool txpending; /* TX waits for the end of a transfer */
size_t txlen; /* Words of the pending TX */
#endif
bool initialized; /* Has SPI interface been initialized */
mutex_t lock; /* Held while chip is selected for mutual
Expand Down Expand Up @@ -287,6 +289,7 @@ static void spi_dmatxsetup(struct stm32_spidev_s *priv,
size_t nwords);
static inline void spi_dmarxstart(struct stm32_spidev_s *priv);
static inline void spi_dmatxstart(struct stm32_spidev_s *priv);
static void spi_txarm(struct stm32_spidev_s *priv, size_t nwords);
#endif

/* NSS rising edge: end of a transfer */
Expand Down Expand Up @@ -719,9 +722,20 @@ static void spi_dumpregs(struct stm32_spidev_s *priv)

static inline bool spi_9to16bitmode(struct stm32_spidev_s *priv)
{
uint32_t regval = spi_getreg(priv, STM32_SPI_CFG1_OFFSET);
return priv->nbits > 8;
}

/****************************************************************************
* Name: spi_inqwords
*
* Description:
* Return the size of the RX queue in words of the current width.
*
****************************************************************************/

return ((regval & SPI_CFG1_CRCSIZE_9BIT) == SPI_CFG1_CRCSIZE_9BIT);
static inline int spi_inqwords(struct stm32_spidev_s *priv)
{
return CONFIG_STM32_SPI_SLAVE_QSIZE / (spi_9to16bitmode(priv) ? 2 : 1);
}

/****************************************************************************
Expand Down Expand Up @@ -806,7 +820,7 @@ static void spi_dmarxsetup(struct stm32_spidev_s *priv, size_t nwords)

dmacfg.paddr = priv->spibase + STM32_SPI_RXDR_OFFSET;
dmacfg.maddr = (uint32_t)priv->inq;
dmacfg.ndata = CONFIG_STM32_SPI_SLAVE_QSIZE;
dmacfg.ndata = spi_inqwords(priv);
dmacfg.cfg1 = priv->rxccr;
dmacfg.cfg2 = 0;

Expand Down Expand Up @@ -954,6 +968,30 @@ static inline void spi_enable(struct stm32_spidev_s *priv, bool state)
}
}

/****************************************************************************
* Name: spi_txarm
*
* Description:
* Arm the TX DMA with nwords from outq. Call only while NSS is high, as
* CFG1 is writable only with SPE=0. Disabling SPI also drops the old TX
* FIFO content, the circular RX DMA keeps running.
*
****************************************************************************/

#ifdef CONFIG_STM32_SPI_DMA
static void spi_txarm(struct stm32_spidev_s *priv, size_t nwords)
{
spi_enable(priv, false);
spi_modifyreg(priv, STM32_SPI_CFG1_OFFSET, SPI_CFG1_TXDMAEN, 0);
spi_putreg(priv, STM32_SPI_IFCR_OFFSET, SPI_IFCR_UDRC);

spi_dmatxsetup(priv, nwords);
spi_dmatxstart(priv);

spi_enable(priv, true);
}
#endif

/****************************************************************************
* Name: spi_setmode
*
Expand Down Expand Up @@ -1159,6 +1197,7 @@ static void spi_bind(struct spi_slave_ctrlr_s *ctrlr,
spi_dmarxsetup(priv, 0);
spi_dmarxstart(priv);
priv->txbusy = false;
priv->txpending = false;
priv->dmarunning = true;
#endif

Expand Down Expand Up @@ -1187,6 +1226,14 @@ static int spi_nssinterrupt(int irq, void *context, void *arg)
{
struct stm32_spidev_s *priv = (struct stm32_spidev_s *)arg;

#ifdef CONFIG_STM32_SPI_DMA
if (priv->txpending)
{
priv->txpending = false;
spi_txarm(priv, priv->txlen);
}
#endif

if (priv->dev != NULL)
{
SPIS_DEV_NOTIFY(priv->dev, SPISLAVE_RX_COMPLETE);
Expand Down Expand Up @@ -1249,6 +1296,7 @@ static void spi_unbind(struct spi_slave_ctrlr_s *ctrlr)
}

priv->txbusy = false;
priv->txpending = false;
priv->dmarunning = false;
}
#endif
Expand Down Expand Up @@ -1290,7 +1338,8 @@ static int spi_enqueue(struct spi_slave_ctrlr_s *ctrlr,
DEBUGASSERT(priv != NULL && priv->dev != NULL);

#ifdef CONFIG_STM32_SPI_DMA
if (priv->config == SIMPLEX_RX || !priv->dmarunning || priv->txbusy)
if (priv->config == SIMPLEX_RX || !priv->dmarunning || priv->txbusy ||
priv->txpending)
{
return 0;
}
Expand All @@ -1307,19 +1356,21 @@ static int spi_enqueue(struct spi_slave_ctrlr_s *ctrlr,
up_clean_dcache((uintptr_t)priv->outq,
(uintptr_t)priv->outq + DMA_ALIGN_UP(len * wordsize));

/* CFG1 is writable only with SPE=0. Disabling SPI also drops the old
* TX FIFO content, the circular RX DMA keeps running.
/* Do not touch a transfer in progress: while NSS is low, arm the TX
* from the NSS rising edge.
*/

flags = enter_critical_section();
spi_enable(priv, false);
spi_modifyreg(priv, STM32_SPI_CFG1_OFFSET, SPI_CFG1_TXDMAEN, 0);
spi_putreg(priv, STM32_SPI_IFCR_OFFSET, SPI_IFCR_UDRC);

spi_dmatxsetup(priv, len);
spi_dmatxstart(priv);
if (stm32_gpioread(priv->nss_pin))
{
spi_txarm(priv, len);
}
else
{
priv->txlen = len;
priv->txpending = true;
}

spi_enable(priv, true);
leave_critical_section(flags);

spi_lock(ctrlr, false);
Expand Down Expand Up @@ -1349,7 +1400,7 @@ static bool spi_qfull(struct spi_slave_ctrlr_s *ctrlr)
#ifdef CONFIG_STM32_SPI_DMA
struct stm32_spidev_s *priv = (struct stm32_spidev_s *)ctrlr;

return priv->txbusy;
return priv->txbusy || priv->txpending;
#else
return false;
#endif
Expand Down Expand Up @@ -1393,8 +1444,7 @@ static void spi_qflush(struct spi_slave_ctrlr_s *ctrlr)
/* Flush the input buffers */

#ifdef CONFIG_STM32_SPI_DMA
priv->ihead =
CONFIG_STM32_SPI_SLAVE_QSIZE - stm32_dmaresidual(priv->rxdma);
priv->ihead = spi_inqwords(priv) - stm32_dmaresidual(priv->rxdma);
#else
priv->ihead = 0;
priv->itail = 0;
Expand All @@ -1403,6 +1453,7 @@ static void spi_qflush(struct spi_slave_ctrlr_s *ctrlr)
/* Flush the output buffers */

#ifdef CONFIG_STM32_SPI_DMA
priv->txpending = false;
if (priv->txbusy)
{
spi_enable(priv, false);
Expand All @@ -1426,31 +1477,35 @@ static void spi_qflush(struct spi_slave_ctrlr_s *ctrlr)
* Handle buffer wrapping around & invalidate the previous buffer cache
*
* Input Parameters:
* ptr - pointer to the rx buffer
* start - start index of just received data
* end - end index of just received data
* priv - Device-specific state data
* start - start index of just received data, in words
* end - end index of just received data, in words
*
* Returned Value:
* new start index; 0 if buffer wrapped around, end otherwise
*
****************************************************************************/

static inline int spi_rx_buffer_free(uint8_t *ptr, int start, int end)
static inline int spi_rx_buffer_free(struct stm32_spidev_s *priv,
int start, int end)
{
size_t wordsize = spi_9to16bitmode(priv) ? 2 : 1;

/* The priv->ihead can only be greater than qsize if the device driver
* returns garbage
*/

if (end >= CONFIG_STM32_SPI_SLAVE_QSIZE)
if (end >= spi_inqwords(priv))
{
up_invalidate_dcache((uintptr_t)&priv->inq[start * wordsize],
(uintptr_t)&priv->inq[
CONFIG_STM32_SPI_SLAVE_QSIZE]);
end = 0;
up_invalidate_dcache((uintptr_t)&ptr[start],
(uintptr_t)&ptr[CONFIG_STM32_SPI_SLAVE_QSIZE]);
}
else
{
up_invalidate_dcache((uintptr_t)&ptr[start],
(uintptr_t)&ptr[end]);
up_invalidate_dcache((uintptr_t)&priv->inq[start * wordsize],
(uintptr_t)&priv->inq[end * wordsize]);
}

return end;
Expand All @@ -1473,12 +1528,13 @@ static inline int spi_rx_buffer_free(uint8_t *ptr, int start, int end)
static size_t spi_qpoll(struct spi_slave_ctrlr_s *ctrlr)
{
struct stm32_spidev_s *priv = (struct stm32_spidev_s *)ctrlr;
size_t wordsize;
int qwords;
int itail;
int ihead;
uint16_t bytes_left;
uint16_t words_left;

DEBUGASSERT(priv != NULL && priv->dev != NULL);
DEBUGASSERT(priv->ihead < CONFIG_STM32_SPI_SLAVE_QSIZE);

#ifdef CONFIG_STM32_SPI_DMA
if (!priv->dmarunning)
Expand All @@ -1491,8 +1547,12 @@ static size_t spi_qpoll(struct spi_slave_ctrlr_s *ctrlr)

spi_lock(ctrlr, true);

wordsize = spi_9to16bitmode(priv) ? 2 : 1;
qwords = spi_inqwords(priv);
DEBUGASSERT(priv->ihead < qwords);

#ifdef CONFIG_STM32_SPI_DMA
itail = CONFIG_STM32_SPI_SLAVE_QSIZE - stm32_dmaresidual(priv->rxdma);
itail = qwords - stm32_dmaresidual(priv->rxdma);
#else
#error Support only simplex mode rx with dma
#endif
Expand All @@ -1505,13 +1565,12 @@ static size_t spi_qpoll(struct spi_slave_ctrlr_s *ctrlr)
/* Receive the end of receive buffer */

priv->ihead += SPIS_DEV_RECEIVE(priv->dev,
(const uint16_t *)&priv->inq[ihead],
CONFIG_STM32_SPI_SLAVE_QSIZE -
ihead);
&priv->inq[ihead * wordsize],
qwords - ihead);

/* Invalidate dcache and wrap around the priv->ihead */

priv->ihead = spi_rx_buffer_free(priv->inq, ihead, priv->ihead);
priv->ihead = spi_rx_buffer_free(priv, ihead, priv->ihead);
}

ihead = priv->ihead;
Expand All @@ -1520,23 +1579,23 @@ static size_t spi_qpoll(struct spi_slave_ctrlr_s *ctrlr)
/* Receive the data between ihead and itail */

priv->ihead += SPIS_DEV_RECEIVE(priv->dev,
(const uint16_t *)&priv->inq[ihead],
&priv->inq[ihead * wordsize],
itail - ihead);

/* Invalidate dcache and wrap around the priv->ihead */

priv->ihead = spi_rx_buffer_free(priv->inq, ihead, priv->ihead);
priv->ihead = spi_rx_buffer_free(priv, ihead, priv->ihead);
}

/* Calculate the number of bytes left in the buffer */
/* Calculate the number of words left in the buffer */

bytes_left = itail < priv->ihead
? CONFIG_STM32_SPI_SLAVE_QSIZE - priv->ihead + itail
words_left = itail < priv->ihead
? qwords - priv->ihead + itail
: itail - priv->ihead;

spi_lock(ctrlr, false);

return bytes_left;
return words_left;
}

/****************************************************************************
Expand Down
6 changes: 3 additions & 3 deletions arch/arm/src/stm32h7/chip.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@
#define ARMV7M_ICACHE_LINESIZE 32 /* 32 bytes (8 words) */

/* SPI kernel clock and SPI clock limits. On the H7R/S (RM0477) SPI1 is
* limited to 130 MHz and the others to 200 MHz, SPI6 has a 16-byte FIFO
* and no SCK limit is applied below the kernel clock.
* limited to 130 MHz and the others to 200 MHz, SPI6 has a 16-byte FIFO.
* The H7R/S master SCK limit is 133 MHz (DS14359).
*/

#ifdef CONFIG_STM32_STM32H7RSXX
# define STM32_SPI_MAX_KER_CK 200000000UL
# define STM32_SPI1_MAX_KER_CK 130000000UL
# define STM32_SPI_CLK_MAX 200000000UL
# define STM32_SPI_CLK_MAX 133000000UL
# define STM32_SPI6_FIFOSIZE 16
#else
# define STM32_SPI_MAX_KER_CK 200000000UL
Expand Down