Microchip MCU PIC18F4550 Example
This section shows the USB HID Device comunicacion process between the host (on GNU/Linux Ubuntu 10-04 32 Bits), and a microcontroler designed to support USB comunication natively without use an external chip.
The MCU used for this example is a Microchip MCU PIC18F4550 (see http://www.microchip.com),and the source code for the MCU was written in ANSI C and compiled with the SDCC (Small Device C Compiler http://sdcc.sourceforge.net/). The USB HID library used with the MCU was a variation of this http://www.nutsvolts.com/index.php?/magazine/article/usb_device_control made by Enrique Olivares, the Copyright is the next:
Firmware framework for USB I/O on PIC 18F2455 (and siblings)
Copyright (C) 2005 Alexander Enzmann
adapted to MCC18 by Alberto Maccioni on 1/8/09
re-adapted to SDCC 2.9.0 by Enrique Olivares (EOL) on 22/06/10
The re-adapted version was presented on this forum http://pic-linux.foroactivo.net/ (Spanish's Forum)
username@localhost:$ sudo groupadd group microchip
username@localhost:$ sudo adduser username microchip
SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="900A", MODE="0666", GROUP="microchip" SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="010B", MODE="0666", GROUP="microchip"
username@localhost:$ sudo shutdown -r now
In this example the script will send a single instruction to the MCU PIC 18F4550 and receive an array of 5 bytes. The MCU PIC 18F4550 will send back an array of 64 bytes but the hid_read only takes the first 5.
n=5; txBuff=init_buff(n); for i=1:n txBuff=setBuffValue(txBuff,i,i); end VendorID="04D8"; ProductID="010B"; VendorID=uint16(hex2dec(VendorID)); ProductID=uint16(hex2dec(ProductID)); init_success=hid_init(); if ( init_success < 0 ) then disp("unable to initialize the HIDAPI Library"); return else disp("HIDAPI Library initialized"); end open_success=hid_open(VendorID,ProductID); if ( open_success < 0 ) then disp("unable to open device"); return else disp("device open"); end write_success=hid_write(txBuff,uint8(n)); if ( write_success < 0 ) then disp("unable to write device"); else disp("device have been written"); end nonblocking_success=hid_set_nonblocking(%t); if ( nonblocking_success < 0 ) then disp("unable to set nonblocking"); else disp("device have been set nonblocking"); end [ rxBuff , read_success ]=hid_read(uint8(n)); if ( read_success < 0 ) then disp("unable to read device"); else disp("device have been read"); disp(rxBuff); end close_success=hid_close(); if ( close_success < 0 ) then disp("unable to closed device"); else disp("device closed"); end exit_success=hid_exit(); if ( init_success < 0 ) then disp("unable to free static data associated with HIDAPI Library"); return else disp("HIDAPI Library have been freed all of the static data associated"); end | ![]() | ![]() |
For some reason the script can not get the strings of the PIC18F4550 example, so instead will get the strings of a PICkit 3 Clone
clc VendorID="04D8"; ProductID="900A"; VendorID=uint16(hex2dec(VendorID)); ProductID=uint16(hex2dec(ProductID)); init_success=hid_init(); if ( init_success < 0 ) then disp("unable to initialize the HIDAPI Library"); return else disp("HIDAPI Library initialized"); end open_success=hid_open(VendorID,ProductID); if ( open_success < 0 ) then disp("unable to open device"); return else disp("Device Open"); end [manufacturer_string,manufacturer_success]=hid_get_manufacturer() if ( manufacturer_success < 0 ) then disp("unable to get manufacturer string"); close_success=hid_close(); exit_success=hid_exit(); return else disp(manufacturer_string); end [product_string,product_success]=hid_get_product() if ( product_success< 0 ) then disp("unable to get product string"); close_success=hid_close(); exit_success=hid_exit(); return else disp(product_string); end [serial_string,serial_success]=hid_get_serial() if ( serial_success < 0 ) then disp("unable to get serial string"); close_success=hid_close(); exit_success=hid_exit(); return else disp(serial_string); end disp("using hid_get_string"); for i=1:3 [indexed_string,string_success]=hid_get_string(uint16(i)) if ( string_success < 0 ) then disp("unable to get indexed string"); close_success=hid_close(); exit_success=hid_exit(); return else disp(indexed_string); end end close_success=hid_close(); if ( close_success < 0 ) then disp("unable to closed device"); else disp("device closed"); end exit_success=hid_exit(); if ( init_success < 0 ) then disp("unable to free static data associated with HIDAPI Library"); return else disp("HIDAPI Library have been freed all of the static data associated"); end hid_exit() | ![]() | ![]() |
HIDAPI Library initialized Device Open Microchip Technology Inc. PICkit 3 DEFAULT_PK3 using hid_get_string Microchip Technology Inc. PICkit 3 DEFAULT_PK3 device closed HIDAPI Library have been freed all of the static data associated | ![]() | ![]() |
In this case there are two HID Devices conected to the host. The script will enumerate one by one and will show the Manufacturer and Product strings of each conected HID Devices
enum_success=hid_enumerate(); if( ~( enum_success < 0 ) ) then success_next = 0; M_Vendor=list(); M_Product=list(); i=0; while( ~( success_next < 0 ) ) i=i+1; [success_dev,vendor_id,product_id]=get_device_string (); M_Vendor($+1)=vendor_id; M_Product($+1)=product_id; success_next=next_device(); end free_success=hid_free_enumeration(); Devices=zeros(i,2) for k=1:i Devices(k,1)=M_Vendor(k) Devices(k,2)=M_Product(k) end disp(Devices); end | ![]() | ![]() |