Unable to send strings through USB CDC on K21F

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Unable to send strings through USB CDC on K21F

Jump to solution
1,448 Views
mjohn
Contributor III

KDS 3.0.0

KSDK 1.2.0

uPC: MK21FN1M0Axxx12

 

 

Hello,

 

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 USBCFG_DEV_KEEP_ALIVE_MODE

#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)

    if( (waitfordatareceive))

    {

        if(comopen == 1)

        {

            OS_Time_delay(30);

            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_hal_khci_enable_interrupts((uint32_t)USB0,INTR_TOKDNE);

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

    }

#endif

#endif

    return;

}

 

 

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.

 

Thanks

Labels (1)
0 Kudos
1 Solution
764 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi matt johnson,

Calling USB_Class_CDC_Send_Data() twice is not a correct way, it needs tp wait for an event called USB_DEV_EVENT_SEND_COMPLETE , which can be referred in C:\Freescale\KSDK_1.2.0\doc\usb\USB Stack Device Reference Manual.pdf, page 27, to start next send transfer, our CDC virtual com example is implemented in that way.

so if you want to append a msg after each echo data, you may send them both with one calling of USB_Class_CDC_Send_Data(), or send the msg after the event of USB_DEV_EVENT_SEND_COMPLETE.

Hope that helps,


Have a great day,
Kan

Freescale Technical Support
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
5 Replies
764 Views
tmeyer
Contributor III

Hi,

I had a which produced similar errors when using the with the virtual com and MQX with KSDK. I was blowing out one of my task stacks. It is worth a check if you haven’t already.

Tim

0 Kudos
764 Views
mjohn
Contributor III

Hey, thanks for the advice! I checked the stacks on my tasks with the Task Aware Debugger, and none of them seemed to have overflowed, they were about 50% capacity at the time I checked. I increased the stack size for all of the tasks, but it did not seem to change the results - the virtual com port would fail in the same fashion.

0 Kudos
765 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi matt johnson,

Calling USB_Class_CDC_Send_Data() twice is not a correct way, it needs tp wait for an event called USB_DEV_EVENT_SEND_COMPLETE , which can be referred in C:\Freescale\KSDK_1.2.0\doc\usb\USB Stack Device Reference Manual.pdf, page 27, to start next send transfer, our CDC virtual com example is implemented in that way.

so if you want to append a msg after each echo data, you may send them both with one calling of USB_Class_CDC_Send_Data(), or send the msg after the event of USB_DEV_EVENT_SEND_COMPLETE.

Hope that helps,


Have a great day,
Kan

Freescale Technical Support
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
764 Views
mjohn
Contributor III

Thank you! That explains a lot! I changed some of my code around to adhere to waiting on the send complete event and now everything is working!

I have just one quick question, are you able to use the USB_Class_CDC_Send_Data and USB_Class_CDC_Recv_Data functions simultaneously in different tasks? Or should I be scheduling these two functions so that they are never executing at the same time?

Thanks again!

0 Kudos
764 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi matt johnson,

There is no problem to use USB_Class_CDC_Send_Data and USB_Class_CDC_Recv_Data in different tasks, you know, when MCU run one task, the others are in pending state till the time slice is expired or the higher priority task release the MCU, so USB_Class_CDC_Send_Data and USB_Class_CDC_Recv_Data should work well independently.


Have a great day,
Kan

Freescale Technical Support
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------