Understanding USB CDC functions

Discuss with MikroElektronika software developers about current library development.
Post Reply
Author
Message
PIC007
Posts: 179
Joined: 28 Aug 2013 18:28

Understanding USB CDC functions

#1 Post by PIC007 » 25 Jun 2015 17:15

Two questions please...

1. Is there meant to be a function in USB that checks if the USB is free before trying to send another packet?

In the test code below, I get strange behavior on the VCP terminal - normally only see "USB CDC Demo-2" coming out when I press the 's' key. If I put a small delay between the two 'CDC Sends' I get both strings, but not always.

Code: Select all

void USBDev_CDCDataReceived(uint16_t size)
{
  USBDev_CDCSetReceiveBuffer(buffer);

  if (buffer[0] == 's')
  {
    strcpy(buffer, "USB CDC Demo-1");
    USBDev_CDCSendData(buffer, strlen(buffer));
    
    strcpy(buffer, "USB CDC Demo-2");
    USBDev_CDCSendData(buffer, strlen(buffer));
  }
}
2. I can not understand the purpose of the USBDev_CDCSetLineCoding function. I am running the VCP demo program that comes with the USB CDC and note that the USBDev_CDCInit() function calls USBDev_CDCSetLineCoding(19200, 0, 0,8 )

The terminal I am connecting the VCP program to, seems not to mind what baud rate is set - it still communicates - and also changing the parameters in the initialization of USBDev_CDCSetLineCoding function seems to make no difference to speed or operation.

Please help me understand what this function is doing?

Thank you.

keshena
Posts: 68
Joined: 07 Dec 2013 09:37
Location: South Africa
Contact:

Re: Understanding USB CDC functions

#2 Post by keshena » 26 Jun 2015 13:59

Hi,
1. You must send data bit by bit, there you'll be guarantied to send all bits.
Do something like this:
// Create a function to send data to Uart when called
void Send_Data_To_Uart(char txt[64])
{
int i;
for(i = 0; i < strlen(txt); i++)
{
buffer = txt;
}
USBDev_CDCSendData(buffer, strlen(txt));
}

//Then when you want to send data, call this function:
Send_Data_To_Uart("USB CDC Demo-1\r\n");
delay_ms(500);
Send_Data_To_Uart("USB CDC Demo-2\r\n");

2. You probably don't see a big difference in speed when using the function calls USBDev_CDCSetLineCoding( ) with different parameters because you are probably sending a small amount of data.

Read this article to learn more on USB CDC, there is a nice example: http://www.studentcompanion.co.za/post/ ... ler-MikroC
Free Microcontroller Tutorials & Projects for Hobbyists and students from beginners to advanced.
Website: https://www.studentcompanion.co.za/cate ... o-for-pic/
Youtube Tutorials: https://www.youtube.com/user/StudentCompanionSA

PIC007
Posts: 179
Joined: 28 Aug 2013 18:28

Re: Understanding USB CDC functions

#3 Post by PIC007 » 26 Jun 2015 16:11

Dear Keshena,

Thank you for your reply, and link to the nice site (based on the same example from mikroE). Can you perhaps enlarge on the response you have provided to my two questions. You state:

1.
You must send data bit by bit, there you'll be guarantied to send all bits.
OK, but the code below is not really doing this. It is preparing a buffer with n characters (bytes), and then sending this in a single call to USBDev_CDCSendData.

Code: Select all

void Send_Data_To_Uart(char txt[64])
{
    int i;
    for(i = 0; i < strlen(txt); i++)
 {
    buffer[i] = txt[i];
 }
  USBDev_CDCSendData(buffer, strlen(txt));
}
In principle this does not seem too different to what I am doing using strcpy to prepare a buffer? The issue is the next string (or buffer of characters) you want to send. How do you check one has finished being transmitted before sending the next - or do you even need to check for this? It seems if I send a second string immediately after the first, with no delay, I only receive the second one.

2. To my question on LineCoding, I seem to be missing something fundamental in my understanding. If I connect HyperTerminal to the VCP program (to send and received the echoed characters), it seems irrelevant what baud rate I set on this terminal - it appears to work for all!

As explained in my original post, the call to USBDev_CDCInit() actually calls USBDev_CDCSetLineCoding(19200, 0, 0,8 ) behind the scenes. This would make me think that the HyperTerminal would need to be set to these same parameters, but I find other baud rates appear to work just as well to communicate with the VCP?

3. Related to this, can you also help me understand the significance of the 9600 baud setting for the VCP hardware port driver under Windows Device Manager? Again it seems unrelated to any of the values set in the VCP program, or on the HyperTerminal?
port1.jpg
port1.jpg (21.62 KiB) Viewed 6073 times
port2.jpg
port2.jpg (31.34 KiB) Viewed 6073 times
Thank you.
Last edited by PIC007 on 29 Jun 2015 12:36, edited 1 time in total.

keshena
Posts: 68
Joined: 07 Dec 2013 09:37
Location: South Africa
Contact:

Re: Understanding USB CDC functions

#4 Post by keshena » 29 Jun 2015 09:45

You must notice we are calling a void Send_Data_To_Uart(char txt[64]) function. In this function there is a for loop which send characters bit by bit depending on the size of the buffer.
Free Microcontroller Tutorials & Projects for Hobbyists and students from beginners to advanced.
Website: https://www.studentcompanion.co.za/cate ... o-for-pic/
Youtube Tutorials: https://www.youtube.com/user/StudentCompanionSA

PIC007
Posts: 179
Joined: 28 Aug 2013 18:28

Re: Understanding USB CDC functions

#5 Post by PIC007 » 29 Jun 2015 12:33

In this function there is a for loop which send characters bit by bit depending on the size of the buffer.
Keshena, please look more carefully and you will note it is not sending bit-by-bit! It is preparing a buffer bit-by-bit, and then sending this buffer in a single call to USBDev_CDCSendData.

Anyone else able to help me on the questions?

Thanks

Post Reply

Return to “Library Development Discussion”