Using 2 SPI Devices on the same pin. Eink display + SD Card

I tried searching for using 2 SPI devices on the same pin, but could not find an answer for my current situation.

I am trying to use an E-ink display( Waveshare 10.3 inch with IT-8951 driver) that loads images using the SD Card Module.

I am currently using the Wemos D1 R32 which only has one set of SPI pins. So I have connected both the devices to the same set of SPI pins.

Both initialize and work perfectly without the other set of pins. I am not able to properly initialize both the devices at the same time.

Even if they initialize occasionally, The E-ink display behaves differently displaying lighter/darker images than the normal and eventually goes dark.

#include <SD.h>
#include <SPI.h>
const int chipSelect = 16;
uint8_t six[6000];
int i=0;

void setup(void)
{

Serial.begin(115200);

         //SD Card Code begin
  Serial.print("Initializing SD Card....");
  //SD.begin(chipSelect);
  if(!SD.begin(16))
  {
    Serial.print("Card failed, or not Present");
  // return;
  }
  Serial.println("Card Initialized");
  File dataFile = SD.open("/data.txt");
  
  if(dataFile)
  {
    Serial.println("six.txt");

 while(dataFile.available())
    { 
     six[i] = dataFile.parseInt();
     i++;
    }
    for(int i=0;i<10;i++)
  {
    Serial.println(six[i]);
    delay(3000);
  }
    dataFile.close();    
  }
  else
  {
    Serial.println("File does not exist or named wrong");
  }
 //SD Card Code end    
    Serial.println("\n\n\n");

    Serial.printf("MISO %i\n", MISO);
    Serial.printf("MOSI %i\n", MOSI);
    Serial.printf("SCK %i\n", SCK);
    Serial.printf("CS %i\n", CS);
    Serial.printf("RESET %i\n", RESET);
    Serial.printf("HRDY %i\n", HRDY);

    Serial.println("Display init ...");
    if(!display_begin()) {
        Serial.println("Display init failed");
        exit(1);
    };
    Serial.println("Display init ok");
       IT8951DisplayArea(0, 0, 1200, 825, 0);
}













//extern unsigned char six[];

//extern unsigned char sixone[];

//extern unsigned char sixthree[];

//extern unsigned char sixfour[];

unsigned int pic_width = 600;
unsigned int pic_height = 412;

void loop() {
    int x = random(0, gstI80DevInfo.usPanelW - pic_width);
    int y = random(0, gstI80DevInfo.usPanelH - pic_height);
    x -= x%4; // somehow the image is distorted when not aligned like this

    display_buffer(six, 300, 300, pic_width, pic_height);
}

While initiating the E-ink, the Serial monitor reads this,

  1. Proper Initialization
Panel(W,H) = (1200,825)

Image Buffer Address = 11A1E0

FW Version = SWv_0.2.1T

LUT Version = M841


  1. Not initialized When using both the SD at the same time.
W: 65535 H: 65535

Image Buffer Address = FFFFFFFF

