2016年4月10日 星期日

usb main descriptor example


設備描述 ***
const uint8_t DeviceDescriptor[12] =
{
//bLength ,
0x12,
//beDescriptorType,說明這裡是用來描述""設備"描述
0x01,
//bcd USB,設定usb版本 1.1
0x10,
0x01,
//bDeviceClass ,不在設備描述中定義出設備類,而在interface Descriptor
//定義設備,故設為0
0x00,
//bDeviceSubClass,同上
0x00,
//bMaxPacketSize *,針對該usb interface EndPoint 0 的大小,
//在PDIUSBD12為16 B ****
0x10,
//idVender,廠商ID。
//實際廠品中不能隨便用,必須向usb協會申請
0x88,
0x88,
//idProduct,產品ID *****
0x01,
0x00,
//bcdDevice, 產品的版本號 *****
0x00,
0x01,
//iManufacturer,廠商的字串索引值。
//當值為0,表示沒有廠商字符串;非0時在主機獲取DeviceDescriptor時,會依此索引值
//放在 wValue中,來選擇不同的字串
0x01,
//iProduct ,產品字串索引值。必須不同 iManufacturer
//即在安裝設備時,系統所跳出的訊息
0x02,
//iSerialNumber,設備序號索引值,指定一個唯一的序號,然後聯合VID及PID來區分不同的設備
0x03,
//bNumConfiguration,該設備所具有的配置數
0x01
};

配置描述集合(配置/接面/端口及HID)
const uint8_t ConfigurationDescriptor[9+9+9+7] =
{
//configure Descriptor
//bLength
0x09,
//bDescriptorType, set 0x02 for Configure
0x02,
//wTotalLenght.配置描述集合的總長度。包含了 configure/interface/endpoint and HID
sizeof(ConfigurationDescriptor)&0xff,
sizeof(ConfigurationDescriptor)>>8 &0xff,
//bNumberInterface,
0x01,
//bConfigure
0x01,
//bmAttribute, **
//D4~D0,always set 0; D5, suport wake from remote; D6 ,device power; D7 ,always set 1
0x80,
//bMaxPower,unit 2mA
0x32,//50mA form bus

//interface descriptor
//bLength ,
0x09,
//bDescriptorType,interface descriptor type 0x04
0x04,
//bIntefaceNumber, start from 0 ,more than zero for use  ... device
0x00,
//bAlternateSetting,備用
0x00,
//bNumEndpoint, 非0 point數目。
0x01,
//bIntefaceClass ,該interface所使用的類。HID mouse is 0x03
0x03,
//bInterfaceSubClass, interface 的子類,HID 1.1 for usb mouse set 0x01
0x01,
//bInterfaceProtocol, keyboard 0x01;mouse 0x02
0x02
//iConfigure, interface的字串索引值,no set 0
0x00,

//HID descriptor
//bLength ,
0x09,
//bDescriptorType,HID descriptor type 0x21
0x21,
//bcdHID  ,HID version 1.1
0x10,
0x01,
//bCountyCode,國家代碼 USA
0x21,
//bNumberDescriptors,下級描述符數目
0x01,
//bDescriptorType, reporttype 0x22
0x22,
//bDescriptorLength
sizeof(ReportDescriptor) & 0xff,
(sizeof(ReportDescriptor) >>8)& 0xff,

//endpoint descriptor
//bLength ,
0x07,
//bDescriptorType,endport descriptor type 0x05
0x05,
//bEndpointAddress, in mouse only one input-endpoint 1
//D7,表示方向,input set 
0x81,
//bmAttributes,D1~D0
//endpoint 傳輸類型,interrupt 3
0x03,
//wMaxPacketSize.
0x10,
0x00,
//bInterval,
0x0A
};

