I just finish my first bootloader and it use simple data encription. I isn't so hard to make one. I put bootloader on end of program memory(DEFINE RESET_ORG), so main app isn't affected with bootloader. If you want to jump to bootloader just execute @ GOTO 1EC00h in main app.
Code for bootloader is simple. Here is part of code for PIC:
You can add EEPROM, yust using READ and WRITECode:DEFINE RESET_ORG 1EC00h 'Var definition and 'Set registers BlockSize VAR BYTE BANKA SYSTEM 'Get BLOCK SIZE from ASM @ MovLW BLOCK_SIZE @ MovWF BlockSize MemSize=79999' Used first 80000 bytes of FLASH PAUSE 6 FOR Adr=0 TO MemSize STEP BlockSize ERASECODE Adr NEXT Adr FOR Adr=0 TO MemSize STEP 128 Err=0 FwReceive: Err=Err+1 IF Err=5THEN 'If PC doesn't respond, do something... ENDIF HSEROUT2 ["A:",DEC6 Adr,13,10] 'Send address to PC HSERIN2 100, FwReceive, [WAIT("D:"), Tmp0, Tmp1] 'Receive same byte twice, to eliminate communication errors.. IF Tmp0<>Tmp1 THEN GOTO FwReceive GOSUB ByteDecode WRITE Adr, Tmp0 'Write will first fill up table then it will write 64 or 128 bytes to memory. depending on BlockSize, Check PBP manual NEXT Adr HSEROUT2 ["C"] @ RESET
Or you can add config bytes, thanks to Darrel Taylor.
http://www.picbasic.co.uk/forum/showthread.php?t=4093
For PC side you can use http://www.microchip.com/stellent/id...pnote=en012031 as starting point.
I menage to translate this app to VB .net, get FLASH, EEPROM and CONFIG data from hex file. From that point it was easy to add simple byte encryption and communication.
Communication code in Vb.net
I tried to put bootloader in main app, but that was very hard. Only way that it could be done is to compile your bootloader, then get flash memory from hex, remove part before and after bootloader, then use ORG 1EC00h and dw(thanks to DT http://www.picbasic.co.uk/forum/show...php?t=3891#LAB) to put data to flash directly.Code:Private Sub tmrWriteToDevice_Tick(sender As Object, e As EventArgs) Handles tmrWriteToDevice.Tick Static RxBuffer As String If SerialPort1.IsOpen = True Then RxBuffer += SerialPort1.ReadExisting If RxBuffer.Length > 0 Then If InStr(RxBuffer, "A:", CompareMethod.Text) > 0 And Mid(RxBuffer, Len(RxBuffer), 1) = Chr(3) Then Dim Adr As Integer, DataPos As Integer DataPos = InStr(RxBuffer, "A:") RxBuffer = Mid(RxBuffer, DataPos + 2, 6) 'Adress string Adr = Convert.ToInt32(RxBuffer, 10) RxBuffer = "" ByteArray(0) = HexFileByte(Adr) 'HexFileByte if loaded with flesh data from hex, and encoded. ByteArray(1) = HexFileByte(Adr) SerialPort1.Write("D:") SerialPort1.Write(ByteArray, 0, 2) ToolStripProgressBar1.Value = Adr / 800 ToolStripStatusLabel1.Text = ToolStripProgressBar1.Value.ToString & "% Complited" ElseIf InStr(RxBuffer, "C" & Chr(3), CompareMethod.Text) > 0 Then ToolStripStatusLabel1.Text = "Data Transfer Complite" ToolStripProgressBar1.Value = 100 End If End If End If End Sub
From this I created another one to boot from I2C memory, so in bootloader there is only I2C communication so it is only about 1K. Another advantige is that you can load I2C memory from any communication that you have on your application(eg serial port, bluetooth, usb etc..) and still be able to use same bootloader. Idea taken from Mike
I hope this will be enough to create your bootloader.




Bookmarks