Page 1 of 1

ESP32C3 > SPI2 > stuck on while

Posted: Thu Dec 05, 2024 9:45 pm
by BugSimpson
Good evening,

I currently have the problem that when I want to send a byte via the SPI interface, it gets stuck in the first while loop.
just why?
What other condition must exist?

Code: Untitled.c Select all


void spiWriteByte(uint8_t data) 
{
int timeout = 0;

//spi->dev->mosi_dlen.usr_mosi_dbitlen = 7;
REG_WRITE( SPI_MS_DLEN_REG, 7 );

//spi->dev->data_buf[0] = data;
REG_WRITE( SPI_Wn_REG(0), data );

//spi->dev->cmd.update = 1;
REG_SET_BIT( SPI_CMD_REG, SPI_UPDATE_bm );

//while (spi->dev->cmd.update);
while ( REG_READ( SPI_CMD_REG ) & SPI_UPDATE_bm )
{
delay(100);
}

//spi->dev->cmd.usr = 1;
REG_SET_BIT( SPI_CMD_REG, SPI_USR_bm );

while ( REG_READ( SPI_CMD_REG ) & SPI_USR_bm )
{
delay(100);
}

}

Re: ESP32C3 > SPI2 > stuck on while

Posted: Fri Dec 06, 2024 12:41 am
by MicroController
As per the TRM, the update bit is a "WT" (write-to-trigger) only bit. It doesn't represent a state which can be read.

Re: ESP32C3 > SPI2 > stuck on while

Posted: Fri Dec 06, 2024 8:26 am
by BugSimpson
As per the TRM, the update bit is a "WT" (write-to-trigger) only bit. It doesn't represent a state which can be read.
ah okay, i see it ;)

I took these examples from the Arduino code for spi where it waits for the bit

Code: Untitled.c Select all


  spi->dev->cmd.update = 1;
while (spi->dev->cmd.update);
or am I misunderstanding that?

original arduino code:

Code: Select all

void spiWriteByte(spi_t *spi, uint8_t data) {
  if (!spi) {
    return;
  }
  SPI_MUTEX_LOCK();
  spi->dev->mosi_dlen.usr_mosi_dbitlen = 7;
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32
  spi->dev->miso_dlen.usr_miso_dbitlen = 0;
#endif
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2
  spi->dev->data_buf[0].val = data;
#else
  spi->dev->data_buf[0] = data;
#endif

#if CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2
  spi->dev->cmd.update = 1;
  while (spi->dev->cmd.update);
#endif
  spi->dev->cmd.usr = 1;
  while (spi->dev->cmd.usr);
  SPI_MUTEX_UNLOCK();
}

Re: ESP32C3 > SPI2 > stuck on while

Posted: Fri Dec 06, 2024 1:40 pm
by MicroController
If that Arduino code does work but you only keep getting the value back you just wrote... Did you power up and un-reset the SPI peripheral first?

Re: ESP32C3 > SPI2 > stuck on while

Posted: Fri Dec 06, 2024 3:38 pm
by BugSimpson
you mean the clock for the spi unit and the reset mode disabled?
yes, I did that.

As you already said that this is a "WT" bit, why do you poll it?