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

Off-by-one bug in readRect #29

Open
pepijndevos opened this issue Jul 21, 2016 · 0 comments · May be fixed by #40
Open

Off-by-one bug in readRect #29

pepijndevos opened this issue Jul 21, 2016 · 0 comments · May be fixed by #40

Comments

@pepijndevos
Copy link

I found that readRect will never read the last pixel.

The below sketch reproduces the problem using the Teensy 3.1, the Adafruit shield and the latest stable Teensyduino.

I fixed it by just incrementing c in readRect, but that's probably not the correct solution.
It seems on the last byte you clear the SR register, which makes the check if there are 3 bytes in the FIFO fail, but not knowing the meaning of all the SPI registers I was unable to quickly see the correct solution.

#include <ILI9341_t3.h> // Hardware-specific library
#include <SPI.h>

#define TFT_DC  9
#define TFT_CS 10
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);

void setup(void) {

  tft.begin();
  tft.fillScreen(ILI9341_BLUE);

  Serial.begin(9600);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.println(F("Waiting for Arduino Serial Monitor..."));
  while (!Serial) {
    if (millis() > 8000) break;
  }

  tft.fillScreen(ILI9341_BLUE);


  uint16_t buf[32*32];
  tft.readRect(0, 0, 32, 32, buf);

  for(int i=0; i<32*32;i++) {
    if(buf[i] != ILI9341_BLUE) {
      Serial.print("Error ");
      Serial.print(i);
      Serial.print(" ");
      Serial.print(buf[i], HEX);
    }
  }

}

void loop() {
}
mudaltsov added a commit to mudaltsov/ILI9341_t3 that referenced this issue Dec 2, 2016
This fixes PaulStoffregen#29

The implementation of readRect had an issue where it would not correctly read
the last pixel from the screen. This was caused by an overflow in the RX FIFO
while waiting for the EOQ flag in the last transmitted byte. The loop only
counted transmitted bytes, so it would fill up the TX FIFO and not wait until
the transfer actually happened. Waiting for EOQ competed the queued transfers
and overflowed the RX FIFO since nothing was reading from it.

The new implementation explicitly tracks the number of RX and TX bytes to
ensure that all bytes are correctly transmitted and received. A few ideas
have been borrowed from KurtE/ILI9341_t3n as well:
* Each byte is received independently and stored in a 3-byte array, which is
  converted and written to the buffer once all 3 colors have been received.
* The TX FIFO is kept full throughout the function to avoid the delay between
  the dummy byte and the first pixel byte. Instead of using EOQ for the dummy
  byte, the results from queued commands and the dummy byte are skipped.
@mudaltsov mudaltsov linked a pull request Dec 2, 2016 that will close this issue
mudaltsov added a commit to mudaltsov/ILI9341_t3n that referenced this issue Dec 3, 2016
(Ported from ILI9341_t3, with appropriate changes to use SPIN)

This fixes PaulStoffregen/ILI9341_t3#29

The implementation of readRect had an issue where it would not correctly read
the last pixel from the screen. This was caused by an overflow in the RX FIFO
while waiting for the EOQ flag in the last transmitted byte. The loop only
counted transmitted bytes, so it would fill up the TX FIFO and not wait until
the transfer actually happened. Waiting for EOQ competed the queued transfers
and overflowed the RX FIFO since nothing was reading from it.

The new implementation explicitly tracks the number of RX and TX bytes to
ensure that all bytes are correctly transmitted and received. A few ideas
have been borrowed from KurtE/ILI9341_t3n as well:
* Each byte is received independently and stored in a 3-byte array, which is
  converted and written to the buffer once all 3 colors have been received.
* The TX FIFO is kept full throughout the function to avoid the delay between
  the dummy byte and the first pixel byte. Instead of using EOQ for the dummy
  byte, the results from queued commands and the dummy byte are skipped.
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

Successfully merging a pull request may close this issue.

1 participant