-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SPI.beginTransaction
does not set SCK value until the first write
#9221
Comments
Here's a complete example to reproduce this: #include <Arduino.h>
#include <SPI.h>
#define DEV_SCK 0
#define DEV_MISO 8
#define DEV_MOSI 1
#define DEV_CS 5
#define SPI_SPEED 100000 // 100 KHz
void setup() {
SPI.begin(DEV_SCK, DEV_MISO, DEV_MOSI);
pinMode(DEV_CS, OUTPUT);
digitalWrite(5, HIGH);
delay(1000); // all delays are unindented for readability
SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE0));
delayMicroseconds(10);
digitalWrite(DEV_CS, LOW);
delayMicroseconds(10);
SPI.write(0xAA);
delayMicroseconds(10);
digitalWrite(DEV_CS, HIGH);
delayMicroseconds(10);
SPI.endTransaction();
delayMicroseconds(10);
SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE3));
delayMicroseconds(10);
digitalWrite(DEV_CS, LOW);
delayMicroseconds(10);
SPI.write(0xAA);
delayMicroseconds(10);
digitalWrite(DEV_CS, HIGH);
delayMicroseconds(10);
SPI.endTransaction();
}
void loop() {} |
@me-no-dev - Please take a look. Thanks! |
I am taking a look what we can do about it :) |
FYI - the workaround that we applied to https://github.com/moononournation/Arduino_GFX was to write a dummy byte after Though I think there might be a better solution (I'm not too familiar with how ESP32 handles SPI under the hood). |
FWIW, the easiest solution for this issue might be to write a dummy byte within the arduino-esp32/libraries/SD/src/sd_diskio.cpp Line 468 in aed7b4f
explicit AcquireSPI(ardu_sdcard_t* card)
: card(card)
{
card->spi->beginTransaction(SPISettings(card->frequency, MSBFIRST, SPI_MODE0));
card->spi->write(0xFF); // dummy write to force correct SCK polarity
}
AcquireSPI(ardu_sdcard_t* card, int frequency)
: card(card)
{
card->spi->beginTransaction(SPISettings(frequency, MSBFIRST, SPI_MODE0));
card->spi->write(0xFF); // dummy write to force correct SCK polarity
}
} ...however this still feels like a workaround. So at this point, I'm wondering about a more generic question: Should
|
void spiTransaction(spi_t * spi, uint32_t clockDiv, uint8_t dataMode, uint8_t bitOrder) |
But I know too little about their behavior and I'm just spitballing at this point...
Turns out my wild guess worked. I just did a quick test and added this code at the end of #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
spi->dev->cmd.update = 1;
while (spi->dev->cmd.update);
#endif ...and it fixed the clock polarity. My assumption is that setting Before fix: After fix: Is this an acceptable fix? I can submit a PR @P-R-O-C-H-Y. |
@and3rson Does the faulty behaviour affects only the C3 and S3? |
@Jason2866 Those were the only ones I have and thus was able to test... Not sure about other models. Additionally, |
So there should be more tests done, if the other MCUs does have this issue too. |
I will test all chips tomorrow. Also this PR may also help with troubles I had adding multiple CS pins support. |
Board
ESP32-C3
Device Description
No hardware
Hardware Configuration
SCK = 0
MISO = 8
MOSI = 1
Version
latest master (checkout manually)
IDE Name
n/a
Operating System
n/a
Flash frequency
40MHz
PSRAM enabled
no
Upload speed
n/a
Description
SPI clock polarity is not affected by beginTransaction until the first write. This leads to slave device ignoring some SPI packets.
This has affected my TFT display, since clock polarity was not being changed before activating the display until the first command was sent.
The only solution was to write a dummy byte directly after
beginTransaction
.Related issue: moononournation/Arduino_GFX#433
I'm not sure if this is the expected behavior, but I would certainly expect
beginTransaction
to immediately update SCK polarity that's appropriate for the selected SPI mode.Sketch
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: