These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbdmain.c Source File

usbdmain.c

00001 /*----------------------------------------------------------------------------
00002  * Name:    usbmain.c
00003  * Purpose: USB Audio Class Demo
00004  * Version: V1.20
00005  *----------------------------------------------------------------------------
00006  *      This software is supplied "AS IS" without any warranties, express,
00007  *      implied or statutory, including but not limited to the implied
00008  *      warranties of fitness for purpose, satisfactory quality and
00009  *      noninfringement. Keil extends you a royalty-free right to reproduce
00010  *      and distribute executable files created using this software for use
00011  *      on NXP Semiconductors LPC microcontroller devices only. Nothing else
00012  *      gives you the right to use this software.
00013  *
00014  * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
00015  *---------------------------------------------------------------------------*/
00016 
00017 #include "LPC17xx.h"                        /* LPC17xx definitions */
00018 #include "lpc_types.h"
00019 
00020 #include "usb.h"
00021 #include "usbcfg.h"
00022 #include "usbhw.h"
00023 #include "usbcore.h"
00024 #include "usbaudio.h"
00025 
00026 /* Example group ----------------------------------------------------------- */
00027 /** @defgroup USBDEV_USBAudio   USBAudio
00028  * @ingroup USBDEV_Examples
00029  * @{
00030  */
00031 uint8_t  Mute;                                 /* Mute State */
00032 uint32_t Volume;                               /* Volume Level */
00033 
00034 #if USB_DMA
00035 uint32_t *InfoBuf = (uint32_t *)(DMA_BUF_ADR);
00036 short *DataBuf = (short *)(DMA_BUF_ADR + 4*P_C);
00037 #else
00038 uint32_t InfoBuf[P_C];
00039 short DataBuf[B_S];                         /* Data Buffer */
00040 #endif
00041 
00042 uint16_t  DataOut;                              /* Data Out Index */
00043 uint16_t  DataIn;                               /* Data In Index */
00044 
00045 uint8_t   DataRun;                              /* Data Stream Run State */
00046 uint16_t  PotVal;                               /* Potenciometer Value */
00047 uint32_t  VUM;                                  /* VU Meter */
00048 uint32_t  Tick;                                 /* Time Tick */
00049 
00050 
00051 /*
00052  * Get Potenciometer Value
00053  */
00054 
00055 void get_potval (void) {
00056   uint32_t val;
00057 
00058   LPC_ADC->ADCR |= 0x01000000;              /* Start A/D Conversion */
00059   do {
00060     val = LPC_ADC->ADGDR;                   /* Read A/D Data Register */
00061   } while ((val & 0x80000000) == 0);        /* Wait for end of A/D Conversion */
00062   LPC_ADC->ADCR &= ~0x01000000;             /* Stop A/D Conversion */
00063   PotVal = ((val >> 8) & 0xF8) +            /* Extract Potenciometer Value */
00064            ((val >> 7) & 0x08);
00065 }
00066 
00067 
00068 /*
00069  * Timer Counter 0 Interrupt Service Routine
00070  *   executed each 31.25us (32kHz frequency)
00071  */
00072 
00073 void TIMER0_IRQHandler(void)
00074 {
00075   long  val;
00076   uint32_t cnt;
00077 
00078   if (DataRun) {                            /* Data Stream is running */
00079     val = DataBuf[DataOut];                 /* Get Audio Sample */
00080     cnt = (DataIn - DataOut) & (B_S - 1);   /* Buffer Data Count */
00081     if (cnt == (B_S - P_C*P_S)) {           /* Too much Data in Buffer */
00082       DataOut++;                            /* Skip one Sample */
00083     }
00084     if (cnt > (P_C*P_S)) {                  /* Still enough Data in Buffer */
00085       DataOut++;                            /* Update Data Out Index */
00086     }
00087     DataOut &= B_S - 1;                     /* Adjust Buffer Out Index */
00088     if (val < 0) VUM -= val;                /* Accumulate Neg Value */
00089     else         VUM += val;                /* Accumulate Pos Value */
00090     val  *= Volume;                         /* Apply Volume Level */
00091     val >>= 16;                             /* Adjust Value */
00092     val  += 0x8000;                         /* Add Bias */
00093     val  &= 0xFFFF;                         /* Mask Value */
00094   } else {
00095     val = 0x8000;                           /* DAC Middle Point */
00096   }
00097 
00098   if (Mute) {
00099     val = 0x8000;                           /* DAC Middle Point */
00100   }
00101 
00102   LPC_DAC->DACR = val & 0xFFC0;             /* Set Speaker Output */
00103 
00104   if ((Tick++ & 0x03FF) == 0) {             /* On every 1024th Tick */
00105     get_potval();                           /* Get Potenciometer Value */
00106     if (VolCur == 0x8000) {                 /* Check for Minimum Level */
00107       Volume = 0;                           /* No Sound */
00108     } else {
00109       Volume = VolCur * PotVal;             /* Chained Volume Level */
00110     }
00111     val = VUM >> 20;                        /* Scale Accumulated Value */
00112     VUM = 0;                                /* Clear VUM */
00113     if (val > 7) val = 7;                   /* Limit Value */
00114   }
00115 
00116   LPC_TIM0->IR = 1;                         /* Clear Interrupt Flag */
00117 }
00118 
00119 
00120 
00121 /*****************************************************************************
00122 **   Main Function  main()
00123 ******************************************************************************/
00124 int main (void)
00125 {
00126   volatile uint32_t pclkdiv, pclk;
00127 
00128 //  SystemInit();
00129 
00130   LPC_PINCON->PINSEL1 &=~((0x03<<18)|(0x03<<20));
00131   /* P0.25, A0.0, function 01, P0.26 AOUT, function 10 */
00132   LPC_PINCON->PINSEL1 |= ((0x01<<18)|(0x02<<20));
00133 
00134   /* Enable CLOCK into ADC controller */
00135   LPC_SC->PCONP |= (1 << 12);
00136 
00137   LPC_ADC->ADCR = 0x00200E04;       /* ADC: 10-bit AIN2 @ 4MHz */
00138   LPC_DAC->DACR = 0x00008000;       /* DAC Output set to Middle Point */
00139 
00140   /* By default, the PCLKSELx value is zero, thus, the PCLK for
00141   all the peripherals is 1/4 of the SystemFrequency. */
00142   /* Bit 2~3 is for TIMER0 */
00143   pclkdiv = (LPC_SC->PCLKSEL0 >> 2) & 0x03;
00144   switch ( pclkdiv )
00145   {
00146     case 0x00:
00147     default:
00148       pclk = SystemCoreClock/4;
00149     break;
00150     case 0x01:
00151       pclk = SystemCoreClock;
00152     break;
00153     case 0x02:
00154       pclk = SystemCoreClock/2;
00155     break;
00156     case 0x03:
00157       pclk = SystemCoreClock/8;
00158     break;
00159   }
00160 
00161   LPC_TIM0->MR0 = pclk/DATA_FREQ - 1;   /* TC0 Match Value 0 */
00162   LPC_TIM0->MCR = 3;                    /* TCO Interrupt and Reset on MR0 */
00163   LPC_TIM0->TCR = 1;                    /* TC0 Enable */
00164   NVIC_EnableIRQ(TIMER0_IRQn);
00165 
00166   USB_Init();               /* USB Initialization */
00167   USB_Connect(TRUE);        /* USB Connect */
00168 
00169   /********* The main Function is an endless loop ***********/
00170   while( 1 );
00171 }
00172 
00173 /******************************************************************************
00174 **                            End Of File
00175 ******************************************************************************/
00176 /*
00177  * @}
00178  */