AnsweredAssumed Answered

STM32F072 usb audio

Question asked by piqx.sg on May 19, 2016
Latest reply on Jun 27, 2017 by Andreas Gaich
Hi guys,

I new to STM32, was mostly use PIC32 for USB audio application but due to cost decided to switch to STM32.

Now I have STM32F0 discovery board with STM32F072RBT6.
I tried to setup USB audio with CubeMX and compile with out no issue. Now I would like to change the Audio Sampling rate to 48KHz instead of 22.1KHz (code default value).
Once change to 48KHz, the code will run into Hard Fault exception. So I take a look into the code and sort of does not understand below part (summarize from various .h and .c)

#define USBD_malloc               (uint32_t *)USBD_static_malloc
 
#define USBD_AUDIO_FREQ       22100
#define AUDIO_OUT_PACKET          (uint32_t)(((USBD_AUDIO_FREQ * 2 * 2) /1000)) //=88.4=88bytes
 
     
/* Number of sub-packets in the audio transfer buffer. You can modify this value but always make sure
  that it is an even number and higher than 3 */
#define AUDIO_OUT_PACKET_NUM                          80
/* Total size of the audio transfer buffer = 88*80=7040 bytes!*/
#define AUDIO_TOTAL_BUF_SIZE                          ((uint32_t)(AUDIO_OUT_PACKET * AUDIO_OUT_PACKET_NUM))
 
 typedef struct
{
   uint8_t cmd;  
   uint8_t data[USB_MAX_EP0_SIZE]; 
   uint8_t len; 
   uint8_t unit;   
}
USBD_AUDIO_ControlTypeDef;
 
 
typedef struct
{
  __IO uint32_t             alt_setting;
  uint8_t                   buffer[AUDIO_TOTAL_BUF_SIZE];
  AUDIO_OffsetTypeDef       offset;
  uint8_t                    rd_enable; 
  uint16_t                   rd_ptr; 
  uint16_t                   wr_ptr; 
  USBD_AUDIO_ControlTypeDef control;  
}
 
////////////////////////////////////////////////////////////////////////////////
// at usbd_conf.c
 
void *USBD_static_malloc(uint32_t size)
{
  //static uint8_t mem[sizeof(USBD_AUDIO_HandleTypeDef)];
  /* USER CODE BEGIN 4 */
  /**
  * To compute the request size you must use the formula:
    AUDIO_OUT_PACKET = (USBD_AUDIO_FREQ * AUDIO_BYTES_PER_FRAME * 2) /1000)
    AUDIO_TOTAL_BUF_SIZE = AUDIO_OUT_PACKET * AUDIO_OUT_PACKET_NUM with
    Number of sub-packets in the audio transfer buffer. You can modify this value but always make sure
    that it is an even number and higher than 3
    AUDIO_OUT_PACKET_NUM = 80
  */ 
  static uint8_t mem[512]; // fixed sized?
  /* USER CODE END 4 */
  return mem;
}
 
////////////////////////////////////////////////////////////////////////////////
// at usbd_audio.c
 
static uint8_t  USBD_AUDIO_Init (USBD_HandleTypeDef *pdev,
                               uint8_t cfgidx)
{
 
USBD_AUDIO_HandleTypeDef;
 
 
  USBD_AUDIO_HandleTypeDef   *haudio;
   
  /* Open EP OUT */
  USBD_LL_OpenEP(pdev,
                 AUDIO_OUT_EP,
                 USBD_EP_TYPE_ISOC,
                 AUDIO_OUT_PACKET);
   
  /* Allocate Audio structure */
  pdev->pClassData = USBD_malloc(sizeof (USBD_AUDIO_HandleTypeDef)); //allocate 512 byte for pClassData which should
 
hold one packet
   
  if(pdev->pClassData == NULL)
  {
    return USBD_FAIL;
  }
  else
  {
    haudio = (USBD_AUDIO_HandleTypeDef*) pdev->pClassData; //??? pdev->pClassData size if 512 but cast into haudio
 
which have size AUDIO_TOTAL_BUF_SIZE+77bytes ?? how does this work?
    haudio->alt_setting = 0;
    haudio->offset = AUDIO_OFFSET_UNKNOWN;  //call to this should generate error as it is out of 512?
    haudio->wr_ptr = 0;
    haudio->rd_ptr = 0; 
    haudio->rd_enable = 0;
  }
 
...
...
}

I don't understand the logic of 
haudio = (USBD_AUDIO_HandleTypeDef*) pdev->pClassData; 
 
as pdev->pClassData point to 512 bytes space while haudio is pointer to USBD_AUDIO_HandleTypeDef which have size AUDIO_TOTAL_BUF_SIZE+77bytes ?? 

Anyone has idea how this work? and if anyone done USB audio for stm32f072 please kindly share what we should to to set different sampling rate.

Thank you very much! especially reading this long post :)

SG

Outcomes