hwtLib.peripheral.usb package¶
This package contains components, interfaces, data types and other utils related to Universal Serial Bus (USB) protocol.
The USB bus is composed of Host (the main device which controls the bus), Hub(s) (a port extender) and a Device(s). The USB protocol stack (v3.2) is composed of multiple layers as depicted in:
Functional Layer:
This layer generates requests which are converted into transactions containing different packets. It also manages end to end data flow between host and device.
Protocol Layer:
Packets originates at transmit part of protocol layer and terminates at receive part of protocol layer. It has various functions which include following.
Ensure end to end reliability of packets.
Ensure effective management of power.
Ensure effective use of Bandwidth.
Link Layer:
This layer manages port to port flow of data between USB host and device. Link layer commands ensure link level data integrity, flow control and power management. This layer handles packet acknowledgement and takes care of error recovery. The Link Layer also handles Header Packet Framing. Its functions are similar to MAC layer of OSI model.
Physical Layer:
This layer offers actual physical connection between two ports. The connection uses a single differential pair (<= USB 2.0) or two differential data pairs (>= USB 3.0) (one transmit path and one receive path). The USB <=2.0 physical layer performs NRZI (Non Return to Zero Invert) encoding and bit stuffing. In the USB >=3.0 and <3.2 physical layer the transmit part of physical layer performs data scrambling, 8b10b encoding, and serialization functions. And the receive part of physical Layer performs de-serialization, 8b10b decoding, data descrambling, and receiver clock and data recovery. In USB >=3.2 it may have additional RX and TX lane.
- note:
USB Documentation, Christopher D. Leary and Devrin TalenDecember 17, 2007
Bus Enumeration
Host waits for at least 100 ms to account for the insertion process.
Host resets the device. Device should now be at address 0 and the defaultcontrol pipe will be open (default state).
Host queries the device descriptor from the devices default control pipe.
The host controller assigns a unique address to the device.
Host queries all configuration descriptors from the device, then sets one.At this point, the device will begin to draw the amount of power requestedby the assigned interface.
Host queries all other remaining descriptors (interface, endpoint, etc.) andsets up any requested interrupt transfers.
USB Endpoints
In the USB specification, a device endpoint is a uniquely addressable portion of a USB device that is the source or sink of information in a communication flow between the host and device. The Ep0 has is dedicated to control and and it provides descriptors for other endpoints.
The Endpoints may support following types of transfer:
Control
Interrupt
Bulk
Isochronous
Similar projects: * https://github.com/WangXuan95/FPGA-USB-Device * https://github.com/im-tomu/valentyusb USB stack written in LiteX * https://github.com/SinSiXX/luna-1 usb sniffer
Subpackages¶
- hwtLib.peripheral.usb.descriptors package
- Submodules
- hwtLib.peripheral.usb.descriptors.bundle module
UsbDescriptorBundleUsbDescriptorBundle.HConst_to_byte_list()UsbDescriptorBundle.__init__()UsbDescriptorBundle.compile_rom()UsbDescriptorBundle.get_descr_bytes()UsbDescriptorBundle.get_descriptor()UsbDescriptorBundle.get_descriptor_index()UsbDescriptorBundle.get_descriptors_from_rom()UsbDescriptorBundle.get_endpoint_meta()UsbDescriptorBundle.pack_descriptor()
UsbEndpointMetaUsbNoSuchDescriptor
- hwtLib.peripheral.usb.descriptors.cdc module
CHAR_FORMATCLASS_REQUESTPARITY_TYPEUSB_CDC_DESCRIPTOR_SUBTYPEUSB_CDC_DESCRIPTOR_SUBTYPE.ABSTRACT_CONTROL_MANAGEMENTUSB_CDC_DESCRIPTOR_SUBTYPE.ATM_NETWORKING_FUNCTIONALUSB_CDC_DESCRIPTOR_SUBTYPE.CALL_MANAGEMENT_FUNCTIONALUSB_CDC_DESCRIPTOR_SUBTYPE.CAPI_CONTROL_MANAGEMENTUSB_CDC_DESCRIPTOR_SUBTYPE.COUNTRY_SELECTION_FUNCTIONALUSB_CDC_DESCRIPTOR_SUBTYPE.DIRECT_LINE_MANAGEMENTUSB_CDC_DESCRIPTOR_SUBTYPE.ETHERNET_NETWORKING_FUNCTIONALUSB_CDC_DESCRIPTOR_SUBTYPE.EXTENSION_UNIT_FUNCTIONALUSB_CDC_DESCRIPTOR_SUBTYPE.HEADERUSB_CDC_DESCRIPTOR_SUBTYPE.MULTI_CHANNEL_MANAGEMENTUSB_CDC_DESCRIPTOR_SUBTYPE.NETWORK_CHANNEL_TERMINALUSB_CDC_DESCRIPTOR_SUBTYPE.PROTOCOL_UNIT_FUNCTIONALUSB_CDC_DESCRIPTOR_SUBTYPE.TELEPHONE_CALL_AND_LINE_STATE_REPORTING_CAPABILITIESUSB_CDC_DESCRIPTOR_SUBTYPE.TELEPHONE_OPERATIONAL_MODESUSB_CDC_DESCRIPTOR_SUBTYPE.TELEPHONE_RINGER_FUNCTIONALUSB_CDC_DESCRIPTOR_SUBTYPE.UNIONUSB_CDC_DESCRIPTOR_SUBTYPE.USB_TERMINAL_FUNCTIONAL
get_default_usb_cdc_vcp_descriptors()make_descriptor_functional_abstract_control_management()make_descriptor_functional_call_management()make_descriptor_functional_header()make_descriptor_functional_union()make_usb_line_coding_default()usb_define_descriptor_functional_union_body_t()usb_define_descriptor_functional_union_t()
- hwtLib.peripheral.usb.descriptors.cdc_ncm module
- hwtLib.peripheral.usb.descriptors.hid module
USB_HID_COUNTRY_CODEUSB_HID_COUNTRY_CODE.ARABICUSB_HID_COUNTRY_CODE.BELGIANUSB_HID_COUNTRY_CODE.CANADIAN_BILINGUALUSB_HID_COUNTRY_CODE.CANADIAN_FRENCHUSB_HID_COUNTRY_CODE.CZECH_REPUBLICUSB_HID_COUNTRY_CODE.DANISHUSB_HID_COUNTRY_CODE.FINNISHUSB_HID_COUNTRY_CODE.FRENCHUSB_HID_COUNTRY_CODE.GERMANUSB_HID_COUNTRY_CODE.GREEKUSB_HID_COUNTRY_CODE.HEBREWUSB_HID_COUNTRY_CODE.HUNGARYUSB_HID_COUNTRY_CODE.INTERNATIONAL_ISOUSB_HID_COUNTRY_CODE.ITALIANUSB_HID_COUNTRY_CODE.JAPAN_KATAKANAUSB_HID_COUNTRY_CODE.KOREANUSB_HID_COUNTRY_CODE.LATIN_AMERICANUSB_HID_COUNTRY_CODE.NETHERLANDS_DUTCHUSB_HID_COUNTRY_CODE.NORWEGIANUSB_HID_COUNTRY_CODE.NOT_SUPPORTEDUSB_HID_COUNTRY_CODE.PERSIAN_FARSIUSB_HID_COUNTRY_CODE.POLANDUSB_HID_COUNTRY_CODE.PORTUGUESEUSB_HID_COUNTRY_CODE.RUSSIAUSB_HID_COUNTRY_CODE.SLOVAKIAUSB_HID_COUNTRY_CODE.SPANISHUSB_HID_COUNTRY_CODE.SWEDISHUSB_HID_COUNTRY_CODE.SWISS_FRENCHUSB_HID_COUNTRY_CODE.SWISS_GERMANUSB_HID_COUNTRY_CODE.SWITZERLANDUSB_HID_COUNTRY_CODE.TAIWANUSB_HID_COUNTRY_CODE.TURKISH_FUSB_HID_COUNTRY_CODE.TURKISH_QUSB_HID_COUNTRY_CODE.UKUSB_HID_COUNTRY_CODE.USUSB_HID_COUNTRY_CODE.YUGOSLAVIA
USB_HID_PROTOCOLUSB_HID_SUBLCASSget_default_usb_hid_mouse_descriptors()make_usb_descriptor_hid()
- hwtLib.peripheral.usb.descriptors.std module
USB_DESCRIPTOR_TYPEUSB_DESCRIPTOR_TYPE.CONFIGURATIONUSB_DESCRIPTOR_TYPE.DEVICEUSB_DESCRIPTOR_TYPE.DEVICE_QUALIFIERUSB_DESCRIPTOR_TYPE.ENDPOINTUSB_DESCRIPTOR_TYPE.FUNCTIONALUSB_DESCRIPTOR_TYPE.HIDUSB_DESCRIPTOR_TYPE.INTERFACEUSB_DESCRIPTOR_TYPE.INTERFACE_POWERUSB_DESCRIPTOR_TYPE.OTHER_SPEED_CONFIGURATIONUSB_DESCRIPTOR_TYPE.REPORTUSB_DESCRIPTOR_TYPE.STRING
USB_DEVICE_CLASSUSB_DEVICE_CLASS.APPLICATION_SPECIFICUSB_DEVICE_CLASS.AUDIOUSB_DEVICE_CLASS.AUDIO_VIDEO_DEVICEUSB_DEVICE_CLASS.BILBOARD_DEVICEUSB_DEVICE_CLASS.CDC_CONTROLUSB_DEVICE_CLASS.CDC_DATAUSB_DEVICE_CLASS.CONTENT_SECURITYUSB_DEVICE_CLASS.DIAGNOSTIC_DEVICEUSB_DEVICE_CLASS.HIDUSB_DEVICE_CLASS.IMAGEUSB_DEVICE_CLASS.MASS_STORAGEUSB_DEVICE_CLASS.MISCELLANEOUSUSB_DEVICE_CLASS.PERSONAL_HEALTHCAREUSB_DEVICE_CLASS.PRINTERUSB_DEVICE_CLASS.SMART_CARDUSB_DEVICE_CLASS.UNSPECIFIEDUSB_DEVICE_CLASS.USB_HUBUSB_DEVICE_CLASS.USB_PIDUSB_DEVICE_CLASS.USB_TYPE_C_BRIDGEUSB_DEVICE_CLASS.VENDOR_SPECIFICUSB_DEVICE_CLASS.VIDEOUSB_DEVICE_CLASS.WIRELESS_CONTROLLER
USB_ENDPOINT_ATTRIBUTES_SYNCHRONISATION_TYPEUSB_ENDPOINT_ATTRIBUTES_TRANSFER_TYPEUSB_ENDPOINT_ATTRIBUTES_USAGE_TYPEUSB_ENDPOINT_DIRmake_usb_descriptor_configuration()make_usb_descriptor_device()make_usb_descriptor_device_qualifier()make_usb_descriptor_endpoint()make_usb_descriptor_interface()make_usb_descriptor_string()make_usb_descriptor_string0()make_usb_device_request_get_descr()usb_define_descriptor_string()usb_define_descriptor_string0()usb_format_bMaxPower()
- hwtLib.peripheral.usb.sim package
- Subpackages
- hwtLib.peripheral.usb.sim.usbip package
- Submodules
- hwtLib.peripheral.usb.sim.usbip.connection module
USBIPConnectionUSBIPConnection.OP_COMMONUSBIPConnection.OP_COMMON_SIZEUSBIPConnection.OP_SUBMITUSBIPConnection.OP_SUBMIT_SIZEUSBIPConnection.__init__()USBIPConnection.connection()USBIPConnection.debug_log()USBIPConnection.getDeviceList()USBIPConnection.handle_op_devlist()USBIPConnection.handle_op_import()USBIPConnection.handle_packet()USBIPConnection.handle_urb_submit()USBIPConnection.handle_urb_unlink()USBIPConnection.make_usbip_header_basic()USBIPConnection.pack_device_desc()USBIPConnection.usbip_ret_submit()
USBIPProtocolErrorException
- hwtLib.peripheral.usb.sim.usbip.constants module
- hwtLib.peripheral.usb.sim.usbip.device module
LIBUSB_TRANSFER_STATUSUSBIPDeviceUSBIPOperationPromiseUSBIPPendingUSBIPSimDeviceUSBIPSimDevice.__init__()USBIPSimDevice.close()USBIPSimDevice.controlRead()USBIPSimDevice.controlWrite()USBIPSimDevice.getBusNumber()USBIPSimDevice.getDevice()USBIPSimDevice.getDeviceAddress()USBIPSimDevice.getDeviceClass()USBIPSimDevice.getDeviceProtocol()USBIPSimDevice.getDeviceSpeed()USBIPSimDevice.getDeviceSubClass()USBIPSimDevice.getNumConfigurations()USBIPSimDevice.getProductID()USBIPSimDevice.getTransfer()USBIPSimDevice.getVendorID()USBIPSimDevice.getbcdDevice()USBIPSimDevice.iterConfigurations()USBIPSimDevice.open()
USBIPSimDeviceConfiguration.getConfigurationValue()USBIPSimDeviceConfiguration.getNumInterfaces()USBIPSimDeviceConfiguration.iterInterfaces()USBIPSimDeviceInterface.iterSettings()USBIPSimDeviceInterfaceSettingUSBIPTransfer
- hwtLib.peripheral.usb.sim.usbip.server module
- hwtLib.peripheral.usb.sim.usbip.session_recorder module
UsbipServerReplayerUsbipServerSessionRecordercut_off_empty_time_segments()filter_empty_in()
- hwtLib.peripheral.usb.sim.usbip package
- Submodules
- hwtLib.peripheral.usb.sim.agent_base module
- hwtLib.peripheral.usb.sim.usb_agent_device module
- hwtLib.peripheral.usb.sim.usb_agent_host module
UsbHostAgentUsbHostAgent.__annotations__UsbHostAgent.__init__()UsbHostAgent.control_read()UsbHostAgent.control_write()UsbHostAgent.download_descriptor()UsbHostAgent.get_max_packet_size()UsbHostAgent.parse_configuration_descriptor_bundle()UsbHostAgent.parse_interface_functional_descriptor()UsbHostAgent.proc()UsbHostAgent.receive_bulk()UsbHostAgent.transmit_bulk()
- Subpackages
- hwtLib.peripheral.usb.usb2 package
- Submodules
- hwtLib.peripheral.usb.usb2.device_cdc_vcp module
- hwtLib.peripheral.usb.usb2.device_common module
Usb2DeviceCommonUsb2DeviceCommon.__annotations__Usb2DeviceCommon.connect_core_and_ep_buffers_common()Usb2DeviceCommon.connect_ep0_data()Usb2DeviceCommon.decode_setup_request()Usb2DeviceCommon.decode_setup_request_class()Usb2DeviceCommon.descriptor_ep0_fsm()Usb2DeviceCommon.descriptor_ep0_fsm_get_descriptor()Usb2DeviceCommon.generat_descriptor_rom()Usb2DeviceCommon.load_usb_device_request()
- hwtLib.peripheral.usb.usb2.device_core module
- hwtLib.peripheral.usb.usb2.device_core_interfaces module
- hwtLib.peripheral.usb.usb2.device_ep_buffers module
- hwtLib.peripheral.usb.usb2.sie_interfaces module
- hwtLib.peripheral.usb.usb2.sie_rx module
- hwtLib.peripheral.usb.usb2.sie_tx module
- hwtLib.peripheral.usb.usb2.ulpi module
IP_UlpiULPI_REGULPI_REG.Carkit_ControlULPI_REG.Carkit_Interrupt_DelayULPI_REG.Carkit_Interrupt_EnableULPI_REG.Carkit_Interrupt_LatchULPI_REG.Carkit_Interrupt_StatusULPI_REG.Carkit_Pulse_ControlULPI_REG.DebugULPI_REG.Function_ControlULPI_REG.Interface_ControlULPI_REG.OTG_ControlULPI_REG.Product_ID_HighULPI_REG.Product_ID_LowULPI_REG.REGS_WITH_SET_AND_CLRULPI_REG.Receive_Polarity_RecoveryULPI_REG.Scratch_RegisterULPI_REG.Transmit_Negative_WidthULPI_REG.Transmit_Positive_WidthULPI_REG.USB_Interrupt_Enable_FallingULPI_REG.USB_Interrupt_Enable_RisingULPI_REG.USB_Interrupt_LatchULPI_REG.USB_Interrupt_StatusULPI_REG.Vendor_ID_HighULPI_REG.Vendor_ID_LowULPI_REG.clr_of()ULPI_REG.set_of()
ULPI_TX_CMDUlpi
- hwtLib.peripheral.usb.usb2.ulpi_agent module
- hwtLib.peripheral.usb.usb2.ulpi_usb_agent module
- hwtLib.peripheral.usb.usb2.utmi module
- hwtLib.peripheral.usb.usb2.utmi_agent module
Utmi_8bAgentUtmi_8bAgent.__annotations__Utmi_8bAgent.__init__()Utmi_8bAgent.actual_link_to_phy_packetUtmi_8bAgent.actual_link_to_phy_packet_setterUtmi_8bAgent.actual_phy_to_link_packetUtmi_8bAgent.actual_phy_to_link_packet_setterUtmi_8bAgent.driver()Utmi_8bAgent.getDrivers()Utmi_8bAgent.getMonitors()Utmi_8bAgent.link_to_phy_packetsUtmi_8bAgent.link_to_phy_packets_setterUtmi_8bAgent.monitor()Utmi_8bAgent.phy_to_link_packetsUtmi_8bAgent.phy_to_link_packets_setter
Utmi_8b_rxAgentUtmi_8b_txAgent
- hwtLib.peripheral.usb.usb2.utmi_to_ulpi module
- hwtLib.peripheral.usb.usb2.utmi_usb_agent module
Submodules¶
hwtLib.peripheral.usb.constants module¶
- attention:
on USB the LSB bits are sent first
- class hwtLib.peripheral.usb.constants.USB_LINE_STATE[source]¶
Bases:
object- J = 1¶
- K = 2¶
- SE0 = 0¶
- SE1 = 3¶
- class hwtLib.peripheral.usb.constants.USB_PID[source]¶
Bases:
objectUSB Protocol layer packet identifier values (Specifies the type of transaction)
- Attention:
visualy written in msb-first, transmited in lsb first
- Note:
packet formats are described in structs in this file
- Note:
if the PID is in 8b format the other 4 bits (check field) are a bitwise inversion of original 4b PID field
- DATA_0 = 3¶
- DATA_1 = 11¶
- DATA_2 = 7¶
- DATA_M = 15¶
- ERR = 12¶
- HS_ACK = 2¶
- HS_NACK = 10¶
- HS_NYET = 6¶
- HS_STALL = 14¶
- PING = 4¶
- PREAMBLE = 12¶
- SPLIT = 8¶
- TOKEN_IN = 9¶
- TOKEN_OUT = 1¶
- TOKEN_SETUP = 13¶
- TOKEN_SOF = 5¶
- classmethod is_data(v: int | RtlSignalBase)[source]¶
- classmethod is_hs(v: int | RtlSignalBase)[source]¶
- classmethod is_token(v: int | RtlSignalBase)[source]¶
- class hwtLib.peripheral.usb.constants.USB_VER[source]¶
Bases:
objectUSB_VER
Speed
1.0 - 2.0
Low Speed 1.5 Mbit/s
1.0 - 2.0
Full Speed 12 Mbit/s
2.0
High Speed 480 Mbit/s
3.0
SuperSpeed 5Gbit/s
3.1
SuperSpeed+ 10Gbit/s
3.2
SuperSpeed+ 20Gbit/s
4.0
SuperSpeed+ 40Gbit/s
- USB1_0 = '1.0'¶
- USB1_1 = '1.1'¶
- USB2_0 = '2.0'¶
- USB3_0 = '3.0'¶
- USB3_1 = '3.1'¶
- USB3_2 = '3.2'¶
- USB4_0 = '4.0'¶
- values = ['1.0', '1.1', '2.0', '3.0', '3.1', '3.2', '4.0']¶
- hwtLib.peripheral.usb.constants.usb2_0_packet_data_t = struct { <HBits, 4bits> pid <HStream len:(1, 1024), align:(0,) <HBits, 8bits>> data <HBits, 16bits> crc }¶
There are three type of handshake packets which consist simply of the USB_PID
ACK - Acknowledgment that the packet has been successfully received.
NAK - Reports that the device temporary cannot send or received data. Also used during interrupt transactions to inform the host there is no data to send.
STALL - The device finds its in a state that it requires intervention from the host.
- hwtLib.peripheral.usb.constants.usb_packet_hs_t = struct { <HBits, 4bits> pid }¶
The SOF (Start of Frame) packet consisting of an 11-bit frame number is sent by the host every 1ms ± 500ns on a full speed bus or every 125 µs ± 0.0625 µs on a high speed bus.
- hwtLib.peripheral.usb.constants.usb_pid_t = <HBits, 4bits>¶
- Attention:
every packet starts with sync and ends in EOP, which is not in data structures below
hwtLib.peripheral.usb.device_request module¶
- class hwtLib.peripheral.usb.device_request.USB_REQUEST[source]¶
Bases:
objectValues for usb_device_request_t.bRequest
- CLEAR_FEATURE = 1¶
- GET_CONFIGURATION = 8¶
- GET_DESCRIPTOR = 6¶
- GET_INTERFACE = 10¶
- GET_STATUS = 0¶
- SET_ADDRESS = 5¶
- SET_CONFIGURATION = 9¶
- SET_DESCRIPTOR = 7¶
- SET_FEATURE = 3¶
- SET_INTERFACE = 17¶
- SYNCH_FRAME = 18¶
- class hwtLib.peripheral.usb.device_request.USB_REQUEST_TYPE_DIRECTION[source]¶
Bases:
objectValues for usb_request_type_t.recipient
- DEV_TO_HOST = 1¶
- HOST_TO_DEV = 0¶
- class hwtLib.peripheral.usb.device_request.USB_REQUEST_TYPE_RECIPIENT[source]¶
Bases:
objectValues for usb_request_type_t.data_transfer_direction
- DEVICE = 0¶
- ENDPOINT = 2¶
- INTERFACE = 1¶
- OTHER = 3¶
- class hwtLib.peripheral.usb.device_request.USB_REQUEST_TYPE_TYPE[source]¶
Bases:
objectValues for usb_request_type_t.type
- CLASS = 1¶
- STANDARD = 0¶
- VENDOR = 2¶
- hwtLib.peripheral.usb.device_request.make_usb_device_request(bmRequestType_recipient: USB_REQUEST_TYPE_RECIPIENT, bmRequestType_type: USB_REQUEST_TYPE_TYPE, bmRequestType_data_transfer_direction: USB_REQUEST_TYPE_DIRECTION, bRequest: USB_REQUEST, wValue: int, wIndex: int, wLength: int)[source]¶