ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
stecoop
 
Posts: 18
Joined: Sat Jul 06, 2019 4:16 pm

ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by stecoop »

I'm using an ItsyBitsy M4 Express to transfer data from an SD card to an SPIFlash Memory.
I've used both breakout boards individually with the ItsyBitsy without any problems, but when I try to use them together, the SD card won't be recognized.

The setup with just the SD Card reader is below:
Image
This works just fine, the SD Card is recognized, but of course, there's no flash memory to transfer to.

The setup with both the SD Card reader and the SPIFlash Memory is below:
Image
This fails miserably, the SD Card is not recognized.

The wiring is the same in both setups, the only difference is that the SPIFlash board is not plugged in in the first set-up.

The test code is based on the ReadWrite sample program:

Code: Select all

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include "SdFat.h"
SdFat32 SD;

#define SD_CS_PIN 9
File myFile;

void setup()
{
  // Open serial communications and wait for port to open:
  pinMode(2,OUTPUT);
  digitalWrite(2,HIGH);
  pinMode(9,OUTPUT);
  digitalWrite(9,HIGH);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("Testing....testing....");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  SD.remove("test.txt");
}

void loop()
{
  // nothing happens after setup
}
NOTE, at this point, the test code doesn't do anything with the SPIFlash Memory, I'm still trying to get the SD Card to be recognized while the flash memory is connected. The chip-select pins are both set HIGH at the very start of the program, but that didn't help.

All three boards are from Adafruit. I could sure use some help getting these to work together.
Attachments
With SPI Flash.jpg
With SPI Flash.jpg (136.5 KiB) Viewed 272 times
Without SPI Flash.jpg
Without SPI Flash.jpg (142.04 KiB) Viewed 272 times

User avatar
stecoop
 
Posts: 18
Joined: Sat Jul 06, 2019 4:16 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by stecoop »

ADDENDUM:

I just finished trying this setup on both an UNO and a MEGA 2560 - it works perfectly with those boards.

The final project will use the ItsyBitsy M4 Express, because of the 192k of memory and high clock speed; the data transfer only has to be performed once in preparation for the final project and it doesn't really matter which board I use for that, though with 19 megabytes of data to transfer from an SD Card to an SPIFlash memory, the ItsyBitsy would have been the faster choice.

I'm still curious why this didn't work with the ItsyBitsy, or if there was something I could have done differently to make it work.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by adafruit_support_mike »

Try lowering the clock rate for the M4's SPI interface.

The photos above show that you're using loose wiring in a breadboard, which means you have a lot of parasitic capaitance and inductance. If the boards work individually with the M4, and the combination works with slower microcontrollers, bus speed is a strong candidate for the source behind any problems.

User avatar
stecoop
 
Posts: 18
Joined: Sat Jul 06, 2019 4:16 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by stecoop »

adafruit_support_mike wrote: Fri Apr 28, 2023 5:03 am Try lowering the clock rate for the M4's SPI interface.

The photos above show that you're using loose wiring in a breadboard, which means you have a lot of parasitic capaitance and inductance. If the boards work individually with the M4, and the combination works with slower microcontrollers, bus speed is a strong candidate for the source behind any problems.
Thank you Mike, for your response.

I did try lowering the SPI speed, (not through the IDE, but through the SPI initialization settings), and even reduced the SD initialization clock to as low as 20 KHZ, through several steps; no success. I'll try doing it through the IDE and see what happens.

If I use the Adafruit SdFat-Fork library, the SD card will not work at all if there is anything else on the SPI bus. If I use Arduino's SD library, the SD card will work but I usually have to press the reset button 1 or two times to get it going. This is especially true with the RA8875 LCD driver board (and, yes, I buffer the MISO pin from the RA8875).

I've suspected there is a timing issue.

I've decided to refocus this project on to the Feather Grand Central M4 Express, because it has a built in SD card reader on a separate SPI bus. I'm waiting delivery of the Grand Central and will be trying it out in a couple of weeks.

