Giter Club home page Giter Club logo

khoih-prog / rp2040_sd Goto Github PK

View Code? Open in Web Editor NEW
32.0 2.0 10.0 153 KB

This library enables you to use SPI SD cards with RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO using either RP2040 Arduino-mbed or arduino-pico core. This SD-Fat v2 can support FAT16, FAT32, exFAT file systems. exFAT supports files larger than 4GB by using uint64_t as file offset.

License: MIT License

C++ 80.12% C 19.77% Shell 0.12%
rp2040 nano-rp2040-connect raspberry-pi-pico arduino-pico arduino-mbed sdfat data-storage fat16 fat32 exfat spi mbed sd-card sd rpi-pico

rp2040_sd's Introduction

RP2040_SD Library

arduino-library-badge GitHub release GitHub contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Why do we need this RP2040_SD library

Features

This is an Arduino library for RP2040-based SPI SD Cards.

It's based on and modified from :

  1. Bill Greiman's SdFat library
  2. Adafruit SD library

Have a look at SdFat's README

SdFat Version 2 supports FAT16/FAT32 and exFAT SD cards. It is mostly backward compatible with SdFat Version 1 for FAT16/FAT32 cards.

exFAT supports files larger than 4GB so files sizes and positions are type uint64_t for classes that support exFAT.

exFAT has many features not available in FAT16/FAT32.  exFAT has excellent support for contiguous files on flash devices and supports preallocation.

If the SD card is the only SPI device, use dedicated SPI mode. This can greatly improve performance.

This library was created as an effort to use SD Card while the arduino-pico core still has issue SD card FILE_WRITE issue #214, which has been fixed from core v1.8.6.

It's better to use the built-in SD library for Earle Philhower's arduino-pico core v1.8.6+.

But if you need to write codes to be used in both Arduino-mbed RP2040 core and Earle Philhower's arduino-pico core without having to modify the code, you can have the option to use this library.


Currently supported Boards

  1. RP2040-based boards such as NANO_RP2040_CONNECT, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. using either Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. Arduino mbed_rp2040 core 3.4.1+ for Arduino (Use Arduino Board Manager) RP2040-based boards, such as Arduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc.. GitHub release
  3. Earle Philhower's arduino-pico core v2.6.3+ for RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. GitHub release


Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for RP2040_SD, then select / install the latest version. You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate to RP2040_SD page.
  2. Download the latest release RP2040_SD-main.zip.
  3. Extract the zip file to RP2040_SD-main directory
  4. Copy whole RP2040_SD-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install RP2040_SD library by using Library Manager. Search for RP2040_SD in Platform.io Author's Libraries
  4. Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File


Examples

  1. CardInfo
  2. DataLogger
  3. DumpFile
  4. Files
  5. ListFiles
  6. NonBlockingWrite
  7. ReadWrite


Example ReadWrite

/*
SD card connection
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:
// Arduino-pico core
** MISO - pin 16
** MOSI - pin 19
** CS - pin 17
** SCK - pin 18
// Arduino-mbed core
** MISO - pin 4
** MOSI - pin 3
** CS - pin 5
** SCK - pin 2
*/
#if !defined(ARDUINO_ARCH_RP2040)
#error For RP2040 only
#endif
#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
#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
#include <SPI.h>
#include <RP2040_SD.h>
File myFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
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
}



Debug Terminal Output Samples

1. CardInfo on MBED RaspberryPi Pico

The following is the sample terminal output when running example CardInfo on MBED RaspberryPi Pico using Arduino-mbed RP2040 core

Starting SD Card CardInfo on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
Wiring is correct and a card is present.

Card type:         SDHC
Clusters:          982896
Blocks x Cluster:  64
Total Blocks:      62905344

Volume type is:    FAT32
Volume size (Kb):  31452672
Volume size (Mb):  30715
Volume size (Gb):  30.00
Initialization done.

