ricardgb opened a new issue, #22464:
URL: https://github.com/apache/nuttx/issues/22464

   **Description**
   
   Two defects of `arch/xtensa/src/esp32s3/esp32s3_qspi.c`, found when a board 
started to use the
   QSPI bus (SPI2, `CONFIG_ESP32S3_SPI_IO_QIO`) with DMA and SPI mode 3.
   
   **1. No link with `CONFIG_ESP32S3_SPI_DMA`**
   
   `esp32s3_qspi_alloc()` uses `ALIGN_UP()`, but the file does not include 
`nuttx/nuttx.h`, which
   defines it. The compiler only warns:
   
   ```
   chip/esp32s3_qspi.c:1117:21: warning: implicit declaration of function 
'ALIGN_UP'; did you mean 'XALIGN_UP'? [-Wimplicit-function-declaration]
   ```
   
   and the link fails as soon as something uses the bus:
   
   ```
   xtensa-esp32s3-elf-ld: 
staging/libarch.a(esp32s3_qspi.o):(.literal.esp32s3_qspi_alloc+0x0): undefined 
reference to `ALIGN_UP'
   esp32s3_qspi.c:(.text.esp32s3_qspi_alloc+0x9): undefined reference to 
`ALIGN_UP'
   ```
   
   Reproduce (master `a63275cdce8`):
   
   ```sh
   ./tools/configure.sh esp32s3-devkit:eth_lan9250
   kconfig-tweak --disable LAN9250_SPI --enable LAN9250_SQI \
                 --disable ESP32S3_SPI_IO_SPI --enable ESP32S3_SPI_IO_QIO \
                 --enable ESP32S3_SPI_DMA
   make olddefconfig && make
   ```
   
   A configuration that selects the driver but does not call 
`esp32s3_qspibus_initialize()` (for
   example `esp32s3-devkit:nsh` with the same three SPI options) links, because 
the object is not
   taken from the library; only the warning shows.
   
   **2. `esp32s3_qspi_setmode()` clears the wrong bits**
   
   ```c
   regval  = getreg32(SPI_MISC_REG(id));
   regval &= SPI_CK_IDLE_EDGE_M;
   regval |= ck_idle_edge << SPI_CK_IDLE_EDGE_S;
   putreg32(regval, SPI_MISC_REG(id));
   
   regval  = getreg32(SPI_USER_REG(id));
   regval &= SPI_CK_OUT_EDGE_M;
   regval |= ck_out_edge << SPI_CK_OUT_EDGE_S;
   putreg32(regval, SPI_USER_REG(id));
   ```
   
   `regval &= MASK` keeps only the bit that is about to be set and clears the 
rest of the register;
   `regval &= ~MASK` was meant (`esp32s3_spi.c` does it with clear and set of 
the bit). So:
   
   * the bits that `esp32s3_qspi_init()` wrote are lost: `SPI_CS1_DIS` and 
`SPI_CS2_DIS` in
     `SPI_MISC_REG`, `SPI_USR_MOSI` and `SPI_CS_HOLD` in `SPI_USER_REG`;
   * the mode bits can be set but not cleared: after mode 3, mode 0 leaves the 
clock idle high.
   
   Mode 0 is the state after the initialization and setting it again returns 
early, so only a user
   of another mode sees this.
   
   Registers of SPI2 on an ESP32-S3 (rev v0.2), read after each call:
   
   | After | `SPI_MISC_REG` master | `SPI_USER_REG` master | `SPI_MISC_REG` 
fixed | `SPI_USER_REG` fixed |
   |---|---|---|---|---|
   | `esp32s3_qspibus_initialize()` | `00000006` | `08000040` | `00000006` | 
`08000040` |
   | `QSPI_SETMODE(MODE3)` | `20000000` | `00000000` | `20000006` | `08000040` |
   | `QSPI_SETMODE(MODE0)` | `20000000` | `00000000` | `00000006` | `08000040` |
   
   **Proposed fix**
   
   PR to follow: include `nuttx/nuttx.h`, and clear only the bit that is 
changed.
   
   *Disclosure: this report and the patch were prepared with the help of an AI 
agent (Claude Code,
   Anthropic); the failures and the fixes were reproduced on real hardware and 
human-reviewed.*
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to