ESP32C3 > SPI2 > stuck on while

BugSimpson
Posts: 22
Joined: Fri Oct 11, 2024 2:14 pm

ESP32C3 > SPI2 > stuck on while

Postby BugSimpson » Thu Dec 05, 2024 9:45 pm

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);
}

}

MicroController
Posts: 2672
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32C3 > SPI2 > stuck on while

Postby MicroController » Fri Dec 06, 2024 12:41 am

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.

BugSimpson
Posts: 22
Joined: Fri Oct 11, 2024 2:14 pm

Re: ESP32C3 > SPI2 > stuck on while

Postby BugSimpson » Fri Dec 06, 2024 8:26 am

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();
}

MicroController
Posts: 2672
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32C3 > SPI2 > stuck on while

Postby MicroController » Fri Dec 06, 2024 1:40 pm

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?

BugSimpson
Posts: 22
Joined: Fri Oct 11, 2024 2:14 pm

Re: ESP32C3 > SPI2 > stuck on while

Postby BugSimpson » Fri Dec 06, 2024 3:38 pm

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?

Who is online

Users browsing this forum: Bing [Bot] and 2 guests