Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Bootloader for Atmega2560 based Arduino clone

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO
View previous topic :: View next topic  
Author Message
Farmer

Bascom Member



Joined: 05 Jun 2011
Posts: 10

PostPosted: Sun Dec 11, 2016 9:04 pm    Post subject: Bootloader for Atmega2560 based Arduino clone Reply with quote

Hi all,
maybe someone can help me flashing a new bootloader onto my Atmega 2560 based china clone. I used the board before and detelted the bootloader (because i dont needed it) Now i want to go back to a bootloader, preferred a version which works with Bascom. I tried to reflash the Arduino one but dont know if i got success...it does not answer in Bascom. The ch340 chip is not the problem, maybe a fuse setting on the avr?
Maybe someone has a manual for the 2560 settings.

Thanks,

Toby
Back to top
View user's profile
Farmer

Bascom Member



Joined: 05 Jun 2011
Posts: 10

PostPosted: Sun Dec 18, 2016 10:53 pm    Post subject: Reply with quote

Hi,
finally i was able to reinstall the original Arduino Bootloader using my USB programmer and Arduino Software. Works now with Bascom, but if possible i want to change over to a Bascom based Bootloader in the future. Does someone has a working bootloder.bas file for the 2560?
And another thing, i cant find a Schematic for the Mega 2560 clone with the CH340g usb driver onboard. Seems impossible to find.

Tobias
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1518
Location: Basel, Switzerland

switzerland.gif
PostPosted: Mon Dec 19, 2016 7:05 am    Post subject: Reply with quote

Hi,

Have a look at bootloader.bas in the samples Directory.

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
Farmer

Bascom Member



Joined: 05 Jun 2011
Posts: 10

PostPosted: Mon Dec 19, 2016 10:11 pm    Post subject: Reply with quote

I found the bootloader.bas before but dont get it work. I think the probem is related to the fuses. Maybe i am wrong with my settings.
Are these the right settings:
boot size should be 1024 words
Bootsz1 = 1
Bootsz0 = 0

And should look in Bascom like this:?


If the settings are right i might have try to program with an AVR Dragon instead of an ceapo USBASP

Tobias
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1518
Location: Basel, Switzerland

switzerland.gif
PostPosted: Mon Dec 19, 2016 10:27 pm    Post subject: Reply with quote

Hi,

1K words for the bootloader Looks correct. Just the address in the text description Looks wrong (FC00 compared to 1FC00)

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
Farmer

Bascom Member



Joined: 05 Jun 2011
Posts: 10

PostPosted: Fri Dec 23, 2016 10:50 pm    Post subject: Reply with quote

Its strange, i changed the bootloader.bas to 2560 and the vector is set to 1fc00 in the file
But when i hit compile i dont get a .hex file in my folder. Is this the usual result? I cant use the dragon to burn with Bascom so i need a .hex file

Tobias
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1518
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sat Dec 24, 2016 9:54 am    Post subject: Reply with quote

Hi,

You Need to set the Option under Options, Compiler, Output, HEX File. The HEX file will land in the same Directory as the bas file.


Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
Farmer

Bascom Member



Joined: 05 Jun 2011
Posts: 10

PostPosted: Wed Dec 28, 2016 8:02 pm    Post subject: Reply with quote

The Option is set but i dont see the message compiling and no file is created in the directory where i kopied my bootloader.bas file into.
My new programmer arrived and using this i can now write the Mega2560 without errors over Bascom. But i still like to get the Bootloader running.

Tobias
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1518
Location: Basel, Switzerland

switzerland.gif
PostPosted: Wed Dec 28, 2016 8:16 pm    Post subject: Reply with quote

Hi,

So you have a new programmer that works from within Bascom. Why not use this programmer to write the bootloader?

I've also seen that Bascom doesn't display the Compiling message box and doesn't create any files. Usually rebooting the PC is enough to solve the problem.

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4722
Location: Holland

blank.gif
PostPosted: Wed Dec 28, 2016 8:46 pm    Post subject: Reply with quote

- check if the folder is writable. when it is protected no files can be created.
- check if you actual run the full version. only the full version can compile the bootloader for the m2560

here is a sample for the m2560 that can also do rs485
Code:

