How can I pass buffer ?

Writing software for systems that use VLSI Solution's devices as slave codecs to a host microcontroller.
bianchi
Senior User
Posts: 245
Joined: Mon 2013-12-30 4:15

How can I pass buffer ?

Post by bianchi »

Guys,

How can I pass data buffer from SDcard to VS1053 buffer ?

Thanks

I tried this below, but no luck :

Code: Select all

   char ReadBuffer[256];
    char str[55];
    

    
    fhandle = FAT32_Open("test1.mp3", FILE_READ);
    FAT32_Size("test1.mp3", &fsize);
    
    for (i = 0; i < fsize; /**/) 
      {  
        FAT32_Read(fhandle, ReadBuffer, 32);
        for (j = 0; j < 32; i++, j++)
          {
             if (i < fsize)
           
                MP3_SDI_Write_32(ReadBuffer[j]);
           }

      }
    UART_Write(CR);
    FAT32_Close(fhandle);
User avatar
Panu
VSDSP Expert
Posts: 2829
Joined: Tue 2010-06-22 13:43

Re: How can I pass buffer ?

Post by Panu »

I found this code from the internet, which seems to be for a similar environment than yours:
github.com/AndrewHazelden/Mini-Sub-Game/blob/master/development Files/minisub_Code/mikroC PRO for PIC32/mp3_player.cp
Read it to understand what the functions do. For example MP3_SDI_Write_32 sends 32 bytes, not 1 byte!

Don't copy-paste the code, it won't work as such.

Code: Select all

