How to add USB Serial port to XMOS Reference Audio project ?

If you have a simple question and just want an answer.
MisterQ
Member++
Posts: 21
Joined: Thu Jan 05, 2017 3:35 pm

How to add USB Serial port to XMOS Reference Audio project ?

Post by MisterQ »

Respected Colleagues,

can somebody from XMOS stuff, or somebody who succeeded to make it to work, post to the forum functional example how to add USB CDC serial port functioning to a XMOS Audio 2.0 reference project?

It is necessary for various tasks where USB HID communication is not enough.

I have read Q&A topic "Adding Virtual Serial Port to Audio USB reference app" http://www.xcore.com/forum/viewtopic.ph ... =cdc+audio but this is a conversation of two programmers who know much about USB, with some code snippets that can not help somebody who is not VERY familiar with USB protocols. By the way, I tried to contact both participants from that communication with private message asking for help, but with no answer jet.

Regards,
Dragan


henk
Respected Member
Posts: 347
Joined: Wed Jan 27, 2016 5:21 pm

Post by henk »

Hello Dragan,

This is not a trivial task, and you may have to do a bit of reading on USB to get yourself familiar with the intricacies of USB.

USB has descriptors that tell you what a device does. These descriptors are organised as containers inside containers inside a container. One of these levels, I think called the "Interface" describes, for example, USB Audio or CDC. At this level you can put two containers side by side, one for USB Audio and one for CDC (and maybe one for HID).

The composition is not a simple concatenation operation; lengths need to be adjusted, end point numbers need to be adjusted, all of which is hairy as the descriptors are in binary.

See <http://www.beyondlogic.org/usbnutshell/usb5.shtml> for a very good description (you may have to start in chapter 1 - get the printed copy, it is worth it).

Once you have done that, the you need to adapt the USB design. This is not that hard; you create an endpoint-task for each extra endpoint you have created in the descriptor, and you add extra calls for dealing with the new part of the descriptor to endpoint 0 (the endpoint that is dealing with all device-wide requests).

You will likely need a USB analyser to get it to work; there is many things that can go wrong.

Cheers,
Henk
User avatar
Thomas
Experienced Member
Posts: 66
Joined: Fri Feb 05, 2010 12:34 pm

Post by Thomas »

Hi Dragan
Here are the details of the code changes required for integrating "AN00124: USB CDC Class as Virtual Serial Port" with UAC2 6.12.5.

- Merge the descriptors from xud_cdc.xc into descriptors.h
- Update SubordinateInterface0 and bDataInterface in descriptors.h for the new index 5 ( IF 5 (alt 0) CDC Data, 0, No class specific protocol required).

Code: Select all

/* Union Functional descriptor */
0x05, /* 0 bLength */
USB_DESCTYPE_CS_INTERFACE,/* 1 bDescriptortype, CS_INTERFACE */
0x06, /* 2 bDescriptorsubtype, UNION */
0x00, /* 3 bControlInterface - Interface 0 */
0x05, /* 4 bSubordinateInterface0 - Interface 1 */
/* Call Management Functional descriptor */
0x05, /* 0 bLength */
USB_DESCTYPE_CS_INTERFACE,/* 1 bDescriptortype, CS_INTERFACE */
0x01, /* 2 bDescriptorsubtype, CALL MANAGEMENT */
0x03, /* 3 bmCapabilities, DIY */
0x05, /* 4 bDataInterface */
- Add the CdcEndpointsHandler to the nested par of usb_audio_core in main.xc
SERVER_INTERFACE(usb_cdc_interface, cdc) has to be added to parameter list of usb_audio_core
- Add to top level par
#ifdef CDC_VSP
        on tile[1]: app_virtual_com_extended(cdc_data, i2c[0]);
        on tile[1]: i2c_master(i2c, 1, p_scl, p_sda, 10); // will be distributed into app_virtual_com_extended
#endif
- Add to main.xc
#ifdef CDC_VSP
#include "i2c_lib.h"
#include "xud_cdc.h"
#include "app_virtual_com_extended.h"
#endif

#ifdef CDC_VSP
// I2C interface ports
on tile[1]: port p_sda = XS1_PORT_1D;  // MDC
on tile[1]: port p_scl = XS1_PORT_1N;  // MIDI TX
#endif
- rename i2c.h from lib_i2c to i2c_lib.h. Otherwise it clashes

