AnsweredAssumed Answered

Unable to send strings through USB CDC on K21F

Question asked by matt johnson on Jul 22, 2015
Latest reply on Jul 23, 2015 by Kan_Li

KDS 3.0.0

KSDK 1.2.0

uPC: MK21FN1M0Axxx12





I am having an issue being able to send strings through the USB CDC SDK library. I have taken code from the example MQX CDC project in the KSDK 1.2.0 install directory and applied it to my project that is using processor expert components and MQX. In my project, I am able to echo back characters sent to my device through the CDC com port. However, when I try to use the function, USB_Class_CDC_Send_Data, to send a string from the uPC, it will work for the first several times, then stop working all together. What is interesting is when the uPC gets into the state where it will no longer send the string, it is still able to echo characters back to the com port connection on the computer.


I have been using the virtual_com.h and .c along with usb_descriptor.h and .c from the example CDC project with a small change to the Virtual_Com_App function in virtual_com.c. My code is as follows and will print "hello world!" after every character echo.


uint8_t msg[20] = "hello world!";

void Virtual_Com_App(void)


    /* User Code */

    if ((0 != g_recv_size) && (0xFFFFFFFF != g_recv_size))


        int32_t i;



        /* Copy Buffer to Send Buff */

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


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

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


        g_recv_size = 0;




    if (g_send_size)


        uint8_t error;

        uint32_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);



        error = USB_Class_CDC_Send_Data(g_app_handle, DIC_BULK_IN_ENDPOINT,

            msg, 12U);





        if (error != USB_OK)


            /* Failure to send Data Handling code here */





    if( (waitfordatareceive))


        if(comopen == 1)



            comopen = 0;


        USB_PRINTF("Enter lowpower\r\n");

        usb_hal_khci_disable_interrupts((uint32_t)USB0, INTR_TOKDNE);

        POWER_SYS_SetMode(kDemoVlps, kPowerManagerPolicyAgreement);

        waitfordatareceive = 0;


        USB_PRINTF("Exit  lowpower\r\n");








I've tracked down where an error occurs and what the error message is. In the function usb_device_send_data, in the file, usb_dev.c, the send fails at this line:


error = (usb_dev_ptr->usb_dev_interface)->dev_get_xd(usb_dev_ptr->controller_handle, &xd_ptr);


with this error message being printed to standard out:


"usb_device_send_data: DEV_GET_XD failed"



I'm not sure what could be causing this error and why it is working initially, but eventually fails after several character echos occur. Any help or guidance is appreciated, I'm pretty stuck at the moment.