Change sampling rate and buffer size,packet size of the usb audio code

Increase the sampling frequency from 32kHz to 48kHz. And then, you'll get transfer size of 960 bytes.

Set the sampling frequency - tSamFreq field of the Type I Format descriptor - DATA_FREQ macro (demo.h)

As the packet size increases, you have to touch to these parameters, too. - wMaxPacketSize of the endpoint descriptor - P_S macro (demo.h)

usbdesc.c

/* Audio Type I Format */
  AUDIO_FORMAT_TYPE_I_DESC_SZ(1),       /* bLength */
  AUDIO_INTERFACE_DESCRIPTOR_TYPE,      /* bDescriptorType */
  AUDIO_STREAMING_FORMAT_TYPE,          /* bDescriptorSubtype */
  AUDIO_FORMAT_TYPE_I,                  /* bFormatType */
  0x01,                                 /* bNrChannels */
  0x02,                                 /* bSubFrameSize */
  16,                                   /* bBitResolution */
  0x01,                                 /* bSamFreqType */
  B3VAL(32000),                         /* tSamFreq */       // <----- B3VAL(48000)
/* Endpoint - Standard Descriptor */
  AUDIO_STANDARD_ENDPOINT_DESC_SIZE,    /* bLength */
  USB_ENDPOINT_DESCRIPTOR_TYPE,         /* bDescriptorType */
  USB_ENDPOINT_OUT(3),                  /* bEndpointAddress */
  //USB_ENDPOINT_TYPE_ISOCHRONOUS,        /* bmAttributes */
  USB_ENDPOINT_TYPE_ISOCHRONOUS | USB_ENDPOINT_SYNC_ASYNCHRONOUS,
  WBVAL(66),                            /* wMaxPacketSize */ // <----- WBVAL(98)
  0x01,                                 /* bInterval */
  0x00,                                 /* bRefresh */
  0x83,                                 /* bSynchAddress */

demo.h
/* Audio Definitions */
#define DATA_FREQ 32000                 /* Audio Data Frequency */ // <--- 48000
#define P_S       32                    /* Packet Size */          // <--- 48

The buffering part of the code expects the buffer size, B_S (ie. P_S), as a number of power of 2, like 2, 4, 8, 16, 32, 64, ... The code abuses "mask" method to keep the buffer indices (DataIn/DataOut) within the array range. For example,

  DataIn += P_S;                          /* Update Data In Index */
    DataIn &= B_S - 1;                      /* Adjust Data In Index */

As we sets P_S to 48, not a number of power of 2, this method failed. Rewrite the buffering code, so that it accept ANY number.

usbuser.c

void USB_SOF_Event (void) {
  DWORD cnt;
  ...
  ...
#if USB_DMA == 0
  if (USB_ReadEP(0x03, (BYTE *)&DataBuf[DataIn])) {
    /* Data Available */
    DataIn += P_S;                          /* Update Data In Index */
//    DataIn &= B_S - 1;                      /* Adjust Data In Index */
    if ( DataIn >= B_S ) {
      DataIn -= B_S;
    }
//    if (((DataIn - DataOut) & (B_S - 1)) == (B_S/2)) {
    if ( DataIn >= DataOut ) {
      cnt = DataIn - DataOut;
    } else {
      cnt = B_S + DataIn - DataOut;
    }
    if ( cnt == (B_S/2)) {
      DataRun = 1;                          /* Data Stream running */
    }
  } else {
    /* No Data */
    DataRun  = 0;                           /* Data Stream not running */
    DataOut  = DataIn;                      /* Initialize Data Indexes */
  }
#endif
}
#endif


void USB_EndPoint3 (DWORD event) {
#if USB_DMA
  USB_DMA_DESCRIPTOR DD;
  DWORD cnt;

  if (event & USB_EVT_OUT_DMA_EOT) {
    /* End of Transfer */
    if (USB_DMA_BufAdr(0x03) != ((DWORD)DataBuf + 2*DataIn)) {
      /* Data Available */
      DataIn += P_C*P_S;                    /* Update Data In Index */
//      DataIn &= B_S - 1;                    /* Adjust Data In Index */
      if ( DataIn >= B_S ) {
        DataIn -= B_S;
      }
//      if (((DataIn - DataOut) & (B_S - 1)) == (B_S/2)) {
    if ( DataIn >= DataOut ) {
      cnt = DataIn - DataOut;
    } else {
      cnt = B_S + DataIn - DataOut;
    }
    if ( cnt == (B_S/2)) {
        DataRun = 1;                        /* Data Stream running */
      }
    } else {
      /* No Data */
      DataRun = 0;                          /* Data Stream not running */
      DataOut = DataIn;                     /* Initialize Data Indexes */
    }
  }

demo.c




void tc0_isr (void) __irq {
  long  val;
  DWORD cnt;




  if (DataRun) {                            /* Data Stream is running */
    val = DataBuf[DataOut];                 /* Get Audio Sample */
//    cnt = (DataIn - DataOut) & (B_S - 1);   /* Buffer Data Count */
    if ( DataIn >= DataOut ) {              /* Buffer Data Count */
      cnt = DataIn - DataOut;
    } else {
      cnt = B_S + DataIn - DataOut;
    }
    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 ( DataOut >= B_S )                   /* Adjust Buffer Out Index */
      DataOut -= B_S;

Errors where in the originally code, so we have to edit out these lines, and then it works:)


Please log in to post comments.