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

📄 frmbootload.frm

📁 PIC24FJ32GA002单片机bootloader rs485通信移植
💻 FRM
📖 第 1 页 / 共 4 页
字号:
      Begin VB.Menu M_MemAccess 
         Caption         =   "EEDATA"
         Index           =   2
      End
      Begin VB.Menu M_MemAccess 
         Caption         =   "CONFIG"
         Index           =   3
      End
      Begin VB.Menu space102 
         Caption         =   "-"
      End
      Begin VB.Menu M_PSendCfg 
         Caption         =   "Send Config"
      End
      Begin VB.Menu space101 
         Caption         =   "-"
      End
      Begin VB.Menu M_About 
         Caption         =   "About"
      End
   End
End
Attribute VB_Name = "frmBootload"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False


Const STATUS_NOT_CON As String = "Not connected..."
Const STATUS_CONNECTING As String = "Connecting..."
Const STATUS_DEVICE_FOUND As String = " detected..."
Const STATUS_DATA_FILE_NOT_FOUND As String = "Data file not found..."
Const STATUS_FAILED_TO_OPEN_PORT As String = "Failed to open port..."
Const STATUS_NO_VERSION_INFO As String = "No firmware version available..."
Const STATUS_FOUND_DEVICE As String = "Device found..."
Const STATUS_NO_DEVICE As String = "Unable to connect to device..."
Const STATUS_READ_FAILURE As String = "Failed to read device..."
Const STATUS_WRITE_FAILURE As String = "Failed to write device..."
Const STATUS_ABORT As String = "Operation aborted..."
Const STATUS_FINISHED As String = "Finished operation..."
Const STATUS_VERIFY_ERROR As String = "Verify error received..."
Const STATUS_RUNMODE_SET As String = "Run mode is set..."
Const STATUS_HEX_FORMAT As String = "HEX file not padded properly..."
Const STATUS_HEX_IMPORTED As String = "HEX file imported..."
Const STATUS_INVALID_HEX As String = "Invalid HEX file..."
Const STATUS_HEX_EXPORTED As String = "HEX file exported..."
Const STATUS_VERIFYOK_ERROR As String = "Verify Ok command failed..."


Const MODE_NOT_CONNECTED As Integer = 0
Const MODE_CONNECTED_IDLE As Integer = 1
Const MODE_WORKING As Integer = 2
Const MODE_DONE As Integer = 3

Const PANEL_STATUS As Integer = 1
Const PANEL_FWVER As Integer = 2
Const PANEL_DEVICE As Integer = 3
Const PANEL_PORT As Integer = 4
Const PANEL_BITRATE As Integer = 5


'Flag used for a
Dim PanelClicked As Integer
Dim HideToolFlag As Byte




Private Sub cConfig_Click()
    M_MemAccess(3).Checked = cConfig.Value
End Sub

Private Sub cDataEE_Click()
    M_MemAccess(2).Checked = cDataEE.Value
End Sub

Private Sub cProgMem_Click()
    M_MemAccess(1).Checked = cProgMem.Value
    If PicBootS.DeviceType = 2 Then
        cFlashConfig.Enabled = cProgMem.Value
    End If
End Sub

Private Sub M_PDevSelector_Click()
    DisconnectDev

    dSelectDevice.Cm_DevList.ListIndex = 0
    dSelectDevice.Show vbModal, frmBootload
End Sub


Private Sub M_PSendCfg_Click()
    dWriteFuses.Show vbModal, frmBootload
End Sub




Private Sub StatusBar1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 2 Then
        If PanelClicked = 1 Then
            PopupMenu M_Program, vbPopupMenuRightButton
        End If
    
        If PanelClicked = 3 Then
            M_PDevSelector_Click
        End If
    
        If PanelClicked = 4 Then
            PopupMenu M_PortSettings, vbPopupMenuRightButton
        End If
    
        If PanelClicked = 5 Then
            PopupMenu M_BitRateSettings, vbPopupMenuRightButton
        End If
    End If
End Sub

Private Sub StatusBar1_PanelClick(ByVal Panel As Panel)
    PanelClicked = Panel.Index
End Sub




