A microSD puzzle

Status
Not open for further replies.

Elf

Well-known member
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?
 
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...
 
Status
Not open for further replies.
Back
Top