$LOADER

Top  Previous  Next

Action

Instruct the compiler to create a boot loader at the specified address.

 

 

Syntax

$LOADER = address

 

Remarks

address

The address where the boot loader is located. You can find this address in the data sheet.

 

Most AVR chips have a so called boot section. Normally a chip will start at address 0 when it resets. This is also called the reset vector.

Chips that have a boot section, split the flash memory in two parts. The boot section is a small part of the normal flash and by setting a fuse bit you select that the chip runs code at the boot sector when it resets instead of the normal reset vector.

Some chips also have fuse bits to select the size of the boot loader.

 

The MCS boot loader sample is a serial boot loader that uses the serial port. It uses the X-modem checksum protocol to receive the data. Most terminal emulators can send X-modem checksum.

 

The sample is written so it supports all chips with a boot section. You need to do the following :

identify the $regfile directive for your chip
un-remark the line and the line with the CONST that is used for conditional compilation
remark all other $regfile lines and CONST lines.
compile the file
program the chip
set the fuse bit so reset is pointed to the boot loader
set the fuse bit so the boot size is 1024 words
select the MCS Boot loader programmer.

 

The boot loader is written to work at a baud rate of 57600. This works for most chips that use the internal oscillator. But it is best to check it first with a simple program. When you use a crystal you might even use a higher speed.

Do not forget that the MCS boot loader must be set to the same baud rate as the boot loader program.

 

Now make a new test program and compile it. Press F4 to start the MCS boot loader. You now need to reset the chip so that it will start the boot loader section. The boot loader will send a byte with value of 123 and the Bascom boot loader receives this and thus starts the loader process.

 

There will be a stand alone boot loader available too. And the sample will be extended to support other AVR chips with boot section too.

notice There is a $BOOT directive too. It is advised to use $LOADER as it allows you to write the boot loader in BASIC.

 

importantYou can not use interrupts in your boot loader program as the interrupts will point to the reset vector which is located in the lower section of the flash. When you start to writing pages, you overwrite this part.

 

 

See also

$BOOT , $LOADERSIZE

 

 

 

Example

'----------------------------------------------------------------

'                          (c) 1995-2005, MCS

'                        Bootloader.bas

'  This sample demonstrates how you can write your own bootloader

'  in BASCOM BASIC

'-----------------------------------------------------------------

'This sample will be extended to support other chips with bootloader

'The loader is supported from the IDE

 

'$regfile = "m88def.dat"

'Const Loader = 88

 

'$regfile = "m32def.dat"

'Const Loaderchip = 32

 

 

'$regfile = "m88def.dat"

'Const Loaderchip = 88

 

$regfile = "m162def.dat"

Const Loaderchip = 162

 

 

#if Loaderchip = 88                                         'Mega88

  $loader = $c00                                         'this address you can find in the datasheet

  'the loader address is the same as the boot vector address

  Const Maxwordbit = 5

  Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

#endif

#if Loaderchip = 32                                         ' Mega32

  $loader = $3c00                                         ' 1024 words

  Const Maxwordbit = 6                                   'Z6 is maximum bit                                   '

  Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

#endif

#if Loaderchip = 8                                         ' Mega8

  $loader = $c00                                         ' 1024 words

  Const Maxwordbit = 5                                   'Z5 is maximum bit                                   '

  Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

#endif

#if Loaderchip = 161                                       ' Mega161

  $loader = $1e00                                         ' 1024 words

  Const Maxwordbit = 6                                   'Z5 is maximum bit                                   '

#endif

#if Loaderchip = 162                                       ' Mega162

  $loader = $1c00                                         ' 1024 words

  Const Maxwordbit = 6                                   'Z5 is maximum bit                                   '

  Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

#endif

 

 

 

Const Maxword =(2 ^ Maxwordbit) * 2                         '128

Const Maxwordshift = Maxwordbit + 1

 

 

$crystal = 8000000

'$crystal = 14745600

$baud = 57600                                               '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

 

'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 Word                                             'this is the Z pointer word

Dim Vl As Byte , Vh As Byte                               ' these bytes are used for the data values

Dim Wrd As Byte , Page As Byte                             'these vars contain the page and word address

'Mega 88 : 32 words, 128 pages

 

 

 

Disable Interrupts                                         'we do not use ints

 

 

Waitms 1000                                                 'wait 1 sec

'We start with receiving a file. The PC must send this binary file

 

'some constants used in serial com

Const Nak = &H15

Const Ack = &H06

Const Can = &H18

 

'we use some leds as indication in this sample , you might want to remove it

Config Portb = Output

Portb = 255                                                 'the stk200 has inverted logic for the leds

 

'$timeout = 1000000                                          'we use a timeout

$timeout = 1000000                                         'we use a timeout

 

'Do

Bstatus = Waitkey()                                     'wait for the loader to send a byte

Print Chr(bstatus);

If Bstatus = 123 Then                                   'did we received value 123 ?

  Goto Loader

End If

'Loop

 

For J = 1 To 10                                             'this is a simple indication that we start the normal reset vector

Toggle Portb : Waitms 100

Next

 

Goto _reset                                                 'goto the normal reset vector at address 0

 

 

'this is the loader routine. It is a Xmodem-checksum reception routine

Loader:

For J = 1 To 3                                             'this is a simple indication that we start the normal reset vector

Toggle Portb : Waitms 500

Next

 

Spmcrval = 3 : Gosub Do_spm                                 ' erase  the first page

Spmcrval = 17 : Gosub Do_spm                               ' re-enable page

 

 

Bretries = 10                                               'number of retries

Do

Csum = 0                                                 'checksum is 0 when we start

Print Chr(nak);                                         ' 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

          Incr Bblocklocal                               'increase local block count

          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(ack);                         'acknowledge

            Else                                       'no match so send nak

                Print Chr(nak);

            End If

          Else

            Print Chr(nak);                             'blocks do not match

          End If

    Case 4:                                             ' end of transmission , file is transmitted

          Print Chr(ack);                               ' send ack and ready

 

          Portb.3 = 0                                   ' simple indication that we are finished and ok

          Goto _reset                                   ' start new program

    Case &H18:                                         ' PC aborts transmission

          Goto _reset                                   ' ready

 

    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:

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

        Page = Page + 1                                   'next page

        Spmcrval = 3 : Gosub Do_spm                       ' erase  next page

        Spmcrval = 17 : Gosub Do_spm                     ' re-enable page

    End If

Next

Toggle Portb.2 : Waitms 10 : Toggle Portb.2             'indication that we write

Return

 

 

Do_spm:

Bitwait Spmcsr.selfprgen , Reset                         ' check for previous SPM complete

Bitwait Eecr.eepe , 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}

 

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. And we will release a command line loader in the future