Go Down

Topic: VS1053 programm stuck in while loop (Read 829 times) previous topic - next topic

Kevsnor

why the code stay in this loop? while (playingMusic && Ready()) {feedBuffer();}

quote]//#include <Adafruit_VS1053.h>
#include <SPI.h>
#include <SD.h>
#define VS1053_FILEPLAYER_TIMER0_INT 255 // allows useInterrupt to accept pins 0 to 254
#define VS1053_FILEPLAYER_PIN_INT 5

#define VS1053_SCI_READ 0x03
#define VS1053_SCI_WRITE 0x02

#define VS1053_REG_MODE  0x00
#define VS1053_REG_STATUS 0x01
#define VS1053_REG_BASS 0x02
#define VS1053_REG_CLOCKF 0x03
#define VS1053_REG_DECODETIME 0x04
#define VS1053_REG_AUDATA 0x05
#define VS1053_REG_WRAM 0x06
#define VS1053_REG_WRAMADDR 0x07
#define VS1053_REG_HDAT0 0x08
#define VS1053_REG_HDAT1 0x09
#define VS1053_REG_VOLUME 0x0B

#define VS1053_GPIO_DDR 0xC017
#define VS1053_GPIO_IDATA 0xC018
#define VS1053_GPIO_ODATA 0xC019

#define VS1053_INT_ENABLE  0xC01A

#define VS1053_MODE_SM_DIFF 0x0001
#define VS1053_MODE_SM_LAYER12 0x0002
#define VS1053_MODE_SM_RESET 0x0004
#define VS1053_MODE_SM_CANCEL 0x0008
#define VS1053_MODE_SM_EARSPKLO 0x0010
#define VS1053_MODE_SM_TESTS 0x0020
#define VS1053_MODE_SM_STREAM 0x0040
#define VS1053_MODE_SM_SDINEW 0x0800
#define VS1053_MODE_SM_ADPCM 0x1000
#define VS1053_MODE_SM_LINE1 0x4000
#define VS1053_MODE_SM_CLKRANGE 0x8000


#define VS1053_SCI_AIADDR 0x0A
#define VS1053_SCI_AICTRL0 0x0C
#define VS1053_SCI_AICTRL1 0x0D
#define VS1053_SCI_AICTRL2 0x0E
#define VS1053_SCI_AICTRL3 0x0F

#define VS1053_DATABUFFERLEN 32

//MP3
#define MP3_CS    7 //Chip select
#define MP3_DCS   6 //Data select
#define MP3_DREQ  3 //Data Request

//COMMON
#define SD_CS  4     //SD select
#define RST   -1    //Reset*/

  typedef volatile uint8_t RwReg;
  typedef uint8_t PortMask;
typedef volatile RwReg PortReg;

volatile boolean BufferLock=false;
static volatile PortReg *clkportreg, *misoportreg, *mosiportreg;
static PortMask clkpin, misopin, mosipin;
volatile boolean playingMusic;

boolean Play (const char *trackname);
boolean Ready(void);
void SPI_write(uint8_t addr, uint16_t data);
void spiwrite(uint8_t c);
void spiwrite(uint8_t *c, uint16_t num);
void feedBuffer(void);
void feedBuffer_noLock(void);
void playData(uint8_t *buf, uint8_t buffsiz);




uint8_t mp3buffer[32];

File currentTrack;
/*************************************************/
void setup() {
  Serial.begin(9600);
  delay(200);
  pinMode (MP3_CS, OUTPUT);
  pinMode (MP3_DCS, OUTPUT);
  pinMode (MP3_DREQ, INPUT);
  pinMode (SD_CS, OUTPUT);
  digitalWrite(SD_CS, HIGH);
 Serial.println("begin");
SPI.begin();

Play("/track001.mp3");
while(playingMusic){}
Serial.println("after play");
}
void loop() {
  // put your main code here, to run repeatedly:
   Serial.println("loop");
   delay(2000);
}

