[Translate to Englisch:] Bootloader für AVR

[Translate to Englisch:] Um Programme auf einen AVR zu übertragen ist normalerweise ein spezieller Programmer (ISP) nötig. Wenn jedoch auf dem AVR ein kleines Programm existiert das Daten über die serielle Schnittstelle entgegen nehmen kann, geht es auch ohne ISP.
Ein solches Programm nennt sich Bootloader und braucht auf dem PC ein passendes Gegenstück. Der Teil auf dem AVR ist als Quellcode bei MCS-Elektronik kostenlos erhältlich, es muß lediglich die Quarzfrequenz und die Baudrate kontrolliert werden.
Für den PC-Teil habe ich einen Code mit Sharpdevelop in VB.NET geschrieben. Damit können BIN-Dateien über ein serielles Kabel an den AVR übertragen werden.

Für die Unterstützung ein besonderer Dank an Burkhard Venus, mit dessen Hilfe der Code entstanden ist.

 

[Translate to Englisch:] AVR-Teil

[Translate to Englisch:] Hier der nahezu unveränderte Bootloader der einmalig auf den AVR mit einem ISP aufgespielt werden muß

(Kopie 1)

'----------------------------------------------------------------
'                          (c) 1995-2009, MCS
'                        Bootloader.bas
'  This sample demonstrates how you can write your own bootloader
'  in BASCOM BASIC
'  VERSION 2 of the BOOTLOADER. The waiting for the NAK is stretched
'  further a bug was resolved for the M64/M128 that have a big page size
'-----------------------------------------------------------------
'This sample will be extended to support other chips with bootloader
'The loader is supported from the IDE

$crystal = 16000000
$baud = 57600                                               'this loader uses serial com
$regfile = "m8def.dat"
Const Loaderchip = 8
#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
Const Maxword =(2 ^ Maxwordbit) * 2                         '128
Const Maxwordshift = Maxwordbit + 1

Dim Fwversion As Byte
Readeeprom Fwversion , 16

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
Dim Temp As Byte

Disable Interrupts                                          'we do not use ints
Echo Off
'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

$timeout = 200000                                           '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:

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

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

Goto _reset                                                 'goto the normal reset vector at address 0

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

  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
  Bstarted = 0                                              ' we were not started yet
  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
             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
             Print Chr(ack);                                ' send ack and ready
             Waitms 20
             Goto _reset                                    ' start new program

       Case 96:
             Print Chr(fwversion);
             Waitms 20
             Goto _reset
       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
      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
          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
 Toggle Portb.2 : Waitms 10 : Toggle Portb.2                'indication that we write
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

[Translate to Englisch:] Der PC-Teil in VB.NET

[Translate to Englisch:] Dieser Code lädt eine vorhandene BIN-Datei auf den AVR über eine serielle Verbindung.

(Kopie 2)

Dim statusbyte As Integer
        Dim i As Integer
        Dim j As Integer
        dim k as integer
        Dim checkSum as Int32
        Dim BlockNr As Byte
        dim Blocks as byte
        Dim status As Integer
        dim FilePath as string
        dim ende as byte
        Dim oFile As System.IO.FileInfo
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            filepath = openfiledialog1.filename
        Else
            exit sub
        End If
        oFile = New System.IO.FileInfo(FilePath)
        dim bytes() as byte
        Dim oFileStream As System.IO.FileStream = oFile.OpenRead()
        Dim lBytes As Long = oFileStream.Length
        If (lBytes = 0) Then exit sub
        Dim fileData(lBytes) As Byte
        dim byteoffset as integer
        button1.Enabled = False
        oFileStream.Read(fileData, 0, lBytes)
        oFileStream.Close()
        blocks = lbytes \ 128
        If lbytes Mod 128 > 0 Then blocks = blocks + 1
        progressbar1.Maximum = blocks
        lblStatus.text = "flash..."
            try
                serialport2.portname = "COM" & strPort
                serialport2.Open
                blocknr = 1
                for i=1 to 80
                    Do
                        j = j + 1
                        serialport2.basestream.WriteByte(123)
                        System.Threading.Thread.Sleep(10)
                        If j > 30 Then
                            continue for
                        End If
                    loop until serialport2.BytesToRead > 0
                    serialport2.basestream.WriteByte(0)
                    Do
                        j = j + 1
                        System.Threading.Thread.Sleep(10)
                        If j > 30 Then
                            continue for
                        End If
                    loop until serialport2.BytesToRead > 0
                    statusbyte = serialport2.ReadByte
                    If statusbyte = 123 Then
                               do
                                    statusbyte = serialport2.ReadByte
                                    If statusbyte <> 21 Then
                                        msgbox("Falscher Status! " & statusbyte)
                                        Exit Do
                                    End If
                                    checksum = 1
                                    serialport2.basestream.WriteByte(1) 'normal Data Block
                                    serialport2.basestream.WriteByte(cByte(BlockNr))
                                    checksum = checksum + BlockNr
                                    serialport2.basestream.WriteByte(cByte(blocknr))
                                    checksum = checksum + BlockNr
                                    if (byteoffset + 128) < lbytes then
                                        serialport2.basestream.Write(filedata,byteoffset,128)
                                        For j = byteoffset To byteoffset + 127
                                            checksum = checksum + filedata(j)
                                        Next j
                                    Else
                                        serialport2.basestream.Write(filedata,byteoffset,lbytes - byteoffset)
                                        status = 0
                                        For j = byteoffset To lbytes - 1
                                            checksum = checksum + filedata(j)
                                        Next j
                                        For k = 1 To (128 - (lbytes - byteoffset))
                                            serialport2.basestream.WriteByte(0)
                                        Next
                                        ende = 1
                                    end if
                                    progressbar1.Value = BlockNr
                                    bytes = BitConverter.GetBytes (checksum)
                                    serialport2.basestream.WriteByte(bytes(0))
                                    Do
                                    loop until serialport2.BytesToRead > 0
                                    statusbyte = serialport2.ReadByte
                                    If statusbyte = 6 Then
                                        byteoffset = byteoffset + 128
                                        blocknr = blocknr + 1
                                    Else
                                        exit do
                                    end if
                                    if ende = 1 then exit do
                                Loop
                                statusbyte = serialport2.ReadByte
                                serialport2.basestream.WriteByte(4)
                                statusbyte = serialport2.ReadByte
                            end if
                        if ende = 1 then exit for
                    System.Threading.Thread.Sleep(200)
                Next
                If statusbyte = 6 Then
                    lblStatus.Text = "success"
                Else
                    lblStatus.Text = "failed"
                End If
                progressbar1.Value = 0
                serialport2.close
            catch fehler As Exception
                msgbox("This serial Port is used by another Program!")
            End Try
        ofile = Nothing
        oFileStream = Nothing
        application.DoEvents
        button1.Enabled = true