nuttx icon indicating copy to clipboard operation
nuttx copied to clipboard

[BUG] ESP32C6 SPI Read Issue

Open FelipeMdeO opened this issue 1 year ago • 16 comments

Description / Steps to reproduce the issue

Hello All,

I am porting MFRC522 rfid card reader to esp32 c6. During port test I am facing issue related SPI message read.

1 - Add breakpoint in mfrc522_getfwversion() function, - I did minor change in code, not necessary: file: mfrc522.c line 1160

image

2 - Run the code until it hits the breakpoint image

3 - In my case I start logic analyze and hit "next" in the gdb. Looking syslog msgs and results in logic Analyze We can see that the rfid IC answer 0xB2, but spi drive can't get this value.

Look images below: image image

On which OS does this issue occur?

[Linux]

What is the version of your OS?

Ubuntu 24

NuttX Version

master

Issue Architecture

[risc-v]

Issue Area

[Drivers]

Verification

  • [X] I have verified before submitting the report.

FelipeMdeO avatar Aug 20 '24 16:08 FelipeMdeO

@fdcavalcanti PTAL

acassis avatar Aug 20 '24 17:08 acassis

Hello @fdcavalcanti

I added a breakpoint in: esp_spi.c:759

Reading: trans.rcv_buffer and trans.send_buffer it always have same value, maybe this behavior explain how the example is working.

Steps: 1 - add breakpoint in mfrc522_getfwversion function. 2 - add brakpoint in esp_spi.c esp_spi_poll_send function

Running code and get trans.* value, you will see: image

