Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Problem Including Bootloader Code in Application Program
Goto page Previous  1, 2
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 412

newzealand.gif
PostPosted: Thu Dec 12, 2019 6:55 am    Post subject: Reply with quote

Hello Andre,
I'm sorry I didnt get back to youn sooner - i missed seeing the email notification.
Yes the bootloader does work and attaching the two lines of code as shown does work, and does allow you to program a chip with the bootloader AND the main code all in one hit.
It just doesn't allow this be be done OTA, for the reasons pointed out by MWS.

_________________
Neil
Back to top
View user's profile
andro

Bascom Member



Joined: 12 Mar 2007
Posts: 70
Location: Ljubljana

slovenia.gif
PostPosted: Thu Dec 12, 2019 7:14 am    Post subject: Reply with quote

Hello

Thanks for the answer. Did You try it also with Xmega ?

BR
andrej
Back to top
View user's profile
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 412

newzealand.gif
PostPosted: Thu Dec 12, 2019 8:37 pm    Post subject: Reply with quote

No only atmega
_________________
Neil
Back to top
View user's profile
andro

Bascom Member



Joined: 12 Mar 2007
Posts: 70
Location: Ljubljana

slovenia.gif
PostPosted: Thu Dec 12, 2019 10:24 pm    Post subject: Reply with quote

Thanks Neil.

When I'll have time to 'play' with this issue I will make a post. It might be interesting also for others.

BR

Andrej
Back to top
View user's profile
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Sun Jun 14, 2020 7:52 pm    Post subject: Bootloader in the main program Reply with quote

Hello, I spent hours trying to include my bootloader into the main program using the method from this post and still not succeded.

I use Atmega 328p and tryied also 328pb, slghtly modified MCS Bootloder (with OLED display).
Structure of My_bootloder is as follows:

$regfile = "m328pbdef.dat"
$crystal = 14745600
$loader = $3800 '2048 ATMEGA328 this address you can find in the datasheet
$timeout = 300000
$prog &HCC , &HC6 , &HC8 , &HFD 'Vector Bootloader 2048
$lib "glcdSSD1309-SPI.lib"
Config Graphlcd = 128x64sed , Si = Portc.5 , Sclk = Portc.2 , A0 = Portc.1 , Rst = Portc.0
Config Com1 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
'***
' Bootloader body here, no interrupts
'***
$include "Update.font" ' this is a true type font with variable spacing

My main program structure is as follows:

$regfile = "m328pbdef.dat"
$crystal = 14745600
$timeout = 20000
$hwstack = 100
$swstack = 100
$framesize = 100
Config Graphlcd = 128x64sed , Si = Portc.5 , Sclk = Portc.2 , A0 = Portc.1 , Rst = Portc.0
Config Com1 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
'***
' Main program body here, interrupts from Com1 and Timer0
'***
$include "Normal.font" ' this is a true type font with variable spacing


When I program Atmega with My_bootloader using STK500 programmer it works well
When I program Atmega with the main program using STK500 programmer it also works well
When I program Atmega with the main program using My_bootloder over bluetooth it also works well

But, when I try to include in the very end of my main program (after $include "Normal.font"):
$boot = $3800 'bootloader address
$inc Bootloader , Nosize , "My_bootloader.bin" 'bootloade bin

The compiler shows error:
Error : 344 Program will overwrite flash memory (or bootloader) [ 27528 too long]

What I am doing wrong?
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 696

poland.gif
PostPosted: Mon Jun 15, 2020 9:34 am    Post subject: Reply with quote

When you compiling bootloader program Bascom will create BIN file for ENTIRE flash so everything from $00 to $3800 will be filled with zeros. So you get info about 99% flash used and BIN probably have 32KB.

You must use some Hex/Bin editor/software and cut those zeros (for example from some external programmer or HexWork, WinHex etc.)

In the new version of Bascom will be option for "$loader [bootonly]" so Bascom can create only bootloader code without zeros on the begining of flash Wink
Back to top
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Thu Jun 18, 2020 9:58 pm    Post subject: Reply with quote

Thanks EDC, your hints are very heplful, as usual Smile
I downloaded HexWorkshop, its nice and easy.
I wanted to cut off FF data at the begening of my bootloader bin file.
And here I found the first problem.
I programmed ATMEGA328PB with my bootloder alone to be sure that everything is ok. Bootloader works well.
Then I read the ATMEGA328PB flash buffer by STK500 programmer (memory lock fusebists are temporary unprogrammed).
The result you can see on picture1
Then I loaded my bootloader bin file into HexWorkshop. The result you can see on picture2.
The data starts at the same address, but looks direfently.
The question is why?

Back to top
View user's profile
Netzman

Bascom Expert



Joined: 25 Nov 2005
Posts: 113
Location: Graz

austria.gif
PostPosted: Fri Jun 19, 2020 4:29 pm    Post subject: Reply with quote

MSB/LSB of the data words are swapped, check your settings in Options->Compiler->Output.

Using the .hex files to create a combined image is a bit easier, since the addresses are also stored in the files: https://www.avrfreaks.net/forum/combining-hex-files
avr-objcopy is part of the avr-binutils: https://www.microchip.com/mplab/avr-support/avr-and-arm-toolchains-c-compilers and is also part of the Atmel Studio installation.

_________________
LCD Menu | Proportional Fonts
Back to top
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Fri Jun 19, 2020 8:20 pm    Post subject: Reply with quote

Thanks Netzman for the hex word swap explanation.
I prepared my bootloader mini version without FFs at the begining using HexWorksop
I compared this bin file with "normal" bootloader bin, its identical, but without these first FFs
Then I prepared a simple program:

$regfile = "m328pbdef.dat"
$crystal = 14745600
$prog &HFF , &HFF , &HD8 , &HFE 'Atmega328PB: FLASH unlocked, ext XTAL slow setup time, Watchdog soft, Vector adres Botl 2048, Brownout 1,8V

Config Portd.1 = Output 'LED

Do
Toggle Portd.1 'blinking LED
Waitms 100
Loop
End

$boot = $3800 'adres bootloadera (double byte) no FF data at the begening
$inc Bootloader , Nosize , "BootLoader_pcb_15_3Kropki_v1_mini.bin" 'bin bootloadera mini HexWorkshop

Compiler displayed 96% Flash usage (it is ok, like loading my bootloader alone), LED blinks, but there is no bootloader code around the end of Flash memory.
There is the main program short code at the very berining of the Flash, the rest is filled with FFs.
I also tryed the same with older Atmega 328P. The result is the same.
What is still wrong?
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 696

poland.gif
PostPosted: Wed Jun 24, 2020 8:04 pm    Post subject: Reply with quote

I`ve tested it today with Arduino Nano board and it works like it should.
On the begining of the flash I have Blink code and at address $3C00 x 2 = $7800 I have bootloader in the Bin file.
I even test this bootloader with some another time periods for blink and it works too Wink

Code:
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128

Config Portb.5 = Output : Led Alias Portb.5

Do
 Toggle Led
  Waitms 300
Loop

$boot = $3c00
$inc Bootloader , Nosize , "cut_boot_m328p.bin"


Back to top
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Tue Jun 30, 2020 4:21 pm    Post subject: Reply with quote

Motivated by the success of EDC with blinking LED I tried to do the same one more time Wink

My code is nearly the same. The diference migth be 2048 bootloader lengh with Oled driver and some fonts.
After several tests I gave up. Unfortunately it still doesnt include Bootloader code into the Flash memory.
My tests were held with Bascom 2082 and ATMEGA 328PB chip.

I did the same with older Bascom 2079 and older ATMEGA 328P chip.
And tadam, it works! Bootloader code is included in the Flash memory, LED blinks.

Unfortunately Bascom 2079 can not program newer ATMEGA 328PB chip.

EDC, which Bascom version have you used?
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 696

poland.gif
PostPosted: Tue Jun 30, 2020 9:01 pm    Post subject: Reply with quote

Always newest Razz
Please try whole folder compiled on 2082.
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 696

poland.gif
PostPosted: Tue Jun 30, 2020 9:26 pm    Post subject: Reply with quote

This is wierd...why use previous(2078) versions for newest chips?
?


Last edited by EDC on Tue Jun 30, 2020 9:29 pm; edited 1 time in total
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page Previous  1, 2
Page 2 of 2

 
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