ArduinoCore-mbed icon indicating copy to clipboard operation
ArduinoCore-mbed copied to clipboard

USBAudio (Giga R1) Faulting - USBPhy_STM32.cpp connected to Mac

Open AndrewCapon opened this issue 2 years ago • 0 comments

Hi Guys,

I'm not sure where to post this, So I thought I would start here.

Taking the following test code:

#include "PluggableUSBAudio.h"

#define BUF_SIZE (128)

USBAudio audio(true, 44100, 2, 44100, 2);
static uint8_t buf[BUF_SIZE];

void setup() 
{
  for (uint32_t u = 0; u < BUF_SIZE; u+=2) 
  {
    buf[u] = 0;
  }
}

void loop() 
{
  if(audio.read(buf, BUF_SIZE))
  {
    audio.write(buf, BUF_SIZE);
  }
}

This causes the Giga R1 to fault due to an assert on line 617 of USBPhy_STM32.cpp, basically the TX fifo buffer is smaller than the packet size.

        MBED_ASSERT(len >= max_packet);

The TX fifo buffers are created from line 423:

    uint32_t total_bytes = 0;

    /* Reserve space in the RX buffer for:
     * - 1 isochonous packet
     * - 2 max sized non-isochonous packets
     * - setup buffer - 10 words as specified by Reference Manual
     * - global nak out - 1 words as specified by Reference Manual
     */
    uint32_t fifo_size = (MAX_PACKET_SIZE_ISO + 4) + (MAX_PACKET_SIZE_NON_ISO + 4) * 2 + (10 * 4) + (1 * 4);
    HAL_PCDEx_SetRxFiFo(&hpcd, (fifo_size / 4));
    total_bytes += fifo_size;

    /* Reserve Tx space up front */
    for (int i = 0; i < NUM_ENDPOINTS; i++) {
        fifo_size =  tx_ep_sizes[i] + 4;
        HAL_PCDEx_SetTxFiFo(&hpcd, i, fifo_size / 4);
        total_bytes += fifo_size;
    }

And the tx_sp_sizes array is statically declared on line 54:

static const uint32_t tx_ep_sizes[NUM_ENDPOINTS] = {
    MAX_PACKET_SIZE_NON_ISO,
    MAX_PACKET_SIZE_NON_ISO,
    MAX_PACKET_SIZE_NON_ISO,
#if defined(TARGET_STM32H7)
    MAX_PACKET_SIZE_NON_ISO,
    MAX_PACKET_SIZE_NON_ISO,
#endif
    MAX_PACKET_SIZE_ISO
};

Those size defines are declared on line 49:

#define MAX_PACKET_SIZE_NON_ISO      64
#define MAX_PACKET_SIZE_ISO          (256 + 128)     // Spec can go up to 1023, only ram for this though

Now connected to a Mac the packet size can be 180 when the Mac is configuring the device, but we have a fifo size of 64. So the assert is hit.

If I change the MAX_PACKET_SIZE_NON_ISO to 180 then the example code works, and it will echo audio back.

Does anyone know about the USBAudio and USBPhy_STM32 and what might be going on here?

Cheers

Andy

AndrewCapon avatar Oct 17 '23 10:10 AndrewCapon