⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memory.bas

📁 Cypress ez-usb an2131的.hex固件程序文件下载程序
💻 BAS
字号:
Attribute VB_Name = "Memory"
' EZ-USB DownloadHex Example
' copyright (c) 2001 Cypress Semiconductor

Public Const TGT_IMG_SIZE = &H10000 '64KB (65,536 Byte) target image
Public Const TGT_SEG_SIZE = 16      '16 byte segments
Public Const MAX_EP0_XFER_SIZE = 1024 * 4 ' 4K Control EP0 transfer limit imposed by OS

Public gDriver As String

Public gHeadless As Boolean

Public gFirmwareCache As CMemCache

Enum HexRecType
    eSegmentRecord = 2
    eDataRecord = 0
    eEndRecord = 1
End Enum

Public Const MAX_INTEL_HEX_RECORD_LENGTH = 16

Type INTEL_HEX_RECORD
   Length As Byte
   Address As Integer
   Type As Byte
   Data(MAX_INTEL_HEX_RECORD_LENGTH) As Byte
End Type

Public Sub DownloadVendAX(Optional hDriver As Long)
    Dim filelength As Long
    Dim buf(3375) As Byte
    Dim x As Long
    Dim temp As String
    Dim result As Long

    Dim hDriverHandle As Long
    Dim lBytesReturned As Long

    Dim DownloadControl As AnchorDownloadControlType
    
    Open App.Path + "\Vend_Ax.bin" For Binary As #2
    
    Get #2, , buf
    
    Close #2
    
    ' set up data structure
    DownloadControl.offset = 0
    
    ' if device driver was opened by a calling routine,
    ' no need to open it again
    If hDriver = 0 Then
    ' now, open the EZ-USB driver
        hDriverHandle = OpenDriver(gDriver)
    Else
        hDriverHandle = hDriver
    End If
    
    ' if the driver opens successfully...
    If hDriverHandle > 0 Then
        ' download the buffer
        
            Reset_Or_Hold_8051 hDriver, eOP_8051_HOLD

            DownloadControl.offset = 0
        
            result = DeviceIoControl(hDriver, _
                IOCTL_EZUSB_ANCHOR_DOWNLOAD, _
                DownloadControl, _
                Len(DownloadControl), _
                buf(0), _
                3376, _
                nBytes, _
                0)
            If result = 0 Then
                i = MsgBox("Problems with code download", vbCritical)
                Exit Sub
            End If
        
            Reset_Or_Hold_8051 hDriverHandle, eOP_8051_RUN
        
        ' if this routine opened the device driver,
        ' then we should close it here, otherwise,
        ' leave it open
        If hDriver = 0 Then
            CloseHandle (hDriverHandle)
        End If

    End If

End Sub


Public Sub Main()

Dim temp As String

Set gFirmwareCache = New CMemCache

' run head-less if commandline execution
temp = Command()

If temp = "" Then
    gHeadless = False
    Load frmDownload
    frmDownload.Show
Else
    gHeadless = True
    x = InStr(temp, " ")
    gPath = Left(temp, x - 1)
    gDriver = Trim(Mid(temp, x))
    LoadHexFile gPath
    End
End If


End Sub


Public Sub LoadHexFile(ByVal fname As String)

Dim offset As Integer
Dim endianConversion As Integer
Dim hDriver As Long
Dim vendReq As VendorOrClassRequestControlType
Dim DownloadControl As AnchorDownloadControlType
Dim buf() As Byte
Dim DeviceDescriptor As USB_DD
Dim ExtLdrFile As String
Dim aSegment As CSegment
Dim progress As Integer
Dim numSeg As Integer

' read file into MemSeg buffer, converting from Intel Hex format
offset = 0
endianConversion = 0
ReadIntelHex 0, fname, offset, endianConversion, False

hDriver = OpenDriver(gDriver)

If hDriver <= 0 Then
    ErrMsg eBadDriver
    Exit Sub
End If

If Not gHeadless Then
    frmDownload.lblComplete.Visible = False
    frmDownload.ProgressBar1.Visible = True
    frmDownload.ProgressBar1.Min = 0
    frmDownload.ProgressBar1.Max = gFirmwareCache.Count + 1
End If


' load all the high-memory segments
bNeedExternalLoader = True
numSeg = 0
For Each Segment In gFirmwareCache
    numSeg = numSeg + 1
    Set aSegment = Segment
    If aSegment.Address >= &H2000 Then
        If bNeedExternalLoader Then
            DownloadVendAX hDriver
            bNeedExternalLoader = False
        End If
        With vendReq
            .recepient = 0
            .requestType = 2
            .request = &HA3
            .value = aSegment.Address
            .index = 0
            .direction = 0
        End With
        
        ReDim buf(aSegment.SegmentSize)
        
        ' unfortunately, VB doesn't let us get at arrays that have been declared "Private",
        ' and VB won't let us declare static arrays in module or class files as "Public", so...
        
        For i = 0 To aSegment.SegmentSize - 1
            buf(i) = aSegment.GetByte(i)
        Next
        
        result = DeviceIoControl(hDriver, _
                    IOCTL_EZUSB_VENDOR_REQUEST, _
                    vendReq, _
                    LenB(vendReq), _
                    buf(0), _
                    aSegment.SegmentSize, _
                    nBytes, _
                    0)
    
        Set aSegment = Nothing
           
        If Not gHeadless Then
            frmDownload.ProgressBar1.value = frmDownload.ProgressBar1.value + 1
        End If
    End If
    
Next

Reset_Or_Hold_8051 hDriver, eOP_8051_HOLD

' then load all the low-memory segments
For Each Segment In gFirmwareCache
    Set aSegment = Segment
    If aSegment.Address < &H2000 Then
    ' ANCHOR-DOWNLOAD
        
        ReDim buf(aSegment.SegmentSize)
        
        For i = 0 To aSegment.SegmentSize - 1
            buf(i) = aSegment.GetByte(i)
        Next

        
        DownloadControl.offset = aSegment.Address
        
        result = DeviceIoControl(hDriver, _
                IOCTL_EZUSB_ANCHOR_DOWNLOAD, _
                DownloadControl, _
                Len(DownloadControl), _
                buf(0), _
                aSegment.SegmentSize, _
                nBytes, _
                0)
        If result = 0 Then
            i = MsgBox("Problems with code download", vbCritical)
            Exit Sub
        End If
    End If
    Set aSegment = Nothing
    
    If Not gHeadless Then
        If frmDownload.ProgressBar1.value < frmDownload.ProgressBar1.Max Then
            frmDownload.ProgressBar1.value = frmDownload.ProgressBar1.value + 1
        End If
    End If
    
Next

Reset_Or_Hold_8051 hDriver, eOP_8051_RUN

CloseHandle hDriver

frmDownload.ProgressBar1.Visible = False
frmDownload.lblComplete.Visible = True

End Sub




