Skip to content
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_BEGIN_TRANSACTION is slowing down transmissions on SAMD51 #400

Open
fvzeppelin opened this issue Sep 6, 2022 · 1 comment
Open

SPI_BEGIN_TRANSACTION is slowing down transmissions on SAMD51 #400

fvzeppelin opened this issue Sep 6, 2022 · 1 comment

Comments

@fvzeppelin
Copy link

fvzeppelin commented Sep 6, 2022

  • Arduino board: Grand Central M4 Express

  • Arduino IDE version (found in Arduino -> About Arduino menu): 1.8.19

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too):

  1. Setup SPITFT with 24MHz SCK (which is hard to set up, s. https://github.com/arduino/ArduinoCore-samd/issues/680. In my case, I used an SSD1331 RGB OLED.
  2. Using the current, unmodified GFX lib 1.11.3, I measure a time between consecutive 8-bit transfers of ~6us.
  3. Modification in Adafruit_SPITFT.cpp: comment out line 2068:
inline void Adafruit_SPITFT::SPI_BEGIN_TRANSACTION(void) {
if (connection == TFT_HARD_SPI) {
#if defined(SPI_HAS_TRANSACTION)
//    hwspi._spi->beginTransaction(hwspi.settings);
#else // No transactions, configure SPI manually...
#if defined(__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1)
    hwspi._spi->setClockDivider(SPI_CLOCK_DIV2);
#elif defined(__arm__)
    hwspi._spi->setClockDivider(11);
#elif defined(ESP8266) || defined(ESP32)
    hwspi._spi->setFrequency(hwspi._freq);
#elif defined(RASPI) || defined(ARDUINO_ARCH_STM32F1)
    hwspi._spi->setClock(hwspi._freq);
#endif
    hwspi._spi->setBitOrder(MSBFIRST);
    hwspi._spi->setDataMode(hwspi._mode);
#endif // end !SPI_HAS_TRANSACTION
  }
}

This modification will reduce the above mentioned delay between consecutive 8-bit transfers to ~0.8us(!).

As to my understanding of the library's design I think that this behaviour is unintended and, additonally, it is really painful because it makes a huge difference in screen build-up: It's the difference between slow top-to-down build-up and an instant one.

P.S. When I had not yet tracked down things to this point, I started a question here: https://forums.adafruit.com/viewtopic.php?t=194182. Nevertheless, I think this issue is appropriate, here.

@fvzeppelin
Copy link
Author

fvzeppelin commented Sep 9, 2022

Here is the code I used now to reproduce the issue on an ItsyBitsy M4. Just provide rgb data in splash.h and you will see the difference with the line commented out as described above while PCHCTRL and BAUD are configured identically in both cases:

#define OLED_CS_PIN  A5
#define OLED_DC_PIN  A4
#define OLED_RST_PIN A3

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
Adafruit_SSD1331 *oled;
#include "splash.h"
uint16_t OLED_Background_Color   = 0x0000;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  SPI.begin();
  oled = new Adafruit_SSD1331(&SPI, OLED_CS_PIN, OLED_DC_PIN, OLED_RST_PIN);
  oled->begin(24000000);
  sercom1.setBaudrateSPI(0);
  oled->fillScreen(OLED_Background_Color);
  oled->drawRGBBitmap(0, 0, (const uint16_t *)image_data_splash_rgb, 96, 64);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("PCHCTRL: ");
  Serial.println(GCLK->PCHCTRL[SERCOM1_GCLK_ID_CORE].reg);
  Serial.print("BAUDREG: ");
  Serial.println(SERCOM1->SPI.BAUD.reg);
  delay(1000);
}

Actually, I do not understand this behaviour, as the mentioned line should not make a difference. Nevetheless, it does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant