Hardware Error in ATMega328p ?

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have come across a most curious behaviour in the ATMega328p. Under certain conditions, doing an st  X,R16 appears to brick my program (but not the chip itself). Storing certain values at XH:XL as shown by the "<- *****" comment below just stops the code running. I apologize for assembler, but this is all I use. I have tried with two different 328's, from different sources. Here is my test code, with a lot of the preamble omitted:

 

; Background:
; This is a highly condensed version of a filing program, only to show a
; curious behaviour of the ATMega328p

; I have not included any of the initialization,
; Setting up the stack,  disabling the Watchdog, clearing RAM, and
; initializing the LED output

; I use a=R16, b=R17 and 'file_pointer' happens to be R13 (must have been
; the wrong choice ;-)

main:
	clr	file_pointer

mm3:
	sbi	LED_port,LED	; Flash LED - at least we've got here
	rcall	wait20ms
	cbi	LED_port,LED
	rcall	wait200ms


	loadZ	test_string
mm1:
	lpm	a,Z+
	cpi	a,0     ; exit on null terminator
	breq	mm2

	mov	XL,file_pointer
	clr	XH

	ldi	b,'f'	; 'f' is no good, 's' is OK

	st	X,b		; <-- !!!!!!!!!!!!!!!!!!!!!!!!!!

	inc	file_pointer
	rjmp	mm1

mm2:
	sbi	LED_port,LED	; Flash LED - and we've got here
	rcall	wait20ms
	cbi	LED_port,LED

self:	rjmp	self

;------------------------------------------------------------------------------

test_string:	.db	"Now is the time for all good men to come to the aid of the party",0,0

;------------------------------------------------------------------------------

.include	"wait.inc"   ; routines to provide delays

;------------------------------------------------------------------------------

.dseg
.org	SRAM_START

file_buffer:	.byte	128	; file output buffer
end_file_buffer:

;------------------------------------------------------------------------------

; End of source code

I have tried a few different letters in the line < ldi  b,'f' > and some also cause a problem. If I had to take a stab at the cause I would say it was a problem in the chip's microcode but I don't even know if this is how Atmel chips work.

This topic has a solution.
Last Edited: Mon. Jan 1, 2018 - 07:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jbug11 wrote:
stops the code running

What, exactly, do you mean by that?

 

How are you observing it?

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In my highly simplified snippet, you never see the second  flash from the LED.

 

The original code was much more complicated and included the ability to write values out on a serial interface, using this I found that the code simply stopped running after it had processed a few characters of the test string.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jbug11 wrote:
In my highly simplified snippet, you never see the second  flash from the LED.

And from that you conclude that the code has, "simply stopped running" ?

 

Have you tried running this in a simulator, so that you can step through and see exactly what's happening?

 

The ATMega328p has on-chip debug - so you can do the same thing in the actual target.

 

http://www.catb.org/~esr/faqs/sm...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Perfect situation and code to run in the Simulator in Atmel Studio, since no advanced peripherals are used. In the Simulator you can track execution flow and inspect register contents and status flags to find out if/why it possibly gets stuck in the mm1 loop - or what else happens. For that, shorten (or cancel out) your delays since they will take a very long time to execute in the simulator.

 

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I admit to never having tried a simulator, but will now go and fire up my Studio 7, learn the simulator, and have a go. May take me a little time to learn.

 

I normally use a text editor in linux, AVRA to do the assembly and avrdude to upload programs to the target with an AVR Dragon, but I also have a Windows 10 machine for Studio 7 which, so far, I've only used to write the fuse bits.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to the 21st Century

 

wink

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@jbug11 said:

If I had to take a stab at the cause I would say it was a problem in the chip's microcode but I don't even know if this is how Atmel chips work.

ROTFLMAO!!!

 

So you are suggesting that the ATmega328P, the chip used in countless Arduino boards, has a hardware error, that is just now being discovered, because your code doesn't do what you expect it to do.  Is that right?

 

Perhaps it is because your code is attempting to overwrite the first 65 addresses in the data space.  So it first clobbers the R0, then R1, .... , and lastly EEDR, but it clearly never makes it to EEDR.

 

 

EDIT:

 

Greg Muth

Portland, OR, US

Atmel Studio 7 (Version: 7.0.1652) on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

Last Edited: Mon. Jan 1, 2018 - 06:25 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greg, you're quite right, and thank you so much. Removes face from palm. I'd got so used to starting my iteration from an already-loaded base value for XH:XL. I sometimes wish learning was not so humiliating.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I sometimes wish learning was not so humiliating.

Been there.  Done that.  Got the T-shirt.  

Greg Muth

Portland, OR, US

Atmel Studio 7 (Version: 7.0.1652) on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jbug11 wrote

I apologize for assembler, but this is all I use

Why? Many developers used low level exclusively. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jbug11 wrote:

I sometimes wish learning was not so humiliating.

 

I can sympathize with that!

 

But.. Better debugging capabilities caters for earlier revelation of the bug which leads to less humiliation.

 

Many here argue that the best tool for debugging is e.g. paper, pen and brain. I agree. But I also think that debugging is, to a large extent, the "art of finding out", and good tools makes it so much easier to find out.

 

Even if this problem has been solved, I urge you to test drive the simulator. Unless you have provisions for on-chip debugging the Simulator is well worth knowing.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jbug11 as in 68HC11 debugger??

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, John. Just keeping my hand in.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:

jbug11 as in 68HC11 debugger??

jbug11 wrote:

Yes, John. Just keeping my hand in.

 

John just made a new BFF for 2018......cheeky

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually John has been a "BFF" since last century, I think, or at least early this century and had the pleasure of his visit at the Ampertronics' worldwide headquarters. cheeky

 

JBUG11 is a on chip debugger for the HC11 (found Version 5 on this computer) but I haven't used it for many years, well since I have been using AVRs at least. It was better version of the Motorola mbug11?? and it would come in very handy when away from the workshop and the Motorola development system, it allowed programming and debugging without hardware.

 

I used it in Hong Kong (2000 AD) when the client there could not make up his mind on what he wanted exactly so it was a matter re-assembling the code and reprogramming on site, single stepping or putting a breakpoint to examine things inside the chip.

 

 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I used NOHAU EMUL-51 and EMUL-68.... Both with Trace boards.  I owned the EMUL-51, was loaned an EMUL-68 by the Motorola FAE who was trying to get me to convert.

 

Great stuff to use. Too bad they are practically worthless on Ebay these days sad

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, did you guys also report a hardware bug when a test 'HC11 app didn't produce the expected results?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We were saved from this embarrassment by the lack of forums such as this (I realize there were bulletin boards - not the same at all). In a way, my OP error was a hangover from the HC11, still with the mental image that the user's RAM started at zero and forgetting that Bogen and Wollan had already absconded with a few handy low RAM addresses for their working registers.

Last Edited: Tue. Jan 2, 2018 - 11:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jbug11 wrote:
forgetting that Bogen and Wollan had already absconded with a few handy low RAM addresses for their working registers.

1)  OK, I'll bite:  On AVR8 architecture, what is special about low RAM addresses, such that kidnapping must be reported?  [I'm trying to think of a RAM-address-space-related instruction that doesn't use 16 bits (or more) of address]

 

2)  Use an Xmega model, where the feature was taken away.  Back when I was your age, >>with<< the feature on an AVR8, one could have a "register variable" and take its address to pass to a function or whatever.  No more on the new-fangled.

 

In other words, in my opinion, I think we have >>lost<< something by not having GP registers memory-mapped.  (Now, the waste of low >>I/O<< space on the unified map -- what a shame)

'

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Back when I was your age,

I think he IS your/our age or older and DOES KNOW a lot about microcontrollers, maybe not about AVRs yet. wink

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
I think he IS your/our age or older and DOES KNOW a lot about microcontrollers,

Just a saying.  But that is really my point -- reitereating

Greg_Muth wrote:
So you are suggesting that the ATmega328P, the chip used in countless Arduino boards, has a hardware error, that is just now being discovered, because your code doesn't do what you expect it to do. Is that right?

No matter how far the pointing goes to use of address space...

 

I've had splendid success with AVR8.  If I were to start now, perhaps a bit passe.  But still pretty solid.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.