Note: 0xee is expected value: ( 0x80 | (MFRC522_VERSION_REG & 0x7e)

FelipeMdeO avatar Aug 20 '24 18:08 FelipeMdeO

Hello @FelipeMdeO, let's figure this out.

We'll start making sure there's no hardware issues.

  • Is it an Espressif dev-kit or a custom board?
  • Which pins are you using with SPI?
  • Any pull-ups on the circuit?
  • ~~Which SPI mode are you working with?~~ just noticed it is already inside the lock function.

fdcavalcanti avatar Aug 20 '24 18:08 fdcavalcanti

Hello @fdcavalcanti

1 - I am using esp32c6 dev kit m and rfid-rc522 rf card read: image

2 - SPI2 CS - 15 SPI2 CLK - 6 SPI2 MOSI - 7 SPI2 MISO - 2

3 - No, direct connection image

I don't know if this the right way to share, but I did changes in fork from nuttx master: https://github.com/FelipeMdeO/nuttx.git branch: esp32-mfrc522-support

Below, my defconfig:

defconfig.zip

FelipeMdeO avatar Aug 20 '24 18:08 FelipeMdeO

I see no issue on C6 driver side. Please use NuttX main branch and try again. We need to be on common ground regarding the source code. Let me know if anything different happens.

fdcavalcanti avatar Aug 21 '24 12:08 fdcavalcanti

Hello @fdcavalcanti,

How do you test in your side? I will try do the same thing on my side.

FelipeMdeO avatar Aug 21 '24 12:08 FelipeMdeO

Hello @fdcavalcanti.

I tested using spi example and known slave device, in this situation it worked properly. So the issue is not in the drive. I will investigate it better, sorry for false bug.

FelipeMdeO avatar Aug 21 '24 13:08 FelipeMdeO

No problem! Good luck.

fdcavalcanti avatar Aug 21 '24 13:08 fdcavalcanti

Hello @fdcavalcanti ,

I am reopen this issue, I will explain.

My issue happens when I tried use MFRC522 driver, this driver uses SPI_SEND function: image

Using SPI_SEND function I cannot get read data properly.

The ESP SPI example uses SPI_EXCHANGE function. When we request once a byte, it send using exchange with nwords=1.

To do a proof, I changed the spi driver to esp_spi_send call esp_spi_exchange instead of esp_spi_poll_send

image

In this way I was able to read MFRF522 FW Version.

@fdcavalcanti can you check SPI_SEND in your side, please ?

FelipeMdeO avatar Aug 23 '24 12:08 FelipeMdeO

Hello,

I was clear in my last explanation, do you need more information?

FelipeMdeO avatar Aug 27 '24 11:08 FelipeMdeO

Hi Felipe. I don't have this hardware to test. But could you please try this inside esp_spi_poll_send:

------------------ arch/risc-v/src/common/espressif/esp_spi.c ------------------
index e84b22b336..4988f17aa3 100644
@@ -750,8 +750,9 @@ static uint32_t esp_spi_poll_send(struct esp_spi_priv_s *priv, uint32_t wd)
   trans.line_mode.cmd_lines = 0;
   priv->ctx->trans_config = trans;
 
-  spi_ll_set_mosi_bitlen(priv->ctx->hw, priv->nbits);
+  // spi_ll_set_mosi_bitlen(priv->ctx->hw, priv->nbits);
   spi_hal_prepare_data(priv->ctx, priv->dev_cfg, &trans);
+  spi_hal_setup_trans(priv->ctx, priv->dev_cfg, &trans);
   spi_hal_user_start(priv->ctx);

Make sure the rest of the file is the same as the main branch.

fdcavalcanti avatar Aug 27 '24 17:08 fdcavalcanti

Hello @fdcavalcanti,

Sorry, not yet. I put side by side comparation between SPI Exchange and SPI Send result. Note that the SPI Send return strange data, like memory address, this happens with current master code too. Maybe you can try use any spi slave device, if it uses SPI Send in the driver.

image

FelipeMdeO avatar Aug 27 '24 18:08 FelipeMdeO

Hi Felipe. I don't have this hardware to test. But could you please try this inside esp_spi_poll_send:

------------------ arch/risc-v/src/common/espressif/esp_spi.c ------------------
index e84b22b336..4988f17aa3 100644
@@ -750,8 +750,9 @@ static uint32_t esp_spi_poll_send(struct esp_spi_priv_s *priv, uint32_t wd)
   trans.line_mode.cmd_lines = 0;
   priv->ctx->trans_config = trans;
 
-  spi_ll_set_mosi_bitlen(priv->ctx->hw, priv->nbits);
+  // spi_ll_set_mosi_bitlen(priv->ctx->hw, priv->nbits);
   spi_hal_prepare_data(priv->ctx, priv->dev_cfg, &trans);
+  spi_hal_setup_trans(priv->ctx, priv->dev_cfg, &trans);
   spi_hal_user_start(priv->ctx);

Make sure the rest of the file is the same as the main branch.

@FelipeMdeO I think you can use the spi tool to so this test. Just do the ordinary loop test, but using send instead of exchange command.

acassis avatar Aug 27 '24 19:08 acassis

Thank you for you suggestion @acassis, very nice.

I did changes in spi_transfer.c to use SPI_SEND instead SPI_EXCHANGE in the spi tool. Transfer is ok, so I don't know whats happen when I am try send/read data to MFRC device.

I will take a look better during next days.

image image

FelipeMdeO avatar Aug 28 '24 13:08 FelipeMdeO

Actually the message was to Filipe Cavalcanti that doesn't have the board: @fdcavalcanti (since you don't have the board) I think you can use the spi tool to so this test. Just do the ordinary loop test, but using send instead of exchange command.

acassis avatar Aug 28 '24 15:08 acassis

Recommend checking if #13780 changed anything

fdcavalcanti avatar Oct 02 '24 17:10 fdcavalcanti

@FelipeMdeO were you able to make this work? Some weeks ago we had some changes on SPI driver and was able to replicate a similar issue to yours (solved now).

fdcavalcanti avatar Jul 23 '25 14:07 fdcavalcanti

Closing due to inactivity.

fdcavalcanti avatar Jul 25 '25 14:07 fdcavalcanti