VLSI Solution Oy VS1103 VLSI Solution Karaoke, MIDI and ADPCM Player Source Code Documentation

Main Page | Class List | File List | Class Members | File Members | Related Pages

vs10xx.c

Go to the documentation of this file.
00001 
00007 #include "vs10xx.h"
00008 #include "lcd.h"
00009 #include "filesys.h"
00010 #include "storage.h"
00011 #include "ui.h"
00012 
00013 
00014 
00015 
00017 unsigned int Mp3ReadRegister (unsigned char addressbyte){
00018   unsigned int resultvalue = 0;
00019 
00020   Mp3SelectControl();
00021   SPIPutCharWithoutWaiting(VS_READ_COMMAND);
00022   SPIPutChar((addressbyte));
00023   SPIPutChar(0xff);
00024   SPIWait();
00025   resultvalue = (SPI_RESULT_BYTE) << 8;
00026   SPIPutCharWithoutWaiting(0xff);
00027   SPIWait();
00028   resultvalue |= (SPI_RESULT_BYTE);
00029   Mp3DeselectControl();
00030   return resultvalue;
00031 }
00032   
00033 bit vs1103_recording_active;
00034 bit vs1103_record_mixer;
00035 bit vs1103_use_line_input;
00036 bit vs1103_do_reset;
00037 bit vs1103_mono_output;
00038 bit vs1103_terminate;
00039 bit vs1103_earSpkHi;
00040 bit vs1103_earSpkLo;
00041 bit vs1103_real_time_midi;
00042 
00043 unsigned char currentMicGain;
00044 
00045 void LoadPatch();
00046 void StartPatch();
00047 
00048 void Vs1103SetMode(){
00049   unsigned char hiMode, loMode;
00050   
00051   hiMode = SM_SDINEW;
00052   loMode = SM_SDI_ADPCM;
00053 
00054   if (vs1103_use_line_input) hiMode |= SM_LINE_IN;
00055   if (vs1103_recording_active) hiMode |= SM_RECORD_ADPCM;
00056   if (vs1103_earSpkLo) hiMode |= SM_EARSPK_1;
00057   if (vs1103_earSpkHi) hiMode |= SM_EARSPK_2;
00058   
00059   if (vs1103_do_reset) loMode |= SM_RESET;
00060   if (vs1103_mono_output) loMode |= SM_DIFF;
00061   if (vs1103_record_mixer) loMode |= SM_RECORD_MIXER;
00062   if (vs1103_terminate) loMode |= SM_OUTOFMIDI;
00063   if (vs1103_real_time_midi) loMode |= SM_RT_MIDI;
00064 
00065   if (vs1103_real_time_midi){
00066     //Enable VS1103 UART interrupt
00067     Mp3WriteRegister(SCI_WRAMADDR, 0xc0, 0x1a);
00068     Mp3WriteRegister(SCI_WRAM, 0x00, 0x3f);
00069   }
00070 
00071   Mp3WriteRegister (SCI_MODE, hiMode, loMode);
00072   ConsoleWrite("SetMode: ");
00073   ConsolePutHex8(hiMode);
00074   ConsolePutHex8(loMode);
00075   ConsolePutChar(13);
00076 
00077   
00078   while (!MP3_DREQ)
00079     ;//wait for operation to complete
00080 
00081 
00082   if (!vs1103_real_time_midi){
00083     //When real-time midi input is not active,
00084     //Uart RX goes to monitor interrupt. To prevent
00085     //from falling to the monitor, disable RX interrupt
00086     //now
00087     Mp3WriteRegister(SCI_WRAMADDR, 0xc0, 0x1a);
00088     Mp3WriteRegister(SCI_WRAM, 0x00, 0x0f);
00089   } 
00090   
00091   StartPatch();
00092   
00093 }
00094 
00095 
00097 void Mp3SoftReset(){
00098 
00099   /* Soft Reset of VS10xx */
00100   vs1103_do_reset = 1;
00101   Vs1103SetMode();
00102   vs1103_do_reset = 0;
00103  
00104   Delay(1); /* One millisecond delay */
00105   while (!MP3_DREQ) /* wait for startup */
00106     ;
00107   
00108   Mp3SetMixerVolume(25,25,25);
00109 
00110   Delay(1);
00111   //Mp3WriteRegister(SCI_CLOCKF, 0xa6, 0x96);
00112 
00113 
00114   // Set default microphone gain
00115   Delay(1);
00116   Mp3WriteRegister(SCI_ADPCMRECCTL, USE_512_BYTE_DREQ, 0x00 | 20);
00117   
00118   
00119   Delay(1); /* One millisecond delay */
00120   
00121   /* Send null bytes to data interface */
00122   Mp3SelectData();
00123   SPIPutCharWithoutWaiting(0);
00124   SPIPutChar(0);
00125   SPIPutChar(0);
00126   SPIPutChar(0);
00127   SPIWait();
00128   Mp3DeselectData();
00129 
00130 }
00131 
00132 
00133 
00134 
00136 void Mp3Reset(){
00137 
00138   vs1103_recording_active = 0;
00139   vs1103_record_mixer = 1;
00140   vs1103_use_line_input = 1;
00141   vs1103_do_reset = 0;
00142   vs1103_mono_output = 0;
00143   vs1103_terminate = 0;
00144   vs1103_earSpkHi = 0;
00145   vs1103_earSpkLo = 0;
00146   vs1103_real_time_midi = 0;
00147   
00148   SPISetSlowClock();
00149 
00150   Mp3PutInReset();
00151   Delay(1);
00152 
00153   /* Send dummy SPI byte to initialize SPI */
00154   SPIPutCharWithoutWaiting(0xFF);
00155 
00156   /* Un-reset MP3 chip */
00157   Mp3DeselectControl();
00158   Mp3DeselectData();
00159   Mp3ReleaseFromReset();
00160 
00161   Delay(1);
00162   while(!MP3_DREQ);
00163   /* Start up analog with no sound */
00164   Mp3SetVolume(0xfe,0xfe);
00165   Mp3SetMixerVolume(0,0,0);
00166   LoadPatch();
00167   Vs1103SetMode();
00168 
00169   /* Set clock register, doubler etc. */
00170   Mp3WriteRegister(SCI_CLOCKF, 0xa6, 0x96); //oli 0xa66c
00171 
00172 
00173   Delay(500);
00174 
00175   Mp3SetMixerVolume(1,1,1);
00176   //ConsoleWrite("Mixer111 ");
00177   //Delay(1000);
00178   //Mp3SetVolume(0xfe,0xfe);
00179   //ConsoleWrite("Mixer 1,1,1 SecondDelay");
00180   //Delay(2000);
00181   Mp3SetVolume(volume,volume);
00182 
00183   SPISetFastClock();
00184 
00185   Mp3SoftReset();
00186 
00187   //ConsoleWrite("Clock:");ConsolePutHex16(Mp3ReadRegister(SCI_CLOCKF));
00188   ConsoleWrite ("\rInit: VS10XX\r");
00189 
00190 }
00191 
00192 
00194 void VsSineTest(){
00195 
00196   /* Reset MP3 chip */
00197   Mp3PutInReset();       /* Pull xRESET low -> hardware reset */
00198   Delay(100);            /* 100 ms delay */
00199 
00200   /* Send dummy SPI byte to initialize SPI of Atmel microcontroller */
00201   SPIPutCharWithoutWaiting(0xFF);
00202 
00203   /* Un-reset MP3 chip */
00204   Mp3DeselectControl();  /* Pull xCS high    */
00205   Mp3DeselectData();     /* Pull xDCS high   */
00206   Mp3ReleaseFromReset(); /* Pull xRESET high */
00207   Delay(100);            /* 100 ms delay     */
00208 
00209   //GREEN_LED = LED_ON;
00210   //RED_LED = LED_ON;
00211 
00212   /* VS10xx Application Notes, chapter 4.8 ---------------------------------*/
00213   /* As an example, let's write value 0x0820 to register 00 byte by byte    */
00214   Mp3SelectControl();    /* Now SPI writes go to SCI port                   */
00215   SPIPutChar(0x02);      /* Send SPI Byte, then wait for byte to be sent.   */
00216   SPIPutChar(0x00);      /* 0x02 was WRITE command, 0x00 is register number */
00217   SPIPutChar(0x08);      /* This byte goes to MSB                           */
00218   SPIPutChar(0x20);      /* ..and this is LSB. (0x20=Allow Test Mode)       */
00219   SPIWait();             /* Wait until Atmel MCU signals SPI write complete */
00220   Mp3DeselectControl();  /* Now SPI writes don't go to SCI port             */
00221 
00222   while (!MP3_DREQ)      /* Wait for DREQ = 1                               */
00223     ;                    /* Do nothing while waiting for DREQ = 1           */
00224 
00225   /* Send a Sine Test Header to Data port                                   */
00226   Mp3SelectData();       /* Now SPI writes go to SDI port                   */
00227 
00228   SPIPutChar(0x53);      /* - This is a special VLSI Solution test header - */
00229   SPIPutChar(0xef);      /* - that starts a sine sound. It's good for     - */
00230   SPIPutChar(0x6e);      /* - testing your code, the chip and also for    - */
00231   SPIPutChar(0x44);      /* - seeing if your MP3 decoder was manufactured - */
00232   SPIPutChar(0x00);      /* - by VLSI Solution oy. ------------------------ */
00233   SPIPutChar(0x00);
00234   SPIPutChar(0x00);
00235   SPIPutChar(0x00);
00236   SPIWait();
00237   Mp3DeselectData();
00238   
00239   //RED_LED = LED_OFF;
00240   Delay (500);           /* 500 ms delay */
00241   //GREEN_LED = LED_OFF;
00242 
00243   /* Stop the sine test sound */
00244   Mp3SelectData();
00245   SPIPutChar(0x45);
00246   SPIPutChar(0x78);
00247   SPIPutChar(0x69);
00248   SPIPutChar(0x74);
00249   SPIPutChar(0x00);
00250   SPIPutChar(0x00);
00251   SPIPutChar(0x00);
00252   SPIPutChar(0x00);
00253   SPIWait();
00254   Mp3DeselectData();
00255 
00256   Delay(500);            /* 500 ms delay */
00257 }  
00258 
00260 void SendZerosToVS10xx(){
00261   Mp3SelectData();
00262   SPIPutCharWithoutWaiting(0);
00263   for (temp.i=0; temp.i<2048; temp.i++){ /* TESTING 1048 TESTING */
00264     while (!MP3_DREQ)
00265       ;
00266     SPIPutChar(0);
00267   }
00268   SPIWait();
00269   Mp3DeselectData();
00270 }  
00271 
00272 
00276 unsigned char PlayDiskSectors (unsigned int nSectorsToPlay){
00277 
00278   while (nSectorsToPlay){
00279 
00280     if (MP3_DREQ){
00281       TransferSectorSDI(sectorAddress.l);
00282       sectorAddress.l++;
00283       nSectorsToPlay--;
00284     }
00285 
00286     AvailableProcessorTime(); //Call the User Interface handler
00287 
00288     /* If playing state is something else than "play normally",
00289        exit returning the request number. */
00290     if ((playingState==PS_END_OF_SONG)||
00291         (playingState==PS_NEXT_SONG)||
00292         (playingState==PS_RECORDING)||
00293         (playingState==PS_PREVIOUS_SONG)){
00294       return playingState;
00295     }
00296     
00297   }
00298   return 0; //OK Exit
00299 }
00300 
00301 
00302 
00303 void Mp3SetMixerVolume(unsigned char gain1, 
00304                        unsigned char gain2, 
00305                        unsigned char gainInput){
00306   unsigned int i;
00307 
00308   i=0x8000; // Mixer Mode On
00309   i |= (gainInput << 10);
00310   i |= (gain2 << 5);
00311   i |= (gain1);
00312 
00313   Mp3WriteRegister(SCI_MIXERVOL, (i >> 8), (i & 0xff));
00314 
00315 }
00316 
00317 void Vs1103B_SetMidiTranspose(int transpose){
00318   Mp3WriteRegister(SCI_WRAMADDR, 0x11, 0x32);
00319   while (!MP3_DREQ);
00320   Mp3WriteRegister(SCI_WRAM,(transpose >> 8),(transpose));
00321 }
00322 
00323 void Vs1103B_SetChannelMask(unsigned int newMask){
00324   Mp3WriteRegister(SCI_WRAMADDR, 0x17, 0x15);
00325   while (!MP3_DREQ);
00326   Mp3WriteRegister(SCI_WRAM,(newMask >> 8),(newMask));
00327 }
00328 
00329 void Vs1103SetMicGain(unsigned char gain){
00330   if (gain>31) gain=31;
00331   currentMicGain = gain;
00332   Mp3WriteRegister(SCI_ADPCMRECCTL, USE_512_BYTE_DREQ, 0x40 | gain);
00333 }
00334 
00335 void Vs1103CancelWave(){
00336   Mp3WriteRegister(SCI_ADPCMRECCTL, USE_512_BYTE_DREQ, 0x80 | currentMicGain);
00337 
00338   while (Mp3ReadRegister(SCI_ADPCMRECCTL) & 0x0080)
00339     ;
00340 }
00341 
00342 
00343 void Vs1103CancelMidi(){
00344   vs1103_terminate = 1;
00345   Vs1103SetMode();        
00346   vs1103_terminate = 0;
00347   // The below lines are recommended but take a little
00348   // too much time for our non-multitasking 8051 
00349   // architecture:
00350   //while (Mp3ReadRegister(SCI_MODE) & SM_OUTOFMIDI)
00351   //  ;
00352 
00353 }

All software copyright 2000-2004 VLSI Solution OY. Redistribution of these software modules is limited to VLSI Solution Oy chip promotional use only. Free or commercial use of these software modules in MP3 players is ok if the product includes chip(s) from VLSI. You can request the complete (compilable) package from mp3@vlsi.fi. This exampe code is provided with good faith to assist You in code development, but under no circumstances will VLSI offer any guarantees on the usability or functionality of any example software or its fitness for any purpose.