Tài liệu Tài liệu hướng dẫn tự làm thiết bị USB (Phần 2) - Pdf 86

Nguyn Ngc Hng Lp TKTL 40 HVKTQS
- 1 -
Phn 2:
Vit Fimware cho Pic18f4550 ( hoc Pic18f2550)
1. Trình biên dch:
Có th vit Fimware bng tt c các trình biên dch thông dng, song theo ý kin
a tôi CCS là trình biên dch h tr vit Fimware cho chíp USB khá tt. Trong
ví d ca CCS có các ví d cho c HID, Costume Driver và CDC. ng thi các
th vin hàm cho USB c xây dng tng i thun li khi s dng vì vy rt
thun li  các bn có th nhanh chóng thc hin USB.
2. Các thc th nghim:
Sau khi phn cng ã c u ni y  bn có th np th
các các fimware có sn tng thích vi phn cng và cm thit b vào máy tính.
u phn cng úng và fimware tng thích máy tính s thông báo “Found
New Hardware” và òi hi cài t Driver cho thit b. Cn lu ý nu phn mn
trong chíp cài t tn s thch anh không khp vi thch anh phn cng s gây
ra li và máy tính không th nhn din thit b. Nu khi ã tng thích v thch
anh mà máy tính vn không detect thit b thì có th phn cng ca bn vn còn
n  hoc cng USB có vn  v tip xúc hãy kim tra k.
u thit b ca bn không b li và fimware chính xác:
Khi nó òi Driver hãy tm thi b qua và thc hin to Driver mi cho nó bng
phn mm WinDriver, khi to xong Driver thì thit b sc tng update
driver, chi tit vic to driver c hng dn  phn 3.
3. Các th vin và hàm s dng chính  vit USB:
Có 3 file bn cn include vào project CCS ca bn là:
- #include <pic18_usb.h>
- #include <usb.c>
- #include <usb_desc_scope1.h>
Hai file trên có sn trong th vin ca CCS cha các nh ngha và các hàm phc
 cho giao tip USB, file th 3 là file mô t thit b c chnh sa t file
usb_desc_scope.h ng có sn trong th vin ca CCS  phù hp vi yêu cu

//// this is not valid. Don't forget in USB there ////
//// are 0 len packets! ////
//// ////
//// usb_get_packet() - Gets one packet that from the host. ////
//// usb_kbhit() must return true before you call ////
//// this routine or your data may not be valid. ////
//// Once usb_kbhit() returns true you want to ////
//// call this as soon as possible to get data ////
//// out of the endpoint buffer so the PC can ////
//// start sending more data, if needed. ////
//// This only receives one packet, if you are ////
//// trying to receive a multi-packet message ////
//// see usb_gets() in usb.c. ////
//// ////
//// usb_detach() - De-attach USB from the system. ////
//// ////
//// usb_attach() - Attach USB to the system. ////
//// ////
//// usb_attached() - Returns TRUE if the device is attached to a ////
//// USB cable. A macro that looks at the defined ////
//// connection sense pin. ////
//// ////
//// usb_task() - Keeps track of connection sense, calling ////
//// usb_detach() and usb_attach() when needed. ////
//// ////
//// For more documentation on these functions read the comments at ////
Nguyn Ngc Hng Lp TKTL 40 HVKTQS
- 3 -
//// each function. ////
//// ////

0xFF, //protocol code, FF = vendor
0x00, //index of string descriptor for interface
//endpoint descriptor
USB_DESC_ENDPOINT_LEN, //length of descriptor
USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (0x05)
0x81, //endpoint number and direction (0x81 = EP1 IN)
0x02, //transfer type supported (0 is control, 1 is iso, 2 is bulk, 3 is interrupt)
USB_EP1_TX_SIZE & 0xFF,USB_EP1_TX_SIZE >> 8, //maximum packet size
supported
0x01, //polling interval in ms. (for interrupt transfers ONLY)
//endpoint descriptor
Nguyn Ngc Hng Lp TKTL 40 HVKTQS
- 4 -
USB_DESC_ENDPOINT_LEN, //length of descriptor
USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (0x05)
0x01, //endpoint number and direction (0x01 = EP1 OUT)
0x02, //transfer type supported (0 is control, 1 is iso, 2 is bulk, 3 is interrupt)
USB_EP1_RX_SIZE & 0xFF,USB_EP1_RX_SIZE >> 8, //maximum packet size
supported
0x01, //polling interval in ms. (for interrupt transfers ONLY)
};
//****** BEGIN CONFIG DESCRIPTOR LOOKUP TABLES ********
//since we can't make pointers to constants in certain pic16s, this is an offset table to find
// a specific descriptor in the above table.
//NOTE: DO TO A LIMITATION OF THE CCS CODE, ALL HID INTERFACES MUST
START AT 0 AND BE SEQUENTIAL
// FOR EXAMPLE, IF YOU HAVE 2 HID INTERFACES THEY MUST BE
INTERFACE 0 AND INTERFACE 1
#define USB_NUM_HID_INTERFACES 0
//the maximum number of interfaces seen on any config

0x02, //index of string descriptor of the product
0x00, //index of string descriptor of serial number
USB_NUM_CONFIGURATIONS //number of possible configurations
};
//////////////////////////////////////////////////////////////////
///
/// start string descriptors
/// String 0 is a special language string, and must be defined. People in U.S.A. can leave this
alone.
///
/// You must define the length else get_next_string_character() will not see the string
/// Current code only supports 10 strings (0 thru 9)
///
//////////////////////////////////////////////////////////////////
//the offset of the starting location of each string.
//offset[0] is the start of string 0, offset[1] is the start of string 1, etc.
const char USB_STRING_DESC_OFFSET[]={0,4,12};
#define USB_STRING_DESC_COUNT sizeof(USB_STRING_DESC_OFFSET)
char const USB_STRING_DESC[]={
//string 0
4, //length of string index
USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
0x09,0x04, //Microsoft Defined for US-English
//string 1
8, //length of string index
USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
'B',0,
'M',0,
'E',0,
//string 2


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status