- add to customdefines.h

Code: Select all

#define CDC_VSP  // Enable Communication Device Class, Virtual Serial Port
#define HID_CONTROLS 0 // disable. p_sw overlaps with CDC Control Buttons
- add to enum USBInterfaceNumber

Code: Select all

#ifdef CDC_VSP
    INTERFACE_NUMBER_CDC_COMMAND,
    INTERFACE_NUMBER_CDC_DATA,
#endif
- Add to endpoint.c in

Code: Select all

case USB_BMREQ_D2H_CLASS_INT:
#ifdef CDC_VSP
                        /* Inspect for CDC Communications Class interface num */
                        if(sp.wIndex == INTERFACE_NUMBER_CDC_COMMAND)
                        {
                            /* Returns  XUD_RES_OKAY if handled,
                             *          XUD_RES_ERR if not handled,
                             *          XUD_RES_RST for bus reset */
                            result = ControlInterfaceClassRequests(ep0_out, ep0_in, sp);
                        }
#endif
- Remove or Comment out in xud_cdc.xc:
Endpoint0
devDesc
cfgDesc
These were merged into the UAC2 codebase

- In xud_cdc.xc, change the XUD init functions

Code: Select all

    /* Initialize all endpoints */
    XUD_ep epint_in = XUD_InitEp(c_epint_in);
    XUD_ep epbulk_out = XUD_InitEp(c_epbulk_out);
    XUD_ep epbulk_in = XUD_InitEp(c_epbulk_in);
- change occurences of usb.h to xud.h
Because UAC2 is using module_xud and not lib_usb

- Add to enum USBEndpointNumber_In before end marker

Code: Select all

#ifdef CDC_VSP
    ENDPOINT_NUMBER_IN_INT_CDC,
    ENDPOINT_NUMBER_IN_BULK_CDC,
#endif
- Add to USBEndpointNumber_Out before end marker

Code: Select all

#ifdef CDC_VSP
    ENDPOINT_NUMBER_OUT_BULK_CDC,
#endif
- In app_virutal_com_extended.xc, change the port declarations to:

Code: Select all

/* PORT_4A connected to the 4 LEDs */
on tile[1]: port p_led = XS1_PORT_4D;

/* PORT_4C connected to the 2 Buttons */
on tile[1]: port p_button = XS1_PORT_4B;
- Update Makefile

Code: Select all

USED_MODULES += lib_i2c lib_gpio lib_xassert
MisterQ
Member++
Posts: 21
Joined: Thu Jan 05, 2017 3:35 pm

Post by MisterQ »

Thomas,

thank you very much for your effort. I think that many programmers will use your instructions.

For now I have implemented all of your instructions except:

- Add the CdcEndpointsHandler to the nested par of usb_audio_core in main.xc
SERVER_INTERFACE(usb_cdc_interface, cdc) has to be added to parameter list of usb_audio_core


I do not know what to do with this. Where exactly to put and what:

For now, without this, I get only 2 errors:
error: use of undeclared identifer `cdc_data'
error: use of undeclared identifer `i2c'

Please advice.

Best regards,
Dragan
shaileshwankhede
Experienced Member
Posts: 65
Joined: Fri Dec 02, 2016 1:30 pm

Post by shaileshwankhede »

Hi Thomas,

Thanks for details on porting CDC class to USB audio. I was struggling with this for whole day because of libusb usage in CDC class implementation.
Then I came across this post and followed as you mentioned. But CDC is not working along with usb audio. USB audio is working as expected.
I did put macro CDC_VSP in customdefines.h of my app.

What looked confusing to me is you are saying to CdcEndpointsHandler in nested par of usb_audio_core, but nested par is not there in usb_audio_core. Also if we pass usb_cdc_interface, it will give error of being used as client and server both in CdcEndpointsHandler and app_virtual_com. So I put these two statements in top par of main in module_usb_audio. Application compiles fine but when I run application, it doesn't seem to hit app_virtual_com.
Also I don't know what to do with c_ep_in[1], c_ep_out[1], c_ep_in[2] as those are not used in parallel statements.

One more thing is I get warning more than 6 cores are used.

