Arduino.cc says SD.h works on ALL architectures, but it’s not working for me.
-
Computer: Raspberry Pi 4 with 8G Memory
-
OS: Linux raspberrypi 5.10.103-v8+ #1529 SMP PREEMPT Tue Mar 8 12:26:46 GMT 2022 aarch64 GNU/Linux - fully updated.
-
Software: Arduino IDE version 1.8.19 - Boards and Libraries fully updated.
-
Board: SparkFun Thing Plus RP2040 Board Info: BN: SparkFun ProMicro RP2040, VID: 1b4f, PID: 0026 https://www.SparkFun.com/products/17745
-
SD Card: SanDisk 32GB Formated as FAT32. And still has data on it from a 5 year old Arduino project that used SD, so I know it’s formatted correctly.
(Also tried a brand new 32GB freshly formated FAT32 Card.)
Possible Problems:
-
The board is a “SparkFun Thing Plus RP2040”, but Tools/Get Board Info returns “SparkFun ProMicro RP2040”. (which is a different SparkFun RP2040 board that does NOT have a Built-In SD Card Slot.) Is that normal?
-
SparkFun Thing Plus RP2040 is an MBED board, but ARDUINO_ARCH_MBED is undefined. i.e. The test “#if defined(ARDUINO_ARCH_MBED)” fails. Is that normal?
What I’ve Tried:
- Tried standard SD.h with no modifications.
Result: “Initialization failed!”
- From the “RP2040 Thing Plus Hookup Guide” under the Hardware Section it says:
The µSD card slot is connected to the following dedicated GPIO:
GPIO 09: DATA 3/CS
GPIO 10: DATA 2
GPIO 11: DATA 1
GPIO 12: DATA 0/CIPO (or Peripheral's SDO)
GPIO 14: CLK/SCK
GPIO 15: CMD/COPI (or Peripheral's SDI)
but doesn’t make it clear how to use that information.
- Tried modifying /home/pi/.arduino15/packages/arduino/hardware/mbed_rp2040/4.0.2/variants/RASPBERRY_PI_PICO/pins_arduino.h
as per https://github.com/khoih-prog/RP2040_SD/issues/1
Added:
#define PIN_SPI_MISO (12u)
#define PIN_SPI_MOSI (15u)
#define PIN_SPI_SCK (14u)
#define PIN_SPI_SS (9u)
Result: “Initialization failed!”
- Tried using RP2040_SD Library https://github.com/khoih-prog/RP2040_SD
Result: “Initialization failed!”
- Also tried adding this just before SD.begin (didn’t document where I got it):
SPI1.setRX(12);
SPI1.setTX(15);
SPI1.setSCK(14);
Result: “Initialization failed!”
- And tried this (didn’t document where I got it):
#if defined(ARDUINO_ARCH_MBED)
#define PIN_SD_MOSI PIN_SPI_MOSI
#define PIN_SD_MISO PIN_SPI_MISO
#define PIN_SD_SCK PIN_SPI_SCK
//#define PIN_SD_SS PIN_SPI_SS
//#define PIN_SD_SS 5
#define PIN_SD_SS 9
#endif
if (!SD.begin(4)) {
...
Result: “Initialization failed!”
// SparkFun_Thing_Plus_RP2040_Test_SD.ino
/*************************************************************************************************/
// Includes
#include <SPI.h>
#include <RP2040_SD.h>
/*************************************************************************************************/
// Defines
#if !defined(ARDUINO_ARCH_RP2040)
#error For RP2040 only
#endif
#if defined(ARDUINO_ARCH_MBED)
#define PIN_SD_MOSI 15
#define PIN_SD_MISO 12
#define PIN_SD_SCK 14
#define PIN_SD_SS 9
#else
#define PIN_SD_MOSI PIN_SPI0_MOSI
#define PIN_SD_MISO PIN_SPI0_MISO
#define PIN_SD_SCK PIN_SPI0_SCK
#define PIN_SD_SS PIN_SPI0_SS
#endif
#define _RP2040_SD_LOGLEVEL_ 4
// Tried these overriding the above #if, but commented them out when they didn't work.
//#define PIN_SPI_MOSI (15u)
//#define PIN_SPI_MISO (12u)
//#define PIN_SPI_SCK (14u)
//#define PIN_SPI_SS (9u)
//
//#define PIN_SD_MOSI 15
//#define PIN_SD_MISO 12
//#define PIN_SD_SCK 14
//#define PIN_SD_SS 9
/*************************************************************************************************/
// Global Constants
const static int iBAUD_RATE = 9600;
/*************************************************************************************************/
// Global Variables
File myFile;
/*************************************************************************************************/
void setup() {
Serial.begin(iBAUD_RATE);
while (!Serial) {
;
}
delay(1000);
#if defined(ARDUINO_ARCH_MBED)
Serial.print("Starting SD Card ReadWrite on MBED ");
#else
Serial.print("Starting SD Card ReadWrite on ");
#endif
Serial.println(BOARD_NAME);
Serial.println(RP2040_SD_VERSION);
Serial.print("Initializing SD card with SS = ");
Serial.println(PIN_SD_SS);
Serial.print("SCK = ");
Serial.println(PIN_SD_SCK);
Serial.print("MOSI = ");
Serial.println(PIN_SD_MOSI);
Serial.print("MISO = ");
Serial.println(PIN_SD_MISO);
if(!SD.begin(PIN_SD_SS)) {
Serial.println("Initialization failed!");
return;
}
Serial.println("Initialization done.");
#define fileName "newtest0.txt"
char writeData[] = "Testing writing to " fileName;
// 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(fileName, FILE_WRITE);
// if the file opened okay, write to it:
if(myFile) {
Serial.print("Writing to ");
Serial.print(fileName);
Serial.print(" ==> ");
Serial.println(writeData);
myFile.println(writeData);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.print("Error opening ");
Serial.println(fileName);
}
// re-open the file for reading:
myFile = SD.open(fileName, FILE_READ);
if (myFile) {
Serial.print("Reading from ");
Serial.println(fileName);
Serial.println("===============");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
Serial.println("===============");
} else {
// if the file didn't open, print an error:
Serial.print("Error opening ");
Serial.println(fileName);
}
}
/*************************************************************************************************/
void loop() {
// nothing happens after setup
}
/*************************************************************************************************/
The above code returns:
Starting SD Card ReadWrite on SPARKFUN_THINGPLUS_RP2040
RP2040_SD v1.0.1
Initializing SD card with SS = 31
SCK = 2
MOSI = 3
MISO = 4
Initialization failed!
Note it did NOT say “Starting SD Card ReadWrite on MBED SPARKFUN_THINGPLUS_RP2040”
Can someone please post a simple SD or RP2040_SD (or other SD Card library) example that works on a SparkFun Thing Plus RP2040?