usbmain.c The main file of the usb reverse audio code
Page last updated 28 Sep 2011, by
0
replies
.
/*---------------------------------------------------------------------------- * Name: usbmain.c * Purpose: USB Audio Class Demo * Version: V1.20 *---------------------------------------------------------------------------- * This software is supplied "AS IS" without any warranties, express, * implied or statutory, including but not limited to the implied * warranties of fitness for purpose, satisfactory quality and * noninfringement. Keil extends you a royalty-free right to reproduce * and distribute executable files created using this software for use * on NXP Semiconductors LPC microcontroller devices only. Nothing else * gives you the right to use this software. * * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. *---------------------------------------------------------------------------*/ #include "LPC17xx.h" /* LPC17xx definitions */ #include "type.h" #include "usb.h" #include "usbcfg.h" #include "usbhw.h" #include "usbcore.h" #include "usbaudio.h" uint8_t Mute; /* Mute State */ uint32_t Volume; /* Volume Level */ #if USB_DMA uint32_t *InfoBuf = (uint32_t *)(DMA_BUF_ADR); short *DataBuf = (short *)(DMA_BUF_ADR + 4*P_C); #else uint32_t InfoBuf[P_C]; short DataBuf[B_S]; /* Data Buffer */ #endif uint16_t DataOut; /* Data Out Index */ uint16_t DataIn; /* Data In Index */ uint8_t DataRun; /* Data Stream Run State */ uint16_t PotVal; /* Potenciometer Value */ uint32_t VUM; /* VU Meter */ uint32_t Tick; /* Time Tick */ uint32_t arg; //uint32_t con; //uint32_t power; #define USR_POWERDOWN (0x104) int semihost_powerdown() { uint32_t arg; return __semihost(USR_POWERDOWN, &arg); } /* * Get Potenciometer Value */ //void get_potval (void) { //uint32_t val; //LPC_ADC->ADCR |= 0x01000000; /* Start A/D Conversion */ //do { //val = LPC_ADC->ADGDR; /* Read A/D Data Register */ //} while ((val & 0x80000000) == 0); /* Wait for end of A/D Conversion */ //LPC_ADC->ADCR &= ~0x01000000; /* Stop A/D Conversion */ //PotVal = ((val >> 8) & 0xF8) + /* Extract Potenciometer Value */ // ((val >> 7) & 0x08); //} /* * Timer Counter 0 Interrupt Service Routine * executed each 31.25us (32kHz frequency) */ void TIMER0_IRQHandler(void) { long val; uint32_t cnt; unsigned short PlaySample; if (DataRun) { /* Data Stream is running */ val = DataBuf[DataOut]; /* Get Audio Sample */ cnt = (DataIn - DataOut) & (B_S - 1); /* Buffer Data Count */ if (cnt == (B_S - P_C*P_S)) { /* Too much Data in Buffer */ DataOut++; /* Skip one Sample */ } if (cnt > (P_C*P_S)) { /* Still enough Data in Buffer */ DataOut++; /* Update Data Out Index */ } DataOut &= B_S - 1; /* Adjust Buffer Out Index */ //if (val < 0) VUM -= val; /* Accumulate Neg Value */ //else VUM += val; /* Accumulate Pos Value */ val *= Volume; /* Apply Volume Level */ val >>= 6; /* Adjust Value */ val += 0x8000; /* Add Bias */ val &= 0xFFFF; /* Mask Value */ } else { val = 0x8000; /* DAC Middle Point */ } if (Mute) { val = 0x8000; /* DAC Middle Point */ } //if (Mute){ //semihost_powerdown();} /* ADAMGR: Inserting my May 30th reversing code here!!! */ { static const unsigned int BufferSize = 1920 * 8 ; static unsigned short Buffer[BufferSize]; static int Index = 0; static int Direction = 1; static int Playback = FALSE; static int ChunkSize = BufferSize; unsigned short ReadSample; /* Default PlaySample to the current sample from USB buffer. */ PlaySample = val; /* Read out the sample from the buffer to be played back */ if (Playback) { PlaySample = Buffer[Index]; } /* Obtain current audio sample from the USB buffer. */ ReadSample = (unsigned short)val; /* Record the sample into the buffer right where a space was freed up from the PlaySample read above */ Buffer[Index] = ReadSample; /* Increment the buffer pointer */ Index += Direction; /* Check to see if the chunk has been filled */ if (Index < 0) { /* Now have a chunk to be played back */ Playback = TRUE; /* Reverse the direction of playback and recording */ Direction *= -1;//Direction; Index = 0; } else if (Index >= ChunkSize) { /* Now have a chunk to be played back */ Playback = TRUE; /* Reverse the direction of playback and recording */ Direction *= -1;//Direction; Index = ChunkSize - 1; } } LPC_DAC->DACR = PlaySample & 0xFFC0; /* Set Speaker Output */ //if ((Tick++ & 0x03FF) == 0) { /* On every 1024th Tick */ //get_potval(); /* Get Potenciometer Value */ if (VolCur == 0x8000) { /* Check for Minimum Level */ Volume = 0; /* No Sound */ } else { Volume = VolCur ;//* PotVal; /* Chained Volume Level */ } //val = VUM >> 20; /* Scale Accumulated Value */ //VUM = 0; /* Clear VUM */ //if (val > 7) val = 7; /* Limit Value */ LPC_TIM0->IR = 1; /* Clear Interrupt Flag */ } /***************************************************************************** ** Main Function main() ******************************************************************************/ int main (void) { volatile uint32_t pclkdiv, pclk; semihost_powerdown(); SystemInit(); LPC_PINCON->PINSEL1 &=~((0x03<<20)); /* P0.25, A0.0, function 01, P0.26 AOUT, function 10 */ LPC_PINCON->PINSEL1 |= ((0x02<<20)); /* Enable CLOCK into ADC controller */ LPC_SC->PCONP |= (1 << 12); LPC_ADC->ADCR = 0x00200E04; /* ADC: 10-bit AIN2 @ 4MHz */ LPC_DAC->DACR = 0x00008000; /* DAC Output set to Middle Point */ /* By default, the PCLKSELx value is zero, thus, the PCLK for all the peripherals is 1/4 of the SystemFrequency. */ /* Bit 2~3 is for TIMER0 */ pclkdiv = (LPC_SC->PCLKSEL0 >> 2) & 0x03; switch ( pclkdiv ) { case 0x00: default: pclk = SystemFrequency/4; break; case 0x01: pclk = SystemFrequency; break; case 0x02: pclk = SystemFrequency/2; break; case 0x03: pclk = SystemFrequency/8; break; } LPC_TIM0->MR0 = pclk/DATA_FREQ; /* TC0 Match Value 0 */ LPC_TIM0->MCR = 3; /* TCO Interrupt and Reset on MR0 */ LPC_TIM0->TCR = 1; /* TC0 Enable */ NVIC_EnableIRQ(TIMER0_IRQn); USB_Init(); /* USB Initialization */ USB_Connect(TRUE); /* USB Connect */ /********* The main Function is an endless loop ***********/ while( 1 ); } int result; /****************************************************************************** ** End Of File ******************************************************************************/
Please log in to post comments.