'----------------------------------------------------------------
'                      (c) 1995-2015, MCS
'                        Bootloader.bas
'  This sample demonstrates how you can write your own bootloader
'  in BASCOM BASIC
'-----------------------------------------------------------------
$crystal = 16000000
$baud = 115200                                              'this loader uses serial com
'It is VERY IMPORTANT that the baud rate matches the one of the boot loader
'do not try to use buffered com as we can not use interrupts

'possible return codes of the PC bootloader.exe
' -6005    Cancel requested
' -6006    Fatal time out
' -6007    Unrecoverable event during protocol
' -6008    Too many errors during protocol
' -6009    Block sequence error in Xmodem
' -6016    Session aborted

$hwstack = 40
$swstack = 40
$framesize = 40

$regfile = "m2560def.dat"
Const Loaderchip = 2560
Const Crs485 = 1                                            'constant that defines that we use RS485 mode

#if Loaderchip = 2560                                       ' Mega2560
   $loader = &H1FC00                                       ' 1024 words
   Const Maxwordbit = 7                                     'Z7 is maximum bit                                   '
   Config Com1 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
   #if Crs485 = 1
      Config Print0 = Portg.5 , Mode = Set                    'RS485
      Config Portg.5 = Output
   #endif
#endif

Const Maxword =(2 ^ Maxwordbit) * 2                         '128
Const Maxwordshift = Maxwordbit + 1
Const Cdebug = 0                                            ' leave this to 0

'Dim the used variables
Dim Bstatus As Byte , Bretries As Byte , Bblock As Byte , Bblocklocal As Byte
Dim Bcsum1 As Byte , Bcsum2 As Byte , Buf(128) As Byte , Csum As Byte
Dim J As Byte , Spmcrval As Byte                            ' self program command byte value

Dim Z As Long                                               'this is the Z pointer word
Dim Vl As Byte , Vh As Byte                                 ' these bytes are used for the data values
Dim Wrd As Word , Page As Word                              'these vars contain the page and word address
Dim Bkind As Byte , Bstarted As Byte

Disable Interrupts                                          'we do not use ints

'some constants used in serial com
Const Cnak = &H15
Const Cack = &H06
Const Ccan = &H18


$timeout = 400000                                           'we use a timeout
'When you get LOADER errors during the upload, increase the timeout value
'for example at 16 Mhz, use 200000

Bretries = 5                                                'we try 5 times
Testfor123:
   #if Cdebug
      Print "Try " ; Bretries
      Print "Wait"
   #endif
   Bstatus = Waitkey()                                         'wait for the loader to send a byte
   #if Cdebug
      Print "Got "
   #endif

   Print Chr(bstatus);

   If Bstatus = 123 Then                                       'did we received value 123 ?
      Bkind = 0                                                'normal flash loader
      Goto Loader
   Elseif Bstatus = 124 Then                                   ' EEPROM
      Bkind = 1                                                ' EEPROM loader
      Goto Loader
   Elseif Bstatus <> 0 Then
      Decr Bretries
      If Bretries <> 0 Then Goto Testfor123                    'we test again
   End If


   #if Cdebug
      Print "RESET"
   #endif
   Goto _reset                                                 'goto the normal reset vector at address 0


