View previous topic :: View next topic |
Author |
Message |
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Fri May 13, 2016 9:54 pm Post subject: Program memory erase after Bootloader error |
|
|
I would like to improve a standard Bascom MCS Bootloader code to erase program memory after getting transmission error.
Normally after Bootloader transmission error AVR needs a manual Reset. Erasing all program memory would prevent from this manual resetting procedure.
It is quite easy to detect a transmission error in the Bootloader code, but how to erase the AVR program memory? As far as I know, there is no Bascom instruction for writing FF into program memory. Bootloader uses spm instruction to write data into program memory.
My question is: How to use the spm instruction to erase all program memory in ATMEGA 2560.
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Fri May 13, 2016 10:44 pm Post subject: |
|
|
What does a manual reset have to do with an empty memory?
A manual reset may for sure make sense, however if you like to do a reset by software, what hinders you to reset the stack pointer and jump to the bootloader? Or activate the watchdog and don't reset it.
If you still like to write the flash with &hFF's, look up where the serial input normally receives a byte and stores it to flash. Then make the bootloader code believe, it receives from serial, while you feed &hFF's. |
|
Back to top |
|
|
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Sat May 14, 2016 9:42 am Post subject: |
|
|
You are right, if transmission error I would directly jump to the bootloader, but finally the program will execute the corrupted program code (broken by transmission error).
The solution would be to write a jump to bootloader instruction at the very beginning of the program code memory.
So in the case of the bootloader transmission error bootloader should write only a jump to botloader instruction at the beginning of program code memory.
It will result in a loop in witch bootloader is waiting for a new transmission and the corrupted program is not executed.
Does anyone have any idea how to write jump to bootloader instruction at the very beginning of the program code memory? Shoud I use spm assembler instruction? What would be the spm code? |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sat May 14, 2016 10:40 am Post subject: |
|
|
Pzx wrote: | What would be the spm code? |
As you're about of modifying the bootloader, you should have some .bas code already and therein is the "spm code".
For example BootLoader.bas in the samples folder, there the subroutine "Do_spm" is responsible for writing the flash.
How the sub is used and what arguments you need to provide, you can learn from the subroutine "Writepage".
You can also simply fill the buffer B(x) with the appropriate values, set the page and call Writepage, which does the work for you.
If you want to alter a different bootloader, you have you learn how it's done there.
About using SPM, i.e. setting up registers and parameters, there's information readily available. |
|
Back to top |
|
|
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Sat May 14, 2016 11:32 am Post subject: |
|
|
Yes, I would like to slightly modify Bootloader.bas code
Could anyone help me with modifying the code to write instruction jump to bootloader at the very beginning of the program memory?
Standard writing page into program memory using spm procedure looks like this:
Writepage: 'write 1 or more pages
If Bkind = 0 Then
For J = 1 To 128 Step 2 'we write 2 bytes into a page
Vl = Buf(j) : Vh = Buf(j + 1) 'get Low and High bytes
lds r0, {vl} 'store them into r0 and r1 registers
lds r1, {vh}
Spmcrval = 1 : Gosub Do_spm 'write value into page at word address
Wrd = Wrd + 2 'word address increases with 2 because LS bit of Z is not used
If Wrd = Maxword Then 'page is full
Wrd = 0 'Z pointer needs wrd to be 0
Spmcrval = 5 : Gosub Do_spm 'write page
Spmcrval = 17 : Gosub Do_spm 're-enable page
Page = Page + 1 'next page
Spmcrval = 3 : Gosub Do_spm 'erase next page
Spmcrval = 17 : Gosub Do_spm 're-enable page
End If
Next
Else 'eeprom
For J = 1 To 128
Writeeeprom Buf(j) , Wrd
Wrd = Wrd + 1
Next
End If
Return
Do_spm:
Bitwait Spmcsr.0 , Reset 'check for previous SPM complete
Bitwait Eecr.1 , Reset 'wait for eeprom
Z = Page 'make equal to page
Shift Z , Left , Maxwordshift 'shift to proper place
Z = Z + Wrd 'add word
lds r30,{Z}
lds r31,{Z+1}
lds r24,{Z+2}
sts rampz,r24 'we need to set rampz also for the M128
Spmcsr = Spmcrval 'assign register
spm 'this is an asm instruction
nop
nop
Return |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sat May 14, 2016 12:35 pm Post subject: |
|
|
If BOOTRST is set then micro always start at addres combined with BOOTSZ0 BOOTSZ1 and dependend used micro.
I think you should add function to existing bootloader that check integrity of main programm. Block CRC or so.
If main programm is corrupted then bootloader should stay and wait for new one. In that way you prevent corrupted code from run. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun May 15, 2016 12:21 am Post subject: |
|
|
EDC wrote: | If BOOTRST is set then micro always start at addres combined with BOOTSZ0 BOOTSZ1 and dependend used micro.
I think you should add function to existing bootloader that check integrity of main programm. Block CRC or so.
If main programm is corrupted then bootloader should stay and wait for new one. In that way you prevent corrupted code from run. |
What the TO wants, can be made to work, it's only necessary to place a JMP to the bootloader at flash address &h0000. After timeout the bootloader executes &h0000, which in return calls the bootloader again.
But all that is not an actual problem, the problem is that the TO on one hand wants something: to modify a bootloader for his purposes, while on the other hand he can not, or does not want to take the efforts to do it himself. Thus he seeks sombody doing his work. While I feel no desire, you're surly welcome if you do. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun May 15, 2016 8:40 am Post subject: |
|
|
Morrning cup of coffe brings me one idea
If bootloader get instruction to flash micro then it can write &HFF to specified Eeprom address.
Because programing protocol check block integrity while programming, after correct programming it can write some value to this specified Eeprom addres again. This will be a sign that everything pass.
This addres can be simply check at every start. If contain &HFF then programming must be done and bootloader should wait for new main app.
Siple writing or check one eeprom address should not growing bootloader code much. |
|
Back to top |
|
|
Pzx
Joined: 05 May 2015 Posts: 39
|
Posted: Sun May 15, 2016 3:39 pm Post subject: |
|
|
Good idea EDC, sometimes morning coffe really helps, thanks
I will try to implement a kind of transmission error flag and store it in standard eeprom memory. It should solve the issue without considering asm instructions |
|
Back to top |
|
|
|