FW Version = ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮(a@?

LUT Version = ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮(a@?

Hi @vikasrreddy

You need both device's CS pins connected and configured. The CS (Chip Select) pin on each component controls if it is using SPI or if something else is using SPI.

@vikasrreddy, as I can't see how your question relates to Interfacing w/ Software on the Computer, your topic has been moved to a more suitable location on the forum.

Hey @kgray9. Thank you for helping out.

I have defined both the CS pins. The CS for the SD Card is defined as chipSelect = 16 in this code and the CS for E-ink is defined in the library of the IT-8951.

They both initialize without any problem when used individually for the same code . But, when they are tried together, they are not working as they should.

I doubt that.

You are connecting the 2 CS pins together? Why?

Please post complete code. It only wastes forum member's time if you post only part of it.

Hey, @PaulRB
Thank you for taking time to help me out.

I am using the library for IT8951 by clashman ( GitHub - clashman/it8951: Quick hack to make the IT8951 display driver board work with Arduino and ESP32 boards. Including example code. ) and modified the main program to include SD Card.

Here is the initial part of IT8951 library where they define all the SPI Pins.

#include <SPI.h>

#define MISO 19
#define MOSI 23
#define SCK 18
#define CS 5

#define RESET 14
#define HRDY 27

#define bcm2835_gpio_write digitalWrite
#define bcm2835_spi_transfer SPI.transfer
#define bcm2835_gpio_lev digitalRead

//Built in I80 Command Code
#define IT8951_TCON_SYS_RUN      0x0001
#define IT8951_TCON_STANDBY      0x0002
#define IT8951_TCON_SLEEP        0x0003
#define IT8951_TCON_REG_RD       0x0010
#define IT8951_TCON_REG_WR       0x0011
#define IT8951_TCON_MEM_BST_RD_T 0x0012
#define IT8951_TCON_MEM_BST_RD_S 0x0013
#define IT8951_TCON_MEM_BST_WR   0x0014
#define IT8951_TCON_MEM_BST_END  0x0015
#define IT8951_TCON_LD_IMG       0x0020
#define IT8951_TCON_LD_IMG_AREA  0x0021
#define IT8951_TCON_LD_IMG_END   0x0022

//I80 User defined command code
#define USDEF_I80_CMD_DPY_AREA     0x0034
#define USDEF_I80_CMD_GET_DEV_INFO 0x0302
#define USDEF_I80_CMD_DPY_BUF_AREA 0x0037
//Panel
#define IT8951_PANEL_WIDTH   1024 //it Get Device information
#define IT8951_PANEL_HEIGHT   758

Oh, you're correct. That is very bad library design! The whole point of a library is that it is re-useable without having to change it's files for each application.

EDIT: ah, after a closer look at that "library", its not truly a library in the Arduino sense, it is intended to be used as example code and adapted to each application.

When you say "using" do you mean "connecting"? In other words, does the SD card work when the display is connected & powered, and the display work when the SD card is connected and powered, but if you upload a sketch that tries to initialise both, only then do they fail? Or do you mean that to get the SD card to work you have to physically disconnect the display, or physically disconnect the SD card to get the display to work?

Please post a link to your display and your SD card module. I know that some SD card modules sold for use with Arduino are not well designed and will not in fact share the SPI bus with other devices.

Hey @PaulRB. Thanks again for your time.

Blockquote In other words, does the SD card work when the display is connected & powered, and the display work when the SD card is connected and powered, but if you upload a sketch that tries to initialise both, only then do they fail? Or do you mean that to get the SD card to work you have to physically disconnect the display, or physically disconnect the SD card to get the display to work?

After I removed the <SPI.h> from the Main() program, The code is running, SD card works perfectly now. But the display is not initializing. I get the same Message in the Serial Port as in the post number 1.

But I am able to clear the e-ink screen if I manually enter the pixel size in the function.

I am guessing that it is not able to display an image because the function for displaying the image is dependent on one of those parameters( like Image Buffer Address or FW Version or LUT Version) that it initially displays on the Serial Monitor.

I think perhaps some parameters of the SPI interface used by the SD card are not compatible with the display and vice-versa. There are functions within the SPI library to help with this situation: SPI.beginTransaction() and SPI.endTransaction() which allow for these different parameters to be adjusted for each device on the bus. How these can be used with the SD library and the display code, I do not know.

Thank you @PaulRB .

I tried a lot of things and then stumbled upon this. Arduino - hacking a micro SD card reader data logger to connect with another SPI device nRF24L01 - YouTube where he drills a hole to disconnect MISO and connects the MISO directly

It somewhat worked, but it is still giving wrong values during initialization and display still doesn't work.

So, I am going to use an ESP32 and have them separately on VSPI and HSPI. Just have to figure out how to alter the code for defining the pins.