Public Function ReadIntelHex(cacheSelector As Integer, strFileName As String, intOffset As Integer, intEndianConversion As Integer, bSpaces As Boolean) As Integer
    
    Dim cache As CMemCache
    
    Dim temp As String
    Dim bSuccess As Boolean
    
    Dim recType As HexRecType
    Dim CurrentSegment As Integer
    Dim cnt As Long
    Dim Address As Long
    Dim totalRead As Long
    Dim segSize As Integer
    
    Dim CNTFIELD As Integer
    Dim ADDRFIELD As Integer
    Dim RECFIELD As Integer
    Dim DATAFIELD As Integer
       
    bSuccess = False
    ptr = 0
    totalRead = 0
    
    Open strFileName For Input As #1
    Do While Not EOF(1)
        Line Input #1, temp
        
        If Left(temp, 1) <> ":" Then
            MsgBox "Bad Record in file: " + temp, vbCritical
            Exit Do
        End If
        
        CNTFIELD = 2 + (bSpaces Or Mid(temp, 1, 1) = " ")
        ADDRFIELD = 4 + 2 * (bSpaces Or Mid(temp, 1, 1) = " ")
        RECFIELD = 8 + 3 * (bSpaces Or Mid(temp, 1, 1) = " ")
        DATAFIELD = 10 + 4 * (bSpaces Or Mid(temp, 1, 1) = " ")
        
        recType = Val(Mid(temp, RECFIELD, 2))
        
        Select Case recType
            Case 2 'eSegmentRecord
                CurrentSegment = 16 * Val(Mid(temp, DATAFIELD, 4))
            Case 0 'eDataRecord
                cnt = HexVal(Mid(temp, CNTFIELD, 2))
                Address = HexVal(Mid(temp, ADDRFIELD, 4))
                
                If Address > TGT_IMG_SIZE Then
                    MsgBox "Error loading file: address out of range", vbCritical
                    Close #1
                    ReadIntelHex = totalRead
                    Exit Function
                End If
                            
                If cacheSelector = 0 Then
                    Set cache = gFirmwareCache
                Else
                    Set cache = gDownloaderCache
                End If
                
                With cache
                    If .Count = 0 Then
                    'make new segment with this rec's starting address and size
                        .AddNewSegment Address, cnt
                    Else
                        
                        segSize = .CurrentSegment.SegmentSize
                        
                        If .CurrentSegment.Address = Address - segSize And _
                            segSize + cnt <= MAX_EP0_XFER_SIZE Then
                           'the segment is contiguous to the last segment, and it's not too big yet
                           ' so grow the segment by the number of bytes in the hex rec
                           .CurrentSegment.Grow cnt
                            
                        Else ' otherwise create a new segment
                            .AddNewSegment Address, cnt
                            
                        End If
                    End If
                
                    i = 0
                    Do While i < cnt
                        .CurrentSegment.PutByte HexVal(Mid(temp, DATAFIELD + i * 2, 2))
                        totalRead = totalRead + 1
                        i = i + 1
                    Loop
                    
                End With
            
            Case 1 'eEndRecord
                Close #1
                ReadIntelHex = totalRead
                Exit Function
        End Select
        
    Loop ' until EOF
    
    Close #1
    ReadIntelHex = -1
    
End Function

Public Function HexVal(s As String) As Long
' convert hex string into equivalent numeric value
   Dim lResult As Long
   Dim iByte As Integer
   Dim sOneByte As String
   Dim sTemp As String
   Dim numBytes As Integer
   Dim power As Integer
   Dim offset As Integer
   
   sTemp = UCase(s)
   
   'make sure even number of chars
   If Len(sTemp) Mod 2 = 1 Then: s = "0" + sTemp
   
   power = 0
   numBytes = Len(sTemp) / 2
   
   Do While numBytes > 0
        iByte = 0
        ' strip low end "byte"
        sOneByte = Right(sTemp, 2)
        sTemp = Left(sTemp, Len(sTemp) - 2)
    
        If Right(sOneByte, 1) > "9" Then
            iByte = 10 + (Asc(Right(sOneByte, 1)) - Asc("A"))
        Else
            iByte = Asc(Right(sOneByte, 1)) - Asc("0")
        End If
         
        If Left(sOneByte, 1) > "9" Then
            iByte = iByte + (16 * (10 + (Asc(Left(sOneByte, 1)) - Asc("A"))))
        Else
            iByte = iByte + (16 * (Asc(Left(sOneByte, 1)) - Asc("0")))
        End If
         
        lResult = lResult + iByte * 2 ^ power
        
        numBytes = numBytes - 1
        power = power + 8
        
     Loop
     
     HexVal = lResult
         
End Function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -