RTC Read issue when running VS1053B.

I am working on a clock with an MP3 Player on an Arduino Due with a 7" TFT Display.

RTC Is DS3231

// TIME 
#include <DS3231.h> // I2C RTC
DS3231  rtc(SDA, SCL); // Init the DS3231 using the hardware interface
Time  t; // Init a Time-data structure

MP3 Is VS1053B SD Included since that is where the mp3 is coming from.

// SD
#include <avr/pgmspace.h>
#include <SPI.h>
#include <SdFat.h>          // SD library with long filenames
#define CARDCS 8 // SD Card chip select pin
SdFat SD;
boolean hasSD=false;

// MP3 

#include <VS1053SdFat.h>

#define VS1053_CS 14 // VS1053 chip select pin. ??? Does it have to be 52 
#define VS1053_DCS 11 // VS1053 DCS pin.
#define VS1053_DREQ 12 // VS1053 DREQ pin.
#define VS1053_RESET 13 // VS1053 RESET pin.
VS1053_FilePlayer musicPlayer = VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);

I have several things running on it but when I added the MP3 Player to the mix it keeps locking up. I disable several pieces of the code and what I discovered has me scratching my head (AGAIN).

If I start the music player the rtc read goes haywire. I am monitoring the RTC on the serial port and updating every second. Here is the code I am using to monitor

  t = rtc.getTime();
  if (oldsec >= 59) oldsec=0;
  if (t.sec >= oldsec+1) {
	  Serial.print(F(" Old Second is "));
	  Serial.print(oldsec);
	  Serial.print(F(" Current Time "));
	  Serial.print(t.hour);
	  Serial.print(F(":"));
	  Serial.print(t.min);
	  Serial.print(F(":"));
	  Serial.println(t.sec);
	  oldsec=t.sec;
  }

And here are the results.

Old Second is 26 Current Time 0:34:27
 Old Second is 27 Current Time 0:34:28
 Old Second is 28 Current Time 3:8:85
 Old Second is 0 Current Time 0:34:28
 Old Second is 28 Current Time 0:34:29
 Old Second is 29 Current Time 0:34:30
 Old Second is 30 Current Time 0:34:31
 Old Second is 31 Current Time 0:34:32
 Old Second is 32 Current Time 0:34:33
 Old Second is 33 Current Time 0:34:34
 Old Second is 34 Current Time 0:34:35
 Old Second is 35 Current Time 0:34:36
 Old Second is 36 Current Time 0:34:37
 Old Second is 37 Current Time 0:34:38
 Old Second is 38 Current Time 0:34:39
 Old Second is 39 Current Time 0:34:40
 Old Second is 40 Current Time 8:22:85
 Old Second is 0 Current Time 0:34:40
 Old Second is 40 Current Time 0:34:41
 Old Second is 41 Current Time 0:34:42

Notice the 8 in the bad reads. They are very consistent. It is always an 8 on the bad read. It will do it randomly but it always does it at the end of a song. If I actually re-enable my analog clock on the display the will lock up the arduino.

If more information is needed let me know. I tried to include the pertinent information.

Actually I am seeing 8:22:85 when the RTC read is bad Quite a bit. This error only occurs while the music is playing.

A link to the MP3 shield would be useful. Is it interfering with the I2C pins?

I am monitoring the RTC on the serial port and updating every second.

All we can see is that, somehow, some when, you read the clock. We can't see when that happens.

If I actually re-enable my analog clock on the display the will lock up the arduino.

Sounds like something is wrong with your code.

The MP3 ans SD use SPI the RTC uses I2C.

I can use microseconds and run the MP3 and the analog clock with no issues. It only crashes when I use the RTC.

I read the RTC on every loop in this example and only update the serial port every second. There are no delays in my loop

Here is most of the code involved.

MP3 Sheild

#include <memorysaver.h> // Trim unneeded definitions from UTFT.
#include <dtostrf.h>
// ##### UTFT ---------------------------------------------------------------------------
#include <UTFT.h> //Display
UTFT myGLCD(CPLD,25,26,27,28); // For DUE with 7.0 CLPD and CTE Sheild

#include <UTFT_Geometry.h>
UTFT_Geometry geo(&myGLCD); // For Drawing triangles and arrows

// ##### TOUCH --------------------------------------------------------------------------
#include <URTouch.h> // Touch Screen
URTouch  myTouch(6,5,32,3,2);// Define Touch Screen

// ##### TIME ---------------------------------------------------------------------------
#include <DS3231.h> // I2C RTC
DS3231  rtc(SDA, SCL); // Init the DS3231 using the hardware interface
Time  t; // Init a Time-data structure
// ##### FONTS --------------------------------------------------------------------------
extern uint8_t Ubuntu[]; //Font size: 24x32 pixels
extern uint8_t SixteenSegment16x24[];
extern uint8_t SixteenSegment24x36[];
extern uint8_t DotMatrix_M_Slash[];
extern uint8_t SixteenSegment40x60[];
extern uint8_t BigFont[];

