Guest User

Untitled

a guest
Sep 15th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1.  
  2. /*----------------------------------------------------------------------------
  3. * Name: usbmain.c
  4. * Purpose: USB Audio Class Demo
  5. * Version: V1.20
  6. *----------------------------------------------------------------------------
  7. * This software is supplied "AS IS" without any warranties, express,
  8. * implied or statutory, including but not limited to the implied
  9. * warranties of fitness for purpose, satisfactory quality and
  10. * noninfringement. Keil extends you a royalty-free right to reproduce
  11. * and distribute executable files created using this software for use
  12. * on NXP Semiconductors LPC microcontroller devices only. Nothing else
  13. * gives you the right to use this software.
  14. *
  15. * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
  16. *---------------------------------------------------------------------------*/
  17.  
  18. #include "LPC17xx.h" /* LPC17xx definitions */
  19. #include "type.h"
  20.  
  21. #include "usb.h"
  22. #include "usbcfg.h"
  23. #include "usbhw.h"
  24. #include "usbcore.h"
  25. #include "usbaudio.h"
  26.  
  27.  
  28.  
  29.  
  30. uint8_t Mute; /* Mute State */
  31. uint32_t Volume; /* Volume Level */
  32.  
  33. #if USB_DMA
  34. uint32_t *InfoBuf = (uint32_t *)(DMA_BUF_ADR);
  35. short *DataBuf = (short *)(DMA_BUF_ADR + 4*P_C);
  36. #else
  37. uint32_t InfoBuf[P_C];
  38. short DataBuf[B_S]; /* Data Buffer */
  39. #endif
  40.  
  41. uint16_t DataOut; /* Data Out Index */
  42. uint16_t DataIn; /* Data In Index */
  43.  
  44. uint8_t DataRun; /* Data Stream Run State */
  45. uint16_t PotVal; /* Potenciometer Value */
  46. uint32_t VUM; /* VU Meter */
  47. uint32_t Tick; /* Time Tick */
  48. uint32_t arg;
  49. //uint32_t con;
  50. //uint32_t power;
  51. #define USR_POWERDOWN (0x104)
  52.  
  53. int semihost_powerdown() {
  54. uint32_t arg;
  55. return __semihost(USR_POWERDOWN, &arg);
  56. }
  57.  
  58.  
  59. /*
  60. * Get Potenciometer Value
  61. */
  62.  
  63. //void get_potval (void) {
  64. //uint32_t val;
  65.  
  66. //LPC_ADC->ADCR |= 0x01000000; /* Start A/D Conversion */
  67. //do {
  68. //val = LPC_ADC->ADGDR; /* Read A/D Data Register */
  69. //} while ((val & 0x80000000) == 0); /* Wait for end of A/D Conversion */
  70. //LPC_ADC->ADCR &= ~0x01000000; /* Stop A/D Conversion */
  71. //PotVal = ((val >> 8) & 0xF8) + /* Extract Potenciometer Value */
  72. // ((val >> 7) & 0x08);
  73. //}
  74.  
  75.  
  76. /*
  77. * Timer Counter 0 Interrupt Service Routine
  78. * executed each 31.25us (32kHz frequency)
  79. */
  80.  
  81. void TIMER0_IRQHandler(void)
  82. {
  83. long val;
  84. uint32_t cnt;
  85. unsigned short PlaySample;
  86.  
  87. if (DataRun) { /* Data Stream is running */
  88. val = DataBuf[DataOut]; /* Get Audio Sample */
  89. cnt = (DataIn - DataOut) & (B_S - 1); /* Buffer Data Count */
  90. if (cnt == (B_S - P_C*P_S)) { /* Too much Data in Buffer */
  91. DataOut++; /* Skip one Sample */
  92. }
  93. if (cnt > (P_C*P_S)) { /* Still enough Data in Buffer */
  94. DataOut++; /* Update Data Out Index */
  95. }
  96. DataOut &= B_S - 1; /* Adjust Buffer Out Index */
  97. //if (val < 0) VUM -= val; /* Accumulate Neg Value */
  98. //else VUM += val; /* Accumulate Pos Value */
  99. val *= Volume; /* Apply Volume Level */
  100. val >>= 6; /* Adjust Value */
  101. val += 0x8000; /* Add Bias */
  102. val &= 0xFFFF; /* Mask Value */
  103. } else {
  104. val = 0x8000; /* DAC Middle Point */
  105. }
  106.  
  107. if (Mute) {
  108. val = 0x8000; /* DAC Middle Point */
  109. }
  110. //if (Mute){
  111. //semihost_powerdown();}
  112.  
  113. /* ADAMGR: Inserting my May 30th reversing code here!!! */
  114. {
  115. static const unsigned int BufferSize = 15 * 960 ;
  116. static unsigned short Buffer[BufferSize];
  117.  
  118. static int Index = 0;
  119. static int Direction = 1;
  120. static int Playback = FALSE;
  121. static int ChunkSize = BufferSize;
  122.  
  123. unsigned short ReadSample;
  124.  
  125. /* Default PlaySample to the current sample from USB buffer. */
  126. PlaySample = val;
  127.  
  128. //wait_us(50);
  129.  
  130. /* Read out the sample from the buffer to be played back */
  131. if (Playback)
  132. {
  133. PlaySample = Buffer[Index];
  134. }
  135.  
  136. /* Obtain current audio sample from the USB buffer. */
  137. ReadSample = (unsigned short)val;
  138.  
  139. /* Record the sample into the buffer right where a space was freed up from the PlaySample read above */
  140. Buffer[Index] = ReadSample;
  141.  
  142. /* Increment the buffer pointer */
  143. Index += Direction;
  144.  
  145. /* Check to see if the chunk has been filled */
  146. if (Index < 0)
  147. {
  148. /* Now have a chunk to be played back */
  149. Playback = TRUE;
  150. /* Reverse the direction of playback and recording */
  151. Direction *= -1;//Direction;
  152. Index = 0;
  153. }
  154. else if (Index >= ChunkSize)
  155.  
  156.  
  157.  
  158. {
  159. /* Now have a chunk to be played back */
  160. Playback = TRUE;
  161. /* Reverse the direction of playback and recording */
  162. Direction *= -1;//Direction;
  163. Index = ChunkSize - 1;
  164. }
  165. }
  166.  
  167. LPC_DAC->DACR = PlaySample & 0xFFC0; /* Set Speaker Output */
  168.  
  169. //if ((Tick++ & 0x03FF) == 0) { /* On every 1024th Tick */
  170. //get_potval(); /* Get Potenciometer Value */
  171. if (VolCur == 0x8000) { /* Check for Minimum Level */
  172. Volume = 0; /* No Sound */
  173. } else {
  174. Volume = VolCur ;//* PotVal; /* Chained Volume Level */
  175. }
  176. //val = VUM >> 20; /* Scale Accumulated Value */
  177. //VUM = 0; /* Clear VUM */
  178. //if (val > 7) val = 7; /* Limit Value */
  179.  
  180.  
  181. LPC_TIM0->IR = 1; /* Clear Interrupt Flag */
  182.  
  183.  
  184. }
  185.  
  186. /*****************************************************************************
  187. ** Main Function main()
  188. ******************************************************************************/
  189. int main (void)
  190. {
  191. volatile uint32_t pclkdiv, pclk;
  192.  
  193. semihost_powerdown();
  194.  
  195. SystemInit();
  196.  
  197. LPC_PINCON->PINSEL1 &=~((0x03<<20));
  198. /* P0.25, A0.0, function 01, P0.26 AOUT, function 10 */
  199. LPC_PINCON->PINSEL1 |= ((0x02<<20));
  200.  
  201. /* Enable CLOCK into ADC controller */
  202. LPC_SC->PCONP |= (1 << 12);
  203.  
  204. LPC_ADC->ADCR = 0x00200E04; /* ADC: 10-bit AIN2 @ 4MHz */
  205. LPC_DAC->DACR = 0x00008000; /* DAC Output set to Middle Point */
  206.  
  207. /* By default, the PCLKSELx value is zero, thus, the PCLK for
  208. all the peripherals is 1/4 of the SystemFrequency. */
  209. /* Bit 2~3 is for TIMER0 */
  210. pclkdiv = (LPC_SC->PCLKSEL0 >> 2) & 0x03;
  211. switch ( pclkdiv )
  212. {
  213. case 0x00:
  214. default:
  215. pclk = SystemFrequency/4;
  216. break;
  217. case 0x01:
  218. pclk = SystemFrequency;
  219. break;
  220. case 0x02:
  221. pclk = SystemFrequency/2;
  222. break;
  223. case 0x03:
  224. pclk = SystemFrequency/8;
  225. break;
  226. }
  227.  
  228.  
  229. LPC_TIM0->MR0 = pclk/DATA_FREQ - 1; /* TC0 Match Value 0 */
  230. LPC_TIM0->MCR = 3; /* TCO Interrupt and Reset on MR0 */
  231. LPC_TIM0->TCR = 1; /* TC0 Enable */
  232. NVIC_EnableIRQ(TIMER0_IRQn);
  233.  
  234. USB_Init(); /* USB Initialization */
  235. USB_Connect(TRUE); /* USB Connect */
  236.  
  237. /********* The main Function is an endless loop ***********/
  238.  
  239. while( 1 );
  240.  
  241.  
  242.  
  243.  
  244. }
  245. int result;
  246.  
  247.  
  248. /******************************************************************************
  249. ** End Of File
  250. ******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment