Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

SD card/USART bootloader

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 773

poland.gif
PostPosted: Tue Nov 24, 2020 4:16 am    Post subject: SD card/USART bootloader Reply with quote

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 Very Happy So...if everything working then your Arduino orange LED should Blink Very Happy

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
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5385
Location: Holland

blank.gif
PostPosted: Tue Nov 24, 2020 9:51 am    Post subject: Reply with quote

thanks for sharing EDC Very Happy .

A dual boot loader is indeed very convenient. And with the additional Ethernet everything is covered.

_________________
Mark
Back to top
View user's profile Visit poster's website
roni27

Bascom Member



Joined: 19 Feb 2017
Posts: 5

germany.gif
PostPosted: Thu Jan 13, 2022 10:27 am    Post subject: Reply with quote

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
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5385
Location: Holland

blank.gif
PostPosted: Thu Jan 13, 2022 10:44 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
roni27

Bascom Member



Joined: 19 Feb 2017
Posts: 5

germany.gif
PostPosted: Thu Jan 13, 2022 12:39 pm    Post subject: Reply with quote

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
View user's profile
roni27

Bascom Member



Joined: 19 Feb 2017
Posts: 5

germany.gif
PostPosted: Fri Jan 14, 2022 10:41 am    Post subject: Reply with quote

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
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 499

PostPosted: Sat Jan 15, 2022 2:24 pm    Post subject: Reply with quote

The last page is not being written to uC?

E
Back to top
View user's profile
roni27

Bascom Member



Joined: 19 Feb 2017
Posts: 5

germany.gif
PostPosted: Sat Jan 15, 2022 6:00 pm    Post subject: Reply with quote

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. Exclamation

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? Question
Back to top
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 499

PostPosted: Sat Jan 15, 2022 9:52 pm    Post subject: Reply with quote

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
View user's profile
roni27

Bascom Member



Joined: 19 Feb 2017
Posts: 5

germany.gif
PostPosted: Sat Jan 22, 2022 1:04 pm    Post subject: Reply with quote

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
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 499

PostPosted: Tue Jan 25, 2022 2:20 pm    Post subject: Reply with quote

It seems the first problem would have caused a failure every time.

E
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum