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

📄 piccomms.bas

📁 PIC24FJ32GA002单片机bootloader rs485通信移植
💻 BAS
📖 第 1 页 / 共 2 页
字号:
                            
                    WriteRangeDevMem = RetStat
                    Exit Function
                End If
            Else
                ProgressInd = 1
            End If
            
            
            i = 0
            
            frmBootload.StatusBar1.Panels(1).Text = "Writing: " & Hex(picA.BootAddr)
        End If

    Loop
    
    If VerifyFlag = False Then
        WriteRangeDevMem = -101
    Else
        WriteRangeDevMem = ProgressInd
    End If
    
    a.Close
End Function


Function VerifyWrite(picV As PIC, outArray() As Byte) As Integer
    Dim RetStat As Integer
    Dim inArray(256) As Byte
    Dim BootAddr As Long
    Dim VerifyFlag As Boolean
    Dim OutLine As String
           
    'Setup data file creation
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set b = fs.OpenTextFile(VB.App.Path & "\" & PicBootS.ErrorLogFile, 8, False, 0)
    
           
    'read written data froom pic
    picV.BootCmd = picV.BootCmd - 1 'read out using read command of same memory type
    
    If (picV.BootCmd = 1) Then
        picV.BytesPerBlock = PicBootS.DeviceRdBlock
    End If    
    
    RetStat = ReadPIC(PicBootS.PortHandle, picV, inArray(0))
    
    BootAddr = picV.BootAddr
    
    'error if read fails
    If RetStat < 0 Then
        VerifyWrite = RetStat
        Exit Function
    End If
    
    VerifyFlag = True
    
    i = 0
    'verify written data
    Do While i < RetStat
        If picV.BootCmd = 1 Then
            If (inArray(i) <> outArray(i) Or _
                inArray(i + 1) <> outArray(i + 1) Or _
                inArray(i + 2) <> outArray(i + 2) Or _
                inArray(i + 3) <> outArray(i + 3)) Then
                
                If PicBootS.AESEnable Then
                    Select Case BootAddr
                    'Device reset vector, user reset vector, and delay address are ignored because
                    'bootloader will intentionally write different values in these locations than what may be
                    'specified in the .hex file causing erroneous varification errors.
                    Case 0
                        'ignore reset vector
                    Case 2
                        'ignore reset vector
                    Case 4
                    Case 6
                        'AES COMPATABILITY - must ignore entire 16 byte block containing reset vector
                    Case PicBootS.UserResetVector
                        'ignore user's reset vector
                    Case ((PicBootS.UserResetVector And 16777200) + 2)
                    Case ((PicBootS.UserResetVector And 16777200) + 4)
                    Case ((PicBootS.UserResetVector And 16777200) + 6)
                        'AES COMPATABILITY - must ignore entire 16 byte block containing user reset vector
                    Case PicBootS.BootDelayAddr
                        'ignore boot delay address
                    Case ((PicBootS.BootDelayAddr And 16777200) + 2)
                    Case ((PicBootS.BootDelayAddr And 16777200) + 4)
                    Case ((PicBootS.BootDelayAddr And 16777200) + 6)
                        'AES COMPATABILITY - must ignore entire 16 byte block containing boot delay addr
                    Case Else
                        'AES COMPATABILITY - must ignore bootloader locations
                        If (BootAddr < PicBootS.BootLoadAddrL Or BootAddr > PicBootS.BootLoadAddrH) Then
                            'otherwise, verify error
                            b.WriteLine ("Verify Error at 0x" & Hex(BootAddr) & " should be: 0x" _
                                        & (Hex(CLng(outArray(i + 3)) * Hex(16777216) + CLng(outArray(i + 2)) * 65536 _
                                        + CLng(outArray(i + 1)) * 256 + CLng(outArray(i)))) & " but read: 0x" _
                                        & (Hex(CLng(inArray(i + 3)) * Hex(16777216) + CLng(inArray(i + 2)) * 65536 + _
                                        CLng(inArray(i + 1)) * 256 + CLng(inArray(i)))))
                                    
                            VerifyFlag = False
                            
                        End If
                    End Select
                Else
                    
                    Select Case BootAddr
                    'Device reset vector, user reset vector, and delay address are ignored because
                    'bootloader will intentionally write different values in these locations than what may be
                    'specified in the .hex file causing erroneous varification errors.
                    Case 0
                        'ignore reset vector
                    Case 2
                        'ignore reset vector
                    Case PicBootS.UserResetVector
                        'ignore user's reset vector
                    Case PicBootS.BootDelayAddr
                        'ignore boot delay address
                    Case Else
                        'otherwise, verify error
                        b.WriteLine ("Verify Error at 0x" & Hex(BootAddr) & " should be: 0x" _
                                    & (Hex(CLng(outArray(i + 3)) * Hex(16777216) + CLng(outArray(i + 2)) * 65536 _
                                    + CLng(outArray(i + 1)) * 256 + CLng(outArray(i)))) & " but read: 0x" _
                                    & (Hex(CLng(inArray(i + 3)) * Hex(16777216) + CLng(inArray(i + 2)) * 65536 + _
                                    CLng(inArray(i + 1)) * 256 + CLng(inArray(i)))))
                        
                        VerifyFlag = False
    
                    End Select
                End If
                
            End If
            
            i = i + 4
        ElseIf picV.BootCmd = 4 Then
            
            If PicBootS.AESEnable Then
                If (inArray(i) <> outArray(i) Or _
                    inArray(i + 1) <> outArray(i + 1) Or _
                    inArray(i + 2) <> outArray(i + 2) Or _
                    inArray(i + 3) <> outArray(i + 3)) Then
                    
                    b.WriteLine ("Verify Error at 0x" & Hex(BootAddr) & " should be: 0x" _
                                & (Hex(CLng(outArray(i + 3)) * Hex(16777216) + CLng(outArray(i + 2)) * 65536 _
                                + CLng(outArray(i + 1)) * 256 + CLng(outArray(i)))) & " but read: 0x" _
                                & (Hex(CLng(inArray(i + 3)) * Hex(16777216) + CLng(inArray(i + 2)) * 65536 + _
                                CLng(inArray(i + 1)) * 256 + CLng(inArray(i)))))
                                
                    VerifyFlag = False
                    
                End If
            
                i = i + 4
            Else
                If (inArray(i) <> outArray(i) Or _
                    inArray(i + 1) <> outArray(i + 1)) Then
                
                    b.WriteLine ("Verify Error at 0x" & Hex(BootAddr) & " should be: 0x" _
                                & Hex(CLng(outArray(i + 1)) * 256 + CLng(outArray(i))) & " but read: 0x" _
                                & Hex(CLng(inArray(i + 1)) * 256 + CLng(inArray(i))))
                                                    
                    VerifyFlag = False
                End If
                
                i = i + 2
            End If
        
        Else
            'Note: Config bits cannot be verified, as unimplemented bits are unknown and read as 0
            i = i + 1
        End If
        
        BootAddr = BootAddr + 2
        
    Loop
    
    b.Close
    
    If VerifyFlag = False Then
        VerifyWrite = -12
    Else
        VerifyWrite = RetStat
    End If
            
End Function


Function EraseRangeDevMem(AddrL As Long, AddrH As Long) As Integer
    Dim RetStat As Integer
    Dim BootAddr As Long
    Dim nBlocks As Byte
    
    AbortFlag = 1
    BootAddr = AddrL
            
    frmBootload.StatusBar1.Panels(1).Text = "Erasing..."
            
    Do While BootAddr < (AddrH + 1)
    
        DoEvents
        
        'check for an abort
        If AbortFlag = 0 Then
            EraseRangeDevMem = -100
            Exit Function
        End If

        nBlocks = (AddrH - AddrL + 1) * PicBootS.DevBytesPerAddr / PicBootS.DeviceErsBlock
        If nBlocks > 255 Then
            nBlocks = 255
        End If
        
        'Go get some data
        RetStat = ErasePIC(PicBootS.PortHandle, BootAddr, nBlocks, 5)
        
        If RetStat < 0 Then
            EraseRangeDevMem = RetStat
            Exit Function
        End If
                  
        BootAddr = BootAddr + ((PicBootS.DeviceErsBlock / PicBootS.DevBytesPerAddr) * nBlocks)
        
        frmBootload.StatusBar1.Panels(1).Text = "Erasing: " & Hex(BootAddr)
    
    Loop
    
    EraseRangeDevMem = 1

End Function


Function WriteConfig(CfgAddr As Long, CfgData As Byte) As Integer
    ReDim InData(10) As Byte

    InData(0) = 7 'command
    InData(1) = 1

    InData(2) = CByte((CfgAddr) And 255)
    InData(3) = CByte(((CfgAddr) And 65280) \ 256)
    InData(4) = CByte(((CfgAddr) And 16711680) \ 65536)
    InData(5) = CfgData

    RetStat = SendGetPacket(PicBootS.PortHandle, InData(0), 6, 255, 1)
    If RetStat < 0 Then
        WriteConfig = RetStat
        Exit Function
    End If

    frmBootload.StatusBar1.Panels(1).Text = "Writing CONFIG: " & Hex(CfgAddr)
    WriteConfig = 1
End Function


Function ReadConfig(CfgAddr As Long) As Integer
    ReDim InData(10) As Byte

    InData(0) = 6 'command
    InData(1) = 1

    InData(2) = CByte((CfgAddr) And 255)
    InData(3) = CByte(((CfgAddr) And 65280) \ 256)
    InData(4) = CByte(((CfgAddr) And 16711680) \ 65536)

    RetStat = SendGetPacket(PicBootS.PortHandle, InData(0), 5, 255, 1)
    If RetStat < 0 Then
        ReadConfig = RetStat
        Exit Function
    End If

    frmBootload.StatusBar1.Panels(1).Text = "Reading CONFIG: " & Hex(CfgAddr)
    ReadConfig = InData(5)
End Function




Function ReadVersion() As String
    ReDim DevID(10) As Byte
    Dim RetStat As Integer
    
    DoEvents

    DevID(0) = 0
    DevID(1) = 2
    RetStat = SendGetPacket(PicBootS.PortHandle, DevID(0), 2, 10, 1)
    
    If RetStat <= 0 Then
        ReadVersion = Empty
    Else
        ReadVersion = "v" & DevID(3) & "." & DevID(2)
    End If
    
End Function



Function ReadDeviceID() As String
    ReDim DevID(10) As Byte
    Dim RetStat As Integer
    Dim picb As PIC
    
    DevID(0) = 0
    DevID(1) = 0

    picb.BootAddr = &HFF0000
    picb.BootCmd = 1
    picb.BytesPerBlock = 4
    picb.BytesPerAddr = 2
    picb.BootDatLen = 4
    picb.MaxRetrys = PicBootS.MaxRetry
    RetStat = ReadPIC(PicBootS.PortHandle, picb, DevID(0))

    If RetStat <= 0 Then
        ReadDeviceID = "0"
    Else
        ReadDeviceID = CStr((DevID(1) * 256) + DevID(0))
    End If
    
End Function



Function GotoRunMode() As Integer
    ReDim DevID(10) As Byte
    Dim RetStat As Integer
    Dim picb As PIC

    DevID(0) = 0
    DevID(1) = 0
    RetStat = SendPacket(PicBootS.PortHandle, DevID(0), 2)
    
End Function





Function Dec2Bin(MyByte As Byte) As String
    Dim CurrentData As Integer
    Dim OldData As Integer
    
    Dec2Bin = ""
    OldData = MyByte
    
    For i = 7 To 0 Step -1
     
        CurrentData = OldData - (2 ^ i)
        If CurrentData < 0 Then
            Dec2Bin = Dec2Bin & "0"
        Else
            OldData = CurrentData
            Dec2Bin = Dec2Bin & "1"
        End If
    
    Next i
    
End Function


Function Dec2Hex(MyInteger As Variant, MyWidth As Variant) As String
    Dim TempWork As String
    Dim TempWidth As Long
    Dim tempInt As Long

    TempWidth = CLng(MyWidth)
    tempInt = CLng(MyInteger)
    
    TempWork = Hex(tempInt)
    
    If Len(TempWork) > TempWidth Then
        Dec2Hex = Mid(TempWork, Len(TempWork) - TempWidth, TempWidth)
        Exit Function
    End If
    
    Do Until Len(TempWork) = TempWidth
        TempWork = "0" & TempWork
    Loop
    
    Dec2Hex = TempWork
End Function



⌨️ 快捷键说明

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