ATMEL Studio 7 - win7/32 Problem with sub's return

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

Hello,

I tried to find an answer to my problem but I am lost. I used to work with AS 4.18 with a serial AVRISP. Now I have a new AVRISP mkII clone which after too much effort It (kind of)  works! I don't know how to operate AS7 and I search around. I wish I had a good tutorial for assembly and AS7.

Any way I built a simple project with a few leds with ATtiny 2313. Just to test everything, I wrote a simple program that flashes a couple of leds. Between flashes I call a delay sub. The program starts and the fisrt led goes on. When it reaches the "rcall delay" it never returns to normal execution. I inserted the delay routine inside the main program, so I make no rcall and it works fine!!

Can someone please help me? I suspect something with usage of the flash, about the stack pointer, I don't know. See below please:

Here is the code which is attached to this as well:

 

; test_coloid.asm
;
; Created: 08-Apr-19 2:06:36 PM
; Author : Python341
;
.NOLIST
.INCLUDE "tn2313def.inc"
.LIST

.def    temp1     = r16
.def    temp2     = r17
.def    temp3     = r18
.def    temp4    = r19
.def    state    = r20

; Code starts here
.CSEG
.ORG $0000
rjmp RESET            ; Reset Handler            0

; **************** End of interrupt service routines ***********

RESET:
ldi     temp1,low(RAMEND)    ; Main program start
out     SPL,temp1             ; Set Stack Pointer to top of RAM

clr        temp1                ; portA in inputs
out        ddra,temp1
ser        temp1
out        Porta,temp1            ; set pull-up resistors at input pins

ser        temp1                ; portB is outputs
out        ddrb,temp1
clr        temp1                ; clear outputs,
out        Portb,temp1

ser        temp1                ; portD is outputs
out        ddrd,temp1

start:    ldi        state,$42
        out        portD,state    
        rcall    ldel
;        ser        temp2
;        ser        temp3
;        ldi        temp4,2
;ldel1:    dec        temp2
;        brne    ldel1
;        ser        temp2
;        dec        temp3
;        brne    ldel1
        ldi        state,$50    ; goto state 2
        out        portD,state    
;        ser        temp2
;        ser        temp3
;        ldi        temp4,2
;ldel2:    dec        temp2
;        brne    ldel2
;        ser        temp2
;        dec        temp3
;        brne    ldel2
        rcall    ldel
        rjmp    start

ldel:    ser        temp2
        ser        temp3
ldel1:    dec        temp2
        brne    ldel1
        ser        temp2
        dec        temp3
        brne    ldel1
        ret

 

Attachment(s): 

Stuck with assembler

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


Python341 wrote:
I wish I had a good tutorial for assembly and AS7.
The user manual perhaps?

 

https://www.microchip.com/webdoc/GUID-ECD8A826-B1DA-44FC-BE0B-5A53418A47BD/index.html?GUID-C32ADD88-CB66-4995-A6F7-ABB19006CBAF

 

Python341 wrote:
When it reaches the "rcall delay" it never returns to normal execution.
How are you observing that . The AVRISPmkII is a programmer not a debugger so you cannot be watching what happens inside the AVR itself so by implication I guess you mean the simulator? As the simulator runs SO slow compared to a real AVR that delay will take a LONG time to execute if you simulate it.

 

Actually, I just tried that. I single stepped the simulator until it mad the first "rcall lde1" then I reset the cycle counter. I also put a break point on the next instruction after the return from the delay then let it run. After about 2 seconds I saw:

 

So the delay routine executed 195,845 cycles. But it DID return after that.

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

clawson wrote:

 

How are you observing that . The AVRISPmkII is a programmer not a debugger so you cannot be watching what happens inside the AVR itself so by implication I guess you mean the simulator? As the simulator runs SO slow compared to a real AVR that delay will take a LONG time to execute if you simulate it.

 

Actually, I just tried that. I single stepped the simulator until it mad the first "rcall lde1" then I reset the cycle counter. I also put a break point on the next instruction after the return from the delay then let it run. After about 2 seconds I saw:

 

So the delay routine executed 195,845 cycles. But it DID return after that.

 

Dear clawson

I did uploaded the hex file to the real target board. I suspect hardware problem to the mcu. I did the simulator and it does return, but not on the real module.

Is there a chance that the compiler does mistake so the hex file is wrong?

Is there a fuse that I don't know which modifies the memory usage and the stack pointer takes wrong value?

I don't know what to think.

Stuck with assembler

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

clawson wrote:

Python341 wrote:
I wish I had a good tutorial for assembly and AS7.
The user manual perhaps?

 

https://www.microchip.com/webdoc/GUID-ECD8A826-B1DA-44FC-BE0B-5A53418A47BD/index.html?GUID-C32ADD88-CB66-4995-A6F7-ABB19006CBAF

 

 

 

This link is for C, not assembly. I will search though for assembly tutorial with AS7

Thanks

Stuck with assembler

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

Yeah I find it quite difficult to take URLs from Atmel's manual as they use frames.
.
OK so if you are observing this in a real AVR I'd say your delay is too short so the port output is changing too rapidly to see it with the naked eye. Try making you 16 bit delay a 24 bit (3 register) delay to slow it by a factor of 256.

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

clawson wrote:

.
OK so if you are observing this in a real AVR I'd say your delay is too short so the port output is changing too rapidly to see it with the naked eye. Try making you 16 bit delay a 24 bit (3 register) delay to slow it by a factor of 256.

 If I take out the ; and the rcall ldel so that no calls take place it works fine. I have an external xtal at 4Mhz and the delay is very well seen, about 250 ms.

 

; test_coloid.asm
;
; Created: 08-Apr-19 2:06:36 PM
; Author : Python341
;
.NOLIST
.INCLUDE "tn2313def.inc"
.LIST

.def    temp1     = r16
.def    temp2     = r17
.def    temp3     = r18
.def    temp4    = r19
.def    state    = r20

; Code starts here
.CSEG
.ORG $0000
rjmp RESET            ; Reset Handler            0

; **************** End of interrupt service routines ***********

RESET:
ldi     temp1,low(RAMEND)    ; Main program start
out     SPL,temp1             ; Set Stack Pointer to top of RAM

clr        temp1                ; portA in inputs
out        ddra,temp1
ser        temp1
out        Porta,temp1            ; set pull-up resistors at input pins

ser        temp1                ; portB is outputs
out        ddrb,temp1
clr        temp1                ; clear outputs,
out        Portb,temp1

ser        temp1                ; portD is outputs
out        ddrd,temp1

start:    ldi        state,$42
        out        portD,state    
;        rcall    ldel
        ser        temp2
        ser        temp3
        ldi        temp4,2
ldel1:    dec        temp2
        brne    ldel1
        ser        temp2
        dec        temp3
        brne    ldel1
        ldi        state,$50    ; goto state 2
        out        portD,state    
        ser        temp2
        ser        temp3
        ldi        temp4,2
ldel2:    dec        temp2
        brne    ldel2
        ser        temp2
        dec        temp3
        brne    ldel2
;        rcall    ldel
        rjmp    start

;ldel:    ser        temp2
;        ser        temp3
;ldel1:    dec        temp2
;        brne    ldel1
;        ser        temp2
;        dec        temp3
;        brne    ldel1
;        ret

 

strange!

Can you please send me the hex file that you produce from the version with the "rcal ldl" in (for ATtiny2313) to compare with mine?

Thanks

 

Stuck with assembler

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

What do you have on PORTB? If you have a short circuit between pins or to one of the power supply pins then the power could die and the chip reset when you output state 42 or 50.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I have cleaned up your code (removed unnecessary bits) and run it on the STK500 (had to invert the bits for that) and it all works well at 1MHz.

 

Leds flash at about 300ms with bit 6 on at all time (bit 6 is common to 0x42 AND 0x50) ,

 

Bit 1 comes on and bit 4 goes off for state $42.

Bit 1 goes off and bit 4 comes on for state $50.

 

(The code paste stuff is absolute rubbish for ASM!! angry)

; test_coloid.asm
;
; Created: 08-Apr-19 2:06:36 PM
; Author : Python341
;
.NOLIST
.INCLUDE "tn2313def.inc"
.LIST

.def	temp1 	= r16
.def	temp2 	= r17
.def	temp3 	= r18
.def	temp4	= r19
.def	state	= r20

; Code starts here
.CSEG
.ORG $0000
rjmp RESET			; Reset Handler			0

; **************** End of interrupt service routines ***********

RESET:
	ldi 	temp1,low(RAMEND)	; Main program start
	out 	SPL,temp1 		; Set Stack Pointer to top of RAM

	clr	temp1			; portA in inputs
	out	ddra,temp1
 	ser	temp1
	out	Porta,temp1		; set pull-up resistors at input pins

	ser	temp1			; portB is outputs
	out	ddrb,temp1
	clr	temp1			; clear outputs,
	out	Portb,temp1

	ser	temp1			; portD is outputs
	out	ddrd,temp1

start:
	ldi	state, ~ $42		;Invert for STK500
	out	portD,state
	rcall	ldel

	ldi	state, ~ $50		; goto state 2 Invert for STK500
	out	portD,state	

	rcall	ldel
	rjmp	start

ldel:
	ser	temp2
	ser	temp3
ldel1:
	dec	temp2
	brne	ldel1
	ser	temp2
	dec	temp3
	brne	ldel1
	ret

 

 

 

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:

What do you have on PORTB? If you have a short circuit between pins or to one of the power supply pins then the power could die and the chip reset when you output state 42 or 50.

Well I don't believe it! I used a new ATtiny2313 on a new bread board, without anything on portB, to which I set the same fuses, and ... it works!! It is not software problem. (I used the rcall ldel) Then I just exchanged the mcu with the old one, which has the same code, and it does NOT work! I exchanged again and it does!!! So this mcu has a problem with memory or stack pointer and it does not return correctly!! Crazy! I cannot think of anything else. I set all fuses to the same values. (I also use the divide-by-8 fuse and an external xtal at 4MHz)

Well, I thank everybody for trying to help me. This was an experience!

 

Stuck with assembler

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

Python341 wrote:
So this mcu has a problem with memory or stack pointer and it does not return correctly!! Crazy!
I seriously doubt that - more likely it's output drivers have been burned by over/reverse voltage or something like that. I imagine the delay code is working just fine (without a debugger you can't possibly know whether it is returning or not) it's just that LED output driving stuff is failing.

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

If there is a short on portd pin/s then maybe the processor just freezes because the supply drops internally.

 

HOWEVER if the code runs with with the inline delay that shoots down my theory. Anyway it seems to be a hardware problem and all is well now.

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:

If there is a short on portd pin/s then maybe the processor just freezes because the supply drops internally.

 

HOWEVER if the code runs with with the inline delay that shoots down my theory. Anyway it seems to be a hardware problem and all is well now.

Thank you very much for your time. Sure it is a hardware problem.

Stuck with assembler

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

Dr Clawson

I see that the link to reply is disabled. I don't know why.

I guess this will be my last post about this issue but I have a point. Here is the two programs that i run on the same real physical board. (I program the chip and obsreve the leds) When I rcall ldel it does not work, but when I don't call it DOES.

Thank all for trying to help me, but this is a very interesting issue:

;THIS WORKS OK
; test_coloid.asm
;
; Created: 08-Apr-19 2:06:36 PM
; Author : Python341
;
.NOLIST
.INCLUDE "tn2313def.inc"
.LIST

.def    temp1     = r16
.def    temp2     = r17
.def    temp3     = r18
.def    temp4    = r19
.def    state    = r20

; Code starts here
.CSEG
.ORG $0000
rjmp RESET            ; Reset Handler            0

; **************** End of interrupt service routines ***********

RESET:
ldi     temp1,low(RAMEND)    ; Main program start
out     SPL,temp1             ; Set Stack Pointer to top of RAM

clr        temp1                ; portA in inputs
out        ddra,temp1
ser        temp1
out        Porta,temp1            ; set pull-up resistors at input pins

ser        temp1                ; portB is outputs
out        ddrb,temp1
clr        temp1                ; clear outputs,
out        Portb,temp1

ser        temp1                ; portD is outputs
out        ddrd,temp1

start:    ldi        state,0
        out        portD,state    
;        rcall    ldel
        ser        temp2
        ser        temp3
ldel1:    dec        temp2
        brne    ldel1
        ser        temp2
        dec        temp3
        brne    ldel1
        ldi        state,$3f
        out        portD,state    
        ser        temp2
        ser        temp3
        ldi        temp4,2
ldel2:    dec        temp2
        brne    ldel2
        ser        temp2
        dec        temp3
        brne    ldel2
;        rcall    ldel
        rjmp    start

;ldel:    ser        temp2
;        ser        temp3
;ldel1:    dec        temp2
;        brne    ldel1
;        ser        temp2
;        dec        temp3
;        brne    ldel1
;        ret
;---------------------------------------------------------
;THIS DOES NOT WORK:
; test_coloid.asm
;
; Created: 08-Apr-19 2:06:36 PM
; Author : Python341
;
.NOLIST
.INCLUDE "tn2313def.inc"
.LIST

.def    temp1     = r16
.def    temp2     = r17
.def    temp3     = r18
.def    temp4    = r19
.def    state    = r20

; Code starts here
.CSEG
.ORG $0000
rjmp RESET            ; Reset Handler            0

; **************** End of interrupt service routines ***********

RESET:
ldi     temp1,low(RAMEND)    ; Main program start
out     SPL,temp1             ; Set Stack Pointer to top of RAM

clr        temp1                ; portA in inputs
out        ddra,temp1
ser        temp1
out        Porta,temp1            ; set pull-up resistors at input pins

ser        temp1                ; portB is outputs
out        ddrb,temp1
clr        temp1                ; clear outputs,
out        Portb,temp1

ser        temp1                ; portD is outputs
out        ddrd,temp1

start:    ldi        state,0
        out        portD,state    
        rcall    ldel
;        ser        temp2
;        ser        temp3
;ldel1:    dec        temp2
;        brne    ldel1
;        ser        temp2
;        dec        temp3
;        brne    ldel1
        ldi        state,$3f
        out        portD,state    
;        ser        temp2
;        ser        temp3
;        ldi        temp4,2
;ldel2:    dec        temp2
;        brne    ldel2
;        ser        temp2
;        dec        temp3
;        brne    ldel2
        rcall    ldel
        rjmp    start

ldel:    ser        temp2
        ser        temp3
ldel1:    dec        temp2
        brne    ldel1
        ser        temp2
        dec        temp3
        brne    ldel1
        ret

 

Stuck with assembler

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

Then maybe the implication is faulty RAM (so the stack doesn't work). Without using CALL/RET write a short RAM test.