writing "16-bit" registers in one go

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

For several register pairs, the AVR-GCC includes specify shortened names, like EEAR that combines (?) EEARH and EEARL. For a long time, I've gotten away with writing these registers with a single assignment, such as:

EEAR =

But is this really safe? I realize it's probably not portable. Should I be splitting my writes into high and low bytes with shifts beforehand, and writing to the actual register names? Or maybe churn out some kind of union? Sorry if this has been answered before, but I couldn't find it.

(I'm working with the atmega64, if it's relevant. )

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

bobasaurus wrote:
But is this really safe?

Define "safe".

If you look at the definition of that symbol, you'll find out that it's tagged "volatile", which means both bytes of it are written no matter what.

There is also "magic" built into avr-gcc which ensures proper order of writes to registers where it is required, e.g. the timers' registers.

But are accesses to it "atomic" against interrupts? No.

bobasaurus wrote:
I realize it's probably not portable.

Define "portable". :-)

JW

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

Quote:

There is also "magic" built into avr-gcc which ensures proper order of writes to registers where it is required

Except that _SFR_MEM16() is ultimately:

#define _MMIO_WORD(mem_addr) (*(volatile uint16_t *)(mem_addr))

and the code generator makes a 16 bits access in low, then high order.

I assume OP is asking about atomicity? As Jan says the access is not atomic so will need protection if the thing really is accessed in two code paths.

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

It makes sense that the 16-bit reads/writes are not atomic, so some care must be taken if sharing them with ISRs. I was more concerned with the read/write byte order, since the datasheet states (under the "Accessing 16-bit Registers" section, though this might not apply to the same register pairs I mentioned):

"To do a 16-bit write, the high byte must be written before the low byte. For a 16-bit read, the low
byte must be read before the high byte."

Wek, you mentioned that avr-gcc has a hack to ensure proper register write ordering to the registers that need it. Does the same apply for reading?

Clawson, if all 16-bit register accesses happen low, then high, would the datasheet's requirements hold for writing?

Thanks for the feedback. Sorry for the somewhat ambiguous question.

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

Quote:

Clawson, if all 16-bit register accesses happen low, then high, would the datasheet's requirements hold for writing?

You can trust GCCs "ADCW", "TCNT1", "OCR1A" and other 16 bit registers - they always work in the right order. If this were not true the AVR-LibC authors would be unlikely to have included something that only works intermittently or not at all!

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

Hmm, where have I seen recent talk on this subject before. :wink:

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

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

Commander Norton again! (T)his triple-posting yields Rama-fications..

:D

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

Larry, I didn't notice the similar post before (found it in your comment history). I'll give it a read through, too.