har-in-air / STM32F411_USB_AUDIO_DAC

USB Audio DAC using inexpensive STM32F411 / 401 "Black Pill" and PCM5102A modules
GNU General Public License v3.0
157 stars 30 forks source link

Playing does not start reliably #14

Closed elagil closed 1 year ago

elagil commented 1 year ago

Hello,

firstly, thanks a lot for your project. It fixes lots of issues with the ST-provided reference implementation.

I ported the functionality of your code into a fresh CubeMX project and got it playing correctly - the SOF mechanism works great. However, playing does not start reliably, after stopping previously.

I noticed that - when that happens - I run into the USBD_AUDIO_IsoOutIncomplete() callback. Then, USBD_AUDIO_DataOut() is never reached again.

My assumption is that when hitting USBD_AUDIO_IsoOutIncomplete(), a previous receive failed. So, I assume that I need to USBD_LL_PrepareReceive() again, so that reception can restart successfully.

My solution looks as follows:

static uint8_t USBD_AUDIO_IsoOutIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
  UNUSED(epnum);

  USBD_AUDIO_HandleTypeDef *haudio;
  haudio = (USBD_AUDIO_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];

  // Flush, just to be safe.
  USBD_LL_FlushEP(pdev, AUDIO_OUT_EP);

  // Prepare Out endpoint to receive first/next audio packet
  (void)USBD_LL_PrepareReceive(pdev, AUDIOOutEpAdd,
                                &haudio->buffer[haudio->wr_ptr],
                                AUDIO_OUT_PACKET);

  return (uint8_t)USBD_OK;
}

Have you encountered similar issues? The above fixes it for me. I never have problems restarting audio playback.

har-in-air commented 1 year ago

Thanks, that's interesting. I'm not sure I've encountered this and I use the USBDAC as an every day headphone driver - it's plugged in right now.Are you saying you used CubeMX to install  the required libraries as well ? I based mine on the DragonMan Makefile based project and made sure once I had a working set of libraries that they were untouched. That's the reason forstaying with the Makefile based project and a non-standard directory structure.   But if you can get it working with CubeIDE and CubeMX you should publish it and send me a link that I can add to the README. I'm sure a lot of people would prefer using an up to date CubeIDE/CubeMX build environment.   Its been a while since I had a working STM development environment on my laptop.  I will update my code with your IsOutIncomplete callback  once I can build and test it. In the meantime if you or anyone else can verify this is compatible with the Makefile project source, let me know. On Wednesday, 15 March, 2023 at 03:14:44 am IST, Adrian @.***> wrote:

Hello,

firstly, thanks a lot for your project. It fixes lots of issues with the ST-provided reference implementation.

I ported the functionality of your code into a fresh CubeMX project and got it playing correctly - the SOF mechanism works great. However, playing does not start reliably, after stopping previously.

I noticed that - when that happens - I run into the USBD_AUDIO_IsoOutIncomplete callback. Then, USBD_AUDIO_DataOut is never reached again.

It seems to me like, when hitting USBD_AUDIO_IsoOutIncomplete, a previous receive failed. So, I assume that I need to USBD_LL_PrepareReceive again in that case.

My solution looks as follows: static uint8_t USBD_AUDIO_IsoOutIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum) { UNUSED(epnum);

USBD_AUDIO_HandleTypeDef haudio; haudio = (USBD_AUDIO_HandleTypeDef )pdev->pClassDataCmsit[pdev->classId];

USBD_LL_FlushEP(pdev, AUDIO_OUT_EP);

/ Prepare Out endpoint to receive next audio packet / (void)USBD_LL_PrepareReceive(pdev, AUDIOOutEpAdd, &haudio->buffer[haudio->wr_ptr], AUDIO_OUT_PACKET);

return (uint8_t)USBD_OK; } Have you encountered similar issues? The above fixes it for me. I never have problems restarting audio playback.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

elagil commented 1 year ago

Are you saying you used CubeMX to install the required libraries as well ?

Yes, that's right! I only edited the minimum amount of files for enabling audio playback. In fact, I only had to add the usbd_audio module and kept the rest of the generated files for a USB Audio device. Also, it's still a Makefile project and does not differ too much in folder structure from your project. It is quite close to what CubeMX generates today.

However, I currently have a reduced feature set, because I don't need the extended functionality (only 16 bit/48 kHz, no volume control), as my DSP/amplifiers run at a fixed 48 kHz. I will create and share a template project, when I am finished.

har-in-air commented 1 year ago

OK. Are you using Windows ? I have only been using the USB DAC on my Android phone and my Ubuntu laptop. Tested it  once on Windows 10 just to check. I will add your IsOutIncomplete callback.  On Wednesday, 15 March, 2023 at 01:21:40 pm IST, Adrian @.***> wrote:

Are you saying you used CubeMX to install the required libraries as well ?

Yes, that's right! I only edited the minimum amount of files for enabling audio playback. In fact, I only had to add the usbd_audio module and kept the rest of the generated files for a USB Audio device.

However, I currently have a reduced feature set, because I don't need the extended functionality (only 16 bit/48 kHz, no volume control), as my DSP/amplifiers run at a fixed 48 kHz. I will create and share a template project, when I am finished.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

elagil commented 1 year ago

Yes, I use Windows 11. Could be related to it, since this is essentially a fix for a host issue.

har-in-air commented 1 year ago

Added suggested fix

elagil commented 1 year ago

@har-in-air After two weeks of struggle, I still failed to get the current ST driver to send out my feedback value to the PC. It might be broken, as forum posts suggest.

I switched to the amazing ChibiOS HAL and RTOS, and implemented the functionality on that basis. Find it here: https://github.com/elagil/usb-i2s-bridge

Documentation and functionality are not completely finished, but usable.

har-in-air commented 1 year ago

I was toying with the idea of migrating the project to an STM32CubeIDE project using the latest HAL libraries, and am now glad I stayed with a working snapshot of the library sources at the time.

elagil commented 1 year ago

I think that was a wise choice! ST also no longer includes any audio demo projects that I could find - so I assume that they never test for it.