Ticking or clicking

11 Sep 2011

Hello,

with this code I get ticking or clicking, it is not present with the origanl code only when I add the reversing code. I think it is something to do with the buffer walk I think it is called but with out a debugger not sure what to really do.


/*----------------------------------------------------------------------------
 * 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 = 15 * 1023;
    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 - 1;   /* 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
******************************************************************************/

I have tried changing the code to seem to make some deference, less clicks

static const unsigned int   BufferSize = 10 * 1023;
    static unsigned short Buffer[BufferSize];
    
    static int           Index = 0;
    static int           Direction = 1;
    static int           Playback = FALSE;
    static int           ChunkSize = 5 * 1023;//BufferSize;

13 Sep 2011

Is somebody gonna tell a newbie why this is happening.

I think it is the to do with the tick, and the rewriting or passing of the information with the reverse code is coursing it to be off course.

I don't know how your all expecting me to just magic code up, or even know what to do when there is not even much information about this code, even baby's have to learn the alphabet.

Keil won't even help unless you pay for the support, one example is all I have it's not good.

13 Sep 2011

I can't even change the sample rate for the code,

because I don't even know how to set the packet size and packet count for different sample rates?

/*----------------------------------------------------------------------------
 *      Name:    usbaudio.h
 *      Purpose: USB Audio Demo Definitions
 *      Version: V1.10
 *----------------------------------------------------------------------------
 *      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 family microcontroller devices only. Nothing 
 *      else gives you the right to use this software.
 *
 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
 *---------------------------------------------------------------------------*/

/* Audio Definitions */
#define DATA_FREQ 32000                 /* Audio Data Frequency */
#define P_S       32                    /* Packet Size */
#if USB_DMA
#define P_C       4                     /* Packet Count */
#else
#define P_C       1                     /* Packet Count */
#endif
#define B_S       (8*P_C*P_S)           /* Buffer Size */

/* Push Button Definitions */
// #define PBINT     0x00004000            /* P0.14 */

/* LED Definitions */
#define LEDMSK    0x000000FF            /* P2.0..7 */

/* Audio Demo Variables */
extern uint8_t  Mute;                      /* Mute State */
extern uint32_t Volume;                    /* Volume Level */
extern uint16_t  VolCur;                    /* Volume Current Value */
#if !USB_DMA
extern uint32_t InfoBuf[P_C];              /* Packet Info Buffer */
extern short DataBuf[B_S];              /* Data Buffer */
#else
extern uint32_t *InfoBuf;
extern short *DataBuf;
#endif
extern uint16_t  DataOut;                   /* Data Out Index */
extern uint16_t  DataIn;                    /* Data In Index */
extern uint8_t   DataRun;                   /* Data Stream Run State */