Tải bản đầy đủ (.pdf) (9 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (97.18 KB, 9 trang )

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


a bn. Ngoài ra còn mt file na là:
- #include .ng dn/ usb_demo_bulk.h>
File này không có sn trong CCS nh các file trên. Nó c to ra khi bn lp
t Project trên CCS qua PIC Wizard, tên file do bn t.
Nguyn Ngc Hng Lp TKTL 40 HVKTQS
- 2 -
4. Các hàm u khin và giao tip USB:
Khi xem các mã ngun ca các file trên trong CCS, bn s thy rt nhiu hàm và
nh ngha khó hiu. Nhng phn ln bn s không cn quan tâm ti các hàm ó
vì chúng c xây dng  trình biên dch s dng. Cái chúng ta quan tâm ch là
p hàm “User Functions” mà CCS ã xây dng sn:
//// **************** USER FUNCTIONS *********************** ////
//// ////
//// usb_init() - Initializes the USB stack, the USB peripheral and ////
//// attaches the unit to the usb bus. Enables ////
//// interrupts. ////
//// ////
//// usb_init_cs() - A smaller usb_init(), does not attach unit ////
//// to usb bus or enable interrupts. ////
//// ////
//// usb_put_packet() - Sends one packet to the host. ////
//// If you need to send a message that spans ////
//// more than one packet then see usb_puts() in ////
//// usb.c ////
//// ////
//// usb_kbhit() - Returns true if OUT endpoint contains data from ////
//// host. ////
//// ////
//// usb_rx_packet_size() - Returns the size of packet that was ////
//// received. usb_kbhit() must return TRUE else ////

//// 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. ////
//// ////

//// The other functions defined in this file are for use by the ////
//// USB code, and is not meant to be used by the user. ////
Các bn có th d dàng tìm hiu thêm cách thc s dng các hàm này qua các
Example và các Comment ca CCS. Vi các hàm này bn ã có thu khin
modul USB ca pic18 khá linh hot và có th m rng chúng  phù hp vi
c ích ca bn.
5. o li file mô t thit b usb_desc_scope1.hc thc hin nh sau:
#DEFINE USB_TOTAL_CONFIG_LEN 32 //config+interface+class+endpoint
//configuration descriptor
char const USB_CONFIG_DESC[] = {
//config_descriptor for config index 1
USB_DESC_CONFIG_LEN, //length of descriptor size
USB_DESC_CONFIG_TYPE, //constant CONFIGURATION (0x02)
USB_TOTAL_CONFIG_LEN,0, //size of all data returned for this config
1, //number of interfaces this device supports
0x01, //identifier for this configuration. (IF we had more than one
configurations)
0x00, //index of string descriptor for this configuration
0xC0, //bit 6=1 if self powered, bit 5=1 if supports remote wakeup (we don't),
bits 0-4 reserved and bit7=1
0x32, //maximum bus power required (maximum milliamperes/2) (0x32 =
100mA)
//interface descriptor 0 alt 0
USB_DESC_INTERFACE_LEN, //length of descriptor
USB_DESC_INTERFACE_TYPE, //constant INTERFACE (0x04)
0x00, //number defining this interface (IF we had more than one interface)
0x00, //alternate setting
2, //number of endpoints, not counting endpoint 0.
0xFF, //class code, FF = vendor defined
0xFF, //subclass code, FF = vendor

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

//for example, if config 1 has 1 interface and config 2 has 2 interfaces you must define this
as 2
#define USB_MAX_NUM_INTERFACES 1
//define how many interfaces there are per config. [0] is the first config, etc.
const char USB_NUM_INTERFACES[USB_NUM_CONFIGURATIONS]={1};
#if (sizeof(USB_CONFIG_DESC) != USB_TOTAL_CONFIG_LEN)
#error USB_TOTAL_CONFIG_LEN not defined correctly
#endif
//////////////////////////////////////////////////////////////////
///
/// start device descriptors
///
//////////////////////////////////////////////////////////////////
//device descriptor
char const USB_DEVICE_DESC[] ={
USB_DESC_DEVICE_LEN, //the length of this report
0x01, //constant DEVICE (0x01)
0x10,0x01, //usb version in bcd
0x00, //class code (if 0, interface defines class. FF is vendor defined)
0x00, //subclass code
0x00, //protocol code
USB_MAX_EP0_PACKET_LENGTH, //max packet size for endpoint 0. (SLOW
SPEED SPECIFIES 8)
0xd8,0x04, //vendor id (0x04D8 is Microchip)
0x01,0x01, //product id
0x00,0x01, //device release number
Nguyn Ngc Hng Lp TKTL 40 HVKTQS
- 5 -
0x01, //index of string description of manufacturer. therefore we point to
string_1 array (see below)

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

46, //length of string index
USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
'D',0,
'a',0,
'T',0,
'a',0,
' ',0,
'T',0,
'r',0,
'a',0,
'n',0,
's',0,
'f',0,
'e',0,
'r',0,

×