[MQX 4.1.0] Patch for USB Host CDC demo

Document created by Kan_Li Employee on Jul 7, 2014
Version 1Show Document
  • View in full screen mode

MQX 4.1.0 provides a USB Host CDC demo in the folder of "C:\Freescale\Freescale_MQX_4_1\usb\host\examples\cdc\cdc_serial", but to get it work, several steps need to be done:

1. Change CDC_EXAMPLE_USE_HW_FLOW in cdc_serial.h to 1.

2. In config\twrk70f120m\user_config.h, interrupt mode for UART should be set.//It depends on the platform, here given TWR-K70F120M is used.
In mqx\source\bsp\twrk70f120m\twrk70f120m.h, specify 'ittyc' instead of 'ttyc'
#define BSP_DEFAULT_IO_CHANNEL "ittyc:" /* OSJTAG-COM polled mode */

3. In usb\host\source\classes\cdc\usb_host_cdc.c, pass fd_ptr instead of data_instance as the argument.
a. Around line 977
//usb_hostdev_tr_init(&tr, (tr_callback) usb_class_cdc_in_data_callback, (void *) data_instance);
usb_hostdev_tr_init(&tr, (tr_callback) usb_class_cdc_in_data_callback, (void *) fd_ptr);
b. Around line 1116
//usb_hostdev_tr_init(&tr, (tr_callback) usb_class_cdc_in_data_callback, (void *) data_instance);
usb_hostdev_tr_init(&tr, (tr_callback) usb_class_cdc_in_data_callback, (void *) fd_ptr);
c. Around line 1226
//usb_hostdev_tr_init(&tr, (tr_callback) usb_class_cdc_out_data_callback, (void *) data_instance);
usb_hostdev_tr_init(&tr, (tr_callback) usb_class_cdc_out_data_callback, (void *) fd_ptr);

With above modification , the Host CDC demo can work with a Device CDC demo, such as the one in "C:\Freescale\Freescale_MQX_4_1\usb\device\examples\cdc\virtual_com", which is an echo demo, it would send back anything charactor that it receives. But to see the echo more clearly, I made it echo back the charactor that greater than what is received, for example, if you type A, and will see B from the HyperTerminal.

To do that , you have to change Virtual_Com_App(void) in the virtual_com.c as below:

void Virtual_Com_App(void)


    /* User Code */



        _mqx_int i;


        /* Copy Buffer to Send Buff */

        for (i = 0; i < g_recv_size; i++)


            printf("Copied: %c\n", g_curr_recv_buf[i]);

            g_curr_send_buf[g_send_size++] = g_curr_recv_buf[i]+1;


        g_recv_size = 0;





        uint8_t error;

        uint8_t size = g_send_size;

        g_send_size = 0;


        error = USB_Class_CDC_Send_Data(g_app_handle, DIC_BULK_IN_ENDPOINT,

            g_curr_send_buf, size);

        if (!error && !(size % DIC_BULK_IN_ENDP_PACKET_SIZE)) {

            /* If the last packet is the size of endpoint, then send also zero-ended packet,

            ** meaning that we want to inform the host that we do not have any additional

            ** data, so it can flush the output.


            error = USB_Class_CDC_Send_Data(g_app_handle, DIC_BULK_IN_ENDPOINT, NULL, 0);


        if(error != USB_OK)


            /* Failure to send Data Handling code here */






After you download code into, for example, TWR-K70F120(Host) and TWR-K60D100M(Device), and assemble them with TWR-SER and TWR-ELEV, you may connect them via USB port as below:


Please also pay attention to J10(USB VBUS Select) and J16(USB Mode Select) according to the function of each tower system.


With HyperTerminal connected with TWR-K70F120M system, you may type in any charactor ended with ENTER, and you will see the charactor echoed back as the attached video.


This issue would be fixed in the next release, and sorry for the inconvenience that has caused.