Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

MCS Bootloader for XMEGA 32E5 not uploading flash program
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Nov 17, 2015 8:22 pm    Post subject: Reply with quote

Even you still don't describe your hardware exactly, it's to understand that you use self-carved boards.
Guessing from the lockup after app-erase &h20, where the contoller draws peaks of current, I'd say the boards VCC isn't buffered well enough.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Tue Nov 17, 2015 9:36 pm    Post subject: Reply with quote

I will have a look with hardware. But since MWS simulated and found nothing, i am afraid it will simply work for me. But i will let you know.
_________________
Mark
Back to top
View user's profile Visit poster's website
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 537

PostPosted: Tue Nov 17, 2015 10:02 pm    Post subject: Reply with quote

Thanks MWS.

We saw the note as well in the Errata if voltage drops below 2V. The PCB has a 2uF tant cap 10mm away from VCC/GND (17/18) and a 474 15mm away from AVCC/GND (32/1), and operates at 3.3V.

BTW, the only version of AVR1316 I could find is dated Nov 2008. I believe the E-series was introduced in early 2013.

We will keep trying to figure this out.

E
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Tue Nov 17, 2015 11:06 pm    Post subject: Reply with quote

I could duplicate the problem. There were several problems.

The source need a change for the XMEGA32E5 : Const Maxwordbit = 6
I could not find this info in the datasheet but found it in the XML file.

Then there was something odd and i think it is a bug in the chip or the datasheet. Or my brain.
The ERASE APP command &H20 to erase the application section, seems to erase the complete flash including the bootloader space, no matter how you change the fuse bits.
The atmel inc file mentions :
#define APP_SECTION_START 0x0000
#define APP_SECTION_SIZE 0x8000
#define APP_SECTION_END (0x0000 + 0x8000 - 1)

So i do not think it should erase the boot space. Maybe you can write an email to atmel support to clear this?

Anyway to solve it just modify the boot loader.

Code:
'----------------------------------------------------------------
'                          (c) 1995-2015, MCS
'                        BootloaderXmega32E5.bas
'  This sample demonstrates how you can write your own bootloader
'  in BASCOM BASIC for the XMEGA 32E5
'-----------------------------------------------------------------
'The loader is supported from the IDE
$crystal = 32000000                               ' xmega128 is running on 32 MHz
$regfile = "xm32E5def.dat"
$hwstack = 40
$swstack = 40
$framesize = 40
'first enabled the osc of your choice
Config Osc = Enabled , 32mhzosc = Enabled                   'internal 2 MHz and 32 MHz enabled

'configure the systemclock
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1       ' we will use 32 MHz and divide by 1 to end up with 32 MHz

$loader = &H4000                                            ' bootloader starts after the application

'this sample uses 38400 baud. To be able to use the Xplain which has a bootloader working at 9600 baud you need to use 9600 baud
Config Com3 = 38400 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8       ' use USART C0
'COM0-USARTC0, COM1-USARTC2, COM2-USARTD0. etc.

Open "COM3:" For Binary As #3

Const Maxwordbit = 6                                        ' Z7 is maximum bit                                   '
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 , Bmincount 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


Disable Interrupts                                'we do not use ints
Rampz = 0
'We start with receiving a file. The PC must send this binary file

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