const uint8_t ReportDescriptor[]=//**
{
    //每行的第一行為其該條目的前綴,格式為
    //D7~D4: bTag, D3~D2:bType, D1~D0, bSize
    //全局條目(bType = 1)條目,選擇用途頁為普通桌面(generic Desktop page)
    //其bSize =1,並接上 0x01
    0x05,0x01,      //USAGE_PAGE(generic Desktop page)
    //局部(bType =2)條目,說明接下來的應用集合用途用於mouse
    0x09,0x02,      //USAGE(mouse)
    //主條目(bType =0),開集合,而後面的數據0x01表示該集合是一個應用集合
    //它的性質在前面由usage_page 及 usage定義為 generic Desktop page mouse
    0xa1,0x01,      //Collection(Application)
    //局部條目,用途為指針集合
    0x09,0x01,       //USAGE(pointer)
    //主條目,開集合,數據0x00 表示為該?物理集合
    //用途由前面的局部條目定義為指針集合
    0xa1,0x00,      //Collection(physical)
    //全區條目,用途頁為按鍵(button page(0x09)
    0x05,0x09,      //USAGE_PAGE(button)
    //局部條目,用途最小值為 1,即左鍵
    0x19,0x01,      //USAGE)MINIMUM(button 1)
    //局部條目,用途最大值為 3,即中鍵
    0x29,0x03,      //USAGE)MAx(button 3)
    //全局條目,返回的數據的邏輯值,最小為0;最大為1
    0x15,0x00,      //LOGICAL)MINIMUM(0)
    //全局條目
    0x25,0x01,      //LOGICAL_MAXIMUM(1)
    //為全局條目,設定數據域最大數目3
    0x95,x03,       //REPORT_COUNT(3)
    //全局條目, 每個數據域的長度為1個位
    0x75,0x01,      //REPORT_SIZE(1)
    //主條目,說明有3個長度1位的數據域(數量及長度由上面的全局條目設定)
    //用來作為輸入,屬性為:data,var,abs. data 表示這些數據可以變動;var,表示
    //這些是獨立,abs 表示為絕對值
    //所以結果是,第一個數據域 0表示 按鍵1(右),第二個數據域 1 右鍵,第三數域 2表示
    //中鍵
    0x81,0x02,       //INPUT(data,var,abs)
    //全局條目,說明數據域數量 為1
    0x95,0x01,       //REPORT_COUNT(1)
    //全局條目,每個數據域長度為 5位
    0x75,0x05,       //REPORT_SIZE(5)
    //主條目,輸入,由前二個全局條目,得長度(5)及數量(1)
    //屬性為常數(即返回的值均為0)
    //在這只是為了湊齊1byte(前面只用了3位(左~中)而填充的數據而已沒有實際用途
    0x81,0x03,       //INPUT(data,var,abs)
    //全局條目,用途頁為普通桌面(generic Desktop page,0x01)
    0x05,0x01,       //USAGE_PAGE(generic Desktop)
    //局域條目,用途為X軸
    0x09,0x30,       //USAGE(x)
    //局域條目,用途為y軸
    0x09,0x31,       //USAGE(y)
    //局域條目,用途為w軸
    0x09,0x38,       //USAGE(wheel)
    //全局條目,說明返回的邏輯最小/大值
    //往右為,X+,下時,Y+,wheel up,w+
    015,0x81,         //LOGICAL_MINIMUM(-127)
    025,0x7f,          //LOGICAL_MAXIMUM(127)
    //全局,數據域長度8位
    0x75,0x08,        //REPORT_COUNT(8)
    //全局,數據數量3
    0x95,0x03,        //REPORT_COUNT(3)
    //主條目,由前全局(三個8 數據域)作為輸入. 屬性為:data,var,rel
    //第一個為X,Y,W. rel:相對值
    0x81,0x06,       //input(data,var,rel
    //關集合,並設bSize=0
    0xc0,
    0xc0
};

在 "設備描述符中,有廠商,產品及序號"
const uint8_t LanguageID[4] =
{
0x04,// bLength
0x03,//string type
//for usa
0x09,
0x04
};

//ManufactureStringDescriptor
http://www.ifreesite.com/unicode-ascii-ansi.htm
您好USB設計
\u60a8\u597dUSB\u8a2d\u8a08
const uint8_t ManfactureStringDescriptor[] =
{
0x10 ,//lenght
0x03,//for string type
0xa8,0x60,
0x7d,0x59,
0x55,0x00,
0x53,0x00,
0x42,0x00,
0x2d,0x8a,
0x08,0x8a
}

//ProductStringDescriptor,即安裝時跳出訊息
http://www.ifreesite.com/unicode-ascii-ansi.htm
自己做滑鼠
 \u81ea\u5df1\u505a\u6ed1\u9f20
const uint8_t ProductStrinDescriptor[] =
{
  11,
  .....
};

//產品序號
//1234
const uint8_t SerialNumberStringDescriptor[] =
{
10, //blenght
0x03, //string type
0x31,0x00,
0x32,0x00,
0x33,0x00,
0x34,0x00
};

沒有留言:

張貼留言