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

📄 piccomms.bas

📁 PIC24FJ32GA002单片机bootloader rs485通信移植
💻 BAS
📖 第 1 页 / 共 2 页
字号:
Attribute VB_Name = "PicComms"
Public Declare Function SendPacket Lib "PICBOOT.dll" (ByVal hComPort As Long, PacketData As Byte, ByVal NumOfBytes As Integer) As Integer
Public Declare Function GetPacket Lib "PICBOOT.dll" (ByVal hComPort As Long, PacketData As Byte, ByVal NumOfBytesLim As Integer) As Integer
Public Declare Function OpenPIC Lib "PICBOOT.dll" (ByVal ComPort As String, ByVal BitRate As Long, ByVal ReadTimeOut As Long) As Long
Public Declare Function ClosePIC Lib "PICBOOT.dll" (ByVal hComPort As Long) As Integer
Public Declare Function SendGetPacket Lib "PICBOOT.dll" (ByVal hComPort As Long, PacketData As Byte, ByVal NumOfBytes As Integer, ByVal NumOfBytesLim As Integer, ByVal NumOfRetrys As Integer) As Integer
Public Declare Function ReadPIC Lib "PICBOOT.dll" (ByVal hComPort As Long, LPpic As PIC, MemData As Byte) As Integer
Public Declare Function WritePIC Lib "PICBOOT.dll" (ByVal hComPort As Long, LPpic As PIC, MemData As Byte) As Integer
Public Declare Function VerifyPIC Lib "PICBOOT.dll" (ByVal hComPort As Long, LPpic As PIC, MemData As Byte) As Integer
Public Declare Function ErasePIC Lib "PICBOOT.dll" (ByVal hComPort As Long, ByVal PICAddr As Long, ByVal nBlock As Byte, ByVal nRetry As Byte) As Integer

Public Type PIC                 'structure used in communications DLL
    BootCmd As Byte
    BootDatLen As Integer
    BootAddr As Long
    BytesPerBlock As Integer
    BytesPerAddr As Byte
    MaxRetrys As Integer
    DeviceType As Integer
    
End Type

Public Type PICBOOT
    PortHandle As Long          'port info
    BitRate As Long
    CommPort As String
    CommTimeOut As Long
    
    MaxPacketSize As Integer
    MaxRetry As Integer

    DeviceMode As Byte          'Auto or manual
    DeviceType As Byte          'PIC16 or PIC18
    DeviceName As String        'device info
    DeviceCode As String
    
    DeviceWrtBlock As Integer      'byte per block
    DeviceRdBlock As Integer
    DeviceErsBlock As Integer
    DevBytesPerAddr As Byte
    ResetVector As String

    DebugLevel As Long
    
    InFileName As String        'file and path for load operation
    OutFileName As String       'file and path for save operation

    ProgMemFile As String       'Data files
    EEDataFile As String
    ConfigFile As String
    EditorFile As String
    ErrorLogFile As String

    AESEnable As Boolean

    ProgMemAddrH As Long        'Mem address limits (inclusive)
    ProgMemAddrL As Long
    EEDataAddrH As Long
    EEDataAddrL As Long
    ConfigAddrH As Long
    ConfigAddrL As Long
    BootLoadAddrH As Long
    BootLoadAddrL As Long
    UserResetVector As Long
    BootDelayAddr As Long
End Type


Public PicBootS As PICBOOT
Public bpic As PIC

Public MyFlag As Byte
Public DataPacket(256) As Byte
Public TimeOutFlag As Byte
Public AbortFlag As Byte







