fl \ 2012/11/16 16:08:11 hex : Print_data last_transfer_size W@ 0 do XfrData i + C@ . loop ; : print_char last_transfer_size W@ 2 do XfrData i + C@ emit 2 +loop ; : tab 9 emit ; : 2tab tab tab ; : 3tab tab tab tab ; \ Display string \ ( n1 n2 -- ) n1:string index n2:string's top address : dispStr swap dup 0 <> if 0 do dup C@ + 1+ loop else drop then .cstr ; \ Display attribute \ ( n -- ) n:attribute data : dispAttribute 1 3 0 do 2dup and if 1 else 0 then i 2 u* + Attribute dispStr i 2 < if ." ," then 1 lshift loop 2drop ; \ Display USB device descriptor and datas from USB device \ ( -- ) : desc hex init_host detect_device d200 waitframe \ ." Issuing USB bus reset" cr \ Reset usb-bus(Send reset-signal[50msec] to peripheral device) BUSRST HCTL Hwreg \ Wait until BUSRST is cleared begin HCTL Hrreg BUSRST and if 0 else 1 then until \ Wait some frames before programming any transfers d200 waitframe \ ." enu1 " st? \ Get the device descriptor 8 maxPacketSize W! \ First request goes to address 0 0 PERADDR Hwreg \ ." enu1 " st? 8 Get_Descriptor_Device 6 + C! \ Get_Descriptor_Device[6]=8 low wLength 0 Get_Descriptor_Device 7 + C! \ Get_Descriptor_Device[7]=0 high wLength \ ." First 8 bytes of Device Descriptor" cr \ Get device-descriptor into XfrFata array \ Get_Descriptor_Device 8 0 do C@++ . loop drop cr Get_Descriptor_Device CTL_Read dup \ ." enu2 " st? if print_error else drop \ Show NAK count for data stage/status stage \ ." NAK count:" IN_nak_count W@ . cr XfrData 7 + C@ maxPacketSize W! \ Print_data cr \ ." EP0 maxPacketsize is " maxPacketSize W@ . ." bytes." cr cr \ Issue another USB bus reset" \ ." Issuing USB bus reset" cr \ Reset usb-bus(Send reset-signal[50msec] to peripheral device) BUSRST HCTL Hwreg \ Wait until BUSRST is cleared begin HCTL Hrreg BUSRST and if 0 else 1 then until \ Wait some frames before programming any transfers d200 waitframe \ Set_Address_to_7 ( note:this request goes to address 0,already in PERADDR register) \ ." Setting address to 0x07" cr \ Set_Address_to_7 8 0 do C@++ . loop drop cr Set_Address_to_7 CTL_Write_ND dup if print_error else drop \ Device gets 2msec recovery time d30 waitframe \ now all transfers go to address 7 7 PERADDR Hwreg \ Get device-descriptor at the assigned address XfrData C@ Get_Descriptor_Device 6 + C! \ Get_Descriptor_Device[6] low wLength ." Device Descriptor ( " Get_Descriptor_Device CTL_Read dup if print_error else \ -----------------Device Descriptor ------------------------------------------ drop IN_nak_count W@ . ." NAKS )" cr \ Print_data cr ." Descriptor Size:" 2tab XfrData C@ . cr ." Descriptor Type:" 2tab XfrData 1+ C@ . cr ." USB version:" 3tab XfrData 3 + C@ h30 + emit 2E emit XfrData 2 + C@ . cr ." Device class:" 3tab XfrData 4+ C@ . cr ." Device subclass:" 2tab XfrData 5 + C@ . cr ." Device Protocol:" 2tab XfrData 6 + C@ . cr ." EP0 MaxPacketsize:" 2tab XfrData 7 + C@ . cr ." Vendor ID:" 3tab XfrData 8 + C@ XfrData 9 + C@ h100 u* + . cr ." Product ID:" 3tab XfrData d10 + C@ XfrData d11 + C@ h100 u* + . cr ." Device release:" 3tab XfrData d12 + C@ XfrData d13 + C@ h100 u* + . cr XfrData d14 + C@ iMFG W! XfrData d15 + C@ iPROD W! XfrData d16 + C@ iSERIAL W! XfrData d17 + C@ numConf W! \ Get String Descriptor 0 str 2+ C! \ index 0 is language ID string 0 str 4 + C! \ lang ID is 0 0 str 5 + C! 4 str 6 + C! \ wLengthL 0 str 7 + C! \ wLengthH \ str 8 0 do C@++ . loop drop cr str CTL_Read 0= \ Get lang ID string if \ Check for ACK (could be a STALL if the device has no strings) ." (Language ID String Descriptor is " Print_data ." )" XfrData 2+ C@ str 4 + C! \ LangID-L XfrData 3 + C@ str 5 + C! \ LangID-H hFF str 6 + C! \ now request a really big string else ." (There is no Lang string)" then cr ." Manufacture str index:" 2tab iMFG W@ dup . if iMFG W@ str 2+ C! str CTL_Read drop \ Get Manufacturer ID string print_char then cr ." Product str index:" 2tab iPROD W@ dup . if iPROD W@ str 2+ C! str CTL_Read drop \ Get Product ID string print_char then cr ." Serial number str index:" tab iSERIAL W@ dup . if iSERIAL W@ str 2+ C! str CTL_Read drop \ Get Serial Number ID string print_char then cr ." Number of Configuration:" tab numConf W@ . cr cr cr then \ -----------------Configuration Descriptor ------------------------------------------ ." Configuration Descriptor (" \ Get the 9-byte configuration descriptor \ ." 9Byte Configuration Descriptor ( " 9 Get_Descriptor_Config 6 + C! 0 Get_Descriptor_Config 7 + C! Get_Descriptor_Config CTL_Read dup 0= \ Get config descriptor into XfrData[] if drop IN_nak_count W@ . ." NAKS )" cr \ Print_data \ Get Full length of all descriptor(Config,Interfase,Endpoint,Class) XfrData 2+ C@ Get_Descriptor_Config 6 + C! \ LengthL XfrData 3 + C@ Get_Descriptor_Config 7 + C! \ LengthH Get_Descriptor_Config CTL_Read drop \ Get config descriptor into XfrData[] \ Print_data cr \ optional configuration string XfrData 6 + C@ iCONFIG W! XfrData 7 + C@ Attr W! XfrData 8 + C@ Power W! XfrData h11 + C@ iINTERFACE W! XfrData h19 + C@ XfrData h1A + C@ hFF u* + wDESCLENGTH W! ." Descriptor Size:" 2tab XfrData C@ . cr ." Descriptor Type:" 2tab XfrData 1+ C@ . cr ." TotalLength:" 3tab XfrData 2+ C@ XfrData 3 + C@ d256 u* + . cr ." Number of Interface:" 2tab XfrData 4 + C@ . cr ." Configuration Value:" 2tab XfrData 5 + C@ . cr ." Configuration str index:" tab XfrData 6 + C@ dup . dup 0 <> if str 2+ C! str CTL_Read drop print_char else drop then cr ." Attribute:" 3tab Attr W@ dup . h40 and 0> if ." self-powered " else ." bus powered " then cr ." MaxPower:" 3tab Power W@ dup . 2 u* decimal . hex ." mA" cr cr \ ----------------- Interface Descriptor ------------------------------------------ Get_Descriptor_Config CTL_Read drop \ Print_data cr ." Interface Descriptor " cr ." Descriptor Size:" 2tab XfrData d9 + C@ . cr ." Descriptor Type:" 2tab XfrData d10 + C@ . cr ." Interafce Number:" 2tab XfrData d11 + C@ . cr ." Alternate Setting:" 2tab XfrData d12 + C@ . cr ." Number of EndPoint:" 2tab XfrData d13 + C@ . cr ." Interface Class:" 2tab XfrData d14 + C@ . cr ." Interface SubClass" 2tab XfrData d15 + C@ . cr ." Interface Protpcol:" 2tab XfrData d16 + C@ dup . dup 1 = if drop ." Kwyboard" else 2 = if ." Mouse" then then cr ." Interface str index:" 2tab iINTERFACE W@ dup . dup 0<> if str 2+ C! \ str 8 0 do C@++ . loop drop cr str CTL_Read drop print_char else drop then cr cr \ ----------------- HID Class Descriptor ------------------------------------------ Get_Descriptor_Config CTL_Read drop \ Print_data cr ." HID Class Descriptor " cr ." Descriptors Size:" 2tab XfrData d18 + C@ . cr ." Descriptor Type:" 2tab XfrData d19 + C@ . cr ." HID version:" 3tab XfrData d21 + C@ h30 + emit 2E emit XfrData d20 + C@ . cr ." Cuntry code:" 3tab XfrData d22 + C@ . cr ." Number of Descriptor:" 2tab XfrData d23 + C@ . cr ." Descriptor Type:" 2tab XfrData d24 + C@ . cr ." Descriptor Length" 2tab XfrData d26 + C@ d256 u* XfrData d25 + C@ + dup . decimal . ." bytes" cr hex cr cr \ ----------------- EndPoint Descriptor ------------------------------------------ ." EndPoint Descriptor " cr ." Descriptors Size:" 2tab XfrData d27 + C@ . cr ." Descriptor Type:" 2tab XfrData d28 + C@ . cr ." EndPoint No/dir:" 2tab XfrData d29 + C@ dup . dup 80 and 0<> if ." OUT " else ." IN " then hF and ." EP" . cr ." Attribute:" 3tab XfrData d30 + C@ dup . 0 over = if ." CONTROL" else 1 over = if ." ISOCHRONOUS" else 2 over = if ." BULK" else ." INTERRUPT" then then then drop cr ." MaxPacketSize:" 3tab XfrData d32 + C@ d256 u* XfrData d31 + C@ + dup . dup Size W! decimal . ." bytes" cr hex ." Interval Time:" 3tab XfrData d33 + C@ dup . decimal . ." msec" cr hex cr then then then Set_Config CTL_Write_ND dup 0= if drop ." Issue SET_CONFIGURATION (" IN_nak_count W@ . ." NAKS )" cr wDESCLENGTH W@ Get_Descriptor 6 + W! \ Get_Descriptor 8 0 do C@++ . loop drop cr Get_Descriptor CTL_Read drop \ dup 0= ." Issue GET_REPORT_DESCRIPTOR (" IN_nak_count W@ . ." NAKS )" cr ." Report Descriptor" cr 0 count W! XfrData tmp W! \ Copy XfrData(address) to tmp begin tmp W@ C@ \ Get first data 3 and dup \ Get byte number \ loop couter is 1/2/3/5 0= if drop 1 else dup 3 = if drop 4 then 1+ then dup \ Copy 1-line of report-descriptor to report_array and display value 0 do tmp W@ i + C@ dup . report_array i + C! loop 2tab dup tmp W@ + tmp W! \ Update address inside tmp count W@ + count W! \ Display Report-descriptor strings report_array C@ hC and 2 rshift \ get bType 0 over = if \ main item report_array C@ dup 4 rshift 8 - main dispStr \ Display first string \ Display datas 3 and 0 <> if \ report_array C@ dup 4 rshift 8 - report_array C@ 4 rshift 8 - dup 0= if \ Input drop report_array 1+ C@ ." (" dispAttribute ." )" else 2 = if report_array 1+ C@ ." (" Collection dispStr ." )" else ." (" report_array 1+ C@ . ." )" then then then else 1 over = if \ global item report_array C@ 4 rshift dup global dispStr \ Display first string 0= if \ Usage Page report_array 1+ C@ dup h92 < if UsagePage ." (" dispStr ." )" else drop report_array C@ 3 and 0 do ." (" report_array 1+ i + C@ . ." )" loop then else report_array C@ 3 and 0 do ." (" report_array 1+ i + C@ . ." )" loop then else \ local item report_array C@ 4 rshift dup local dispStr \ Display first string 0= if \ Generic_Desktop_Page report_array 1+ C@ dup h9 h2F between if drop report_array C@ 3 and 0 do ." (" report_array 1+ i + C@ . ." )" loop else dup h38 > if drop report_array C@ 3 and \ st? 0 do ." (" report_array 1+ i + C@ . ." )" loop else d39 - Generic_Desktop_Page ." (" dispStr ." )" then then else report_array C@ 3 and 0 do ." (" report_array 1+ i + C@ . ." )" loop then thens drop cr count W@ last_transfer_size W@ = if 1 else 0 then until \ Display datas from USB-device cr begin d10 delms 1 Size W@ IN_Transfer drop \ st? Size W@ 0 do XfrData i + C@ . loop cr fkey? swap drop until else drop then wait_disconnect ; decimal