Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Where does the bootloader erase EEPROM

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

Bascom Member



Joined: 02 Jan 2007
Posts: 68

blank.gif
PostPosted: Tue Jan 24, 2023 8:14 am    Post subject: Where does the bootloader erase EEPROM Reply with quote

I have the bootloader working but it is erasing the EEPROM.
to my knowledge I am not sending EEPROM information.
I want to just send the program so that calibration settings can remain in EEPROM.

There are bits of the bootloader that I don't understand for example, where and how does it erase the EEPROM?

Thanks,


(BASCOM-AVR version : 2.0.8.5 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2105

blank.gif
PostPosted: Tue Jan 24, 2023 8:56 am    Post subject: Re: Where does the bootloader erase EEPROM Reply with quote

syndetic1 wrote:
I have the bootloader working but it is erasing the EEPROM.

Ah, yes, THE bootloader.

Generally: Did you check about the EESAVE fuse bit?

In detail, supply information about:
- type of MCU
- make/type of loader, Bascom-one? If yes, attach the Bascom source you've used.
- software interface, Bascom GUI or other?
Back to top
View user's profile
syndetic1

Bascom Member



Joined: 02 Jan 2007
Posts: 68

blank.gif
PostPosted: Wed Jan 25, 2023 2:06 am    Post subject: Where does the bootloader erase EEPROM Reply with quote

Thank you for your reply.
I am using a Mega128

I am programming from within BASCOM and also I bought the Bascom stand alone uploader.
Both behave the same way.

The boot loader is essentially the Bascom one. I have taken out lines pertaining to other CPUs. I have added in a delay that jumps to the users program if the uploader does not start to send new code. Listing below.

I don't understand which line actually erases the EEPROM.

In response to the earlier response,
Fuse Byte High controls the boot loader. I had it set as follows:
1101 1000 = Hex D8
Bit 0 BOOTRST = 0 means the micro boots into the boot loader.
Bit 3 -> EESAVE 1=EEPROM Erased with chip erase and 0 means dont erase with chip erase.
As I am programming using the boot loader, I am not using an external programmer to issue a chip erase but obviously the boot loader can erase the Flash and the EEPROM.
I was not sure whether Fuse Byte High bit 3 is involved with the boot-loader memory erase so I change bit 3 to zero.
Fuse Byte High is now D0
Then I set some EEPROM locations to non zero values and reload my firmware using the boot loader.
Upon checking, I see that the EEPROM is all zero again.
I do not think EESAVE affects in system programming.




Code:


'----------------------------------------------------------------
'                          (c) 1995-2020, 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
'-----------------------------------------------------------------
'V1-3  3-OCT-2022 Frank Thomson
'  Added a timeout of 10 seeconds after which it jumps to the user program

'
'-------------------------------------------------------------------------------
'|   7    |    6   |    5   |    4   |    3   |    2   |    1   |    0   |
'--------------------------------------------------------------------------------
'| SPMIE  | RWWSB  | SIGRD  | RWWSRE | BLBSET | PGERS  | PGERS  | SPMEN  |
'                                                                   |
' This bit enables the SPM instruction for the next four clock cycles.
' check for previous SPM complete
$hwstack = 40
$swstack = 40
$framesize = 40
$crystal = 8000000
$baud = 9600                                               '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

$regfile = "m128def.dat"
Const Loaderchip = 128


' Mega128
    $loader = &HF000                                        ' 1024 words
    Const Maxwordbit = 7                                    'Z7 is maximum bit                                   '
    Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0


'MAXWORDBIT value, how to determine it?
'Maxwordbit should have the same value as the maximum Z bit.
'For Example , For The M88 the PDF Shows Pcword : Pc[4 : 0] And Z Value Z5 : Z1.
' In That Case The Maximum Bit is Z5 , Thus Bit 5 , Meaning Maxwordbit Is 5.

Const Maxword =(2 ^ Maxwordbit) * 2                         '128
Const Maxwordshift = Maxwordbit + 1



'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
Dim ExitDelayCounter as Long
ExitDelayCounter = 500000

Disable Interrupts                                          'we do not use ints

'Waitms 100                                                  'wait 100 msec 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
$timeout = 400000  ' Used by the waitkey statement that we are trying to get rid of
'we use some leds as indication in this sample , you might want to remove it
'Config Pinb.2 = Output
'Portb.2 = 1                                                 'the stk200 has inverted logic for the leds
'Config Pinb.3 = Output
'Portb.3 = 1



Print "M128 Boot Loader V1.3"
Bretries = 5                                                'we try 5 times
Testfor123:
Do                                                                                                        ' As at Ver 1-3 3-10-2022 Replaced Waitkey with Inkey
  if ExitDelayCounter > 0 Then
     Decr ExitDelayCounter
  else
     ' Hit zero - Goto User Program
     Print "LOADER Timeout - Jumping to User Program"
     Goto _reset
  End if
  Bstatus = Inkey()
  If Bstatus = 123 Then                             'did we received value 123  "{" ?
     If Bretries > 0 Then
        Decr Bretries
     Else
        Print Chr(bstatus);
        Goto Loader                                 ' yes so run bootloader
     End If
  Elseif Bstatus = 27 Then            '<ESC>
          Print "Jumping to User Program at 0"
          Goto _reset                               'goto the normal reset vector at address 0
     'End If
  Elseif Bstatus = 13 Then           ' <cr>
     Print "LOADER Press ESC to Exit"
  End If
Loop




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

  Do
     Bstatus = Waitkey()
  Loop Until Bstatus = 0

  ' Frank: Not usig Portb.2 LED
  'For J = 1 To 3                                            'this is a simple indication that we start the normal reset vector
  '   Toggle Portb.2 : Waitms 50
  'Next

  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(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
            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
                  Incr Bblocklocal                          'increase local block count
               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
            '  Waitms 100                                   ' OPTIONAL REMARK THIS IF THE DTR SIGNAL ARRIVES TO EARLY
             Print Chr(ack);                                ' send ack and ready

             Portb.3 = 0                                    ' simple indication that we are finished and ok
             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
 'Toggle Portb.2 : Waitms 10 : Toggle Portb.2                'Frnk: Deleted.  indication that we write
Return

'   The SPMEN bit will auto-clear upon completion of an SPM instruction
'   Bitwait  address until 0
Do_spm:
'                                                             Wait until Spmcsr.0 is zero
  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.
 


[/code]
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2105

blank.gif
PostPosted: Wed Jan 25, 2023 3:41 pm    Post subject: Reply with quote

I don't see an chip erase either, but I would not exclude such idea, especially if it does not cost me anything more than attaching an ISP and flipping EESAVE to 0.

You did use the original loader, removed some bones while leaving others in:
Code:
 Else                                                       'eeprom
     For J = 1 To 128
       Writeeeprom Buf(j) , Wrd
       Wrd = Wrd + 1
     Next
 End If

This code normally writes the EEProm.

I would remove it or at least put a print for debug info in it, to watch if it is accessed nonetheless.
In case it is, this hints to something seriously going wrong.

Did you think about the possibility the Bascom interface sends a 124 plus EEProm data?
With the original loader no problem, as it knows how to handle it.

Your code however ignores the 124, but would compare following EEProm data against 123, this would jump to the loader-label.
Assume the sent EEProm data further contains a following zero, then it makes it already to line 115 of your code.
From there it's difficult to imagine what's happening, as things ran out of sync.

To solve this potential trap, I would leave in all the 124-related code and only remove/comment:
Code:
Writeeeprom Buf(j) , Wrd
Back to top
View user's profile
syndetic1

Bascom Member



Joined: 02 Jan 2007
Posts: 68

blank.gif
PostPosted: Tue Jan 31, 2023 11:03 pm    Post subject: Reply with quote

Thanks MWS,
Commented out Writeeeprom Buf(j) , Wrd and it worked.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR 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