'this is the loader routine. It is a Xmodem-checksum reception routine
Loader:
   #if Cdebug
      Print "Clear buffer"
   #endif
   Do
      Bstatus = Waitkey()
   Loop Until Bstatus = 0


   If Bkind = 0 Then
      Spmcrval = 3 : Gosub Do_spm                            ' erase  the first page
      Spmcrval = 17 : Gosub Do_spm                           ' re-enable page
   End If

   Bretries = 10                                            'number of retries

   Do
      Bblocklocal = 1
      Bstarted = 0                                              ' we were not started yet
      Csum = 0                                                  'checksum is 0 when we start
      Print Chr(cnak);                                      ' firt time send a nack
      Do

         Bstatus = Waitkey()                                     'wait for statuse byte

         Select Case Bstatus
            Case 1:                                              ' start of heading, PC is ready to send
               Csum = 1                                        'checksum is 1
               Bblock = Waitkey() : Csum = Csum + Bblock       'get block
               Bcsum1 = Waitkey() : Csum = Csum + Bcsum1    'get checksum first byte
               For J = 1 To 128                                'get 128 bytes
                  Buf(j) = Waitkey() : Csum = Csum + Buf(j)
               Next
               Bcsum2 = Waitkey()                              'get second checksum byte
               If Bblocklocal = Bblock Then                    'are the blocks the same?
                  If Bcsum2 = Csum Then                        'is the checksum the same?
                     Gosub Writepage                           'yes go write the page
                     Print Chr(cack);                       'acknowledge
                     Incr Bblocklocal                       'increase local block count
                  Else                                      'no match so send nak
                     Print Chr(cnak);
                  End If
               Else
                  Print Chr(cnak);                          'blocks do not match
               End If
            Case 4:                                              ' end of transmission , file is transmitted
               If Wrd > 0 And Bkind = 0 Then                  'if there was something left in the page
                  Wrd = 0                                    'Z pointer needs wrd to be 0
                  Spmcrval = 5 : Gosub Do_spm                'write page
                  Spmcrval = 17 : Gosub Do_spm               ' re-enable page
               End If
            '  Waitms 100                                   ' OPTIONAL REMARK THIS IF THE DTR SIGNAL ARRIVES TO EARLY
               Print Chr(cack);                             ' send ack and ready

               Waitms 20
               Goto _reset                                    ' start new program
            Case &H18:                                           ' PC aborts transmission
               Goto _reset                                    ' ready
            Case 123 : Exit Do                                   'was probably still in the buffer
            Case 124 : Exit Do
            Case Else
               Exit Do                                           ' no valid data
         End Select
      Loop
      If Bretries > 0 Then                                      'attempte left?
         Waitms 1000
         Decr Bretries                                          'decrease attempts
      Else
         Goto _reset                                            'reset chip
      End If
   Loop



'write one or more pages
Writepage:
   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
         R0 = Vl                                            'store them into r0 and r1 registers
         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}

   #if _romsize > 65536
   !   lds r24,{Z+2}
   !   sts rampz,r24                                        ' we need to set rampz also for the M128
   #endif

   Spmcsr = Spmcrval                                         'assign register
   !spm                                                     'this is an asm instruction
   !nop
   !nop
Return


'How you need to use this program:
'1- compile this program
'2- program into chip with sample elctronics programmer
'3- select MCS Bootloader from programmers
'4- compile a new program for example M88.bas
'5- press F4 and reset your micro
' the program will now be uploaded into the chip with Xmodem Checksum
' you can write your own loader.too
'A stand alone command line loader is also available


'How to call the bootloader from your program without a reset ???
'Do
'   Print "test"
'   Waitms 1000
'   If Inkey() = 27 Then
'      Print "boot"
'      Goto &H1C00
'   End If
'Loop

'The GOTO will do the work, you need to specify the correct bootloader address
'this is the same as the $LOADER statement.
 

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

Bascom Member



Joined: 05 Jun 2011
Posts: 10

PostPosted: Wed Dec 28, 2016 10:04 pm    Post subject: Reply with quote

Thanks for the testfile, got it working before i read your posting.
I'm sure you want to know what was wrong. I cant believe it that something happens to me, i am telling everybody: Always check the sample files for missing stack values. Add them to the sample and everything works fine. Very Happy
Wasn't there an clear error when stack values are missing in the Program?
The old programmer had a problem with AVRs >128k so i needed a new one anyway. The AVR-Dragon is to expensive for everyday usage.

Tobias
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4722
Location: Holland

blank.gif
PostPosted: Wed Dec 28, 2016 10:16 pm    Post subject: Reply with quote

you probably use 2078 ? it was a known problem.
_________________
Mark
Back to top
View user's profile Visit poster's website
Farmer

Bascom Member



Joined: 05 Jun 2011
Posts: 10

PostPosted: Wed Dec 28, 2016 10:27 pm    Post subject: Reply with quote

...Not known for me until now. Sad
After the huge learning effect for me it seems to be update-time. Will the update be availible via update-wiz in January? Much easier for me than downloading manually and then upgrading.

Tobias
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO 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