Saturday, May 9, 2015

PIC18f - USB - CDC

The most two common USB protocols are HID and CDC. Most of the popular open source projects which use the microchip are using the CDC for interfacing with PC. For example, the popular bus pirate, logic scrimp, ir toy are using CDC. The dangerous prototype has their open source USB stack so the bus pirate, logic  scrimp, logic pirate are all using the Honken USB Stack source. Another popular open source stack is M-Stack (Free USB Stack for PIC 16F, 18F, 24F, and 32MX Microcontrollers), it has more organised source code and more detail commented. However it only has support on x series compiliers like xc8, xc16, and xc32.
Last, is the microchip default USB stacks, the most common, least problem and abundant of examples. Shouldn't be troublesome if you are using microchip library and microchip compiler. In this tutorial, I am using microchip library.

     I like CDC over HID because it has higher transfer rate, and it appeared as serial communication. The latter feature ensures all of the terminal software can be used. My favourite terminal software is termite,

it is easy to use, no installation required, auto detect and auto configure the serial port.
There are other popular terminal software like tera-term, and real term. Both of these are popular among hobbies as the functions provided are vast and stable. Nevertheless, the learning curve for both software are longer. Whereas, Termite is like a software for dummy, basically no learning curve at all.
To start a project USB CDC project, the easiest way is modifying the Microchip example. Leaving the example file as it is, copy all the source codes to your workspace as shown in the figure below.


It is hard to locate all source file but you can refer back to here, it is more or less the same.

For the USB to run at full speed, the USB need to operate at 48Mhz. The below configuration is to scale the frequency to 48Mhz (everything is at the example file).

This example is going to do three things,
First is the detection of button pressed,

 and if the button is pressed, then "Button Pressed" is printed as shown at code above.

Second is the sending the exact strings with a plus one back to PC as shown at code below:


The third thing is the indication of USB states.



Since the CDC acts like series communication, at the usb_function_cdc,c mentions the serial communication configuration:

You can try to modify the parameters but after some googling, I found out that these parameters are dummy and has not effect when you setting up the serial communication.

...................................................................................................................................................................
After you program everything into PIC, and plug the PIC into PC you will see this message,


You need to go device manager and manually detect the USB driver, you need to click "Browse my computer for driver software"

the driver is provided by MICROCHIP
Usually located at "C:\microchip_solutions_v2013-06-15\USB\Device - CDC - Basic Demo\inf".

If the driver is successfully installed, following will be appeared


.................................................................................................................................................................
Time to play, double click on the Termite terminal software, and you will see the following

Everything is automatically configured. If you click at the "Settings" icon, you can see this
which is different from what we programmed in PIC. It can be concluded the setting in source code doesnt effect the setting at PC side.

Try type any character in the termite terminal and you can the exact plus one back from PIC.


I am using the cytron development kit SK40c, so when I press the SW2 button on the board, I can get the string "Button Pressed"

Since I solely supply the board using the supply from USB, I didnt test the USB status led blinking.

The source file created for SK40c is shared in github.