As for the SPIFlash memory, I've abandoned it altogether. Although I did eventually get the pixel data transfered from the SD card to the SPIFlash, when I sent the data from the SPIFlash to the LCD display, the colors were "off":
Yellow was almost white, red was beige, blue was cyan, green was almost yellow; but black was still black.
I spent days analyzing the data, and swapping bytes and bits around, to see what was going on but could not find the problem. The same pixel data, transfered from the SD card to the LCD display comes out correctly. I wanted to use the SPIFlash for data storage for the added speed (and, boy, is it fast!) but if I can't get the colors right, I'll stay with the slower speed of the SD card.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by adafruit_support_mike »

The incorrect colors sound like you have the wrong color order in the NeoPixel declaration, or when you do the color encoding.

How are you encoding the RGB data when you write it to the SD card and the SPIFlash?

User avatar
stecoop
 
Posts: 18
Joined: Sat Jul 06, 2019 4:16 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by stecoop »

adafruit_support_mike wrote: Fri Apr 28, 2023 10:23 pm The incorrect colors sound like you have the wrong color order in the NeoPixel declaration, or when you do the color encoding.

How are you encoding the RGB data when you write it to the SD card and the SPIFlash?
This has nothing to do with NeoPixels. This is putting pixel data, RGB565, on the LCD display driven by Adafruit's RA8875 breakout board. When read from the SD card the colors are correct; when read from the SPIFlash, the colors are wrong. I do a byte-for-byte transfer from from the SD card to the SPIFlash...no translating, byte swapping or bit-flipping. The color errors are as if there are one or two bits getting turned on during the transfer, bit(s) that would normally be off. I may get around to examining individual bytes in the SPIFlash data, but right now I have a lot of other projects burdening me.

User avatar
Claraa12
 
Posts: 1
Joined: Mon May 01, 2023 2:15 am

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by Claraa12 »

stecoop wrote: Fri Apr 21, 2023 5:36 pm I'm using an ItsyBitsy M4 Express to transfer data from an SD card to an SPIFlash Memory.
I've used both breakout boards individually with the ItsyBitsy without any problems, but when I try to use them together, the SD card won't be recognized.

The setup with just the SD Card reader is below:
Image
This works just fine, the SD Card is recognized, but of course, there's no flash memory to transfer to.

The setup with both the SD Card reader and the SPIFlash Memory is below:
Image
This fails miserably, the SD Card is not recognized.

The wiring is the same in both setups, the only difference is that the SPIFlash board is not plugged in in the first set-up.

The test code is based on the ReadWrite sample program:

Code: Select all

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include "SdFat.h"
SdFat32 SD;

#define SD_CS_PIN 9
File myFile;

void setup()
{
  // Open serial communications and wait for port to open:
  pinMode(2,OUTPUT);
  digitalWrite(2,HIGH);
  pinMode(9,OUTPUT);
  digitalWrite(9,HIGH);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("Testing....testing....");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  SD.remove("test.txt");
}

void loop()
{
  // nothing happens after setup
}
NOTE, at this point, the test code doesn't do anything with the SPIFlash Memory, I'm still trying to get the SD Card to be recognized while the flash memory is connected. The chip-select pins are both set HIGH at the very start of the program, but that didn't help.

All three boards are from Adafruit. I could sure use some help getting these to work together.
Hi guys,
Thank you so much for the information.

User avatar
stecoop
 
Posts: 18
Joined: Sat Jul 06, 2019 4:16 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by stecoop »

stecoop wrote: Sat Apr 29, 2023 4:51 pm
adafruit_support_mike wrote: Fri Apr 28, 2023 10:23 pm The incorrect colors sound like you have the wrong color order in the NeoPixel declaration, or when you do the color encoding.

