Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any exaples for RP2040? #654

Open
Stass48 opened this issue Nov 25, 2023 · 3 comments
Open

Any exaples for RP2040? #654

Stass48 opened this issue Nov 25, 2023 · 3 comments

Comments

@Stass48
Copy link

Stass48 commented Nov 25, 2023

Hello! I want to play MP3 from SD Card on RP2040. Can someone help me with it? Examples in library is not working for me.

@maxgerhardt
Copy link
Contributor

Well now this is linked to earlephilhower/arduino-pico#1854

@Stass48
Copy link
Author

Stass48 commented Mar 5, 2024

Here's the working code. You need to solder a jumper on the PCM5102 module (shown in the photo) and power the module from 5 V. To avoid jerks during playback, you need to increase the SPI speed, as shown in the code. Also, use overclocking up to 200 MHz. Under these conditions, MP3 320 kbps Stereo is played.

image

#include "AudioFileSourceSD.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"

AudioGeneratorMP3 *mp3;
AudioFileSourceSD *file;
AudioOutputI2S *out;

// SPI0 for internal SD card
const int INT_SD_SCK = 18;
const int INT_SD_MOSI = 19;
const int INT_SD_MISO = 20;
const int INT_SD_CS = 21;

// You may need a fast SD card. Set this as high as it will work (40MHz max).
#define SPI_SPEED SD_SCK_MHZ(40) // Why not 50?

// Pins for I2S
const int PIN_BCLK = 2; // BCK
const int PIN_WCLK = 3; // LCK
const int PIN_DOUT = 4; // DIN
// Solder SCK jumper on module! VCC of PCM5102 module = 5V!

void setup() {
  SPI.setRX(INT_SD_MISO);
  SPI.setTX(INT_SD_MOSI);
  SPI.setSCK(INT_SD_SCK);
  SPI.setCS(INT_SD_CS);

  Serial.begin(115200);
  delay(1000);

  if (SD.begin(INT_SD_CS, SPI_SPEED, SPI)) Serial.println("SD OK!");
  Serial.println("Sample MP3 playback begins...");

  audioLogger = &Serial;
  file = new AudioFileSourceSD("/demo.mp3");
  out = new AudioOutputI2S();
  out->SetPinout(PIN_BCLK, PIN_WCLK, PIN_DOUT);
  mp3 = new AudioGeneratorMP3();
  mp3->begin(file, out);
}

void loop() {
  if (mp3->isRunning()) {
    if (!mp3->loop()) mp3->stop();
  } else {
    Serial.println("MP3 done");
    delay(1000);
  }
}

@Stass48
Copy link
Author

Stass48 commented Mar 5, 2024

Now I am interested in other questions.
In what mode does the memory card work? DEDICATED_SPI or SHARED_SPI? How can you control these modes? For example, set the DEDICATED_SPI mode.
If I have two SD cards, how can I choose which card will play files from?
For example, if I write:

#define SPI_CLOCK SD_SCK_MHZ(50)
SdFs rdSd;
FsFile rdFile;
#define SD_RD_CS_PIN 17
#define SD_RD_CONFIG SdSpiConfig(SD_RD_CS_PIN, DEDICATED_SPI, SPI_CLOCK, &SPI)

SdFs wrSd;
FsFile wrFile;
#define SD_WR_CS_PIN 13
#define SD_WR_CONFIG SdSpiConfig(SD_WR_CS_PIN, DEDICATED_SPI, SPI_CLOCK, &SPI1)

...

if (!rdSd.begin(SD_RD_CONFIG)) rdSd.initErrorHalt("rdSd");
if (!wrSd.begin(SD_WR_CONFIG)) wrSd.initErrorHalt("wrSd");

**How do I play a file from each memory card?

Another interesting question. Does the library support multithreading? RP2040 has 2 cores. Perhaps by using both cores, overclocking could be avoided.**

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants