Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: A microSD puzzle

  1. #1

    A microSD puzzle

    My board has four SPI ports with common mosi, miso, and sck pins. CS pins are 9, 10, 15, and 24. I can use either the builtin microSD reader or an external reader connected to one of the SPI ports.

    The code to initialize the SD card:
    Code:
    // SPI
    #define PIN_SPI_CS_10 10  
    #define PIN_SPI_CS_9 9
    #define PIN_SPI_CS_15 15
    #define PIN_SPI_CS_24 24
    	
    	
    	// Enable SD card reader
    	if (SD.begin(BUILTIN_SDCARD)) // Builtin microSD card
    	{
    		microSD_Id = 1;
    		Serial.println("microSD-1");
    	}
    	else if (SD.begin(PIN_SPI_CS_24)) // SPI microSD CS on pin 24
    	{
    		microSD_Id = 24;
    		Serial.println("microSD-24");
    	}
    	else if (SD.begin(PIN_SPI_CS_15)) // SPI microSD CS on pin 15
    	{
    		microSD_Id = 15;
    		Serial.println("microSD-15");
    	}
    
    	else if (SD.begin(PIN_SPI_CS_9)) // SPI microSD CS on pin 9
    	{
    		microSD_Id = 9;
    		Serial.println("microSD-9");
    	}
    	else if (SD.begin(PIN_SPI_CS_10)) // SPI microSD CS on pin 10
    	{
    		microSD_Id = 10;
    		Serial.println("microSD-10");
    	}
    
    	else // No card
    	{
    		microSD_Id = 0;
    	}
    
    	Serial.print("microSD_Id: ");
    	Serial.println(microSD_Id);
    The puzzle is when the reader is plugged into the CS port 15 or 24, the code reports microSD_Id as the first SPI port in the if block. Rearranging the block will change the reported id. It correctly identifies ports 9 and 10 no matter which order they appear in the if block. The microSD card is read correctly no matter which id has been reported. So my questions are, what's going on here and what will happen if more than one port is connected to peripheral devices?

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,108
    With that many devices if they are not properly tri-stated and using pullups there could easily be bus confusion.

    Refer to this page for more details :: pjrc.com/better-spi-bus-design-in-3-steps/ to see if it offers any help

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,740
    Again really hard to know how you have the hardware setup. That is you have those 4 CS pins, Do you have them with external Pull Up resistors?

    If not you need to either do that and/or you may need to make sure that only one of these have a potentially LOW value...
    Something like:
    Code:
    // SPI
    #define PIN_SPI_CS_10 10  
    #define PIN_SPI_CS_9 9
    #define PIN_SPI_CS_15 15
    #define PIN_SPI_CS_24 24
    	
    
            // Make sure all CS pins are HIGH
    	pinMode(PIN_SPI_CS_10, OUTPUT); digitalWrite(PIN_SPI_CS_10, HIGH);
    	pinMode(PIN_SPI_CS_9, OUTPUT); digitalWrite(PIN_SPI_CS_9, HIGH);
    	pinMode(PIN_SPI_CS_15, OUTPUT); digitalWrite(PIN_SPI_CS_15, HIGH);
    	pinMode(PIN_SPI_CS_24, OUTPUT); digitalWrite(PIN_SPI_CS_24, HIGH);
    
    	// Enable SD card reader
    	if (SD.begin(BUILTIN_SDCARD)) // Builtin microSD card
    	{
    		microSD_Id = 1;
    		Serial.println("microSD-1");
    	}
    	else if (SD.begin(PIN_SPI_CS_24)) // SPI microSD CS on pin 24
    	{
    		microSD_Id = 24;
    		Serial.println("microSD-24");
    	} 
    	else 
    	{
    		digitalWrite(PIN_SPI_CS_24, HIGH); // make sure it is not setup to be Active...
    		if (SD.begin(PIN_SPI_CS_15)) // SPI microSD CS on pin 15
    		{
    			microSD_Id = 15;
    			Serial.println("microSD-15");
    		}
    
    		else
    		{
    			digitalWrite(PIN_SPI_CS_15, HIGH); // make sure it is not setup to be Active...
    			 if (SD.begin(PIN_SPI_CS_9)) // SPI microSD CS on pin 9
    			{
    				microSD_Id = 9;
    				Serial.println("microSD-9");
    			}
    ...
    Again the idea is at most you only want one of these 4 pins to be LOW, and if they are floating, it could easily be random values on what pin might it think it is active on...

  4. #4
    Thanks, adding the internal pullups fixed it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •