Example #1
0
int main() {
	init_platform();

	static XGpio pshBtns;
	static XIntc intCtrl;

	Xuint32 Sample_L;
	Xuint32 Sample_R;

	pAudioData = (DDR2_BASEADDR + 0x02000000);
    pFFTData  = (pAudioData + lNumSamples);

	lBtnStateOld = 0x00000000;
	fsRunAction = 0;

	/*
	 *Initialize the driver structs for the Push button and interrupt cores.
	 *This allows the API functions to be used with these cores.
	 */
	XGpio_Initialize(&pshBtns, BTNS_DEVICE_ID);
	XIntc_Initialize(&intCtrl, INTC_DEVICE_ID);

	/*
	 * Connect the function PushBtnHandler to the interrupt controller so that
	 * it is called whenever the Push button GPIO core signals an interrupt.
	 */
	XIntc_Connect(&intCtrl, BTNS_IRPT_ID, PushBtnHandler, &pshBtns);

	/*
	 * Enable interrupts at the interrupt controller
	 */
	XIntc_Enable(&intCtrl, BTNS_IRPT_ID);

	/*
	 * Register the interrupt controller with the microblaze
	 * processor and then start the Interrupt controller so that it begins
	 * listening to the interrupt core for triggers.
	 */
	microblaze_register_handler(XIntc_DeviceInterruptHandler, INTC_DEVICE_ID);
	microblaze_enable_interrupts();
	XIntc_Start(&intCtrl, XIN_REAL_MODE);

	/*
	 * Enable the push button GPIO core to begin sending interrupts to the
	 * interrupt controller in response to changes in the button states
	 */
	XGpio_InterruptEnable(&pshBtns, lBtnChannel);
	XGpio_InterruptGlobalEnable(&pshBtns);

	/*
	 * Wait for AC97 to become ready
	 */
	while (!(AC97_Link_Is_Ready(AC97_BASEADDR)))
		;

	/*
	 * Set TAG to configure codec
	 */
	AC97_Set_Tag_And_Id(AC97_BASEADDR, 0xF800);

	/*
	 * Enable audio output and set volume
	 */
	AC97_Unmute(AC97_BASEADDR, AC97_MASTER_VOLUME_OFFSET);
	AC97_Unmute(AC97_BASEADDR, AC97_HEADPHONE_VOLUME_OFFSET);
	AC97_Unmute(AC97_BASEADDR, AC97_PCM_OUT_VOLUME_OFFSET);

	AC97_Set_Volume(AC97_BASEADDR, AC97_MASTER_VOLUME_OFFSET, BOTH_CHANNELS,
			VOLUME_MAX);
	AC97_Set_Volume(AC97_BASEADDR, AC97_HEADPHONE_VOLUME_OFFSET, BOTH_CHANNELS,
			VOLUME_MIN);
	AC97_Set_Volume(AC97_BASEADDR, AC97_PCM_OUT_VOLUME_OFFSET, BOTH_CHANNELS,
			VOLUME_MAX);

	while (1) {
		/**************************
		 * Play recorded sample
		 **************************/
		if (sampleMax != 0) {
			if (fsRunAction & bitPlay) {
				/*
				 * Set AC'97 codec TAG to send and receive data in the PCM slots
				 */
				AC97_Set_Tag_And_Id(AC97_BASEADDR, 0x9800);

				Count_Samples = 0;

				// Xil_Out32(LED_BASEADDR, bitPlayLED); //turn on LED
				int showLED = 0;
				while ((Count_Samples / 8) < lNumSamples) {
					/*
					 * Block execution until next frame is ready
					 */
					AC97_Wait_For_New_Frame(AC97_BASEADDR);

					/*
					 * Read audio data from memory
					 */
					Sample_L = XIo_In32 (pAudioData + Count_Samples);
					Count_Samples = Count_Samples + 4;
					Sample_R = XIo_In32 (pAudioData + Count_Samples);
					Count_Samples = Count_Samples + 4;

					/* our code */
					/* if(Count_Samples < 100) {  */
					//	printf("Left: %i",(int)Sample_L);
					//	printf("Right: %i",(int)Sample_R);
					/* } */
					if (showLED % 4000 == 0) {
						/*						middleLeft /= 8000;
						 middleRight /= 8000; */
						// Xil_Out32(LED_BASEADDR, Sample_L & AC97_META_MASK);
						// printf("Left: %d",(int)Sample_L);
						// printf("Right: %d",(int)Sample_R);
						setVolumeLEDs(Sample_L & AC97_DATA_MASK,
								Sample_R & AC97_DATA_MASK);
						/*						middleLeft = 0;
						 middleRight = 0;
						 } else {
						 middleLeft += Sample_L;
						 middleRight += Sample_R; */
					}

					/*
					 * Send audio data to codec
					 */XIo_Out32 ((AC97_BASEADDR + AC97_PCM_OUT_L_OFFSET), Sample_L);
					XIo_Out32 ((AC97_BASEADDR + AC97_PCM_OUT_R_OFFSET), Sample_R);
					showLED++;
				}

				Xil_Out32(LED_BASEADDR, 0); //Turn off LED
				fsRunAction = 0; //Forget any button presses that occurred
			}
		}
		/**************************
		 * Output a square wave
		 **************************/
		if (fsRunAction & bitGenWave) {
			//generate square on left, right and then both channels
			GenSquare(AC97_BASEADDR, LEFT_CHANNEL, 1000, 500);
			GenSquare(AC97_BASEADDR, RIGHT_CHANNEL, 1000, 500);
			GenSquare(AC97_BASEADDR, BOTH_CHANNELS, 1000, 500);
			fsRunAction = 0;
		}
		/**************************
		 * LEDTest
		 **************************/
		if (fsRunAction & bitLEDTest) {
			/*	Xil_Out32(LED_BASEADDR, volumeLED0);
			 sleepTimer(1000);
			 Xil_Out32(LED_BASEADDR, volumeLED1);
			 sleepTimer(1000);
			 Xil_Out32(LED_BASEADDR, volumeLED2);
			 sleepTimer(1000);
			 Xil_Out32(LED_BASEADDR, volumeLED3);
			 sleepTimer(1000);
			 Xil_Out32(LED_BASEADDR, volumeLED4);
			 sleepTimer(5000);
			 Xil_Out32(LED_BASEADDR, volumeLED5);
			 sleepTimer(5000);
			 Xil_Out32(LED_BASEADDR, volumeLED6);
			 sleepTimer(5000);
			 Xil_Out32(LED_BASEADDR, volumeLED7);
			 sleepTimer(5000);
			 Xil_Out32(LED_BASEADDR, 0);
			 Xil_Out32(LED_BASEADDR, allLEDs);
			 sleepTimer(5000);
			 Xil_Out32(LED_BASEADDR, 0);
			 fsRunAction = 0;*/
			Xil_Out32(LED_BASEADDR, volumeLED7);
		  	fftSample(pAudioData, lNumSamples, 0);
			Xil_Out32(LED_BASEADDR, 0);
//			fftSample(pFFTData, pAudioData, lNumSamples, 1);
//			addOriginMeta();
//			if (compareValues(pAudioData, pFFTData)) {
//				Xil_Out32(LED_BASEADDR, allLEDs);
//				sleepTimer(5000);
//				Xil_Out32(LED_BASEADDR, 0);
//				Xil_Out32(LED_BASEADDR, allLEDs);
//				sleepTimer(5000);
//				Xil_Out32(LED_BASEADDR, 0);
//			} else {
//				Xil_Out32(LED_BASEADDR, volumeLED0);
//				sleepTimer(1000);
//				Xil_Out32(LED_BASEADDR, volumeLED1);
//				sleepTimer(1000);
//				Xil_Out32(LED_BASEADDR, volumeLED2);
//				sleepTimer(1000);
//				Xil_Out32(LED_BASEADDR, volumeLED3);
//				sleepTimer(1000);
//				Xil_Out32(LED_BASEADDR, volumeLED4);
//			}
			fsRunAction = 0;

		}
		/**************************
		 * Record audio from input
		 **************************/
		if (fsRunAction & bitRec) {
			AC97_Set_Tag_And_Id(AC97_BASEADDR, 0xF800); //Set to configure

			/*
			 * Select input source, enable it, and then set the volume
			 */
			if (Xil_In32(SW_BASEADDR) & bitSw0) {
				AC97_Select_Input(AC97_BASEADDR, BOTH_CHANNELS,
						AC97_LINE_IN_SELECT);
				AC97_Unmute(AC97_BASEADDR, AC97_LINE_IN_VOLUME_OFFSET);
				AC97_Set_Volume(AC97_BASEADDR, AC97_LINE_IN_VOLUME_OFFSET,
						BOTH_CHANNELS, VOLUME_MAX);
			} else {
				AC97_Select_Input(AC97_BASEADDR, BOTH_CHANNELS, AC97_MIC_SELECT);
				AC97_Unmute(AC97_BASEADDR, AC97_MIC_VOLUME_OFFSET);
				AC97_Set_Volume(AC97_BASEADDR, AC97_MIC_VOLUME_OFFSET,
						BOTH_CHANNELS, VOLUME_MID);
			}
			//set record gain
			AC97_Set_Volume(AC97_BASEADDR, AC97_RECORD_GAIN_OFFSET,
					BOTH_CHANNELS, 0x00);

			AC97_Set_Tag_And_Id(AC97_BASEADDR, 0x9800); //Set to Send/Receive data

			Count_Samples = 0;

			Xil_Out32(LED_BASEADDR, bitRecLED); //Turn on LED

			middle = 0;
			sampleMax = MIN_UINT;
			countSamples = 0;
			sampleMin = MAX_UINT;
			while ((Count_Samples / 8) < lNumSamples) {
				AC97_Wait_For_New_Frame(AC97_BASEADDR);

				/*
				 * Read audio data from codec
				 */
				Sample_L = XIo_In32(AC97_BASEADDR + AC97_PCM_IN_L_OFFSET);
				Sample_R = XIo_In32(AC97_BASEADDR + AC97_PCM_IN_R_OFFSET);

				/*
				 * our code
				 */
				captureSampleReference(Sample_L & AC97_DATA_MASK,
						Sample_R & AC97_DATA_MASK);

				/*
				 * Write audio data to memory
				 */

				XIo_Out32 (pAudioData + Count_Samples, Sample_L);
				Count_Samples = Count_Samples + 4;
				XIo_Out32 (pAudioData + Count_Samples, Sample_R);
				Count_Samples = Count_Samples + 4;
			}

			//Set Tag and ID to configure the codec
			AC97_Set_Tag_And_Id(AC97_BASEADDR, 0xF800);

			/*
			 * Disable the input source
			 */
			if (Xil_In32(SW_BASEADDR) & bitSw0) {
				AC97_Mute(AC97_BASEADDR, AC97_LINE_IN_VOLUME_OFFSET);
			} else {
				AC97_Mute(AC97_BASEADDR, AC97_MIC_VOLUME_OFFSET);
			}
			middle = middle / countSamples;
			Xil_Out32(LED_BASEADDR, 0); //Turn off LED
			fsRunAction = 0;
		}

	}

	cleanup_platform();

	return 0;
}
Example #2
0
File: ac97.c Project: Angurboda/ABP
void GenSquare (Xuint32 AC97_Baseaddr, u8 channel, int freq_HZ, int milisec)
{
	Xuint32 i, j;
	Xuint32 PCM_DATA;
	Xuint32 Status;
	Xuint32 TAG_AND_ID_DATA;
	Xuint32 count_skip_frame=0;
	int line_in_muted;
	int mic_muted;
	Xuint32 num_frames_send;
	Xuint32 Sample_Rate;

	i=0;

	Sample_Rate = Get_Sample_Rate (AC97_Baseaddr);

	//Here should be 1000 and 2, respectively. To review the calculations

	num_frames_send = (Sample_Rate /2000) * milisec;

	Xuint32 frames_to_skip = Sample_Rate / (4*freq_HZ);


	line_in_muted =  AC97_IsMuted (AC97_Baseaddr, AC97_LINE_IN_VOLUME_OFFSET);
	mic_muted = AC97_IsMuted (AC97_Baseaddr, AC97_MIC_VOLUME_OFFSET);

	//mute MIC and LINE-IN inputs if not muted and unmute PCM output first
    if (!(line_in_muted))
    		Status = AC97_Mute (AC97_Baseaddr, AC97_LINE_IN_VOLUME_OFFSET);

    if (!(mic_muted))
    	Status = AC97_Mute (AC97_Baseaddr, AC97_MIC_VOLUME_OFFSET);

    Status = AC97_Unmute (AC97_Baseaddr, AC97_MASTER_VOLUME_OFFSET);
   Status = AC97_Unmute (AC97_Baseaddr, AC97_HEADPHONE_VOLUME_OFFSET);
   Status = AC97_Unmute (AC97_Baseaddr, AC97_PCM_OUT_VOLUME_OFFSET);

//change TAG to send valid data in the PCM slots
   TAG_AND_ID_DATA = 0x9800;
   AC97_Set_Tag_And_Id (AC97_Baseaddr, TAG_AND_ID_DATA );

// set the first value for the PCM data;
   PCM_DATA = 0x0001FFFF;


// wait for a frame
   AC97_Wait_For_New_Frame (AC97_Baseaddr);

   j=0;
	//send the square-wave frames
	while (j<num_frames_send)
	{
	   j++;
	   //wait for the next frame;
	  AC97_Wait_For_New_Frame (AC97_Baseaddr);

	  //send out PCM_DATA to the AC'97 codec
	  switch (channel)
	  {
	  case LEFT_CHANNEL:{
	  		XIo_Out32 ((AC97_Baseaddr + AC97_PCM_OUT_L_OFFSET), PCM_DATA);
	   		XIo_Out32 ((AC97_Baseaddr + AC97_PCM_OUT_R_OFFSET), 0x00000000);
	  }
	  break;
	  case RIGHT_CHANNEL:
	  {
	  		XIo_Out32 ((AC97_Baseaddr + AC97_PCM_OUT_L_OFFSET), 0x00000000);
	   		XIo_Out32 ((AC97_Baseaddr + AC97_PCM_OUT_R_OFFSET), PCM_DATA);
	  }
	  break;
	  case BOTH_CHANNELS:
	  {
	  		XIo_Out32 ((AC97_Baseaddr + AC97_PCM_OUT_L_OFFSET), PCM_DATA);
	   		XIo_Out32 ((AC97_Baseaddr + AC97_PCM_OUT_R_OFFSET), PCM_DATA);
	  }
	  break;
	  }

	  //wait until the PCM data has to be changed
	  count_skip_frame++;
	  if (count_skip_frame >= frames_to_skip)
	  {
	   PCM_DATA = ~PCM_DATA;
	   count_skip_frame = 0;
	  }
	}

	//set 0 to both PCM channels in order to not let the amplifier close to saturation
	XIo_Out32 ((AC97_Baseaddr + AC97_PCM_OUT_L_OFFSET), 0x00);
    XIo_Out32 ((AC97_Baseaddr + AC97_PCM_OUT_R_OFFSET), 0x00);

	//unmute MIC and LINE-IN inputs if were muted previously
    if (!(line_in_muted))
   		Status = AC97_Unmute (AC97_Baseaddr, AC97_LINE_IN_VOLUME_OFFSET);

    if (!(mic_muted))
    	Status = AC97_Unmute (AC97_Baseaddr, AC97_MIC_VOLUME_OFFSET);

}
Example #3
0
int main()
{
    //init_platform();

    static XGpio pshBtns;
    static XIntc intCtrl;

    Xuint32 Count_Samples;
    Xuint32 Sample_L;
    Xuint32 Sample_R;

    lBtnStateOld = 0x00000000;
    fsRunAction = 0;

	/*
	 *Initialize the driver structs for the Push button and interrupt cores.
	 *This allows the API functions to be used with these cores.
	 */
	XGpio_Initialize(&pshBtns, BTNS_DEVICE_ID);
	XIntc_Initialize(&intCtrl, INTC_DEVICE_ID);

	/*
	 * Connect the function PushBtnHandler to the interrupt controller so that
	 * it is called whenever the Push button GPIO core signals an interrupt.
	 */
	XIntc_Connect(&intCtrl, BTNS_IRPT_ID, PushBtnHandler, &pshBtns);

	/*
	 * Enable interrupts at the interrupt controller
	 */
	XIntc_Enable(&intCtrl, BTNS_IRPT_ID);

	/*
	 * Register the interrupt controller with the microblaze
	 * processor and then start the Interrupt controller so that it begins
	 * listening to the interrupt core for triggers.
	 */
	microblaze_register_handler(XIntc_DeviceInterruptHandler, INTC_DEVICE_ID);
	microblaze_enable_interrupts();
	XIntc_Start(&intCtrl, XIN_REAL_MODE);

	/*
	 * Enable the push button GPIO core to begin sending interrupts to the
	 * interrupt controller in response to changes in the button states
	 */
	XGpio_InterruptEnable(&pshBtns, lBtnChannel);
	XGpio_InterruptGlobalEnable(&pshBtns);

	/*
	 * Wait for AC97 to become ready
	 */
    while (!(AC97_Link_Is_Ready (AC97_BASEADDR)));

    /*
     * Set TAG to configure codec
     */
    AC97_Set_Tag_And_Id (AC97_BASEADDR, 0xF800);

    /*
     * Enable audio output and set volume
     */
    AC97_Unmute (AC97_BASEADDR, AC97_MASTER_VOLUME_OFFSET);
    AC97_Unmute (AC97_BASEADDR, AC97_HEADPHONE_VOLUME_OFFSET);
    AC97_Unmute (AC97_BASEADDR, AC97_PCM_OUT_VOLUME_OFFSET);

    AC97_Set_Volume (AC97_BASEADDR, AC97_MASTER_VOLUME_OFFSET,
       						     BOTH_CHANNELS, VOLUME_MAX);
    AC97_Set_Volume (AC97_BASEADDR, AC97_HEADPHONE_VOLUME_OFFSET,
       						     BOTH_CHANNELS, VOLUME_MIN);
    AC97_Set_Volume (AC97_BASEADDR, AC97_PCM_OUT_VOLUME_OFFSET,
       						     BOTH_CHANNELS, VOLUME_MAX);


    while (1)
    {
    	/**************************
    	 * Play recorded sample
    	 **************************/
    	if (fsRunAction & bitPlay)
    	{
    		/*
			 * Set AC'97 codec TAG to send and receive data in the PCM slots
			 */
			AC97_Set_Tag_And_Id (AC97_BASEADDR, 0x9800);

			Count_Samples =  0;

			Xil_Out32(LED_BASEADDR, bitPlayLED); //turn on LED

			while ((Count_Samples/8) < lNumSamples)
			{
				/*
				 * Block execution until next frame is ready
				 */
				AC97_Wait_For_New_Frame (AC97_BASEADDR);

				//<-------------------------------------------------------------
				// Play 2 different audio clips based on SW7. Use code below to
				// choose where in memory read the audio data from.
				// Also, display the playing time on the UART.
				// Hint: Use the variables "Counter_Examples" and #define lSampleRate


				/*
				 * Read audio data from memory
				 */
				Sample_L = XIo_In32 (pAudioData_0 + Count_Samples);
				Count_Samples = Count_Samples +4;
				Sample_R = XIo_In32 (pAudioData_0 + Count_Samples);
				Count_Samples = Count_Samples +4;


				//<-------------------------------------------------------------

				/*
				 * Send audio data to codec
				 */
				XIo_Out32 ((AC97_BASEADDR + AC97_PCM_OUT_L_OFFSET), Sample_L);
				XIo_Out32 ((AC97_BASEADDR + AC97_PCM_OUT_R_OFFSET), Sample_R);
			}

			Xil_Out32(LED_BASEADDR, 0); //Turn off LED
			fsRunAction = 0; //Forget any button presses that occurred
    	}
    	/**************************
    	 * Output a square wave
    	 **************************/
//    	if (fsRunAction & bitGenWave)
//    	{
//    		//generate square on left, right and then both channels
//    		GenSquare(AC97_BASEADDR, LEFT_CHANNEL, 1000, 500);
//    		GenSquare(AC97_BASEADDR, RIGHT_CHANNEL, 1000, 500);
//    		GenSquare(AC97_BASEADDR, BOTH_CHANNELS, 1000, 500);
//    		fsRunAction = 0;
//    	}
    	/**************************
    	 * Record audio from input
    	 **************************/
    	if (fsRunAction & bitRec)
    	{
    		AC97_Set_Tag_And_Id (AC97_BASEADDR, 0xF800); //Set to configure

    		/*
    		 * Select input source, enable it, and then set the volume
    		 */
    		if (Xil_In32(SW_BASEADDR) & bitSw0)
			{
			   AC97_Select_Input (AC97_BASEADDR, BOTH_CHANNELS,
									   AC97_LINE_IN_SELECT);
			   AC97_Unmute (AC97_BASEADDR, AC97_LINE_IN_VOLUME_OFFSET);
			   AC97_Set_Volume (AC97_BASEADDR, AC97_LINE_IN_VOLUME_OFFSET,
									 BOTH_CHANNELS, VOLUME_MAX);
			}
			else
			{
				AC97_Select_Input (AC97_BASEADDR, BOTH_CHANNELS,
									   AC97_MIC_SELECT);
				AC97_Unmute (AC97_BASEADDR, AC97_MIC_VOLUME_OFFSET);
				AC97_Set_Volume (AC97_BASEADDR, AC97_MIC_VOLUME_OFFSET,
									BOTH_CHANNELS, VOLUME_MID);
			}
			//set record gain
			AC97_Set_Volume (AC97_BASEADDR, AC97_RECORD_GAIN_OFFSET,
									 BOTH_CHANNELS, 0x00);

		   AC97_Set_Tag_And_Id (AC97_BASEADDR, 0x9800); //Set to Send/Receive data

		   Count_Samples =  0;

		   Xil_Out32(LED_BASEADDR, bitRecLED);  //Turn on LED

		   while ((Count_Samples/8) < lNumSamples)
		   {
				 AC97_Wait_For_New_Frame (AC97_BASEADDR);

				/*
				 * Read audio data from codec
				 */
				Sample_L = XIo_In32(AC97_BASEADDR + AC97_PCM_IN_L_OFFSET);
				Sample_R = XIo_In32(AC97_BASEADDR + AC97_PCM_IN_R_OFFSET);

				//<-------------------------------------------------------------
				// Store 2 different audio clips based on SW7. Use code below to
				// choose to store where each audio clip will be stored at.
				// Also, display the recording time on the UART.
				// Hint: Use the variables "Counter_Examples" and #define lSampleRate

				/*
				 * Write audio data to memory
				 */
				XIo_Out32 (pAudioData_0 + Count_Samples, Sample_L);
				Count_Samples = Count_Samples +4;
				XIo_Out32 (pAudioData_0 + Count_Samples, Sample_R);
				Count_Samples = Count_Samples +4;


				//<-------------------------------------------------------------

		   }

			//Set Tag and ID to configure the codec
			AC97_Set_Tag_And_Id (AC97_BASEADDR, 0xF800);

		   /*
		    * Disable the input source
		    */
		   if (Xil_In32(SW_BASEADDR) & bitSw0)
		   {
			  AC97_Mute (AC97_BASEADDR, AC97_LINE_IN_VOLUME_OFFSET);
		   }
		   else
		   {
			  AC97_Mute (AC97_BASEADDR, AC97_MIC_VOLUME_OFFSET);
		   }

		   Xil_Out32(LED_BASEADDR, 0); //Turn off LED
    	   fsRunAction = 0;
    	}

    }

    //cleanup_platform();

    return 0;
}