Function ReadRangeDevMem(RdAddrL As Long, RdAddrH As Long, BytsPerAddr As Byte, BCom As Byte, OutFile As String) As Integer
    ReDim InData(1024) As Byte
    Dim RetStat As Integer
    Dim FileLine As String
    Dim BootDatLen As Integer
    Dim BootAddr As Long
    Dim picA As PIC
    
    'Setup data file creation
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile(VB.App.Path & "\" & OutFile, True)
    
    'Translate to HEX file byte address
    AddrL = RdAddrL * 2
    AddrH = RdAddrH * 2
    
    AbortFlag = 1
    BootAddr = AddrL

            
    Do While BootAddr < (AddrH)
    
        DoEvents
        
        'check for an abort
        If AbortFlag = 0 Then
            ReadRangeDevMem = -100
            Exit Function
        End If
    
        'limit the packet size
        If (AddrH + 2) - BootAddr > ((PicBootS.MaxPacketSize - 5)) Then
            picA.BootDatLen = (PicBootS.MaxPacketSize - 5)
        Else
            picA.BootDatLen = (AddrH + 2) - BootAddr
        End If
               
        picA.BootAddr = BootAddr / 2
        picA.BootCmd = BCom
        picA.BytesPerAddr = BytsPerAddr

        '***MODIFIED FOR AES - WHEN USING EE AND CONFIG WITH ENCRYPTION, BLOCK SIZE CHANGES
        'DUE TO EXTRA PHANTOM BYTES
        If PicBootS.AESEnable Then
            Select Case picA.BootCmd
                Case 4
                    picA.BytesPerBlock = 2
                Case 6
                    picA.BytesPerBlock = 1
                Case Else
                    picA.BytesPerBlock = PicBootS.DeviceRdBlock
            End Select
        Else
            picA.BytesPerBlock = PicBootS.DeviceRdBlock
        End If
        picA.MaxRetrys = PicBootS.MaxRetry
        picA.DeviceType = PicBootS.DeviceType
        
        RetStat = ReadPIC(PicBootS.PortHandle, picA, InData(0))
        If RetStat < 0 Then
            ReadRangeDevMem = RetStat
            Exit Function
        End If
        
        
        'Format the data
        For i = 0 To RetStat - 1
    
            Select Case BCom
                Case 4   'Data EE
                    
                    If BootAddr Mod 16 = 0 Then
                        FileLine = Dec2Hex(BootAddr, 6)
                    End If
                    
                    FileLine = FileLine & " " & Dec2Hex(CLng(InData(i)), 2)
                    BootAddr = BootAddr + 1
                    
                    '***MODIFIED FOR AES - Must read 2 phantom bytes from device
                    'as they are used for HEX encyrpt/decrypt
                    If Not PicBootS.AESEnable Then
                    
                        'Pad in 2 extra bytes as 0 for HEX file (32 bit addresses)
                        If (i + 1) Mod 2 = 0 Then
                            FileLine = FileLine & " 00 00"
                            BootAddr = BootAddr + 2
                        End If
                    End If
                    
                    If BootAddr Mod 16 = 0 Then
                        a.WriteLine (FileLine)
                    End If
    
                Case 6    'Config
                    
                    If PicBootS.AESEnable Then
                        
                        '***MODIFIED FOR AES - Must read all 4 from device
                        'as they are used for HEX encyrpt/decrypt
                                        
                        If BootAddr Mod 16 = 0 Then
                            FileLine = Dec2Hex(BootAddr, 7)
                        End If
                        
                        FileLine = FileLine & " " & Dec2Hex(CLng(InData(i)), 2)
                        BootAddr = BootAddr + 1
                        
                        If BootAddr Mod 16 = 0 Then
                            a.WriteLine (FileLine)
                        End If
                        
                    Else
                    
                        'Read 1 byte and pad in 3 extra bytes as 0 for HEX file (32 bit addresses)
                        FileLine = Dec2Hex(BootAddr, 7)
                        FileLine = FileLine & " " & Dec2Hex(CLng(InData(i)), 2) & " 00 00 00"
                        BootAddr = BootAddr + 4
                        a.WriteLine (FileLine)
                        
                    End If

                Case Else  'Prog Mem
                    
                    If BootAddr Mod 16 = 0 Then
                        FileLine = Dec2Hex(BootAddr, 6)
                    End If
                    
                    FileLine = FileLine & " " & Dec2Hex(CLng(InData(i)), 2)
                    BootAddr = BootAddr + 1
                    
                    If BootAddr Mod 16 = 0 Then
                        a.WriteLine (FileLine)
                    End If
            End Select

        Next i
       
        frmBootload.StatusBar1.Panels(1).Text = "Reading: " & Hex(BootAddr / 2)
    
    Loop
    ReadRangeDevMem = 1
    a.Close
End Function


Function WriteRangeDevMem(BlockSize As Integer, BytsPerAddr As Byte, BCom As Byte, InFile As String) As Integer
    ReDim OutData(256) As Byte
    Dim RetStat As Integer
    Dim ProgressInd As Integer
    Dim FileLine As String
    Dim picA As PIC
    Dim OldBootAddr As Long
    Dim ReadNext As Boolean
    Dim FirstLine As Boolean
    Dim i As Integer
    Dim LinesLeft As Integer
    Dim BytesLeft As Integer
    Dim tempBootAddr As Long
    Dim VerifyFlag As Boolean
    Dim TmpStr As String

    ReadNext = True
    FirstLine = True
    i = 0
    LinesLeft = BlockSize / 16
    
    'Setup data file creation
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.OpenTextFile(VB.App.Path & "\" & InFile, 1, False, 0)
    Set b = fs.CreateTextFile(PicBootS.ErrorLogFile, True)
    b.Close
            
    AbortFlag = 1
    ProgressInd = 1
    
    VerifyFlag = True

    Do While a.AtEndOfStream <> True
        DoEvents

        FileLine = a.ReadLine


        'check for an abort
        If AbortFlag = 0 Then
            If ProgressInd = -101 Then
                Exit Do
            End If
            
            ProgressInd = -100
            Exit Do
        End If
           

        picA.BootCmd = BCom
        picA.BytesPerAddr = BytsPerAddr
        picA.BytesPerBlock = BlockSize
        picA.MaxRetrys = PicBootS.MaxRetry
        picA.DeviceType = PicBootS.DeviceType
        
        'Check command type - handle PM write as full row, dataEE by line
        If BCom = 5 Then   'Data EE
            picA.BootAddr = (CLng(Val("&H" & Mid(FileLine, 1, 6) & "&")) And FULL_ADDRESS_MASK) \ BytsPerAddr
            
            i = 0
            j = 0
            TmpStr = Mid(FileLine, i * 3 + 8, 2)
            Do While TmpStr <> ""
                If PicBootS.AESEnable Then
                    
                    '***MODIFIED FOR AES: Must send upper unused 2 bytes because it is used during encryption/decrypt
                    OutData(j) = CByte(Val("&H" & TmpStr))
                    j = j + 1
                
                Else
                    
                    'Data EE only implements 16 bits, don't send upper 2 bytes
                    If (((i + 2) Mod 4) = 0) Or (((i + 1) Mod 4) = 0) Then
                        'Do not use upper 2 bytes of data
                    Else
                        OutData(j) = CByte(Val("&H" & TmpStr))
                        j = j + 1
                    End If
                    
                End If
                    
                i = i + 1
                TmpStr = Mid(FileLine, i * 3 + 8, 2)
            Loop
            
            picA.BootDatLen = j
            
        ElseIf BCom = 7 Then   'Config fuses
            
            picA.BootAddr = (CLng(Val("&H" & Mid(FileLine, 1, 7) & "&")) And FULL_ADDRESS_MASK) \ BytsPerAddr
            
            i = 0
            j = 0
            TmpStr = Mid(FileLine, i * 3 + 9, 2)
            Do While TmpStr <> ""
       
       
               If PicBootS.AESEnable Then
                    
                    '***MODIFIED FOR AES: Must send upper unused 3 bytes because it is used during encryption/decrypt
                    OutData(j) = CByte(Val("&H" & TmpStr))
                    j = j + 1
                
                Else
                        
                    'Config only implements 8 bits, don't send upper 3 bytes
                    If (((i + 3) Mod 4) = 0) Or (((i + 2) Mod 4) = 0) Or (((i + 1) Mod 4) = 0) Then
                        'Do not use upper 3 bytes of data
                    Else
                        OutData(j) = CByte(Val("&H" & TmpStr))
                        j = j + 1
                    End If
                End If
                
                i = i + 1
                TmpStr = Mid(FileLine, i * 3 + 9, 2)
            Loop
            
            picA.BootDatLen = j

        Else                'Program Memory

            If i = 0 Then
                picA.BootDatLen = BlockSize
                picA.BootAddr = (CLng(Val("&H" & Mid(FileLine, 1, 6) & "&")) And FULL_ADDRESS_MASK) / BytsPerAddr 'word addressed

            End If
            
            j = 0
            Do While j < 16

                OutData(i) = CByte(Val("&H" & Mid(FileLine, j * 3 + 8, 2)))
                
                i = i + 1
                j = j + 1
            Loop

        End If
        

        
        If BCom <> 2 Or (i Mod picA.BootDatLen = 0) Then
            
       
            'Write row/line
            RetStat = WritePIC(PicBootS.PortHandle, picA, OutData(0))
            If RetStat < 0 Then
                WriteRangeDevMem = RetStat
                Exit Function
            End If
                                           
            'Verify written row/line
            RetStat = VerifyWrite(picA, OutData)
            If RetStat < 0 Then
                If RetStat = -12 Then
                    ProgressInd = -101
                    VerifyFlag = False
                Else

⌨️ 快捷键说明

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