Files found on the card (name, date and size in bytes): 
DATALOG.TXT		144
TEST.TXT		36
NEWTEST.TXT		36
NEWTEST1.TXT		18
MYDATA~1.TXT		0
NEWTEST2.TXT		54
NEWTEST3.TXT		54
NEWTEST4.TXT		72
NEWTEST0.TXT		297
NEWDATA.TXT		18

2. DumpFile on MBED RaspberryPi Pico

The following is the sample terminal output when running example DumpFile on MBED RaspberryPi Pico using Arduino-mbed RP2040 core

Starting SD Card DumpFile on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
Initialization done.
====== Data ======
Testing RP2040 SD
====== Data ======
[SD] Closed " newdata.txt ", nfilecount =  0

3. ListFiles on MBED RaspberryPi Pico

The following is the sample terminal output when running example ListFiles on MBED RaspberryPi Pico using Arduino-mbed RP2040 core

Starting SD Card ListFiles on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
Initialization done.
DATALOG.TXT		126
TEST.TXT		36
NEWTEST.TXT		36
NEWTEST1.TXT		18
MYDATA~1.TXT		0
NEWTEST2.TXT		54
NEWTEST3.TXT		54
NEWTEST4.TXT		72
NEWTEST0.TXT		297
Print Directory done!
writeData = Testing RP2040 SD
readData = Testing RP2040 SD
DATALOG.TXT		144
TEST.TXT		36
NEWTEST.TXT		36
NEWTEST1.TXT		18
MYDATA~1.TXT		0
NEWTEST2.TXT		54
NEWTEST3.TXT		54
NEWTEST4.TXT		72
NEWTEST0.TXT		297
NEWDATA.TXT		18
Print Directory done!

4. ReadWrite on MBED RaspberryPi Pico

The following is the sample terminal output when running example ReadWrite on MBED RaspberryPi Pico using Arduino-mbed RP2040 core

Starting SD Card ReadWrite on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
initialization done.
Writing to newtest0.txt ==> Testing writing to newtest0.txt
[SD] Closed " newtest0.txt ", nfilecount =  0
done.
Reading from newtest0.txt
===============
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
[SD] Closed " newtest0.txt ", nfilecount =  0
===============

5. Files on MBED RaspberryPi Pico

The following is the sample terminal output when running example Files on MBED RaspberryPi Pico using Arduino-mbed RP2040 core

Starting SD Card Files on MBED RaspberryPi Pico
MBED RP2040_SD v1.0.1
Initializing SD card with SS = 5
SCK = 2
MOSI = 3
MISO = 4
Initialization done.
example.txt doesn't exist.
Creating example.txt
writeData = 0xDEADBEEF
[SD] Closed " example.txt ", nfilecount =  0
readData = 0xDEADBEEF
[SD] Closed " example.txt ", nfilecount =  0
example.txt exists.
Removing example.txt...
example.txt doesn't exist.


6. CardInfo on RASPBERRY_PI_PICO

The following is the sample terminal output when running example CardInfo on RASPBERRY_PI_PICO using arduino-pico core

Starting SD Card CardInfo on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
Wiring is correct and a card is present.

Card type:         SDHC
Clusters:          1965597
Blocks x Cluster:  32
Total Blocks:      62899104

Volume type is:    FAT32
Volume size (Kb):  31449552
Volume size (Mb):  30712
Volume size (Gb):  29.99
Initialization done.

Files found on the card (name, date and size in bytes): 
DATALOG.TXT		267
DEMO.TXT		360960
CANADA~1.JPG		11156
TEST.TXT		72
NEWTEST4.TXT		132
NEWTEST0.TXT		330
NEWDATA.TXT		54

7. DumpFile on RASPBERRY_PI_PICO

The following is the sample terminal output when running example DumpFile on RASPBERRY_PI_PICO using arduino-pico core

Starting SD Card DumpFile on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
Initialization done.
====== Data ======
Testing RP2040 SD
====== Data ======
[SD] Closed " newdata.txt ", nfilecount =  0

8. ListFiles on RASPBERRY_PI_PICO

The following is the sample terminal output when running example ListFiles on RASPBERRY_PI_PICO using arduino-pico core