How are you encoding the RGB data when you write it to the SD card and the SPIFlash?
This has nothing to do with NeoPixels. This is putting pixel data, RGB565, on the LCD display driven by Adafruit's RA8875 breakout board. When read from the SD card the colors are correct; when read from the SPIFlash, the colors are wrong. I do a byte-for-byte transfer from from the SD card to the SPIFlash...no translating, byte swapping or bit-flipping. The color errors are as if there are one or two bits getting turned on during the transfer, bit(s) that would normally be off. I may get around to examining individual bytes in the SPIFlash data, but right now I have a lot of other projects burdening me.
Last night I had some time to look at the pixel data on the SPIFlash memory; I did a byte-by-byte, bit-by-bit, manual examination of the data (very tedious). The pixel data on the SPIFlash is correct; therefore, the problem lies with reading the data and sending it to the RA8875 LCD driver.

Note, the program that reads from the SD card, and the program that reads from the SPIFlash are virtually identical except for where the data is coming from; both read the data into an SRAM buffer 128,000 bytes in size, then stream the data to the LCD driver. The biggest difference is in how the data is loaded into the buffer; the SD card program fills the buffer 400 bytes at a time; the SPIFlash program fills the buffer in one big gulp of 128,000 bytes. I wouldn't think that should make a difference, but, when I get some more free time, I'll try setting up each program to fill the buffer by identical methods: first 400 bytes at a time, then the 128,000 byte big-gulp; and see if there's a difference.

Going back to the original problem of the SD card not working with other devices on the SPI bus with the ItsyBitsy M4 Express; some experimentation with a 74HC125 tri-state buffer yielded some interesting results. When I use the RA8875 LCD driver with another device on the SPI bus, I have to use the 74HC125 on the MISO pin of the RA8875, because it doesn't tri-state it's MISO pin; and this works just fine. However, if I also route the MISO pin of the SD Card through the tri-state buffer, the SD Card doesn't work at all...and this applies to the ItsyBitsy, Mega2560, and the UNO. Obviously, the problem is the propagation delay through the tri-state buffer, but another way to look at it is the software is trying to read/write the SD card too soon after asserting the chip-select pin, and the SD card is simply not ready, i.e. the SD Card is slow to respond to it's chip-select pin. With slower processors, like the UNO and MEGA, this isn't an issue, even if the SPI bus is shared. But with faster processors like the ItsyBitsy, it becomes an issue, especially if the SPI bus is shared. I have not dug into the internals of the SPI software, and I don't plan to; this is just my opinion based on my experiments and experience.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by adafruit_support_mike »

That's good information, and I'll put it on the list of things to explore.

That said, the use of HC logic might also be a factor.. it's typically limited to bus frequencies of about 25MHz at 3.3V. AHC logic is typically 2x to 3x faster than HC (up to about 80MHz), and LVC logic is at least 2x faster than AHC (up to 200MHz).

The exact frequencies depend on supply voltage, load capacitance, and temperature. It sounds like an HC device would have been working at the upper end of its range though. Try swapping in an LVC buffer and see if that helps.

User avatar
stecoop
 
Posts: 18
Joined: Sat Jul 06, 2019 4:16 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by stecoop »

adafruit_support_mike wrote: Thu May 04, 2023 2:22 am That's good information, and I'll put it on the list of things to explore.

That said, the use of HC logic might also be a factor.. it's typically limited to bus frequencies of about 25MHz at 3.3V. AHC logic is typically 2x to 3x faster than HC (up to about 80MHz), and LVC logic is at least 2x faster than AHC (up to 200MHz).

The exact frequencies depend on supply voltage, load capacitance, and temperature. It sounds like an HC device would have been working at the upper end of its range though. Try swapping in an LVC buffer and see if that helps.
I suspected I may have made a bad choice when selecting the 74HC125; I knew there were faster buffers available, but the HC device was available quickly and cheaply, and it met the speed requirements for the RA8875.

Hopefully this weekend I'll get the time to explore the buffer filling techniques I mentioned earlier; the Grand Central I ordered arrived a few days ago...I haven't even unwrapped it yet; a big project I've been tied up with (nothing to do with Arduinos) is finally winding down.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by adafruit_support_mike »

stecoop wrote: Thu May 04, 2023 10:18 am I suspected I may have made a bad choice when selecting the 74HC125; I knew there were faster buffers available, but the HC device was available quickly and cheaply, and it met the speed requirements for the RA8875.
The timing limits aren't everyday knowledge, and it sounds like your system was working right up to the edge of the HC family's abilities.

