RAMPZ register question

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

Sorry - can't find this answer in Atmel-docs

Assembly language...

writing the RAMPZ register (and others?) using STS instead of OUT does not work. I thought that sts and out had the same effect.

Where is my thinking wrong?

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

I'm a C person, so beware of below advice.

The registers which live in IO space (PORTx, PINx, etc) have corresponding SRAM aliases in the SRAM address space - hence why you can manipulate the SRAM alias with the STS instruction and have the application work. If you can't use STS for the RAMPZ register, then I guess it must not have been allocated a SRAM alias address.

The datasheets usually have the address for IO registers with the SRAM alias address in brackets next to it. Does the RAMPZ register have a bracketed address?

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

Last Edited: Wed. Jul 5, 2006 - 06:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

They have the same effect, but you have to use different addresses. The address of a register using STS is 0x20 higher than its address using OUT. The RAMPZ address (in a mega128) is 0x3b for OUT and 0x5b for STS.

Regards,
Steve A.

The Board helps those that help themselves.

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

ah ha, thanks !

Too bad the assembler doesn't error on STS into I/O space.

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

STS into I/O space is valid instruction!! U can access I/O with STS also. See above (Koshchi's post) ...

Example M128:
RAMPZ address is 0x3b for OUT and 0x5b for STS.
OUT 0x3B,R0 - U can access RAMPZ, but
STS 0x3B,R0 - U can access PORTA (0x3B-0x20 = 0x1B!!!)

VFX.
http://www.vfx.hu

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

STS is a 64k byte wide address range instruction that takes two CPU cycles. OUT is a very limited 64 byte address range instruction that only takes one CPU cycle. OUT is simply an optimized 1 cycle version of STS targeted at some very specific I/O registers. The AVR assembler include I/O register equates try and take advantage of this available instruction optimization.

Try these macros when programming in AVR assembly 2.1.

; AVR {in/lds} - {out/sts} automatic selection macros
;
;	The device include file must be used to define SRAM_START
;

; usage: InReg reg, addr
.macro InReg
    .if @1 < 0x40
        in @0, @1
    .elif ((@1 >= 0x60) && (@1 < SRAM_START))
        lds @0,@1
    .else
       .error "InReg: Invalid I/O register address"
    .endif
.endmacro

; usage: OutReg addr, reg
.macro OutReg
    .if @0 < 0x40
        out @0, @1
    .elif ((@0 >= 0x60) && (@0 < SRAM_START))
        sts @0,@1
    .else
       .error "OutReg: Invalid I/O register address"
    .endif
.endmacro

Now you can use code like this:

ldi ZL, low(FLASH_address * 2)
ldi ZH, high(FLASH_address * 2)
ldi r16, byte3(FLASH_address * 2)
OutReg RAMPZ, r16  ; FLASH Page Z Select Register (17th bit extension for the Z register pair)

Just make sure you always use the correct AVR device include file and the included I/O register equate names. Then you can forget about checking to see if you need STS or OUT. The same goes for LDS and IN with the InReg macro.

Last Edited: Wed. Jul 5, 2006 - 07:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmm thanks Mike - I had not seen those macros. I wonder where these kinds of macros are documented, or does one just browse the files?

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

Quote:

I wonder where these kinds of macros are documented, or does one just browse the files?

See this current thread:
https://www.avrfreaks.net/index.p...
where Mike said (in part)

Quote:
I forgot to mention one caveat about the macros. They do not work with the original version 1 assembler and some early version 2 assembler builds (I think these were all beta builds before the official version 2 release). I forgot how many AVRstudio releases back they still work in. Of course they have always worked in 4.12 and all of its service packs. It is only ever possibly a problem for much older AVRstudio assembler versions.

and
Quote:
Actually the original version of those macros came from Roland Kruse at ATMEL. It was his support answer to my complaint about the in/out lds/sts problems.

Lee

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

Actually the original version of those macros came from Roland Kruse at ATMEL. It was his support answer to my complaint about the in/out lds/sts problems. The original versions are buried deep in the AVRfreaks posts archive. I think he is trying to get it into the AVR assembler 2.1 documentation with some other helpful macros, but it takes time and resources.

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

Hi Lee,

I just decided to just state AVR assembler 2.1 since it should work with all released versions of 2.1. It seemed a little better than the long winded explanation that only a very few people are probably ever interested in. Anyway I edited and posted before I saw your post. I was not trying to be redundant.