Starting SD Card ListFiles on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
Initialization done.
DATALOG.TXT		249
DEMO.TXT		82432
CANADA~1.JPG		11156
TEST.TXT		72
NEWTEST4.TXT		132
NEWTEST0.TXT		330
NEWDATA.TXT		36
Print Directory done!
writeData = Testing RP2040 SD
readData = Testing RP2040 SD
DATALOG.TXT		267
DEMO.TXT		82432
CANADA~1.JPG		11156
TEST.TXT		72
NEWTEST4.TXT		132
NEWTEST0.TXT		330
NEWDATA.TXT		54
Print Directory done!

9. ReadWrite on RASPBERRY_PI_PICO

The following is the sample terminal output when running example ReadWrite on RASPBERRY_PI_PICO using arduino-pico core

Starting SD Card ReadWrite on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
initialization done.
Writing to newtest0.txt ==> Testing writing to newtest0.txt
[SD] Closed " newtest0.txt ", nfilecount =  0
done.
Reading from newtest0.txt
===============
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
Testing writing to newtest0.txt
[SD] Closed " newtest0.txt ", nfilecount =  0
===============

10. Files on RASPBERRY_PI_PICO

The following is the sample terminal output when running example Files on RASPBERRY_PI_PICO using arduino-pico core

Starting SD Card Files on RASPBERRY_PI_PICO
RP2040_SD v1.0.1
Initializing SD card with SS = 17
SCK = 18
MOSI = 19
MISO = 16
Initialization done.
example.txt doesn't exist.
Creating example.txt
writeData = 0xDEADBEEF
[SD] Closed " example.txt ", nfilecount =  0
readData = 0xDEADBEEF
[SD] Closed " example.txt ", nfilecount =  0
example.txt exists.
Removing example.txt...
example.txt doesn't exist.


Debug

Debug is enabled by default on Serial.

You can also change the debugging level (RP2040_SD_LOGLEVEL) from 0 to 4

// These define's must be placed at the beginning before #include "RP2040_SD.h"
// _RP2040_SD_LOGLEVEL_ from 0 to 4
#define _RP2040_SD_LOGLEVEL_       0

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.

Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.



Issues

Submit issues to: RP2040_SD issues


TO DO

  1. Search for bug and improvement.

DONE

  1. Basic SD-wrapper library for RP2040-based boards, such as NANO_RP2040_CONNECT, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, using Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.
  2. Add Version String
  3. Add Table of Contents
  4. Add astyle using allman style. Restyle the library


Contributions and Thanks

Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.

  1. Based on and modified from the Bill Greiman's SdFat library to add support to RP2040-based boards using Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.
  2. Based on and modified from the Adafruit SD library to add support to RP2040-based boards using Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core.
greiman
⭐️ Bill Greiman

adafruit
⭐️ Adafruit


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library

License

  • The library is licensed under MIT

Copyright

  • (C) Copyright 2009 by William Greiman
  • (C) Copyright 2010 SparkFun Electronics
  • (C) Copyright 2021 by Khoi Hoang

rp2040_sd's People

Contributors

khoih-prog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rp2040_sd's Issues

Cant use SPI1 on rp2040 pi pico board for SD

Hi, I am currently using Earle's V2.2.0 in Arduino boards manager , which is version 2.0.0.rc7 and your library rp2040_sd.
My board: Cytron Maker Pi Pio rev 1.2 uses a rpi-pico board SPI1 for the SD and wired up as follows:

#define PIN_SPI1_MISO (11u)
#define PIN_SPI1_MOSI (12u)
#define PIN_SPI1_SCK (10u)
#define PIN_SPI1_SS (15u)
in pins_arduino.h - directory for pipico is configured as above

in common.h:
static const uint8_t SS = PIN_SPI1_SS;
static const uint8_t MOSI = PIN_SPI1_MOSI;
static const uint8_t MISO = PIN_SPI1_MISO;
static const uint8_t SCK = PIN_SPI1_SCK;