void Play_MP3(){	
	if(file_loaded) {
				
		while (file_size > BUFFER_SIZE) {	
			for (i=0; i<BUFFER_SIZE; i++) {
				Mmc_Fat_Read(BufferLarge + i);
			}	
			for (i=0; i<BUFFER_SIZE/32; i++) {
				MP3_SDI_Write_32(BufferLarge + i*32);
			}	
			file_size -= BUFFER_SIZE;	
		}
	
		...
		

bianchi
Senior User
Posts: 245
Joined: Mon 2013-12-30 4:15

Re: How can I pass buffer ?

Post by bianchi »

This function is working very well....how can I change p variable with the data from SDcard ?????

Code: Select all

/*******************************************************************************
* Function Hello()
* ------------------------------------------------------------------------------
* Overview: Function Hello
* Input: Nothing
* Output: Nothing
*******************************************************************************/
void hello(void)
{
   unsigned char *p;
    int i;
   
   p = HelloMP3;
   while (p <= &HelloMP3[sizeof(HelloMP3)-1]) 
   {
      while (DREQ == 0);
      BSYNC = 1;
      BSYNC = 0;
      MP3_SDI_Write_32(p);
      p += 32;
   }
   // End of file - send 2048 zeros before next file
    BSYNC = 0;

    for (i=0; i<2048; i++) 
    {
       while (DREQ == 0); // wait here until DREQ is high again
        MP3_SDI_Write(0x00);
      }
        Delay_ms(10);
		//SPIWait(); // Wait until SPI transfer is completed
    BSYNC = 1;
    UART_Write_Text("Hello Function Finished!\n");
}
bianchi
Senior User
Posts: 245
Joined: Mon 2013-12-30 4:15

Re: How can I pass buffer ?

Post by bianchi »

I've made this function, please correct me if I'm wrong, I can't hear the sound, but it can finish correctly...

Code: Select all

/*******************************************************************************
* Function MP3_Play_2()
* ------------------------------------------------------------------------------
* Overview: Function reading file
* Input: Nothing
* Output: Nothing
*******************************************************************************/


void MP3_Play_2()
{

  unsigned long i, file_size2;
  char fileHandle;

      fileHandle=FAT32_Open("test1.mp3",FILE_READ);
   if (fileHandle>=0){
        FAT32_Size("test1.mp3",&file_size2);
          UART_Write_Text("Opening file test1.mp3 = OK!\n");
          // send file blocks to MP3 SDI
        while (file_size2 > BUFFER_SIZE)
        {
                   for (i=0; i<BUFFER_SIZE; i++) 
                    {
                       FAT32_Read(fileHandle,mp3_buffer+i,BUFFER_SIZE);
                    }

                      BSYNC = 0;
                      for (i=0; i<BUFFER_SIZE/32; i++) 
                      {
                          MP3_SDI_Write_32(mp3_buffer+i*32);
                      }

                        for (i=0; i<2048; i++)
                          {
                             while (DREQ == 0); // wait here until DREQ is high again
                              MP3_SDI_Write(0x00);
                            }
                              Delay_ms(10);
                      		//SPIWait(); // Wait until SPI transfer is completed
                          BSYNC = 1;
                          file_size2 -= BUFFER_SIZE;

       }

        // send the rest of the file to MP3 SDI
         for (i=0; i<file_size; i++)
           {
            Fat32_Read(fileHandle,mp3_buffer+i,BUFFER_SIZE);
           }
        for (i=0; i<file_size2; i++)
        {
          MP3_SDI_Write(mp3_buffer[i]);
        }
        FAT32_Close(fileHandle);
        UART_Write_Text("Function MP3_Play_2 Finished!\n");
    }
    else {
     UART_Write_Text("Failed opening file!\n");
    }
}

MP3 data write function :

Code: Select all

void MP3_SDI_Write_32(char *data_) {
    char i;

    Mmc_Chip_Select = 1;
    MP3_CS = 1;


    while (DREQ == 0);             // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
      BSYNC = 0;
    for(i = 0; i < 32; i++)

        SPI1_Write(data_[i]);

    BSYNC = 1;
}
Thanks
Last edited by bianchi on Sun 2016-03-13 13:11, edited 1 time in total.
bianchi
Senior User
Posts: 245
Joined: Mon 2013-12-30 4:15

Re: How can I pass buffer ?

Post by bianchi »

Diagnostis message on UART :

Code: Select all

MP3 CHIP AND SDCARD TEST STARTS
DIAGNOSTIC MESSAGE : 
1. FAT32 Initialized
2. MP3 Initialized
VS Sine Test Begin!
SET VOLUME BEGIN Line226!
VS Sine Test END!
Hello Function Finished!
Opening file test1.mp3 = OK!
Function MP3_Play_2 Finished!
bianchi
Senior User
Posts: 245
Joined: Mon 2013-12-30 4:15

Re: How can I pass buffer ?

Post by bianchi »

is it about Chip select for MMC and VS1053 on and off ??

I see from this link you gave me :
https://raw.githubusercontent.com/Andre ... _player.cp

Code: Select all

void Play_MP3(){

 if(file_loaded) {

 UART1_Write_Line("Play started.");
 Mmc_Fat_Reset(&file_size);


 while (file_size > BUFFER_SIZE) {

 for (i=0; i<BUFFER_SIZE; i++) {
 Mmc_Fat_Read(BufferLarge + i);
 }

 for (i=0; i<BUFFER_SIZE/32; i++) {
 MP3_SDI_Write_32(BufferLarge + i*32);
 }

 file_size -= BUFFER_SIZE;




 }


 for (i=0; i<file_size; i++) {
 Mmc_Fat_Read(BufferLarge + i);
 }
 for (i=0; i<file_size; i++) {
 MP3_SDI_Write(BufferLarge[i]);
 }


 UART1_Write_Line("Play finished.");
 }
 else{

 UART1_Write_Line("Unable to play MP3 file.");
 }

}
User avatar
Panu
VSDSP Expert
Posts: 2829
Joined: Tue 2010-06-22 13:43

Re: How can I pass buffer ?

Post by Panu »

I've made this function, please correct me if I'm wrong, I can't hear the sound, but it can finish correctly...
That code shows some promise, but why are there two loops? You only need one loop to read the file and send it to VS1053. The 2048 zeros; forget about them at this time. You just send them after listening to the whole song.

The loop to play a file should be very simple, something like this:

Code: Select all

      while (file_size > BUFFER_SIZE) {   
         //Read BUFFER_SIZE bytes from file to buffer
         FAT32_Read(fileHandle, mp3_buffer, BUFFER_SIZE);
         
         // Send BUFFER_SIZE bytes from the buffer to the VS1053
         for (i=0; i<BUFFER_SIZE/32; i++) {
            MP3_SDI_Write_32(mp3_buffer + i*32);
         }   
         file_size -= BUFFER_SIZE;   
      }
Your code has a lot of extra stuff, throw that away. MP3_SDI_Write_32() seems to handle XDCS (BSYNC) so don't handle it a second time in your code.


PS. Here's MP3_SDI_Write_32() rewritten to follow VLSI coding guidelines a little bit more, so it's more understandable:

Code: Select all

void MP3_SDI_Write_32(char *data_) {
	char i;
	MP3_CS = 1; //Make sure XCS (SCI chip select) is high
	while (DREQ == 0) {
		//Do nothing, wait for DREQ to become high.
	}	
	BSYNC = 0; //Pull XDCS (SDI chip select) low
	for (i=0; i<32; i++) {   //For 32 bytes,
		SPI_Write(data_[i]); //Send a byte.
	}
	BSYNC = 1; //Pull XDCS (SDI chip select) high
}
bianchi
Senior User
Posts: 245
Joined: Mon 2013-12-30 4:15

Re: How can I pass buffer ?

Post by bianchi »

Thanks... i ll give a try ...wish me luck
bianchi
Senior User
Posts: 245
Joined: Mon 2013-12-30 4:15

Re: How can I pass buffer ?

Post by bianchi »

Here's my complete function, please give me correction, thanks

Code: Select all

/*******************************************************************************
* Function MP3_Play_2()
* ------------------------------------------------------------------------------
* Overview: Function reading file
* Input: Nothing
* Output: Nothing
*******************************************************************************/


void MP3_Play_2()
{

  unsigned long i, file_size2;
  char fileHandle;

      fileHandle=FAT32_Open("test1.mp3",FILE_READ);
   if (fileHandle>=0){
        FAT32_Size("test1.mp3",&file_size2);
          UART_Write_Text("Opening file test1.mp3 = OK!\n");
          // send file blocks to MP3 SDI
        while (file_size2 > BUFFER_SIZE)
        {
 
          
                        FAT32_Read(fileHandle,mp3_buffer,BUFFER_SIZE);
             
           
                    
                      for (i=0; i<BUFFER_SIZE/32; i++) 
                      {
                          MP3_SDI_Write_32(mp3_buffer+i*32);
                      }
                   
                          file_size2 -= BUFFER_SIZE;

       }

        // send the rest of the file to MP3 SDI
       
            Fat32_Read(fileHandle,mp3_buffer,BUFFER_SIZE);
        
        for (i=0; i<file_size2; i++)
        {
          MP3_SDI_Write(mp3_buffer+i);
        }
        FAT32_Close(fileHandle);
        UART_Write_Text("Function MP3_Play_2 Finished!\n");
    }
    else {
     UART_Write_Text("Failed opening file!\n");
    }
}

   
SDI32 function

Code: Select all

void MP3_SDI_Write_32(char *data_) {
    char i;

    
    MP3_CS = 1;


    while (DREQ == 0);             // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
      BSYNC = 0;
    for(i = 0; i < 32; i++)

        SPI1_Write(data_[i]);

    BSYNC = 1;
}
bianchi
Senior User
Posts: 245
Joined: Mon 2013-12-30 4:15

Re: How can I pass buffer ?

Post by bianchi »

the function running properly but I can't hear sound, vsine and hello test are ok ..

Diagnostic :

Code: Select all

VS Sine Test Begin!
SET VOLUME BEGIN Line226!
VS Sine Test END!
Hello Function Finished!
Opening file test1.mp3 = OK!
Function MP3_Play_2 Finished!
VS Sine Test Begin!
SET VOLUME BEGIN Line226!
VS Sine Test END!
Post Reply