$timeout = 600000                                           '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 : Bmincount = 3                      'we try 10 times and want to get 123 at least 3 times
Do
   Bstatus = Waitkey(#3)                                     'wait for the loader to send a byte

   If Bstatus = 123 Then                           'did we received value 123 ?
      If Bmincount > 0 Then
         Decr Bmincount
      Else
         Print #3 , Chr(bstatus);
         Goto Loader                                        ' yes so run bootloader
      End If
   Else                                                      'we received some other data
      If Bretries > 0 Then                                   'retries left?
         Bmincount = 3
         Decr Bretries
      Else
         Goto Proces_reset                                   'goto the normal reset vector at address 0
      End If
   End If
Loop




'this is the loader routine. It is a Xmodem-checksum reception routine
Loader:
   Do
      Bstatus = Waitkey(#3)
   Loop Until Bstatus = 0
'   Spmcrval = &H20 : Gosub Do_spm                           ' erase  all app pages
'^^^^^^^ THIS LINE REMARKED
   Bretries = 10                                            'number of retries

   Do
      Csum = 0                                              'checksum is 0 when we start
      Bblocklocal = 1

      Print #3 , Chr(cnak);                                 ' firt time send a nack
      Do

         Bstatus = Waitkey(#3)                              'wait for statuse byte

         Select Case Bstatus
            Case 1:                                         ' start of heading, PC is ready to send
               Csum = 1                                     'checksum is 1
               Bblock = Waitkey(#3) : Csum = Csum + Bblock  'get block
               Bcsum1 = Waitkey(#3) : Csum = Csum + Bcsum1  'get checksum first byte
               For J = 1 To 128                             'get 128 bytes
                  Buf(j) = Waitkey(#3) : Csum = Csum + Buf(j)
               Next
               Bcsum2 = Waitkey(#3)                         '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 #3 , Chr(cack);                  'acknowledge
                     Incr Bblocklocal                       'increase local block count
                  Else                                         'no match so send nak
                     Print #3 , Chr(cnak);
                  End If
               Else
                  Print #3 , Chr(cnak);                     'blocks do not match
               End If
            Case 4:                                    ' end of transmission , file is transmitted
               If Wrd > 0 Then                      'if there was something left in the page
                  Wrd = 0                          'Z pointer needs wrd to be 0
                  Spmcrval = &H25 : Gosub Do_spm            'write page
                  '^^^^^USE THE NEW ERASE AND PROGRAM COMMAND
               End If
               Print #3 , Chr(cack);                        ' send ack and ready
               Waitms 20
               Goto Proces_reset
            Case &H18:                                      ' PC aborts transmission
               Goto Proces_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 Proces_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
      R0 = Vl                                               'store them into r0 and r1 registers
      R1 = Vh
      Spmcrval = &H23 : 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 = &H25 : Gosub Do_spm                     'write page
         '^^^^^USE THE NEW ERASE AND PROGRAM COMMAND
         Page = Page + 1                                    'next page
      End If
   Next
Return


Do_spm:
   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}

   Nvm_cmd = Spmcrval
   Cpu_ccp = &H9D
   ! spm                                                    'this is an asm instruction
Do_spm_busy:
   ! lds r23, NVM_STATUS
   ! sbrc r23,7 ;if  busy bit is cleared  skip next instruc tion
   ! rjmp do_spm_busy
Return



Proces_reset:
   Rampz = 0
   Print #3 , "RST"
   Goto _reset                                              'start at address 0

'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
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 537

PostPosted: Tue Nov 17, 2015 11:20 pm    Post subject: Reply with quote

THANK-YOU MARK!!!

And I apologize for keeping you so late.

We'll try it and post any ideas/improvements.

E
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Nov 18, 2015 7:33 am    Post subject: Reply with quote

enniom wrote:
The PCB has a 2uF tant cap 10mm away from VCC/GND (17/18) and a 474 15mm away from AVCC/GND (32/1), and operates at 3.3V.

Even there seem to be a solution found for the issue, notice that this is no proper hardware design.
It's not the big capacitors, doing the trick, it's the tiny 100nF ceramics on each VCC/GND pair, which are, due to low ISR, able to deliver these short, but high current peaks. Tantals, while also being lower ISR-types aren't that suitable and in bigger capacitance even not necessary to supply the controller itself. If other hardware is responsible for voltage drops - that's a different story then.
Quote:
BTW, the only version of AVR1316 I could find is dated Nov 2008. I believe the E-series was introduced in early 2013.

I've addressed the point of no other, or special AN is available, means the standard procedure from the even older AN should work. That the &h20 did not, as Mark found out, is something new however.
Back to top
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 537

PostPosted: Wed Nov 18, 2015 12:24 pm    Post subject: Reply with quote

MWS,

Your previous post is why I so very much look forward to you getting involved in replying to my requests for help ... there is ALWAYS something to learn from your review and suggestions.

Quote:
Until contrary is proven, I refuse to believe that Atmel lets such an incredible buggy controller out in the wild, as one may believe from reading the ton of your posted problems here in the forum concerning this XMega.


In this case (and with Mark's help), I like to think I was able to return the favor.

Thanks. Look forward to continued constructive exchanges.
E
Back to top
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 537

PostPosted: Wed Nov 18, 2015 1:55 pm    Post subject: Reply with quote

To Atmel Technical Service:
Quote:
We found that when using the Bootloading function and issuing the commands:

NVM_CMD = &H20
SPM

which would normally erase the Application space prior to re-programming, instead erases the complete memory -including the Bootloader area.

In addition, the LockBits do not protect the Bootloader area if this command is used.


Reply from Atmel:
Quote:
Thank you for contacting Atmel technical support.

The entire flash will get erased always when application/bootloader erase is called. A bug has already been posted regarding the same and the datasheet will be updated as soon as possible. Sorry for the inconvenience caused.

Thank you for your valuable feedback.


The response misses the point that when NVM_CMD = &H20 is used, only the Application area should be erased, not the "entire flash"; and, there is no mention of the LockBits. We'll see what happens.

E
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Nov 18, 2015 6:08 pm    Post subject: Reply with quote

enniom wrote:
The response misses the point that when NVM_CMD = &H20 is used, only the Application area should be erased, not the "entire flash"

&h20 actually is the command app_erase, and they write that the whole flash gets erased. Additionally they call it a bug.
According data sheet an app_erase should erase only the program space, so the default condition is known to the user, while a bug report has to tell what differs.
Atmel did not miss the point imho.
Quote:
The entire flash will get erased always when application/bootloader erase is called.

Atmel still may be lucky, as well it is a pretty nasty bug, at least it's not a security sensitive one.
It simply kills itself, if tested. Only those sticking an untested bootloader to their code may have a bad day Very Happy
Back to top
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 537

PostPosted: Thu Nov 19, 2015 4:53 am    Post subject: Reply with quote

Quote:
Atmel did not miss the point imho.


Having discussed many issues with you in the last 5 years, I can conclude there is no "h" in your opinions! Smile Smile
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Thu Nov 19, 2015 9:12 pm    Post subject: Reply with quote

thanks for reporting to atmel. i added this info to the help.
_________________
Mark
Back to top
View user's profile Visit poster's website
andro

Bascom Member



Joined: 12 Mar 2007
Posts: 94
Location: Ljubljana

slovenia.gif
PostPosted: Sun Jul 18, 2021 4:05 pm    Post subject: Reply with quote

Hello!

I've been using e5 series for several years. However, just now I encountered the problem with bootloader, and I found this rather old forum post.

I tried with Mark's code from this post, but no success. I'm receiving code -6008.

Is this issue with E series fixed or not? I took a look at the samples, but did not find a bootloader sample for xmega32e5 or 16e5.

Thanks for Your kind help.
BR
Andrej
Back to top
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 537

PostPosted: Sun Jul 18, 2021 5:43 pm    Post subject: Reply with quote

.
The modifications described in Mark's post above work perfectly.


Code:
'   Spmcrval = &H20 : Gosub Do_spm                           ' erase  all app pages
'^^^^^^^ THIS LINE REMARKED

'AND

'In Main and in Writepage sub

                  Spmcrval = &H25 : Gosub Do_spm            'write page
                  '^^^^^USE THE NEW ERASE AND PROGRAM COMMAND
 


I think you'll find that Error Code -6008 may be a communication problem between PC and uC.

E
Back to top
View user's profile
andro

Bascom Member



Joined: 12 Mar 2007
Posts: 94
Location: Ljubljana

slovenia.gif
PostPosted: Sun Jul 18, 2021 9:49 pm    Post subject: Reply with quote

Enniom, thanks for a quick response

I have adopted and used bootloader on xmegas 128a1,128 a3, 256a3, 32a4u....
and newer had any problems. Till now with E family.
I have just upgraded to 2804 and tested different baudrates (9600, 38400, 115200), but always the same error.
I use Com1, but this should not be an isseu.
In my application I 'drive' FEC TP-100 thermal printer without any problems on this port.
So, I realy doubt, the HW to be the problem. At least not my part of the HW Smile
Here is the code - maybe someone can see a mistake, that I do not see:

Code:
$crystal = 32000000                               ' xmega128 is running on 32 MHz
$regfile = "xm32E5def.dat"
$hwstack = 40
$swstack = 40
$framesize = 40
'first enabled the osc of your choice
Config Osc = Enabled , 32mhzosc = Enabled                   'internal 2 MHz and 32 MHz enabled

'configure the systemclock
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1       ' we will use 32 MHz and divide by 1 to end up with 32 MHz

$loader = &H4000                                            ' bootloader starts after the application

'this sample uses 38400 baud. To be able to use the Xplain which has a bootloader working at 9600 baud you need to use 9600 baud
Config Com1 = 9600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8       ' use USART C0
'COM0-USARTC0, COM1-USARTC2, COM2-USARTD0. etc.

Open "COM1:" For Binary As #1



Const Maxwordbit = 6                                        ' Z7 is maximum bit                                   '
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 , Bmincount 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


Disable Interrupts                                'we do not use ints
Rampz = 0
'We start with receiving a file. The PC must send this binary file

'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 : Bmincount = 3                      'we try 10 times and want to get 123 at least 3 times
Do
   Bstatus = Waitkey(#1)                                    'wait for the loader to send a byte

   If Bstatus = 123 Then                           'did we received value 123 ?
      If Bmincount > 0 Then
         Decr Bmincount
      Else
         Print #1 , Chr(bstatus);
         Goto Loader                                        ' yes so run bootloader
      End If
   Else                                                      'we received some other data
      If Bretries > 0 Then                                   'retries left?
         Bmincount = 3
         Decr Bretries
      Else
         Goto Proces_reset                                   'goto the normal reset vector at address 0
      End If
   End If
Loop




'this is the loader routine. It is a Xmodem-checksum reception routine
Loader:
   Do
      Bstatus = Waitkey(#1)
   Loop Until Bstatus = 0
'   Spmcrval = &H20 : Gosub Do_spm                           ' erase  all app pages
'^^^^^^^ THIS LINE REMARKED
   Bretries = 10                                            'number of retries

   Do
      Csum = 0                                              'checksum is 0 when we start
      Bblocklocal = 1

      Print #1 , Chr(cnak);                                 ' firt time send a nack
      Do

         Bstatus = Waitkey(#1)                              'wait for statuse byte

         Select Case Bstatus
            Case 1:                                         ' start of heading, PC is ready to send
               Csum = 1                                     'checksum is 1
               Bblock = Waitkey(#1) : Csum = Csum + Bblock  'get block
               Bcsum1 = Waitkey(#1) : Csum = Csum + Bcsum1  'get checksum first byte
               For J = 1 To 128                             'get 128 bytes
                  Buf(j) = Waitkey(#1) : Csum = Csum + Buf(j)
               Next
               Bcsum2 = Waitkey(#1)                         '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 #1 , Chr(cack);                  'acknowledge
                     Incr Bblocklocal                       'increase local block count
                  Else                                         'no match so send nak
                     Print #1 , Chr(cnak);
                  End If
               Else
                  Print #1 , Chr(cnak);                     'blocks do not match
               End If
            Case 4:                                    ' end of transmission , file is transmitted
               If Wrd > 0 Then                      'if there was something left in the page
                  Wrd = 0                          'Z pointer needs wrd to be 0
                  Spmcrval = &H25 : Gosub Do_spm            'write page
                  '^^^^^USE THE NEW ERASE AND PROGRAM COMMAND
               End If
               Print #1 , Chr(cack);                        ' send ack and ready
               Waitms 20
               Goto Proces_reset
            Case &H18:                                      ' PC aborts transmission
               Goto Proces_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 Proces_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
      R0 = Vl                                               'store them into r0 and r1 registers
      R1 = Vh
      Spmcrval = &H23 : 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 = &H25 : Gosub Do_spm                     'write page
         '^^^^^USE THE NEW ERASE AND PROGRAM COMMAND
         Page = Page + 1                                    'next page
      End If
   Next
Return


Do_spm:
   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}

   Nvm_cmd = Spmcrval
   Cpu_ccp = &H9D
   ! spm                                                    'this is an asm instruction
Do_spm_busy:
   ! lds r23, NVM_STATUS
   ! sbrc r23,7 ;if  busy bit is cleared  skip next instruc tion
   ! rjmp do_spm_busy
Return



Proces_reset:
   Rampz = 0
   Print #1 , "RST"
   Goto _reset  


BR
Andrej
Back to top
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 537

PostPosted: Sun Jul 18, 2021 10:09 pm    Post subject: Reply with quote

.
The only difference I see is that I used a different timeout to make it work with 2.8.0.3:
Code:

$Timeout = 16000000
 


I'm sure you checked it - but be sure no other software is occupying the COM port.

Other than that, maybe Mark can help.

E
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
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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