Help to fix custom CDC_Send function

Discussion created by lpcware Employee on Jun 15, 2016
Content originally posted in LPCWare by crudo on Sun Sep 15 19:22:40 MST 2013

I am using a custom code of CDC example from 177x_8x_PDL_110602 package (I am not using LPCOpen).
I am having a trouble with CDC_Send function. That is the scenary: An python script send messages to LPC1788 serial USB, they are send time to time (200ms,300ms). The message is: "ping\x00", without quotes. When the LPC received the ping message it send back to PC this message: "resp 0\x00". After any time of messages interchange (perfectly), the LPC begins send the message wrong, like this: "r 0\x00", "sp 0\x00", "esp 0\x00", "r\x00", "\x00", ...

Bellow is the code that send the message:

void CDC_Send(const uint8_t *data, uint32_t data_size)
    // block until all bytes be sent
    while (ringbuff_size(g_tx_buffer) > 0);

    // puts the data into ring buffer
    ringbuff_write(g_tx_buffer, data, data_size);

    // forces the first send

void CDC_BulkIn(void)
    uint8_t buffer[USB_CDC_BUFSIZE];
    uint32_t bytes_to_write;

    if (!g_cdc_initialized) return;

    memset(buffer, 0, USB_CDC_BUFSIZE);

    bytes_to_write = ringbuff_read(g_tx_buffer, buffer, USB_CDC_BUFSIZE);

    if (bytes_to_write > 0)
        USB_WriteEP(CDC_DEP_IN, buffer, bytes_to_write);

The SetMode (command 0xF3) use the reset value (zero), so don't happen interrupts by NAK.
After a lot of time debuging I tried to put a delay after USB_WriteEP function, the delay value is 100us. In this way the messages interchange don't fail. So, I would like to fix this (remove the delay). Any idea?