'******************************************************************************
'Main Form related functions
'******************************************************************************
Private Sub Form_Load()

    On Error GoTo ErrorHandler

    'Set the size
    Me.Height = Toolbar1.Height + StatusBar1.Height + cFlashConfig.Height * 4 + 400
    Me.Width = Toolbar1.Width + 1000
                
    SetDisplayMode MODE_NOT_CONNECTED
    
    PicBootS.ProgMemFile = GetSetting("PIC24FBOOT", "progmem")
    PicBootS.EEDataFile = GetSetting("PIC24FBOOT", "eedata")
    PicBootS.ConfigFile = GetSetting("PIC24FBOOT", "config")
    PicBootS.ErrorLogFile = GetSetting("PIC24FBOOT", "errorlog")
    PicBootS.CommTimeOut = Val(GetSetting("PIC24FBOOT", "CommTimeOut"))
    PicBootS.DebugLevel = Val(GetSetting("PIC24FBOOT", "debuglevel"))
    PicBootS.DeviceMode = Val(GetSetting("PIC24FBOOT", "devicemode"))
    PicBootS.MaxRetry = Val(GetSetting("PIC24FBOOT", "maxretry"))
    PicBootS.EditorFile = GetSetting("PIC24FBOOT", "editor")
    PicBootS.UserResetVector = CLng("&H" & "0" & GetSetting("PIC24FBOOT", "userresetvector"))
    PicBootS.BootDelayAddr = CLng("&H" & "0" & GetSetting("PIC24FBOOT", "bootdelay"))
    '***MODIFIED FOR AES: Read AES setting out of .ini file to see if encryption is enabled
    PicBootS.AESEnable = CBool(GetSetting("PIC24FBOOT", "AESEnable"))

    '***MODIFIED FOR AES: When using encryption QP must know bootloader location in case it falls
    'inside an otherwise valid 16-byte block.
    If PicBootS.AESEnable Then
        PicBootS.BootLoadAddrH = CLng("&H" & "0" & GetSetting("PIC24FBOOT", "bootaddrhi"))
        PicBootS.BootLoadAddrL = CLng("&H" & "0" & GetSetting("PIC24FBOOT", "bootaddrlo"))
    End If
    If CInt(GetSetting("PIC24FBOOT", "selectdevwin")) Then
        dSelectDevice.Cm_DevList.ListIndex = 0
        dSelectDevice.Show vbModal, frmBootload
    End If
        
    PicBootS.ProgMemAddrH = &H200
    PicBootS.ProgMemAddrL = &H200
    PicBootS.EEDataAddrH = 0
    PicBootS.EEDataAddrL = 0
    PicBootS.ConfigAddrH = &H300000
    PicBootS.ConfigAddrL = &H300000
    
       
    MyIndex = GetSetting("PIC24FBOOT", "portindex")
    M_FPPort_Click (CInt(MyIndex))
    MyIndex = GetSetting("PIC24FBOOT", "bitrateindex")
    M_FPBaud_Click (CInt(MyIndex))
    
    StatusBar1.Panels(PANEL_STATUS).Text = STATUS_NOT_CON
    
    cFlashConfig.Enabled = False
    
    Exit Sub
    
ErrorHandler:
    
    StatusBar1.Panels(PANEL_STATUS).Text = "Core error:  " & Err.Description
    Err.Clear
End Sub


Private Sub Form_Unload(Cancel As Integer)
    'Close port if open
    If PicBootS.PortHandle > 0 Then
        ClosePIC (PicBootS.PortHandle)
        PicBootS.PortHandle = 0
    End If

    'Kill Fuse configurator
    Unload dWriteFuses
End Sub


Private Sub Form_Resize()

    On Error GoTo ErrorHandler
   
    If Me.Height < 2000 Then
        Me.Height = 2000
    End If
    If Me.Width < 5500 Then
        Me.Width = 5500
    End If
    
    Exit Sub
ErrorHandler:
    Err.Clear

End Sub
'******************************************************************************