/*************************************************/
boolean Play (const char *trackname){
  SPI_write(VS1053_REG_MODE, VS1053_MODE_SM_LINE1 | VS1053_MODE_SM_SDINEW); // reset playback
  Serial.println("spi1");
  SPI_write(VS1053_REG_WRAMADDR, 0x1e29); // resync
  Serial.println("spi2");
  SPI_write(VS1053_REG_WRAM, 0);
  Serial.println("spi3");
 
  currentTrack = SD.open(trackname);
  noInterrupts();
Serial.println("no interrupt");
  SPI_write(VS1053_REG_DECODETIME, 0x00);
  SPI_write(VS1053_REG_DECODETIME, 0x00);
Serial.println("spi 4+5");
  playingMusic = true;

  while (!Ready()){}
    Serial.println("!ready");
  while (playingMusic && Ready()) {feedBuffer();}
    Serial.println("feedbuffer done");

  interrupts();
  return true;
}
/*************************************************/
boolean Ready(void){
Serial.println("DREQ");
return digitalRead(MP3_DREQ);

}
/*************************************************/
void SPI_write(uint8_t addr, uint16_t data){
  Serial.println("send");
  digitalWrite(MP3_CS, LOW); 
  spiwrite(VS1053_SCI_WRITE);        //0x02
  spiwrite(addr);
  spiwrite(data >> 8);
  spiwrite(data & 0xFF);
  digitalWrite(MP3_CS, HIGH); 
  Serial.println("spi done1");

/*************************************************/
void spiwrite(uint8_t c){
  uint8_t x __attribute__ ((aligned (32))) = c;
  spiwrite(&x, 1);
    Serial.println("spi done2");
}
/*************************************************/
void spiwrite(uint8_t *c, uint16_t num){
clkportreg = portOutputRegister(digitalPinToPort(13));
clkpin = digitalPinToBitMask(13);
misoportreg = portInputRegister(digitalPinToPort(12));
misopin = digitalPinToBitMask(12);
mosiportreg = portOutputRegister(digitalPinToPort(11));
mosipin = digitalPinToBitMask(11);
 
     while (num--) {
      for (int8_t i=7; i>=0; i--) {
 *clkportreg &= ~clkpin;
  if (c[0] & (1<<i)) {
    *mosiportreg |= mosipin;
  } else {
    *mosiportreg &= ~mosipin;
  }
  *clkportreg |= clkpin;
      }
      *clkportreg &= ~clkpin;   // Make sure clock ends low

      c++;
    }
      Serial.println("spi done3");
}
/*************************************************/
void feedBuffer (void){
    Serial.println("function feedbuffer");
noInterrupts();
  if (BufferLock) {
    interrupts();
    return;
  }
  BufferLock = true;
  interrupts();
  feedBuffer_noLock();
  BufferLock = false;
    Serial.println("feedbuffer done");
}
/*************************************************/
void feedBuffer_noLock(void){
    Serial.println("function no_lock");
   if ((! playingMusic)||(! currentTrack)||(! Ready())){
    return; // paused or stopped
  }
    // Feed the hungry buffer! :)
  while (Ready()) {
    // Read some audio data from the SD card file
    int bytesread = currentTrack.read(mp3buffer, VS1053_DATABUFFERLEN);
   
    if (bytesread == 0) {
      // must be at the end of the file, wrap it up!
      playingMusic = false;
      currentTrack.close();
      break;
    }

    playData(mp3buffer, bytesread);
  }
    Serial.println("no_lock done");
}
/*************************************************/
void playData(uint8_t *buf, uint8_t buffsiz) {
  digitalWrite(MP3_DCS, LOW);
  spiwrite(buf, buffsiz);
  digitalWrite(MP3_DCS, HIGH);
    Serial.println("play data done");
}[/quote]

Robin2

To make it easy for people to help you please modify your post and use the code button </>


Code: [Select]
so your code
looks like this
and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.



The usual reason for being stuck in a WHILE loop is because the variable that is tested in the WHILE statement is never updated inside the WHILE loop.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up