USB CDC Library documentation.


1. Declaration of the user's Receive and SendBuffers

This is done like:
var 
    ReceiveBuffer: array[64] of byte;
    SendBuffer:    array[20] of byte;
The maximum value for both buffersizes is 64 (that is the size of the īnternal USB_CDC buffers). It is preferred that the receivebuffer is 64 bytes, otherwise parts or usb data could be lost.

2. The interrupt procedure

In the interrupt procedure of the application the CDC processing procedure should be called. This procedure does all CDC-USB processing (including handling of the "internal" USB_CDC send and receivebuffers) based on interrupts received from the PIC's SIE (the USB engine).
This is to be done like this:
procedure interrupt;
begin
  USB_Interrupt; // USB_CDC processing procedure
end;
The CDC initialization procedure ("InitUsb") takes care of the enabling of the necessary interrupts.

Important: There is no need to "force" extra interrupts, e.g. via a timer, the "USB_Interrupt" procedure will only do something when actually an inperrupt came from the SIE (the PIC's USB engine).

3. Initialization

The initialization of the USB_CDC software is like
  InitUsb;                    // Init USB_CDC and start the enumeration process
  repeat until ConfiguredUsb; // wait for the completion of the USB enumeration    
As you can see, the initialisation of USB_CDC and starting up of the USB enumeration procedure is done with a call to "InitUsb". This procedure does not wait for the USB enumeration process to complete.
The completeness of the enumeration process can be checked with "ConfiguredUsb": it returns true when emumeration is complete. One should wait for a complete enumeration before attempting further USB_CDC activities.

4. Receiving data (PC --> PIC)

Receiving data is done like this:
  NrofBytesReceived := Read USB_CDC_Read(@ReceiveBuffer, NrBytesToReceive);
  if NrofBytesReceived > 0 then
  begin
    // process received data here
  end;
As you can see, this routine takes two parameters: the address of the user's receivebuffer and the number of bytes "to receive". The latter is in fact the maximum number of bytes to be copied from the "internal USB_CDC receivebuffer" into the user's receivebuffer (again, it is preferred that the receivebuffer's size is 64 bytes to prevent loss of usb data). The function returns the actual bytes that are received and copied into the user's receivebuffer.
Make sure that "NrBytesToReceive" is smaller than or equal to the size of the user's receivebuffer.

5. Sending data (PIC --> PC)

Sending data is done like this:
  repeat until USB_CDC_Write(@SendBuffer, NrBytesToSend); 
Be carefull: this can block your software (it is waiting for CDC_Write to succeed).

As you can see, two parameters are required by this function: the address of the sendbuffer and the number of bytes to be sent.
Make sure the "NrBytesToSend" is smaller than or equal to the size of the user's sendbuffer.

The function "USB_CDC_Write" returns "true" if success (data was accepted and will be sent), "false" if no success (data is not accepted to be sent -- the previous send operation was not finished yet, the "internal USB_CDC sendbuffer is still occupied --).

Remark: "USB_CDC_Write" does not wait for the actual transmission from the PIC to the PC to happen. It only "prepares" things: it copies data from the user's "SendBuffer" into the "internal" CDC-USB sendbuffer and tells it to send. After this the SIE (the PIC's USB engine) takes care of the actual send action.

6. Technical data



-------------------------------------------