Private Sub ConnectToPIC()
    Dim TempReturn As String
    Dim RetStat As Integer
    Dim picb As PIC
    Dim DevID(20) As Byte
        
    On Error GoTo ErrorHandler

    
    If PicBootS.PortHandle <= 0 Then
        PicBootS.PortHandle = OpenPIC(PicBootS.CommPort, PicBootS.BitRate, PicBootS.CommTimeOut)
    End If
    If PicBootS.PortHandle < 0 Then
        StatusBar1.Panels(PANEL_STATUS) = STATUS_FAILED_TO_OPEN_PORT
        Toolbar1.Buttons(9).Value = tbrUnpressed
        Exit Sub
    End If
    
    StatusBar1.Panels(PANEL_STATUS).Text = STATUS_CONNECTING
    
    'Read associated device name
    If PicBootS.DeviceMode = 0 Then 'manual or automatic
        
        PicBootS.DeviceCode = ReadDeviceID
        
        If PicBootS.DeviceCode = 0 Then
            StatusBar1.Panels(PANEL_STATUS) = STATUS_NO_DEVICE
            SetDisplayMode MODE_NOT_CONNECTED
            Exit Sub
        End If
        
        PicBootS.DeviceName = GetSetting("DEVICELIST", PicBootS.DeviceCode)
        
        If PicBootS.DeviceName = "" Then
            PicBootS.DeviceName = GetSetting("DEVICELIST", 0) 'PICUNKNOWN
        End If
        
    End If
    
    
    'Get firmware version
    StatusBar1.Panels(PANEL_FWVER) = ReadVersion
    If StatusBar1.Panels(PANEL_FWVER) = Empty Then
        StatusBar1.Panels(PANEL_STATUS) = STATUS_NO_VERSION_INFO
    End If


    'Read in the memory ranges
    PicBootS.ProgMemAddrL = CLng("&H" & "0" & GetSetting(PicBootS.DeviceName, "pmrangelow"))
    PicBootS.ProgMemAddrH = CLng("&H" & "0" & GetSetting(PicBootS.DeviceName, "pmrangehigh"))
    PicBootS.EEDataAddrL = CLng("&H" & "0" & GetSetting(PicBootS.DeviceName, "eerangelow"))
    PicBootS.EEDataAddrH = CLng("&H" & "0" & GetSetting(PicBootS.DeviceName, "eerangehigh"))
    PicBootS.ConfigAddrL = CLng("&H" & "0" & GetSetting(PicBootS.DeviceName, "cfgrangelow"))
    PicBootS.ConfigAddrH = CLng("&H" & "0" & GetSetting(PicBootS.DeviceName, "cfgrangehigh"))
   
    'Grab device specific UserResetVector and BootDelay locations if defined, otherwise use PIC24FBOOT defaults
    If GetSetting(PicBootS.DeviceName, "userresetvector") <> "" Then
        PicBootS.UserResetVector = CLng("&H" & "0" & GetSetting(PicBootS.DeviceName, "userresetvector"))
    End If
    If GetSetting(PicBootS.DeviceName, "bootdelay") <> "" Then
        PicBootS.BootDelayAddr = CLng("&H" & "0" & GetSetting(PicBootS.DeviceName, "bootdelay"))
    End If
      
    PicBootS.DevBytesPerAddr = Val(GetSetting(PicBootS.DeviceName, "bytesperaddr"))
    PicBootS.MaxPacketSize = Val(GetSetting(PicBootS.DeviceName, "maxpacketsize"))
    PicBootS.DeviceErsBlock = Val(GetSetting(PicBootS.DeviceName, "eraseblock"))
    PicBootS.DeviceRdBlock = Val(GetSetting(PicBootS.DeviceName, "readblock"))
    PicBootS.DeviceWrtBlock = Val(GetSetting(PicBootS.DeviceName, "writeblock"))
    PicBootS.DeviceType = Val(GetSetting(PicBootS.DeviceName, "devicetype"))
    

    '*** AES MODIFICATIONS: Cannot read reset vector due to encryption.
    '*** Bootloader must protect its own reset vector
    If PicBootS.AESEnable = False Then
        'Read in bootloader reset vector
        'read boot reset vector before erasing
        DevID(0) = 1 'command
        DevID(1) = 2

        DevID(2) = 0
        DevID(3) = 0
        DevID(4) = 0
        
        'Go get some data
        RetStat = SendGetPacket(PicBootS.PortHandle, DevID(0), 5, 255, 5)
        If RetStat < 0 Then
            frmBootload.StatusBar1.Panels(1).Text = "Reset Vector not read"
        Else
            PicBootS.ResetVector = ""
            For i = 0 To 7
                PicBootS.ResetVector = PicBootS.ResetVector & Dec2Hex(DevID(i + 5), 2) & " "
            Next i
        End If
        
    End If
    
    If PicBootS.DeviceType = 2 Then 'If PIC24FJ device w/ flash config words
        cFlashConfig.Enabled = True
        cFlashConfig.Value = 1
    End If

       
    'Read in the config bytes
    dWriteFuses.C_ConfigBytes.Clear
    For i = PicBootS.ConfigAddrL To PicBootS.ConfigAddrH
        TempReturn = GetSetting(PicBootS.DeviceName, Dec2Hex(CLng(i), 6))
        If StrComp(TempReturn, "") <> 0 Then
            dWriteFuses.C_ConfigBytes.AddItem TempReturn
            dWriteFuses.C_ConfigBytes.ItemData(MyCount) = i
            MyCount = MyCount + 1
        End If
    Next i
    
    StatusBar1.Panels(PANEL_DEVICE) = PicBootS.DeviceName
    StatusBar1.Panels(PANEL_STATUS) = STATUS_FOUND_DEVICE

    SetDisplayMode MODE_CONNECTED_IDLE
    
    Exit Sub

'Handle some errors
ErrorHandler:
    If Err.Description = STATUS_FAILED_TO_OPEN_PORT Then
        
    End If
    StatusBar1.Panels(PANEL_STATUS).Text = "Core error:  " & Err.Description

⌨️ 快捷键说明

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