fl 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 ; : enumerate_device hex ." 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 \ low wLength:Get_Descriptor_Device[6]=8 high wLength:Get_Descriptor_Device[7]=0 \ 8 Get_Descriptor_Device 6 + W! \ st? ." First 8 bytes of Device Descriptor" cr \ Get device-descriptor into XfrFata array 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 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 h12 Get_Descriptor_Device 6 + C! \ Get_Descriptor_Device[6]=8 low wLength ." ----------------------------------------------" cr ." Device Descriptor ( " Get_Descriptor_Device CTL_Read dup if print_error else drop IN_nak_count W@ . ." NAKS )" cr Print_data cr \ ---------------------------------------------------------------------------------------------------- \ Get Device Descriptor XfrData 8 + C@ XfrData 9 + C@ FF u* + VID W! XfrData hA + C@ XfrData B + C@ FF u* + PID W! XfrData hE + C@ iMFG W! XfrData hF + C@ iPROD W! XfrData h10 + C@ iSERIAL W! ." USB " XfrData 3 + C@ h30 + emit 2E emit XfrData 2 + C@ . cr ." This device has " XfrData h11 + C@ . ." configuration" cr ." Vendor ID is 0x" VID W@ . cr ." Product ID is 0x" PID W@ . cr \ ." ----------------------------------------------" cr \ ---------------------------------------------------------------------------------------------------- \ 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 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 \ ." ----------------------------------------------" cr \ ---------------------------------------------------------------------------------------------------- iMFG W@ if iMFG W@ str 2+ C! str CTL_Read drop \ Get Manufacturer ID string ." Manuf. string is " print_char else ." There is no Manuf. string" cr then cr iPROD W@ if iPROD W@ str 2+ C! str CTL_Read drop \ Get Product ID string ." Product string is " print_char else ." There is no Product string" then cr iSERIAL W@ if iSERIAL W@ str 2+ C! str CTL_Read drop \ Get Serial Number ID string ." S/N string is " print_char else ." There is no Serial Number" then cr ." ----------------------------------------------" cr \ ---------------------------------------------------------------------------------------------------- \ Get the 9-byte configuration descriptor ." 9Byte Configuration Descriptor ( " 9 Get_Descriptor_Config 6 + C! 0 Get_Descriptor_Config 7 + C! \ 9 Get_Descriptor_Config 6 + W! Get_Descriptor_Config CTL_Read dup 0= \ Get config descriptor into XfrData[] if drop IN_nak_count W@ . ." NAKS )" cr Print_data cr \ 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 \ XfrData 2+ W@ Get_Descriptor_Config 6 + W! \ LengthL LengthH Get_Descriptor_Config CTL_Read drop \ Get config descriptor into XfrData[] cr ." Full Configuration Data" cr last_transfer_size W@ 0 do i 0<> if i hF and 0= if cr thens \ CR every 16 numbers XfrData i + C@ . loop cr \ optional configuration string XfrData 6 + C@ iCONFIG W! XfrData h11 + C@ iINTERFACE W! XfrData h19 + C@ XfrData h1A + C@ hFF u* + wDESCLENGTH W! ." Configuration " XfrData 5 + C@ . ." has " XfrData 4 + C@ dup . ." interface" 1 > if ." s" then cr ." This device is " XfrData 7 + C@ h40 and 0> if ." self-powered " else ." bus powered " then ." and uses " decimal XfrData 8 + C@ 2 u* . ." milliamps" cr ." ----------------------------------------------" cr \ ---------------------------------------------------------------------------------------------------- last_transfer_size W@ 0 do XfrData i + 1+ C@ dup 4 = if \ Interface Descriptor drop ." Interface " XfrData i + 2+ C@ . ." Alternate Setting " XfrData i + 3 + C@ . ." has" cr else 5 = if \ EndPoint Descriptor ." --Endpoint " XfrData i + 2+ C@ dup hF and . h80 and if ." -IN " else ." -OUT " ." is type " then cr ." MaxPacketSize is " XfrData i + 4 + C@ . ." bytes" cr XfrData i + 3 + C@ 0 over = if ." CONTROL" else 1 over = if ." ISOCHRONOUS" else 2 over = if ." BULK" else ." INTERRUPT with a polling interval of " XfrData i + 6 + C@ . ." msec" then then then drop cr then then XfrData i + C@ i + 1- seti loop ." ----------------------------------------------" cr \ ---------------------------------------------------------------------------------------------------- iCONFIG W@ dup 0<> if str 2+ C! str CTL_Read drop \ Get Config string ." Config string is " print_char else drop ." There is no Config string." then cr \ str descriptor for Interface iINTERFACE W@ dup 0<> if str 2+ C! str CTL_Read drop ." Interface string is " print_char else drop ." There is no Interface string." then cr cr thens ; : usb init_host detect_device d200 waitframe enumerate_device hex \ Set_Config CTL_Read dup 0= st? Set_Config_KB CTL_Read dup 0= st? if drop ." Issue SET_CONFIGURATION (" IN_nak_count W@ . ." NAKS )" cr \ Print_data cr wDESCLENGTH W@ Get_Descriptor 6 + W! Get_Descriptor CTL_Read dup 0= if drop ." Issue GET_REPORT_DESCRIPTOR (" IN_nak_count W@ . ." NAKS )" cr ." Report Descriptor" cr last_transfer_size W@ 0 do XfrData i + C@ . i 2 u/mod drop 0<> if cr then loop cr then else drop then wait_disconnect ; decimal