Modify sample frequency (USB Audio project)

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dears I have problem to change sample frequency for USB audio demo project  USB Audio Device (FreeRTOS) https://github.com/Infineon/mtb-example-psoc6-usb-audio-device-freertos in Modus Toolbox 3.0

I modify USB descriptor to include 16000 Hz freq

ravo_1-1670932637897.png

And add new PLL_FREQ definition in audio_app.c

#define PLL_FREQ_FOR_48KHZ 55296000 /* in Hz */
#define PLL_FREQ_FOR_44KHZ 50803200 /* in Hz */
#define PLL_FREQ_FOR_16KHZ 18432000 /* in Hz */

And ofcourse modify app_set_clock to adapt to new freq:

void audio_app_set_clock(uint32_t sample_rate)
{
/* Wait till the CapSense is ready */
while (!touch_is_ready())
{
vTaskDelay(1);
};

switch (sample_rate)
{
case AUDIO_SAMPLING_RATE_48KHZ:
{
cyhal_clock_set_frequency(&pll_clock, PLL_FREQ_FOR_48KHZ, &tolerance_0_p); // PLL_FREQ_FOR_48KHZ
break;
}
case AUDIO_SAMPLING_RATE_44KHZ:
{
cyhal_clock_set_frequency(&pll_clock, PLL_FREQ_FOR_44KHZ, &tolerance_0_p);
break;
}
case AUDIO_SAMPLING_RATE_16KHZ:
{
cyhal_clock_set_frequency(&pll_clock, PLL_FREQ_FOR_16KHZ, &tolerance_0_p);
break;
}
default:
break;

But it did not help, in Windows Repro/HeadPhones  advanced settings ... I see only 24bit/48000 and 24bit/44100 Hz

(before I do any modification - only compiled demo, there was only grayed 48000 Hz)

ravo_2-1670933082595.png

Could somebody help to get it works ?

Regards

Radim

0 Likes
15 Replies
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

Every time you change the USB descriptor, you need to make sure to uninstall the USB driver first, otherwise you might have some driver issues on Windows. Alternatively, you can change the PID value in the device descriptor under the device descriptor tree window, so a new device enumerates. 

A side note. Note that this CE originally sets the PLL as the source for the CPU clocks. If you change it to ~18 MHz, you will significantly drop the CPU frequency. You actually can keep the PLL frequency the same as for 48KHz, since both frequencies are multiple.

ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL ,

thanks for mention that it needs to uninstall USB drivers. I just do it once more, but still in USB Audio settings for Speakers there is still only 44100 and 48000 Hz

ravo_0-1671013248222.png

And If I replace 44100 Hz to 16000 Hz in USB desriptors variable tSampFreq = "16000; 48000;" than Windows show only 48000 Hz and it is grayed out(not available to change them)

ravo_0-1671013836799.png

Do you have another idea what could be still wrong ?

Regards

Radim

0 Likes
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

The driver might not be uninstalling correctly. Try to change the PID and let the OS re-enumerate it as a new device.

0 Likes
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL ,

I just tried to modify VID & PID but after re-enumeration, it is same as previous (only grayed out 48kHz). So it did not helped me. I also studied USB Audio from usb.org if I have been omitted something else. But I did not found anything.

Radim

0 Likes
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

If you add an additional frequency, you need to go change this #define AUDIO_STREAMING_EPS_NUMBER in audio.h. Currently it is set to 2.

Also try to set the Audio IN and Audio OUT to have the exact same frequencies options. I think the firmware doesn't distinguish between them.

0 Likes
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL ,

thanks for this info. But it did not help. I try to find where AUDIO_STREAMING_EPS_NUMBER  is used and only in usb_comm.c

is definition

uint8_t usb_comm_sample_frequency[AUDIO_STREAMING_EPS_NUMBER][AUDIO_SAMPLE_FREQ_SIZE];

both def are now 3U

Radim

Radim

0 Likes
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL 

I verified once again and also using USB Device Tree Viewer utility:

        ------- Audio Streaming Format Type Descriptor --------
bLength                  : 0x11 (17 bytes)
bDescriptorType          : 0x24 (Audio Interface Descriptor)
bDescriptorSubtype       : 0x02 (Format Type)
bFormatType              : 0x01 (FORMAT_TYPE_I)
bNrChannels              : 0x02 (2 channels)
bSubframeSize            : 0x03 (3 bytes per subframe)
bBitResolution           : 0x18 (24 bits per sample)
bSamFreqType             : 0x03 (supports 3 sample frequencies)
tSamFreq[1]              : 0x03E80 (16000 Hz)
tSamFreq[2]              : 0x0AC44 (44100 Hz)
tSamFreq[3]              : 0x0BB80 (48000 Hz)
Data (HexDump)           : 11 24 02 01 02 03 18 03 80 3E 00 44 AC 00 80 BB   .$.......>.D....
                           00                                                .

But still I am unable to view these frequencies in Device Properties and playback frequency is still only 48000 Hz.

Could be another problem here ?

Regards

Radim

 

0 Likes
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL  do you have some another idea what could be wrong, that I am unable to set USB sampling freq to 16 kHz, I did all setup as you write... but Windows still report only 48 kHz

Best Regards

Radim

0 Likes
lock attach
Attachments are accessible only for community members.
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

I'm attaching a modified version of the code example running at 16 KHz. Here is the list of things that changed:

1) USB descriptor was updated to only support 16 KHz. I also changed the PID to avoid driver issue.

2) Initialized the I2S to run at 16 KHz