// ##### UTFT_BUTTONS -------------------------------------------------------------------
#include <UTFT_Buttons.h> // UTFT Buttons Add-On
UTFT_Buttons  myButtons(&myGLCD, &myTouch);

// ##### SD  ----------------------------------------------------------------------------
#include <avr/pgmspace.h>
#include <SPI.h>
#include <SdFat.h>          // SD library with long filenames
#define CARDCS 8 // SD Card chip select pin
SdFat SD;
boolean hasSD=false;

// ##### MP3 ----------------------------------------------------------------------------

#include <VS1053SdFat.h>

/* MP3 Header Row
VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);
D8	---	Use For SD Card CS
D9  --- Used for TFT Backlight
D10 --- Used for TFT Flash
D11	--- Use for  MP3 DREQ
D12	--- Use for  MP3 DCS	
D13	--- Use for  MP3 RESET
*/
// ##### Define Pins for MP3 ------------------------------------------------------------
#define VS1053_CS 14 // VS1053 chip select pin. ??? Does it have to be 52 
#define VS1053_DCS 11 // VS1053 DCS pin.
#define VS1053_DREQ 12 // VS1053 DREQ pin.
#define VS1053_RESET 13 // VS1053 RESET pin.
VS1053_FilePlayer musicPlayer = VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);

// ##### SD File Variables
#define MAX_NO_TRACKS  20 //50 tracks maximum per album
#define MAX_NAME_LEN  30 //50 tracks maximum per album
char filebuffer[MAX_NAME_LEN +MAX_NAME_LEN +2];
char currentSong[MAX_NAME_LEN +1]; //79 char max length for filename (including .mp3)
char currentAlbum[MAX_NAME_LEN +1];//79 char max length for directory (album) name
FatPos_t tracks[MAX_NO_TRACKS]; 
byte serialPos=0; byte numberAlbums=0; byte numberTracks=0; byte totalTracks=0;

//PERSISTANT VARIABLES
bool randomPlay=false; byte albumNumber = 0; byte trackNumber = 1;
File dir;
byte lastkeypress=0; unsigned long press_ms=0; unsigned long spress_ms=0;


// #####  SENSORS -----------------------------------------------------------------------
int FIn = A11 ; //47ohm Resistor to +5vdc  Black Wire 
int BIn = A10 ; //4.7K to Ground and 100K to 12V 
int WIn = A9 ;  //47ohm Resistor to +5vdc Red Wire
int OIn = A8 ;  //100ohm Resistor to +5vdc Green Wire

int FVal = 0;  float FL = 0;  float FLSnap = 0 ; float PFL = 0; //FUEL
int FSV; int FSH; int FEV; int FEH;
int BVal = 0;  float BV = 0;  float BVSnap = 0;  float PBV = 0; //BATTERY
int BSV; int BSH; int BEV; int BEH;
int WVal = 0;  float WT = 0;  float WTSnap = 0;  float PWT = 0; //WATER
int WSV; int WSH; int WEV; int WEH; 
int OVal = 0;  float OP = 0;  float OPSnap = 0 ; float POP = 0; //OIL
int OSV; int OSH; int OEV; int OEH;

// ##### Clock --------------------------------------------------------------------------
int Width = 34; int Length = 250; // Set Gauge Size
int clockCenterX=600;
int clockCenterY=208;
int oldsec=0;

// ##### Setup --------------------------------------------------------------------------
void setup() {
	// Open serial communications and wait for port to open:
	Serial.begin(9600);
	//while (!Serial) {; // wait for serial port to connect. Needed for native USB port only
	//}	
  initSD();
  rtc.begin(); // Initialize the rtc object... // If this is missing from the setup System will not initialize.
  //t = rtc.getTime();// Get data from the DS3231
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_HI);
  myGLCD.InitLCD(LANDSCAPE);
  myGLCD.fillScr (VGA_BLACK); //Blank out display page 0
  myGLCD.setFont(SixteenSegment16x24);
  drawGaugePage();
  //dispClock();
  
  initSD(); //MP3 Player
  initMusicPlayer(); //MP3 Player
  playMP3();
  t = rtc.getTime();
  oldsec=t.sec;
}