This is one of those cases where being able to scope the signals helps immensely.. the slopes and curves of slow charge/discharge timing become obvious once you have the equipment to display them.

User avatar
stecoop
 
Posts: 18
Joined: Sat Jul 06, 2019 4:16 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by stecoop »

adafruit_support_mike wrote: Fri May 05, 2023 2:57 am
stecoop wrote: Thu May 04, 2023 10:18 am I suspected I may have made a bad choice when selecting the 74HC125; I knew there were faster buffers available, but the HC device was available quickly and cheaply, and it met the speed requirements for the RA8875.
The timing limits aren't everyday knowledge, and it sounds like your system was working right up to the edge of the HC family's abilities.

This is one of those cases where being able to scope the signals helps immensely.. the slopes and curves of slow charge/discharge timing become obvious once you have the equipment to display them.
As a matter of fact, I recently purchased an oscilloscope; I found the the SPI clock frequency is just over 23 mhz.
Although the RA8875 lists a maximum clock frequency of 20 mhz., it seems to be handling the 23 mhz just fine.
I did play around with forcing lower and higher frequncies; lower was possible, usually, but it wouldn't go any higher. I don't remember the specific frequencies now, but I did observe that some lower frequencies wouldn't work, and some would; I assume it's because the software would adjust to the closest frequency the clock system could produce, or fall to the default of 20 mhz. (23 mhz.).

I'm still learning how to use this new-fangled digital recording scope; I may try to see what signals are running in and out of the SD card.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by adafruit_support_mike »

In this case, the main feature to watch is the curvature of the rising and falling edges. That's caused by signals having to charge and discharge the parasitic capacitance of the load.

If the capacitive load gets too high, the rising and falling edges curve enough to violate the setup and hold times for the signal protocol.

At the opposite end of the spectrum, ringing on fast rising and falling edges is a sign of parasitic inductance and capacitance in the wiring. The wire has a natural resonant frequency, and with no resistance to bleed off the energy, it bounces back and forth. Ringing tends to occur in the 20MHz to 50MHz range, so it's worth watching.

You can damp out ringing by adding series resistance through the wiring, at the cost of slowing the rising and falling edges down a bit. The trick is to find a resistor value that kills most of the ringing without adding too much delay. 120 Ohms is usually a good place to start.

If you have a 4-channel scope, watch MOSI, MISO, and SCK all at the same time. There's always a chance that you might see some offsets in the rising edges that can cause problems.

User avatar
stecoop
 
Posts: 18
Joined: Sat Jul 06, 2019 4:16 pm

Re: ItsyBitsy M4 Express with SD Card Reader AND SPI Flash Memory

Post by stecoop »

stecoop wrote: Mon May 01, 2023 10:57 am
Last night I had some time to look at the pixel data on the SPIFlash memory; I did a byte-by-byte, bit-by-bit, manual examination of the data (very tedious). The pixel data on the SPIFlash is correct; therefore, the problem lies with reading the data and sending it to the RA8875 LCD driver.
This evening, I revisited transfering pixel data from the SPIFlash memory to the RA8875 LCD driver; I breadboarded the set up a little diffrently this time, using wires as short as possible, avoiding paralleling data wires, and keeping the power wires as far from the data wires as possible. As a result, all the pixel data displayed correctly; and it is very fast, something I want for the final project. When I build the final project, components will be installed as close to each other as possible and wire lengths will be greatly minimized.

I want to thank Mike for his help and suggestions, which gave ideas of what to look for and try in resolving some of these problems.

Lady Ada, if your reading this, I have a "wish-list request": these LCD panels I buy from Adafruit (7" 800 x 480) are capable of displaying 24-bit colors, but the RA8875 driver will only use 16-bit color. Is there any possibility you'll come out with a driver breakout board similar to the RA8875, but able to use full 24-bit color?

Locked
Please be positive and constructive with your questions and comments.

Return to “Itsy Bitsy Boards”