3) Updated feedback endpoint sample rate

4) Updated the enpoint buffer sizes

5) Commented out code to update the PLL. 

6) Updated the MCLK frequency

Hope this helps.

ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL ,

many thanks for this code, I will check soon and write back.

Best Regards

Radim

0 Likes
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL ,

I just tried to test the code in Modus Toolbox 3.0, but I was unable to import it to modus toolbox.  After import it has different BSP CY8CKIT-062S2-43012. I tried to run make getlibs...

mtbgetlibs: getlibs failed
WARNING:Trying to latest-lock to release-v1.4.0 but this version doesn't exist in the database. This problem can occur if the locking_commit.log is out of date. Locking to release-v1.3.0.
ERROR:there was an error retreiving the asset 'https://github.com/cypresssemiconductorco/TARGET_CY8CKIT-062S2-43012W' commit 'latest-v2.X' - fatal: could not read Username for 'https://github.com': terminal prompts disabled

And also tried converting to CY8CPROTO-062-4343W without success... same error message.

after updating original BSP from 2.1.x -> 3.x it could be converted and look that it is working. Windows correctly see 24bit/16khz.

So many thanks, I will examine when I was wrong...

Best Regards

Radim

0 Likes
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL 

I just see only one difference that you do, I have USB descriptor with 3 frequencies "16000; 44100; 48000;" and make appropriate changes in audio_app.c, audio_feed.c

You have only one frequency 16000;

and you assign usb_comm_clock_configured = true; on startup