// ##### LOOP ---------------------------------------------------------------------------
void loop() {
  
  gaugeRead(50);
  //turnSignalLeft();
  //turnSignalRight();
  //dispClock();
  t = rtc.getTime();
  if (oldsec >= 59) oldsec=0;
  if (t.sec >= oldsec+1) {
	  Serial.print(F(" Old Second is "));
	  Serial.print(oldsec);
	  Serial.print(F(" Current Time "));
	  Serial.print(t.hour);
	  Serial.print(F(":"));
	  Serial.print(t.min);
	  Serial.print(F(":"));
	  Serial.println(t.sec);
	  oldsec=t.sec;
	  if (oldsec == 59) oldsec=0;
  }
  
  
}
void initMusicPlayer(){

	Serial.println(F(">>> Intializing VS1053 MP3 Player chip"));
    if (! musicPlayer.begin()) { // initialise the music player
		Serial.println(F(">>> Could not Find VS1053, Do you have the correct pins defined"));
		while (1); //This stops the code until the mp3 initializes.
	}
    //if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT)) // Timer Interrupt
	if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)) // Hardware DREQ Interrupt
	delay(100); // Give VS1053 some time to initialize. 
	Serial.println(F(">>> MP3 OK"));
	musicPlayer.setVolume(0,0); // Set volume for left, right channels. lower numbers == louder volume!
}

// ##### SD -----------------------------------------------------------------------------
void initSD(){
  numberAlbums=0;
   if (!SD.begin(CARDCS)) {
    Serial.println(F(">>> SD Not Found!")); //SD failed, or not present
    hasSD=false;  // don't do anything more
  }
  else {
    hasSD=true;
    Serial.println(F(">>> SD OK"));
	listSDContents();

  }
}
void playMP3(){
	getSong(trackNumber);
	  sprintf(filebuffer,"%s/%s",currentAlbum,currentSong); // Sets Up the variable to play the track.
	  Serial.println(F("CURRENT TRACK"));
	  Serial.println(filebuffer);      
	if (musicPlayer.paused()) musicPlayer.pausePlaying(false); // Start the music player back up it it is paused. 
	if (! musicPlayer.startPlayingFile(filebuffer)) { // Play the track.
		Serial.print("Could not open file ");
		Serial.println(filebuffer);
		delay(1000);
	}
	else{
	  Serial.println(F("Started playing "));
	  Serial.println(filebuffer);
	}
}

The MP3 ans SD use SPI the RTC uses I2C.

That does NOT mean that the MP3 shield doesn't also (mis)use other pins. If it uses the I2C pins for ANY reason, that will cause issues when you need to use them for I2C.

PaulS:
That does NOT mean that the MP3 shield doesn't also (mis)use other pins. If it uses the I2C pins for ANY reason, that will cause issues when you need to use them for I2C.

The shield does not plug into the arduino any more. I had to rewire it. I am using The ISCP header for miso mosi reset VCC and Ground. The select pins are Digital 8, 10, 11, 12, and 13. 9 is used to control the backlight on the display.

i2c is on 20 and 21

I also pulled all of the pins that were could be causing an issue off of the TFT Shield.

D8 --- Use For SD Card CS
D9 --- Used for TFT Backlight
D10 --- Used for TFT Flash
D11 --- Use for MP3 DREQ
D12 --- Use for MP3 DCS
D13 --- Use for MP3 RESET

I trimmed everything out but the MP3 and RTC. Still failing here is the code.

// TIME 
#include <DS3231.h> // I2C RTC
DS3231  rtc(SDA, SCL); // Init the DS3231 using the hardware interface

// SD
#include <avr/pgmspace.h>
#include <SPI.h>
#include <SdFat.h>          // SD library with long filenames
#define CARDCS 8 // SD Card chip select pin
SdFat SD;
boolean hasSD=false;

// MP3 

#include <VS1053SdFat.h>

//  Pins for MP3
#define VS1053_CS 9 // VS1053 chip select pin.
#define VS1053_DCS 11 // VS1053 DCS pin.
#define VS1053_DREQ 12 // VS1053 DREQ pin.
#define VS1053_RESET 13 // VS1053 RESET pin.
VS1053_FilePlayer musicPlayer = VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);

void setup() {

  Serial.begin(9600);
  rtc.begin(); // Initialize the rtc object.
  initSD();
  initMusicPlayer(); //MP3 Player
  playMP3();
  }

void loop() {
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  delay (1000);
  
}

void playMP3(){
	if (musicPlayer.paused()) musicPlayer.pausePlaying(false); // Start the music player back up it it is paused. 
	if (! musicPlayer.startPlayingFile("test.mp3")) { // Play the track.
		Serial.print("Could not open test.mp3 ");
		delay(1000);
	}
	else{
	  Serial.println(F("Started playing test.mp3"));
	}
}

void initMusicPlayer(){

	Serial.println(F(">>> Intializing VS1053 MP3 Player chip"));
    if (! musicPlayer.begin()) { // initialise the music player
		Serial.println(F(">>> Could not Find VS1053, Do you have the correct pins defined"));
		while (1); //This stops the code until the mp3 initializes.
	}
    //if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT)) // Timer Interrupt
	if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)) // Hardware DREQ Interrupt
	delay(100); // Give VS1053 some time to initialize. 
	Serial.println(F(">>> MP3 OK"));
	musicPlayer.setVolume(0,0); // Set volume for left, right channels. lower numbers == louder volume!
}

