Attribute VB_Name = "USB_STUFF"
Option Explicit

Public Const VRSMBus As Integer = &HDE
Public Const VRREAD As Integer = 1
Public Const VRWRITE As Integer = 0
Public HexFilePath As String

            
Public DataBuffer(20) As Byte
Public FirmWareAddress(512) As Byte

Public Declare _
    Function LoadFirmWare _
    Lib "USBFunctions" _
    Alias "?LoadFirmWare@@YGFPAD@Z" _
    (ByRef FrmWrAddr As Byte) As Integer

Public Declare _
    Function VendorRequest _
    Lib "USBFunctions" _
    Alias "?VendorIORequest@@YGFFJJFFPAD@Z" _
    (ByVal MyReq As Integer, _
    ByVal MyVal As Long, _
    ByVal MyIndx As Long, _
    ByVal MyDir As Integer, _
    ByVal BUFLEN As Integer, _
    ByRef MyBuf As Byte) As Integer

'Direction -> SETUPDAT[0]
'Request   -> SETUPDAT[1]
'MyVal     -> SETUPDAT[3]:SETUPDAT[2]
'MyIndx    -> SETUPDAT[5]:SETUPDAT[4]
'Length    -> SETUPDAT[6]

Public Function PortRead(DeviceAddress As Long, AddrPtr As Long) As Integer
PortRead = VendorRequest(VRSMBus, DeviceAddress, AddrPtr, VRREAD, 1, DataBuffer(0))
PortRead = DataBuffer(0)
End Function

Public Function PortWrite(DeviceAddress As Long, AddrPtr As Long, DataOut As Long) As Integer
PortWrite = VendorRequest(VRSMBus, DeviceAddress, CLng(256 * DataOut + AddrPtr), VRWRITE, 0, 0)
End Function


Public Function DownLoadFirmware() As Integer
HexFilePath = App.Path + "\AD5933_34FW.hex"
ParseString (HexFilePath)
DownLoadFirmware = LoadFirmWare(FirmWareAddress(0))
End Function


Public Sub ParseString(strIn As String)
Dim i As Integer

For i = 0 To Len(strIn) - 1
    FirmWareAddress(i) = Asc(Mid(strIn, i + 1, 1))
Next i
FirmWareAddress(i) = 0

End Sub

Public Sub USBFailureAction()
Dim i As Form

For Each i In Forms
    Unload i
Next i
End

End Sub