when i try any of the sdcard ino files, it correctly uses GP15 for the SS but constantly uses SPI0 for the miso/mosi/clock
How can i correct this?
thanks in advance for your work
board / pinout details attached
Maker-Pi-Pico-bottom-800x800

Maker-Pi-Pico-Pin-Ref-Front-rev1-2-800-lres
.

Using build in SD Card in Sparkfun Thing Plus RP2040 board

I am not quit sure if this is an issue or my lag of knowledge but it seems that the lib is not using the correct pin assignments when using below #defines...

For the board Thing Plus RP2040 the following pins (GPIOs) are used for the SPI communication.

MOSI  15
MISO  12
SCK    14
SS      9

With an oscilloscope I can see that SS is brought low but the other lines are not doing anything.
Hence, initialisation fails.
I am using this code for initialisation , which is derived from one of your examples

/*
  SD card connection

  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:
   // Arduino-pico core
   ** MISO - pin 16
   ** MOSI - pin 19
   ** CS   - pin 17
   ** SCK  - pin 18

   // Arduino-mbed core
   ** MISO - pin 4
   ** MOSI - pin 3
   ** CS   - pin 5
   ** SCK  - pin 2
*/


#if !defined(ARDUINO_ARCH_RP2040)
  #error For RP2040 only
#endif

#if defined(ARDUINO_ARCH_MBED)
  
  #define PIN_SD_MOSI       15              <-------- My assignments
  #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

#include <SPI.h>
#include <RP2040_SD.h>

File myFile;

bool initSDCard()
{

    #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.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);
  
// FAILS HERE
  if (!SD.begin(100000, PIN_SD_SS)) 
  {
    Serial.println("Initialization failed!");
    return false;
  }
  return true;
}

It seems using above #defines will not change actual drive lines used by the SPI lib ?

I made an experiment with Micropython on the same board and everything works like a charm.
So, no issue with the used µSD card whatsoever.

RP2040_SD lib version 1.0.0
PlatformIO version: 5.1.1
platform=raspberrypi 1.2.0
based on Arduino-mbed RP2040 v2.3.1
Sparkfun Thing Plus RP2040
OS: Windows 10 /macOS

SD problems with another SPI BUS (different pins)

I am trying to use this library on:
rpi pico
using Arduino IDE
with the latest libraries as of now
I am trying to use some other pins then those on the sketch but they should also be fine since i checked the pin out.
I also tested all relevant pins with a blink sketch and they all work well. Additionally i also tested the SD card and breakout on an Uno and they work fine.
Note: SD is formatted as FAT32 and is 2G. Also I tried the dataloger sketch and i have the same problems.

Serial output:

  • is a card inserted?
  • is your wiring correct?
  • did you change the chipSelect pin to match your shield or module?
    Starting SD Card CardInfo on RASPBERRY_PI_PICO
    RP2040_SD v1.0.1
    Initializing SD card with SS = 17
    SCK = 18
    MOSI = 19
    MISO = 16
    initialization failed. Things to check:
  • is a card inserted?
  • is your wiring correct?
  • did you change the chipSelect pin to match your shield or module?

my code:
https://github.com/immortal-sniper1/RP2040_basic_board/tree/main/SW/CardInfo_alt_lib

Using SPI1 for StackyPi possible?

I want to use the SD card on StackyPi. The SD card is connected to SPI1. SPI0 is going to the Raspberry Pi header. What can I do to change?

Claus

Missing open file append

I don't know if this is the right way to
but as far as i read the lib supports open file for read and open file for write
is open file for append not implemented ?

Pit

The Library Runs Only Once

Describe the bug

I am Using the Raspberry Pi Pico RP2040 and arduino ide to code. When i Upload simple blink code and remove pico from computer it keeps working when i simply power it. But when i am using this library to interact the SD card it works when i upload it and check the serial monitor it worked fine but when i remove it from the system and power the pico it won't work. Even after unplug when i plug back to system and open serial monitor it even does not restart code. Can you help me why this issue happened?

I did not write custom code i am using the one of your examples.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.