void initSD(){
   if (!SD.begin(CARDCS)) {
    Serial.println(F(">>> SD Not Found!")); //SD failed, or not present
    hasSD=false;  // don't do anything more
  }
  else {
    hasSD=true;
    Serial.println(F(">>> SD OK"));
  }
}

and here is the serial monitor output. I trimmed the most of the good reads out.

>>> SD OK
>>> Intializing VS1053 MP3 Player chip
DEBUG: Card CS 8
DEBUG: Reset 13
DEBUG: MP3_CS 9
DEBUG: MP3_DCS 11
DEBUG: MP3_DREQ 12
DEBUG: MP3_MOSI 0
DEBUG: MP3_MISO 0
DEBUG: MP3_CLK 0
DEBUG: Using Hardware SPI
DEBUG: Getting ready for reset 
DEBUG: Reset Done
DEBUG: Using IRQ 12
>>> MP3 OK
Started playing test.mp3
Monday 01.01.2000 -- 00:00:07
...
Monday 01.01.2000 -- 00:00:15
 01.01.2000 -- 00:00:17
Monday 01.01.2000 -- 00:00:18
...
Monday 01.01.2000 -- 00:01:12
Monday 00.40.2120 -- 00:01:14
Monday 01.01.2000 -- 00:01:15
...
Monday 01.01.2000 -- 00:01:48
`pG8y 01.01.2000 -- 00:01:49
Monday 01.01.2000 -- 00:01:50
...
Monday 01.01.2000 -- 00:04:11

I am thinking it is a conflict in the libraries but I am not sure what to look for.

#define VS1053_DCS 11 // VS1053 DCS pin.
#define VS1053_DREQ 12 // VS1053 DREQ pin.
#define VS1053_RESET 13 // VS1053 RESET pin.

So, the shield uses the SPI pins but NOT SPI.

DEBUG: Using Hardware SPI

How the hell can it do that when you have defined the SPI pins to be used for other purposes?

With the same code, but the call to playMP3() commented out, does the program get the correct time from the RTC?

With the same code, expect the MP3 DCS, DREQ, and RESET connected to other pins, does the RTC provide the correct time?

With the same code, but the call to playMP3() commented out, does the program get the correct time from the RTC? --- YES it also gets the correct time if the music is not playing.

With the same code, expect the MP3 DCS, DREQ, and RESET connected to other pins, does the RTC provide the correct time? --- I have tried several different combinations of these with no change.

The shield has 11 Pins used

6 of them I do not define They are MISO, MOSI, RESET, VCC, GROUND, and SCK. These are on the ISCP Header, They are the Hardware SPI for DUE.

I have tried several different combinations of these with no change.

The Due has two sets of I2C pins, IIRC. Can you connect the RTC to the other pins, and modify the library to use the other I2C instance?

Or, perhaps you could use the Report to moderator link, and asked that the thread be moved to the Due part of the forum, where you might get better answers.

Also, could you post a link to the VS1053SdFat library? Perhaps a clue will present itself.

To troubleshoot your project, you could, stepwise:

1/ Connect the RTC device alone to the DUE and debug until it works
http://forum.arduino.cc/index.php?topic=168977.0

2/ Connect the VS1053B device alone to the DUE and debug until it works

Since the DS3231 communicates via I2C and the VS1053B via SPI, unless you have somewhere a blocking code, they should work together on the DUE.

They work separately.

They work together unless the music is playing.

I have tried different pin configurations.
I have tried a different DUE.
I have tried a different RTC.

It will run for a few seconds up to 10 minutes without locking up but it does lock up.

My thought is that it is a IRQ or a memory issue but I am not sure how to look at that.

I don't see what : #include <avr/pgmspace.h> does in your code.

BTW, select Serial.begin(250000); instead of 9600 bauds to speed up Serial printings.

#include <avr/pgmspace.h> was for another part of my library. I trimmed this down to just the RTC and MP3 and missed removing that. It is removed now.

Maybe the well known issue with the I2C: unlike AVRs, Sam3x8e has no glitch filtering( e.g. If you have a relay close to your board).

You can use a fast-mode compliant isolator like the PCA9517A. The isolator filters 50ns glitches and increases fall time (or RC filters with 20 ohms series résistors and a 200 pf to ground).

In the back of my mind I am thinking something along the lines of an interrupt issue. Is there a way to output the ISR's to the serial port when they happen. I want see what interrupts are happening when.

There is definitely a conflict between the MP3 and the RTC. I can add delays and get better results but it still crashes. It is either a memory stomp or interrupts.