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

Committer:
frank26080115
Date:
Sun Mar 20 05:38:56 2011 +0000
Revision:
0:bf7b9fba3924

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frank26080115 0:bf7b9fba3924 1 /*----------------------------------------------------------------------------
frank26080115 0:bf7b9fba3924 2 * Name: usbmain.c
frank26080115 0:bf7b9fba3924 3 * Purpose: USB Audio Class Demo
frank26080115 0:bf7b9fba3924 4 * Version: V1.20
frank26080115 0:bf7b9fba3924 5 *----------------------------------------------------------------------------
frank26080115 0:bf7b9fba3924 6 * This software is supplied "AS IS" without any warranties, express,
frank26080115 0:bf7b9fba3924 7 * implied or statutory, including but not limited to the implied
frank26080115 0:bf7b9fba3924 8 * warranties of fitness for purpose, satisfactory quality and
frank26080115 0:bf7b9fba3924 9 * noninfringement. Keil extends you a royalty-free right to reproduce
frank26080115 0:bf7b9fba3924 10 * and distribute executable files created using this software for use
frank26080115 0:bf7b9fba3924 11 * on NXP Semiconductors LPC microcontroller devices only. Nothing else
frank26080115 0:bf7b9fba3924 12 * gives you the right to use this software.
frank26080115 0:bf7b9fba3924 13 *
frank26080115 0:bf7b9fba3924 14 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
frank26080115 0:bf7b9fba3924 15 *---------------------------------------------------------------------------*/
frank26080115 0:bf7b9fba3924 16
frank26080115 0:bf7b9fba3924 17 #include "LPC17xx.h" /* LPC17xx definitions */
frank26080115 0:bf7b9fba3924 18 #include "lpc_types.h"
frank26080115 0:bf7b9fba3924 19
frank26080115 0:bf7b9fba3924 20 #include "usb.h"
frank26080115 0:bf7b9fba3924 21 #include "usbcfg.h"
frank26080115 0:bf7b9fba3924 22 #include "usbhw.h"
frank26080115 0:bf7b9fba3924 23 #include "usbcore.h"
frank26080115 0:bf7b9fba3924 24 #include "usbaudio.h"
frank26080115 0:bf7b9fba3924 25
frank26080115 0:bf7b9fba3924 26 /* Example group ----------------------------------------------------------- */
frank26080115 0:bf7b9fba3924 27 /** @defgroup USBDEV_USBAudio USBAudio
frank26080115 0:bf7b9fba3924 28 * @ingroup USBDEV_Examples
frank26080115 0:bf7b9fba3924 29 * @{
frank26080115 0:bf7b9fba3924 30 */
frank26080115 0:bf7b9fba3924 31 uint8_t Mute; /* Mute State */
frank26080115 0:bf7b9fba3924 32 uint32_t Volume; /* Volume Level */
frank26080115 0:bf7b9fba3924 33
frank26080115 0:bf7b9fba3924 34 #if USB_DMA
frank26080115 0:bf7b9fba3924 35 uint32_t *InfoBuf = (uint32_t *)(DMA_BUF_ADR);
frank26080115 0:bf7b9fba3924 36 short *DataBuf = (short *)(DMA_BUF_ADR + 4*P_C);
frank26080115 0:bf7b9fba3924 37 #else
frank26080115 0:bf7b9fba3924 38 uint32_t InfoBuf[P_C];
frank26080115 0:bf7b9fba3924 39 short DataBuf[B_S]; /* Data Buffer */
frank26080115 0:bf7b9fba3924 40 #endif
frank26080115 0:bf7b9fba3924 41
frank26080115 0:bf7b9fba3924 42 uint16_t DataOut; /* Data Out Index */
frank26080115 0:bf7b9fba3924 43 uint16_t DataIn; /* Data In Index */
frank26080115 0:bf7b9fba3924 44
frank26080115 0:bf7b9fba3924 45 uint8_t DataRun; /* Data Stream Run State */
frank26080115 0:bf7b9fba3924 46 uint16_t PotVal; /* Potenciometer Value */
frank26080115 0:bf7b9fba3924 47 uint32_t VUM; /* VU Meter */
frank26080115 0:bf7b9fba3924 48 uint32_t Tick; /* Time Tick */
frank26080115 0:bf7b9fba3924 49
frank26080115 0:bf7b9fba3924 50
frank26080115 0:bf7b9fba3924 51 /*
frank26080115 0:bf7b9fba3924 52 * Get Potenciometer Value
frank26080115 0:bf7b9fba3924 53 */
frank26080115 0:bf7b9fba3924 54
frank26080115 0:bf7b9fba3924 55 void get_potval (void) {
frank26080115 0:bf7b9fba3924 56 uint32_t val;
frank26080115 0:bf7b9fba3924 57
frank26080115 0:bf7b9fba3924 58 LPC_ADC->ADCR |= 0x01000000; /* Start A/D Conversion */
frank26080115 0:bf7b9fba3924 59 do {
frank26080115 0:bf7b9fba3924 60 val = LPC_ADC->ADGDR; /* Read A/D Data Register */
frank26080115 0:bf7b9fba3924 61 } while ((val & 0x80000000) == 0); /* Wait for end of A/D Conversion */
frank26080115 0:bf7b9fba3924 62 LPC_ADC->ADCR &= ~0x01000000; /* Stop A/D Conversion */
frank26080115 0:bf7b9fba3924 63 PotVal = ((val >> 8) & 0xF8) + /* Extract Potenciometer Value */
frank26080115 0:bf7b9fba3924 64 ((val >> 7) & 0x08);
frank26080115 0:bf7b9fba3924 65 }
frank26080115 0:bf7b9fba3924 66
frank26080115 0:bf7b9fba3924 67
frank26080115 0:bf7b9fba3924 68 /*
frank26080115 0:bf7b9fba3924 69 * Timer Counter 0 Interrupt Service Routine
frank26080115 0:bf7b9fba3924 70 * executed each 31.25us (32kHz frequency)
frank26080115 0:bf7b9fba3924 71 */
frank26080115 0:bf7b9fba3924 72
frank26080115 0:bf7b9fba3924 73 void TIMER0_IRQHandler(void)
frank26080115 0:bf7b9fba3924 74 {
frank26080115 0:bf7b9fba3924 75 long val;
frank26080115 0:bf7b9fba3924 76 uint32_t cnt;
frank26080115 0:bf7b9fba3924 77
frank26080115 0:bf7b9fba3924 78 if (DataRun) { /* Data Stream is running */
frank26080115 0:bf7b9fba3924 79 val = DataBuf[DataOut]; /* Get Audio Sample */
frank26080115 0:bf7b9fba3924 80 cnt = (DataIn - DataOut) & (B_S - 1); /* Buffer Data Count */
frank26080115 0:bf7b9fba3924 81 if (cnt == (B_S - P_C*P_S)) { /* Too much Data in Buffer */
frank26080115 0:bf7b9fba3924 82 DataOut++; /* Skip one Sample */
frank26080115 0:bf7b9fba3924 83 }
frank26080115 0:bf7b9fba3924 84 if (cnt > (P_C*P_S)) { /* Still enough Data in Buffer */
frank26080115 0:bf7b9fba3924 85 DataOut++; /* Update Data Out Index */
frank26080115 0:bf7b9fba3924 86 }
frank26080115 0:bf7b9fba3924 87 DataOut &= B_S - 1; /* Adjust Buffer Out Index */
frank26080115 0:bf7b9fba3924 88 if (val < 0) VUM -= val; /* Accumulate Neg Value */
frank26080115 0:bf7b9fba3924 89 else VUM += val; /* Accumulate Pos Value */
frank26080115 0:bf7b9fba3924 90 val *= Volume; /* Apply Volume Level */
frank26080115 0:bf7b9fba3924 91 val >>= 16; /* Adjust Value */
frank26080115 0:bf7b9fba3924 92 val += 0x8000; /* Add Bias */
frank26080115 0:bf7b9fba3924 93 val &= 0xFFFF; /* Mask Value */
frank26080115 0:bf7b9fba3924 94 } else {
frank26080115 0:bf7b9fba3924 95 val = 0x8000; /* DAC Middle Point */
frank26080115 0:bf7b9fba3924 96 }
frank26080115 0:bf7b9fba3924 97
frank26080115 0:bf7b9fba3924 98 if (Mute) {
frank26080115 0:bf7b9fba3924 99 val = 0x8000; /* DAC Middle Point */
frank26080115 0:bf7b9fba3924 100 }
frank26080115 0:bf7b9fba3924 101
frank26080115 0:bf7b9fba3924 102 LPC_DAC->DACR = val & 0xFFC0; /* Set Speaker Output */
frank26080115 0:bf7b9fba3924 103
frank26080115 0:bf7b9fba3924 104 if ((Tick++ & 0x03FF) == 0) { /* On every 1024th Tick */
frank26080115 0:bf7b9fba3924 105 get_potval(); /* Get Potenciometer Value */
frank26080115 0:bf7b9fba3924 106 if (VolCur == 0x8000) { /* Check for Minimum Level */
frank26080115 0:bf7b9fba3924 107 Volume = 0; /* No Sound */
frank26080115 0:bf7b9fba3924 108 } else {
frank26080115 0:bf7b9fba3924 109 Volume = VolCur * PotVal; /* Chained Volume Level */
frank26080115 0:bf7b9fba3924 110 }
frank26080115 0:bf7b9fba3924 111 val = VUM >> 20; /* Scale Accumulated Value */
frank26080115 0:bf7b9fba3924 112 VUM = 0; /* Clear VUM */
frank26080115 0:bf7b9fba3924 113 if (val > 7) val = 7; /* Limit Value */
frank26080115 0:bf7b9fba3924 114 }
frank26080115 0:bf7b9fba3924 115
frank26080115 0:bf7b9fba3924 116 LPC_TIM0->IR = 1; /* Clear Interrupt Flag */
frank26080115 0:bf7b9fba3924 117 }
frank26080115 0:bf7b9fba3924 118
frank26080115 0:bf7b9fba3924 119
frank26080115 0:bf7b9fba3924 120
frank26080115 0:bf7b9fba3924 121 /*****************************************************************************
frank26080115 0:bf7b9fba3924 122 ** Main Function main()
frank26080115 0:bf7b9fba3924 123 ******************************************************************************/
frank26080115 0:bf7b9fba3924 124 int main (void)
frank26080115 0:bf7b9fba3924 125 {
frank26080115 0:bf7b9fba3924 126 volatile uint32_t pclkdiv, pclk;
frank26080115 0:bf7b9fba3924 127
frank26080115 0:bf7b9fba3924 128 // SystemInit();
frank26080115 0:bf7b9fba3924 129
frank26080115 0:bf7b9fba3924 130 LPC_PINCON->PINSEL1 &=~((0x03<<18)|(0x03<<20));
frank26080115 0:bf7b9fba3924 131 /* P0.25, A0.0, function 01, P0.26 AOUT, function 10 */
frank26080115 0:bf7b9fba3924 132 LPC_PINCON->PINSEL1 |= ((0x01<<18)|(0x02<<20));
frank26080115 0:bf7b9fba3924 133
frank26080115 0:bf7b9fba3924 134 /* Enable CLOCK into ADC controller */
frank26080115 0:bf7b9fba3924 135 LPC_SC->PCONP |= (1 << 12);
frank26080115 0:bf7b9fba3924 136
frank26080115 0:bf7b9fba3924 137 LPC_ADC->ADCR = 0x00200E04; /* ADC: 10-bit AIN2 @ 4MHz */
frank26080115 0:bf7b9fba3924 138 LPC_DAC->DACR = 0x00008000; /* DAC Output set to Middle Point */
frank26080115 0:bf7b9fba3924 139
frank26080115 0:bf7b9fba3924 140 /* By default, the PCLKSELx value is zero, thus, the PCLK for
frank26080115 0:bf7b9fba3924 141 all the peripherals is 1/4 of the SystemFrequency. */
frank26080115 0:bf7b9fba3924 142 /* Bit 2~3 is for TIMER0 */
frank26080115 0:bf7b9fba3924 143 pclkdiv = (LPC_SC->PCLKSEL0 >> 2) & 0x03;
frank26080115 0:bf7b9fba3924 144 switch ( pclkdiv )
frank26080115 0:bf7b9fba3924 145 {
frank26080115 0:bf7b9fba3924 146 case 0x00:
frank26080115 0:bf7b9fba3924 147 default:
frank26080115 0:bf7b9fba3924 148 pclk = SystemCoreClock/4;
frank26080115 0:bf7b9fba3924 149 break;
frank26080115 0:bf7b9fba3924 150 case 0x01:
frank26080115 0:bf7b9fba3924 151 pclk = SystemCoreClock;
frank26080115 0:bf7b9fba3924 152 break;
frank26080115 0:bf7b9fba3924 153 case 0x02:
frank26080115 0:bf7b9fba3924 154 pclk = SystemCoreClock/2;
frank26080115 0:bf7b9fba3924 155 break;
frank26080115 0:bf7b9fba3924 156 case 0x03:
frank26080115 0:bf7b9fba3924 157 pclk = SystemCoreClock/8;
frank26080115 0:bf7b9fba3924 158 break;
frank26080115 0:bf7b9fba3924 159 }
frank26080115 0:bf7b9fba3924 160
frank26080115 0:bf7b9fba3924 161 LPC_TIM0->MR0 = pclk/DATA_FREQ - 1; /* TC0 Match Value 0 */
frank26080115 0:bf7b9fba3924 162 LPC_TIM0->MCR = 3; /* TCO Interrupt and Reset on MR0 */
frank26080115 0:bf7b9fba3924 163 LPC_TIM0->TCR = 1; /* TC0 Enable */
frank26080115 0:bf7b9fba3924 164 NVIC_EnableIRQ(TIMER0_IRQn);
frank26080115 0:bf7b9fba3924 165
frank26080115 0:bf7b9fba3924 166 USB_Init(); /* USB Initialization */
frank26080115 0:bf7b9fba3924 167 USB_Connect(TRUE); /* USB Connect */
frank26080115 0:bf7b9fba3924 168
frank26080115 0:bf7b9fba3924 169 /********* The main Function is an endless loop ***********/
frank26080115 0:bf7b9fba3924 170 while( 1 );
frank26080115 0:bf7b9fba3924 171 }
frank26080115 0:bf7b9fba3924 172
frank26080115 0:bf7b9fba3924 173 /******************************************************************************
frank26080115 0:bf7b9fba3924 174 ** End Of File
frank26080115 0:bf7b9fba3924 175 ******************************************************************************/
frank26080115 0:bf7b9fba3924 176 /*
frank26080115 0:bf7b9fba3924 177 * @}
frank26080115 0:bf7b9fba3924 178 */