I update audio_app_set_clock function to adapt 16kHz (

audio_app.c:

/*******************************************************************************
* Function Name: audio_app_set_clock
********************************************************************************
* Summary:
* Update the PLL clock to achieve the desired sample rate.
*
* Parameters:
* sample_rate: new sample rate to be enforced.
*
*******************************************************************************/
void audio_app_set_clock(uint32_t sample_rate)
{
/* Wait till the CapSense is ready */
//while (!touch_is_ready())
//{
// vTaskDelay(1);
//};

switch (sample_rate)
{
case AUDIO_SAMPLING_RATE_48KHZ:
{
cyhal_clock_set_frequency(&pll_clock, PLL_FREQ_FOR_48KHZ, &tolerance_0_p);
break;
}
case AUDIO_SAMPLING_RATE_44KHZ:
{
cyhal_clock_set_frequency(&pll_clock, PLL_FREQ_FOR_44KHZ, &tolerance_0_p);
break;
}
case AUDIO_SAMPLING_RATE_16KHZ:
{
cyhal_clock_set_frequency(&pll_clock, PLL_FREQ_FOR_16KHZ, &tolerance_0_p);
break;
}
default:
break;
}

/* Update the USB Reset clock based on the new frequency */
cyhal_clock_set_frequency(&usb_rst_clock, USB_CLK_RESET_HZ, &tolerance_1_p);

/* Set flag to indicate that the clock was configured */
usb_comm_clock_configured = true;

/* Update baseline to compensate change in the clock */
touch_update_baseline();
}

audio_feed.c:

/*******************************************************************************
* Function Name: audio_feed_update_sample_rate
********************************************************************************
* Summary:
* Set the audio streaming sample rate.
*
*******************************************************************************/
void audio_feed_update_sample_rate(uint32_t sample_rate)
{
switch (sample_rate)
{
/* The sample rate in the feedback endpoint is represented with 3 bytes
* as an fraction number - X.Y, where:
* X = (Byte[2] << 2) + (Byte[1] >> 6)
* Y = (Byte[1] & 0x3F)) + (Byte[0] & 0xF0)
*/
case AUDIO_SAMPLING_RATE_48KHZ:
audio_feed_data[0] = 0x00u;
audio_feed_data[1] = 0x00u;
audio_feed_data[2] = 0x0Cu; //0x0Cu
break;

case AUDIO_SAMPLING_RATE_44KHZ:
audio_feed_data[0] = 0x40u;
audio_feed_data[1] = 0x06u;
audio_feed_data[2] = 0x0Bu;
break;

case AUDIO_SAMPLING_RATE_16KHZ:
audio_feed_data[0] = 0x00u;
audio_feed_data[1] = 0x00u;
audio_feed_data[2] = 0x04u;
break;

default:
break;
}
}

include\audio.h

/*******************************************************************************
* Constants from USB Audio Descriptor
*******************************************************************************/
#define AUDIO_OUT_ENDPOINT_SIZE (102U)
#define AUDIO_IN_ENDPOINT_SIZE (102U)
#define AUDIO_FEEDBACK_ENDPOINT_SIZE (3U)

#define AUDIO_FRAME_DATA_SIZE (32u)

Maybe the problem was in this file, I suppose that it's value is updated to correspond with USB descriptor... but it looks that it did not and need to by manually edited.

I don't know what other could be wrong, but now I can continue with only one freq. 16kHz... and hope that I will be able to continue.

Best Regards

Radim

0 Likes
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL 

I would like to kindly reopen this old case. Now I would like to open your project once more.

I just do project import in Modus Toolbox 3.0. After that I am unable to refresh Quick Panel. I have only "Library Manager 2.0" available (not USB, Device config etc...)

When I tried compile project

C:/users/xxxx/ModusToolbox/tools_3.0/make/core_version1/startex-v1.mk:315: *** Build support for the target device not found. Run "make getlibs" to ensure all required build and code dependencies are present.. Stop.

So I tried run command "make getlibs" in terminal windows , but it did not helped me.

Here is report from terminal:

$ make getlibs
Tools Directory: C:/Users/xxx/ModusToolbox/tools_3.0
Searching installed tools in progress...
Searching installed tools complete

==============================================================================
= Importing libraries =
==============================================================================
mtbgetlibs: retreived MTBQueryAPI interface
0: Loading the application in directory 'C:/Users/xxx/mtw3/mtb-example-psoc6-usb-audio-de
0: Finished loading the application data (736 ms total, 593 make)
0: Loading the ModusToolbox Technology Packs and Early Access Packs
0: Finished loading the ModusToolbox Technology Packs and Early Access Packs (2 ms)
0: Loading the tools information
0: Finished loading the tools information (24 ms)
0: Loading the manifest data
0: Finished loading the manifest data (3876 ms)
0: Loaded ModusToolbox 2.x application
0: ToolsDir is 'C:/Users/xxx/ModusToolbox/tools_3.0'
0: loaded asset 'TARGET_CY8CKIT-062S2-43012'
0: loaded asset 'usbdev'
0: loaded asset 'clib-support'
0: loaded asset 'freertos'
0: loaded asset 'TARGET_CY8CPROTO-062-4343W'
0: loaded asset 'capsense'
0: loaded asset 'abstraction-rtos'
0: loaded asset 'core-lib'
0: loaded asset 'recipe-make-cat1a'
0: loaded asset 'core-make'
0: loaded asset 'psoc6cm0p'
0: loaded asset 'mtb-hal-cat1'
0: loaded asset 'mtb-pdl-cat1'
mtbgetlibs: getlibs failed
WARNING:Trying to latest-lock to release-v1.4.0 but this version doesn't exist in the databa
ERROR:there was an error retreiving the asset 'https://github.com/cypresssemiconductorco/TAR
mtbgetlibs: completed in 13182 milliseconds
==============================================================================
= Import complete =
==============================================================================

Could you plese help me with this?

Thanks in advance

Radim

0 Likes
ravo
Level 5
Level 5
10 likes received 5 comments on blog 5 solutions authored

Dear @RodolfoGL ,

Small update, now import was succesfull. I forgot to do manual update of BSP from from 2.1.x -> 3.x. As I wrote in previous message. No it could be converted and look that it is working.

But I have one question. When I open Device configurator and CLK_HF1 settings, I just see that Clock Output is not configured to be used by I2S system. Is it ok ?

ravo_0-1683641564742.png

Where the connection is done in code ? I am unable to find the initialisation, I see only code related to PLL settings, and update audio clock...

void audio_app_clock_init(void)
{
/* Initialize the PLL */
cyhal_clock_get(&pll_clock, &CYHAL_CLOCK_PLL[0]);
cyhal_clock_init(&pll_clock);

/* Get the reset USB clock */
cyhal_clock_get(&usb_rst_clock, &CYBSP_USB_CLK_DIV_obj);
}

Thanks in advance for your help

Regards

Radim

0 Likes
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

Hi Radim,

When using the I2S HAL driver, the connection is done in code. You might need to look at the cyhal_i2s_init().

0 Likes