Ohne zu Debuggen bleibt der Code < 1 K Word

Debugen=0 dann ohne LCD Anzeigen zwischendurch


Baud=19200 reicht vom Speed und ist sicherer , wird ja nicht jeden Tag bei Kunden upgedatet.
Bei mir geht jede Baudzahl nach oben auch.

Um mir nicht selbst ins Knie zu schießen habe ich die relevanten stellen geschwärtzt. "****"


Code:
' Bootloader für Mega 32


$crystal = 16000000
$baud = 19200                                                                   '38400                                                                   'this loader uses serial com
$regfile = "m32def.dat"

Config Portb = Output
Config Lcd = 20 * 2
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.6 , Rs = Portb.5
Config Lcdbus = 4

Config Pind.0 = Input                                                           'RxD für RS232
Config Pind.1 = Output                                                          'TxD für RS232

Const Debugen = 0                                                               'Zu Testzwecke auf 1 setzen


$loader = $3c00                                                                 '$3800= 2048 word    $3c00= 1024 words


Const Maxwordbit = 6                                                            'Z6 is maximum bit                                   '
Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Const Maxword = 128                                                             'Nur für Mega 32
Const Maxwordshift = Maxwordbit + 1

'Dim the used variables
Dim Blockstatus As Byte
Dim Fehlerversuche As Byte
Dim Blocknummer As Byte
Dim Blocklocalnummer As Byte
Dim Vor_checksumme_vom_pc As Byte
Dim Checksumme_vom_pc As Byte
Dim Buffer(128) As Byte
Dim Checksumme As Byte
Dim Schleife As Byte
Dim Spmcrval As Byte                                                            ' self program command byte value

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




Disable Interrupts                                                              'we do not use ints


'some constants used in serial com
Const Nak = &H15
Const Ack = &H06
Const Can = &H18


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


Waitms 100
Initlcd
Cursor Off Noblink
Cls
Locate 1 , 1
Lcd "Init"
Blocklocalnummer = 0
For Schleife = 0 To 15
  #if Debugen = 0
   Lcd "."
  #endif
  #if Debugen = 1
   Lcd "?"
  #endif

  Blockstatus = Waitkey()                                                       'Wartet ob der PC die Ladesequenz beginnt
  Print Chr(blockstatus);                                                       'Echo zurück
  If Blockstatus = 123 Then                                                     'Wenn Ladesequenz=123 dann wird der normale Flash geladen
                     Ladeziel = 0                                               '
                     Goto Loader
                   End If
  #if Debugen = 1
  If Blockstatus = 124 Then                                                     'Wenn Ladesequenz=124 dann wird ind EEPROM geladen
                     Ladeziel = 1                                               '
                     Goto Loader
                   End If
  #endif
Next Schleife

Goto _reset                                                                     'Wenn zu lange gewartet dann springe ins normale Program
'################################################################################################################################
Loader:
  Locate 1 , 1
  Lcd "Update              "                                                    'die eigendliche Laderoute
  Do
     Blockstatus = Waitkey()
  Loop Until Blockstatus = 0



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


Fehlerversuche = 10                                                             'number of retries
'######################  Einleseschleife ###############################################
Do
  Checksumme = 0                                                                'checksum is 0 when we start
  Print Chr(nak);                                                               ' firt time send a nack
  Do
    Blockstatus = Waitkey()                                                     'wait for statuse byte

    Select Case Blockstatus
       Case 1 To 3:                                                             ' 1=normal 2=Invers  3=Krüpt
            Incr Blocklocalnummer                                               'increase local block count
            Checksumme = 1                                                      'checksum is 1

            Blocknummer = Waitkey()                                             'hole Block Nr die übertragen werden soll .Start ist 1

            Checksumme = Checksumme + Blocknummer                               'get block
            Vor_checksumme_vom_pc = Waitkey()
            Checksumme = Checksumme + Vor_checksumme_vom_pc                     'get checksum first byte
                For Schleife = 1 To 128                                         'get 128 bytes
                  Buffer(schleife) = Waitkey()
                  Checksumme = Checksumme + Buffer(schleife)
                  If Blockstatus = 2 Then Buffer(schleife) = Not Buffer(schleife)
                  If Blockstatus = 3 Then
                                    Kruept = ********
                                    Kruept = Kruept - *****
                                    Buffer(schleife) = Buffer(schleife) ******** Kruept
                                 End If
                Next
            Checksumme_vom_pc = Waitkey()                                       'Warte auf Checksumme vom PC
            #if Debugen = 1
            Locate 1 , 1
            Lcd "Uebertrage Block:"
            Lcd Blocknummer
            Locate 2 , 1
            Lcd "Bstatus :"
            Lcd Blockstatus
            Lcd "   "
            #endif
            If Blocklocalnummer = Blocknummer Then                              'are the blocks the same?
                                                If Checksumme_vom_pc = Checksumme Then       'is the checksum the same?
                                                                                    Gosub Schreibe_seite       '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:                                                                  ' Beendet die Übertragung mit erfolg
             If Wrd > 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);
             #if Debugen = 1                                                    ' send ack and ready
             Locate 2 , 1
             Lcd "Bstatus :"
             Lcd Blockstatus
             Lcd "   "
             Locate 1 , 21
             Lcd "Update Erfolgreich"
             Wait 5
             #endif
             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
' Fehlerübertragungsbehandlung
  If Fehlerversuche > 0 Then                                                    'attempte left?
     Waitms 1000
     Decr Fehlerversuche                                                        'decrease attempts
  Else
     Goto _reset                                                                'reset chip
  End If
Loop
'####################### Ende Einleseschleife #############################
Schreibe_seite:                                                                 'Beschreibt eine oder mehr seiten
 If Ladeziel = 0 Then
                   For Schleife = 1 To 128 Step 2                               'we write 2 bytes into a page
                     Vl = Buffer(schleife)
                     Vh = Buffer(schleife + 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

                 #if Debugen = 1
                 Else                                                           'eeprom
                  For Schleife = 1 To 128
                    Writeeeprom Buffer(schleife) , Wrd
                    Wrd = Wrd + Schleife
                  Next
                #endif
                End If

Return
'###############################################################################
Do_spm:
  Bitwait Spmcsr.0 , Reset                                                      ' check for previous SPM complete
  Bitwait Eecr.1 , Reset                                                        'wait for eeprom

  Zeiger = Page                                                                 'make equal to page
  Shift Zeiger , Left , Maxwordshift                                            'shift to proper place
  Zeiger = Zeiger + Wrd                                                         'add word
  lds r30,{Zeiger}
  lds r31,{Zeiger+1}


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

Gruß Gento