Audio over USB not working consistently

Hi all,

i am working on nrf5340 device (sdk v1.5.1)which is connected to microphone.Audio recorded in mic is transmitted via USB to PC(testing on PC right now).while device is transmitting the audio ,if usb is disconnected and connected again (twice or thrice)it stops sending the audio data and i need reboot the device to resume the operation.I am not sure what is causing the issue.Can anyone help me with this?

Note-i am sending 192 bytes per millisecond on USB and creating memory pool of 1920 bytes from which allocating memory dynamically for every transmission.

Thanks.

Parents
  • Hi,

    Can you share more information about your project? And have you based it on a sample (for instance from zephyr/samples/subsys/usb/audio)? What have you found when debugging? Please elaborate so that we have as much as possible to go on.

  • Hi Einar,

    Thanks for the response.

    I am using following function which is called every 1 ms from which memory is allocated for buffer and audio is transmitted over USB.

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /* This callback is called whenever the host requests for audio data. This happens every 1ms
    * (isochronous endpoint). We must send AUDIO_TX_SIZE number of bytes in every transaction
    * and then update the read index. */
    static void audio_data_request_cb(const struct device *dev)
    {
    int ret = 0;
    size_t size = AUDIO_TX_SIZE;
    struct net_buf *buffer = net_buf_alloc(&mic_pool, K_NO_WAIT); //freed automatically on successful send
    if (buffer) {
    audio_buf_alloc_fail = 0;
    //int buffer_id = net_buf_id(buffer);
    //LOG_ERR("buffer_id = %d", buffer_id);
    #ifdef MANUAL_UPSAMPLE //for 16 to 48Khz upsample using manual data copy
    check_audio_array_bounds(&read_index, size);
    memcpy(buffer->data, &audio_cache_buffer[read_index], size);
    read_index = read_index + (int)size;
    ret = usb_audio_send(dev, buffer, size);
    if (ret) {
    net_buf_unref(buffer);
    }
    #else //for 16 to 48Khz upsample using interpolator
    struct usb_audio_data_t *usb_audio_buff;
    usb_audio_buff = (struct usb_audio_data_t *)k_fifo_get(&fifo_usb_audio_pkt, K_NO_WAIT);
    if (NULL != usb_audio_buff)
    {
    memcpy(buffer->data, usb_audio_buff->data, size);
    usb_audio_pkt_free(usb_audio_buff);
    }
    else
    {
    memset(buffer,11,AUDIO_TX_SIZE);
    }

    ret = usb_audio_send(dev, buffer, size);
    if (ret) {
    net_buf_unref(buffer);
    }
    #endif
    }
    //!TODO:Need to fix , this is work-around
    else
    {
    audio_buf_alloc_fail++;
    if (audio_buf_alloc_fail > 10) {
    // sys_reboot(0);
    }
    }
    }

    static void unref_audio_buffer(const struct device *dev,
    struct net_buf *buffer,
    size_t size)
    {
    if (buffer) {
    net_buf_unref(buffer);
    }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if USB is not removed,everything works fine.when USB is removed(twice or thrice),net_buf_alloc() is unable to allocate the memory and audio data is not transmitted over USB and i have to soft reboot the device to get it back to normal operation.Below is the logs which has the errors when failure occurs-

    [00:01:33.368,988] [0m<inf> BQ25792: USB_EVENT
    05 00 05 00 bb bb 01 01 00 00 00 00 |........ .... [0m
    [00:01:33.369,049] [0m<inf> BQ25792: Puting USB event[0m
    [00:01:33.369,049] [0m<inf> BQ25792: EXIT :- bq25792_send_usb_event[0m
    [00:01:33.369,293] [0m<inf> BQ25792: ENTER :- bq25792_battery_monitor[0m
    [00:01:33.369,293] [0m<inf> BQ25792: ENTER :- bq25792_handle_usb_interrupt[0m
    [00:01:33.369,659] [0m<inf> BQ25792: Reg= BQ25792_CHRG_STAT_0,reg_val=0x7, ret = 0 [0m
    [00:01:33.370,056] [0m<inf> BQ25792: Reg= BQ25792_CHRG_FLAG_0,reg_val=0x7, ret = 0 [0m
    [00:01:33.370,056] [0m<inf> BQ25792: pchrg_stat_0.VBUS_PRESENT_STAT=1[0m
    [00:01:33.370,056] [0m<inf> BQ25792: pchrg_flag_0.VBUS_PRESENT_FLAG=1[0m
    [00:01:33.370,117] [0m<inf> BQ25792: ENTER :- bq25792_clr_pwr_plugged_interrupt[0m
    [00:01:33.370,147] [0m<inf> BQ25792: size = 1[0m
    [00:01:33.372,192] [0m<inf> usb_cdc_acm: Device suspended[0m
    [00:01:33.375,762] [0m<inf> BQ25792: ret=0[0m
    [00:01:33.375,762] [0m<inf> BQ25792: EXIT :- bq25792_clr_pwr_plugged_interrupt[0m
    [00:01:33.375,885] [0m<inf> BQ25792: EXIT :- bq25792_handle_usb_interrupt[0m
    [00:01:33.375,946] [0m<inf> BQ25792: EXIT :- bq25792_battery_monitor[0m
    [00:01:33.375,946] [0m<inf> ROTOR_MANGER: g_curr_rotor_pos 1 =0,[0m
    [00:01:33.376,007] [0m<inf> ROTOR_MANGER: device wokeup from pwer save mode g_curr_rotor_pos 2 =0,[0m
    [00:01:33.376,007] [0m<inf> pwr_sav_tmr: ENTER :- pwrsv_send_pwr_save_on_event[0m
    [00:01:33.376,129] [0m<inf> pwr_sav_tmr: EXIT :- pwrsv_send_pwr_save_on_event[0m
    [00:01:33.376,617] [0m<inf> pwr_sav_tmr: ENTER :- pwrsv_process_pwr_save_evnts[0m
    [00:01:33.376,617] [0m<inf> pwr_sav_tmr: ENTER :- pwrsv_dev_power_on[0m
    [00:01:33.376,678] [0m<inf> pwr_sav_tmr: g_pwr_dwn_mode=1[0m
    [00:01:33.376,678] [0m<inf> pwr_sav_tmr: ENTER :- pwrsv_dev_power_on[0m
    [00:01:33.376,739] [0m<inf> pwr_sav_tmr: EXIT :- pwrsv_process_pwr_save_evnts[0m
    [00:01:33.531,494] [0m<inf> usb_cdc_acm: Device resumed[0m
    [00:01:33.531,555] [0m<inf> usb_cdc_acm: from suspend[0m
    [00:01:33.822,387] [1;31m<err> usb_nrfx: nRF USBD write error: 195887115[0m
    [00:01:33.822,540] [1;31m<err> usb_transfer: Transfer error -5, ep 0x88[0m
    [00:01:33.825,073] [1;31m<err> usb_nrfx: nRF USBD write error: 195887115[0m
    [00:01:33.825,134] [1;31m<err> usb_transfer: Transfer error -5, ep 0x88[0m
    [00:01:33.825,286] [1;31m<err> usb_nrfx: nRF USBD write error: 195887115[0m
    [00:01:33.825,347] [1;31m<err> usb_transfer: Transfer error -5, ep 0x88[0m
    [00:01:33.826,080] [0m<inf> usb_cdc_acm: Device configured[0m
    [00:01:33.826,263] [0m<inf> BQ25792: ENTER :- bq25792_send_cas_connected_event[0m
    [00:01:33.826,416] [0m<inf> BQ25792: EVNT_CAS_CONNECTED
    05 00 05 00 bb bb 01 0f 00 00 00 00 |........ .... [0m
    [00:01:33.826,568] [0m<inf> BQ25792: Puting EVNT_CAS_CONNECTED event[0m
    [00:01:33.826,599] [0m<inf> BQ25792: ENTER :- bq25792_send_cas_connected_event[0m
    [00:01:33.826,843] [1;31m<err> usb_transfer: No transfer slot available[0m
    [00:01:33.827,728] [1;31m<err> usb_transfer: No transfer slot available[0m
    [00:01:33.831,024] [0m<inf> BQ25792: ENTER :- bq25792_battery_monitor[0m
    [00:01:33.831,024] [0m<inf> ROTOR_MANGER: ENTER :- rm_stop_display_bat_status[0m
    [00:01:33.832,336] [0m<inf> ROTOR_MANGER: EXIT :- rm_stop_display_bat_status[0m
    [00:01:33.832,366] [0m<inf> BQ25792: EXIT :- bq25792_battery_monitor[0m
    [00:01:33.945,953] [1;31m<err> usb_transfer: No transfer slot available[0m
    [00:01:38.397,552] [0m<inf> BQ25792: ENTER :- bq25792_send_usb_event[0m
    [1;31m--- 3 messages dropped ---

    Can you help with this available information or you need more information regarding this?

    Thanks.

Reply
  • Hi Einar,

    Thanks for the response.

    I am using following function which is called every 1 ms from which memory is allocated for buffer and audio is transmitted over USB.

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /* This callback is called whenever the host requests for audio data. This happens every 1ms
    * (isochronous endpoint). We must send AUDIO_TX_SIZE number of bytes in every transaction
    * and then update the read index. */
    static void audio_data_request_cb(const struct device *dev)
    {
    int ret = 0;
    size_t size = AUDIO_TX_SIZE;
    struct net_buf *buffer = net_buf_alloc(&mic_pool, K_NO_WAIT); //freed automatically on successful send
    if (buffer) {
    audio_buf_alloc_fail = 0;
    //int buffer_id = net_buf_id(buffer);
    //LOG_ERR("buffer_id = %d", buffer_id);
    #ifdef MANUAL_UPSAMPLE //for 16 to 48Khz upsample using manual data copy
    check_audio_array_bounds(&read_index, size);
    memcpy(buffer->data, &audio_cache_buffer[read_index], size);
    read_index = read_index + (int)size;
    ret = usb_audio_send(dev, buffer, size);
    if (ret) {
    net_buf_unref(buffer);
    }
    #else //for 16 to 48Khz upsample using interpolator
    struct usb_audio_data_t *usb_audio_buff;
    usb_audio_buff = (struct usb_audio_data_t *)k_fifo_get(&fifo_usb_audio_pkt, K_NO_WAIT);
    if (NULL != usb_audio_buff)
    {
    memcpy(buffer->data, usb_audio_buff->data, size);
    usb_audio_pkt_free(usb_audio_buff);
    }
    else
    {
    memset(buffer,11,AUDIO_TX_SIZE);
    }

    ret = usb_audio_send(dev, buffer, size);
    if (ret) {
    net_buf_unref(buffer);
    }
    #endif
    }
    //!TODO:Need to fix , this is work-around
    else
    {
    audio_buf_alloc_fail++;
    if (audio_buf_alloc_fail > 10) {
    // sys_reboot(0);
    }
    }
    }

    static void unref_audio_buffer(const struct device *dev,
    struct net_buf *buffer,
    size_t size)
    {
    if (buffer) {
    net_buf_unref(buffer);
    }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if USB is not removed,everything works fine.when USB is removed(twice or thrice),net_buf_alloc() is unable to allocate the memory and audio data is not transmitted over USB and i have to soft reboot the device to get it back to normal operation.Below is the logs which has the errors when failure occurs-

    [00:01:33.368,988] [0m<inf> BQ25792: USB_EVENT
    05 00 05 00 bb bb 01 01 00 00 00 00 |........ .... [0m
    [00:01:33.369,049] [0m<inf> BQ25792: Puting USB event[0m
    [00:01:33.369,049] [0m<inf> BQ25792: EXIT :- bq25792_send_usb_event[0m
    [00:01:33.369,293] [0m<inf> BQ25792: ENTER :- bq25792_battery_monitor[0m
    [00:01:33.369,293] [0m<inf> BQ25792: ENTER :- bq25792_handle_usb_interrupt[0m
    [00:01:33.369,659] [0m<inf> BQ25792: Reg= BQ25792_CHRG_STAT_0,reg_val=0x7, ret = 0 [0m
    [00:01:33.370,056] [0m<inf> BQ25792: Reg= BQ25792_CHRG_FLAG_0,reg_val=0x7, ret = 0 [0m
    [00:01:33.370,056] [0m<inf> BQ25792: pchrg_stat_0.VBUS_PRESENT_STAT=1[0m
    [00:01:33.370,056] [0m<inf> BQ25792: pchrg_flag_0.VBUS_PRESENT_FLAG=1[0m
    [00:01:33.370,117] [0m<inf> BQ25792: ENTER :- bq25792_clr_pwr_plugged_interrupt[0m
    [00:01:33.370,147] [0m<inf> BQ25792: size = 1[0m
    [00:01:33.372,192] [0m<inf> usb_cdc_acm: Device suspended[0m
    [00:01:33.375,762] [0m<inf> BQ25792: ret=0[0m
    [00:01:33.375,762] [0m<inf> BQ25792: EXIT :- bq25792_clr_pwr_plugged_interrupt[0m
    [00:01:33.375,885] [0m<inf> BQ25792: EXIT :- bq25792_handle_usb_interrupt[0m
    [00:01:33.375,946] [0m<inf> BQ25792: EXIT :- bq25792_battery_monitor[0m
    [00:01:33.375,946] [0m<inf> ROTOR_MANGER: g_curr_rotor_pos 1 =0,[0m
    [00:01:33.376,007] [0m<inf> ROTOR_MANGER: device wokeup from pwer save mode g_curr_rotor_pos 2 =0,[0m
    [00:01:33.376,007] [0m<inf> pwr_sav_tmr: ENTER :- pwrsv_send_pwr_save_on_event[0m
    [00:01:33.376,129] [0m<inf> pwr_sav_tmr: EXIT :- pwrsv_send_pwr_save_on_event[0m
    [00:01:33.376,617] [0m<inf> pwr_sav_tmr: ENTER :- pwrsv_process_pwr_save_evnts[0m
    [00:01:33.376,617] [0m<inf> pwr_sav_tmr: ENTER :- pwrsv_dev_power_on[0m
    [00:01:33.376,678] [0m<inf> pwr_sav_tmr: g_pwr_dwn_mode=1[0m
    [00:01:33.376,678] [0m<inf> pwr_sav_tmr: ENTER :- pwrsv_dev_power_on[0m
    [00:01:33.376,739] [0m<inf> pwr_sav_tmr: EXIT :- pwrsv_process_pwr_save_evnts[0m
    [00:01:33.531,494] [0m<inf> usb_cdc_acm: Device resumed[0m
    [00:01:33.531,555] [0m<inf> usb_cdc_acm: from suspend[0m
    [00:01:33.822,387] [1;31m<err> usb_nrfx: nRF USBD write error: 195887115[0m
    [00:01:33.822,540] [1;31m<err> usb_transfer: Transfer error -5, ep 0x88[0m
    [00:01:33.825,073] [1;31m<err> usb_nrfx: nRF USBD write error: 195887115[0m
    [00:01:33.825,134] [1;31m<err> usb_transfer: Transfer error -5, ep 0x88[0m
    [00:01:33.825,286] [1;31m<err> usb_nrfx: nRF USBD write error: 195887115[0m
    [00:01:33.825,347] [1;31m<err> usb_transfer: Transfer error -5, ep 0x88[0m
    [00:01:33.826,080] [0m<inf> usb_cdc_acm: Device configured[0m
    [00:01:33.826,263] [0m<inf> BQ25792: ENTER :- bq25792_send_cas_connected_event[0m
    [00:01:33.826,416] [0m<inf> BQ25792: EVNT_CAS_CONNECTED
    05 00 05 00 bb bb 01 0f 00 00 00 00 |........ .... [0m
    [00:01:33.826,568] [0m<inf> BQ25792: Puting EVNT_CAS_CONNECTED event[0m
    [00:01:33.826,599] [0m<inf> BQ25792: ENTER :- bq25792_send_cas_connected_event[0m
    [00:01:33.826,843] [1;31m<err> usb_transfer: No transfer slot available[0m
    [00:01:33.827,728] [1;31m<err> usb_transfer: No transfer slot available[0m
    [00:01:33.831,024] [0m<inf> BQ25792: ENTER :- bq25792_battery_monitor[0m
    [00:01:33.831,024] [0m<inf> ROTOR_MANGER: ENTER :- rm_stop_display_bat_status[0m
    [00:01:33.832,336] [0m<inf> ROTOR_MANGER: EXIT :- rm_stop_display_bat_status[0m
    [00:01:33.832,366] [0m<inf> BQ25792: EXIT :- bq25792_battery_monitor[0m
    [00:01:33.945,953] [1;31m<err> usb_transfer: No transfer slot available[0m
    [00:01:38.397,552] [0m<inf> BQ25792: ENTER :- bq25792_send_usb_event[0m
    [1;31m--- 3 messages dropped ---

    Can you help with this available information or you need more information regarding this?

    Thanks.

Children
  • Hi,

    I don't recognize this code from any example, so I assume this is from a project you started from scratch? Can you share more information about it?

    Based on what you write they key seems to be that net_buf_alloc() fails, which points at a form of memory leak (you fail to free the memory as needed). As you write this happens when the USB is removed, it indicates that in that case you fail to free some data that should have been freed in your mic_pool memory pool. If have no insight into that part of your code, but that is what you should look into next.

Related