Can you please take some time to review my code or suggest what I am doing wrong.
I have attached module_usb_audio main.xc file. Let me know if any other files are required.

Thanks,
Shailesh
Attachments
main.xc
(16.59 KiB) Downloaded 737 times
main.xc
(16.59 KiB) Downloaded 737 times
shaileshwankhede
Experienced Member
Posts: 65
Joined: Fri Dec 02, 2016 1:30 pm

Post by shaileshwankhede »

Update:

Hi Thomas,

I was doing few mistakes. Now I got correct interpretation of what to be done.
But now with CDC_VSP, even default USB audio is not working (device Manager shows as USB audio device cannot start). I doubted warning of running more than 6 cores, so I changed app_virtual_com() to different tile 0.
But same problem. Please find attached updated changed files.
I am not getting what's going wrong. Even could not debug these threads in debug mode.

Can you have a look at the updated files to see what could be wrong.

PS. I am using module_usb_audio version 6.15.2 and integrating AN00124_CDC_VCOM_class.

Update 2:
In Linux or android USB audio is working correctly. Maybe windows doesn't support composite device (USB audio and CDC)?
But in android still couldn't get CDC working with this conf.

Now only problem I think is I have simply copied devDesc and cfgDesc from xud_cdc to module_usb_audio descriptor.h and those are not being used.
I need to create two interface descriptors for CDC and merge into audio configuration desc.
I will try it. If anybody has done and got both working, please share.

Thanks,
Shailesh
Attachments
module_usb_audio_cdc.rar
(27.7 KiB) Downloaded 705 times
module_usb_audio_cdc.rar
(27.7 KiB) Downloaded 705 times
MisterQ
Member++
Posts: 21
Joined: Thu Jan 05, 2017 3:35 pm

Post by MisterQ »

Respected Shailesh,

I am also waiting somebody to solve this problem, because I can not do this.

If you find solution, please post it here.

Thomas, can you please help?

Regards,
Dragan
shaileshwankhede
Experienced Member
Posts: 65
Joined: Fri Dec 02, 2016 1:30 pm

Post by shaileshwankhede »

Yeah sure! Presently I can see CDC interfaces and endpoints as part of XMOS USB audio descriptor with software TDD after integration of CDC descriptor.
But still CDC is not working properly. It is getting detected as COM port by Android now. But as soon as host tries to open device, connection is dropping.
So its partially working I can say. I will try to debug more!

Thanks,
Shailesh
shaileshwankhede
Experienced Member
Posts: 65
Joined: Fri Dec 02, 2016 1:30 pm

Post by shaileshwankhede »

Hello,

Somebody from XMOS please help on this thread! I tried a lot but no luck so far.
Host is able to open declared USB serial device but data communication (write and read) is not working.
Below are my updated file and I am sure there is some slight miss.
One more thing is Number of cores being used, now it is 7 which is more than allowed 6 for XUD.
However there doesn't seem to clocking or freezing issue as my audio related application continues to work fine.
Another problem is, I am not able to debug module_usb_audio files when debugging my application.
Is it not possible?

Hoping to get some help soon!

Thanks,
Shailesh
Attachments
descriptors.h
(124.99 KiB) Downloaded 723 times
descriptors.h
(124.99 KiB) Downloaded 723 times
main.xc
(16.89 KiB) Downloaded 656 times
main.xc
(16.89 KiB) Downloaded 656 times
mariojim22
Junior Member
Posts: 4
Joined: Fri Mar 03, 2017 4:27 pm

Post by mariojim22 »

Hello, every body,

I just attach my project.
Thomas give the clue, but there is more thigs to do. You have to delete config descriptor of CDC, adapt numbers of endpoints and interfaces, add new elements to arrays of endpoints, and enums. All that thigs i did and you can see it in my files because all is between "#ifdef CDC". So if you want to use CDC, all you need to do is define CDC = 1.

If someone have questions....just ask.

P.D.:sorry for my english.
Attachments
Adio_plus_VCOM_MC_CMOS.rar
Project with AUDIO and VCOM enabled
(1.81 MiB) Downloaded 773 times
Adio_plus_VCOM_MC_CMOS.rar
Project with AUDIO and VCOM enabled
(1.81 MiB) Downloaded 773 times
Post Reply