View previous topic :: View next topic |
Author |
Message |
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Tue Nov 24, 2020 4:16 am Post subject: SD card/USART bootloader |
|
|
Hello.
If you use SD card in your device you can upgrade firmware from it.
If you also have internet connection then you can even upgrade your device remotely.
So if you, for example, have Mega2560 with Ethernet shield you can do this easly.
One condition is that you have uC with 4096 words bootspace. So Mega2560, Mega644, Mega128 , Mega1284 and others can do this.
This bootloader is some kind universal. It is compatible with MCS Bootloader so firmware can be uploaded via Serial (USB/RS232/RS485) directly from the Bascom IDE or dedicated standalone PC software. This is convienant when developing software. But when you ship your device to someone then it can be upgraded by sending only BIN file to that person. You can encrypt your software if you want so it can be flashed only into your device.
For flashing new software you must rename BIN file into "example.bin" and load it into the SD card with some card rider for PC. Then you must add file "update.txt" with atleast one char inside. During boot bootloader will search for "update.txt" and if found it then search for "example.bin". After flashing the file "update.txt" will be deleted.
Like I wrote earlier, if you have Internet like Wizz5100/Wizz5500 you can download new firmware from your server (VPS for example), store it on the card and then set the boot_flag "boot_status" in the eeprom. Then during boot bootloader find it and try to search for "example.bin"
I attach example for Arduino Mega2560 with Ethernet shield. If you use Bascom for write bootloader then programmer try to set the FuseBits automatically to 4096 Words boot size. If your programmer dont support this then you must set it manually and eventually BOOTRST too.
After flashing with programmer you can insert the card with the attached Blink code So...if everything working then your Arduino orange LED should Blink
There is some bytes left for additional checks like CRC or Crypt (this is simplified version of bootloader Im using). You can disable Debug for spare some additional bytes. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5921 Location: Holland
|
Posted: Tue Nov 24, 2020 9:51 am Post subject: |
|
|
thanks for sharing EDC .
A dual boot loader is indeed very convenient. And with the additional Ethernet everything is covered. _________________ Mark |
|
Back to top |
|
|
roni27
Joined: 19 Feb 2017 Posts: 5
|
Posted: Thu Jan 13, 2022 10:27 am Post subject: |
|
|
Hello,
I have adapted the program "SD card/USART bootloader" to an Atmega1284P.
Updating the firmware from the SD card is done without error message.
But, depending on the size of the updating firmware, the program does not run properly on the Atmega1284P after programming.
I noticed that all bytes after the last (read) complete page (128 bytes) are not programmed into the Atmega1284P.
Maybe someone can help me, that would be very nice.
The end of the original BIN file, read with a hex editor
Adr ....00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
098E0 6F 73 73 65 6E 20 77 65 72 64 65 6E 21 20 46 65
098F0 68 6C 65 72 63 6F 64 65 3A 20 00 00 46 65 68 6C
09900 65 72 2D 43 6C 6F 73 65 3A 20 00 00 23 00 43 6C
09910 6F 73 65 2C 20 00
The end of the program from the Atmega1284P, loaded from the chip into the buffer
Adr....00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
098E0 73 6F 65 73 20 6E 65 77 64 72 6E 65 20 21 65 46
098F0 6C 68 72 65 6F 63 65 64 20 3A 00 00 65 46 6C 68
09900 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
As it looks the last 22 bytes of the bin file are not programmed into the Atmega1284P.
The bin file has a size of 39190 bytes (&H9916).
These 39190 bytes / 128 (bytes per page) = 306 pages and rest 22 bytes
306 pages * 128 (bytes per page) = 39168 bytes (&H9900)
From address &H9900 in the Atmega1284P the last 22 bytes are missing.
In my opinion the problem is in the program command
Line 143 If Wcnt > 0 Then Gosub Writepage
from here on the bin file with the remaining 22 bytes is not programmed correctly into the Atmega1284P.
If the bin file (with the same BootLoader in the Atmega184) is transferred with the "MCS bootloader",
the program is programmed correctly into the Atmega1284.
When I read the bin file from the SD card and write it back to the SD card as a new file, both bin files are the same size.
I conclude that my SD card functions work correctly.
Kind regards |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5921 Location: Holland
|
Posted: Thu Jan 13, 2022 10:44 am Post subject: |
|
|
either you can make sure the bin file is a multiple of 128. or add some code to fill the buffer.
If Wcnt > 0 Then Gosub Writepage
would become :
Code: | If Wcnt > 0 Then
while wcnt<128
incr wcnt
Buf(wcnt) = 255
wend
Gosub Writepage
end if
|
_________________ Mark |
|
Back to top |
|
|
roni27
Joined: 19 Feb 2017 Posts: 5
|
Posted: Thu Jan 13, 2022 12:39 pm Post subject: |
|
|
Hello Mark,
thank you very much for the quick help.
I have changed the boot program, Buf(wcnt) is described correctly,
but from address 09900 the remaining bytes are not programmed into the chip.
098F0 6C 68 72 65 6F 63 65 64 20 3A 00 00 65 46 6C 68
09900 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
I am still missing something here.
Kind regards |
|
Back to top |
|
|
roni27
Joined: 19 Feb 2017 Posts: 5
|
Posted: Fri Jan 14, 2022 10:41 am Post subject: |
|
|
I continue to search for my mistake.
In the subprogram "Writepage" the last page is still read correctly,
displayed in the loop "For J = 1 To 128 Step 2" in the variables Vl and Vh
09900 65 72 2D 43 6C 6F 73 65 3A 20 00 00 23 00 43 6C
09910 6F 73 65 2C 20 00 FF FF FF FF FF FF FF FF FF FF
09920 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
But in the chip it says from address 09900
09900 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
I try to narrow down the error further.
Many greetings |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sat Jan 15, 2022 2:24 pm Post subject: |
|
|
The last page is not being written to uC?
E |
|
Back to top |
|
|
roni27
Joined: 19 Feb 2017 Posts: 5
|
Posted: Sat Jan 15, 2022 6:00 pm Post subject: |
|
|
Yes, the last 128Byte are missing in the chip.
Now I have changed the boot program like this,
If Wcnt > 0 Then
if the variable Page from the subprogram Writepage is odd, I write again a block with 128Byte into the chip.
If Wcnt > 0 Then
While Wcnt < 128
Incr Wcnt
Buf(wcnt) = 255
Wend
Wcnt = 0
Gosub Writepage
Page_ungerade = Page Mod 2
If Page_ungerade = 1 Then
For X = 1 To 128
'Print Hex(buf(x)) ; " ";
Buf(x) = 255
Next X
Gosub Writepage
End If
End If
Now the program size in the chip is correct.
But, depending on the size of the bin file, there are now wrong values in the chip.
This is not yet ok.
If the bin file (with the same BootLoader in the Atmega184) is transferred with the "MCS Bootloader",
the program is always programmed correctly into the chip.
Who has another tip? |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sat Jan 15, 2022 9:52 pm Post subject: |
|
|
Quote: | But, depending on the size of the bin file, there are now wrong values in the chip.
This is not yet ok. |
Sounds still like a problem with the last write to the uC.
E |
|
Back to top |
|
|
roni27
Joined: 19 Feb 2017 Posts: 5
|
Posted: Sat Jan 22, 2022 1:04 pm Post subject: |
|
|
I think I have found the error in the program.
I have made two changes.
1. before the loop in which the bytes are read from the SD-Card, the four commands have to be
must be inserted
Spmcrval = 3 : Gosub Do_spm '000_00011 erase the first page
Spmcrval = 17 : Gosub Do_spm '000_10001 re-enable page
Code: |
If File_len > 0 Then
Debug #usb , "DOS Bootloader"
Open "programm.bin" For Binary As #card
Spmcrval = 3 : Gosub Do_spm '000_00011 erase the first page
Spmcrval = 17 : Gosub Do_spm '000_10001 re-enable page
Do
Get #card , Helpb
|
2. as described in the last post on 15.01.2022 I have described with an odd Page
again a buffer with 128 bytes with &HFF written.
Code: | If Wcnt > 0 Then 'if there was something left in the page
While Wcnt < 128
Incr Wcnt
Buf(wcnt) = 255
Wend
Wcnt = 0
Gosub Writepage
Page_ungerade = Page Mod 2 'Anzahl Page auf ungerade pruefen
If Page_ungerade = 1 Then
For X = 1 To 128
Buf(x) = 255
Next X
Gosub Writepage
End If
End If
|
With these changes, upgrading the firmware from the SD card now works for me without any problems.
Enclosed the modified program.
Many greetings |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Tue Jan 25, 2022 2:20 pm Post subject: |
|
|
It seems the first problem would have caused a failure every time.
E |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Mon Oct 17, 2022 8:47 pm Post subject: |
|
|
The problem was still elsewhere. I read and compare flash programmed by USART and from CARD.
In some bytes/file length variations there was lack of last 128 bytes but only in the CARD version offcourse.
I found what happen some time ago but always forget to share this solution.
The solution can be found in the original Select Case Bstatus.
MCS Bootloader finalizing transmission by sending "4". Then page that was written must be "saved":
Code: | If Wrd > 0 Then 'if there was something left in the page
Spmcrval = 5 : Gosub Do_spm 'write page
Spmcrval = 17 : Gosub Do_spm ' re-enable page
End If |
Because this must be done in this code twice I made a subroutine from it and name it "Final".
Now ending looks like that and works everytime:
Code: | Close #card
If Wcnt > 0 Then
Do
Incr Wcnt
Buf(wcnt) = &H1A
Loop Until Wcnt = 128
Gosub Writepage
End If
Gosub Final
|
Sorry for inconvienance |
|
Back to top |
|
|
|