Effect of modifying a read-only bit in a register

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

Some bits in certain registers are designated as read or write only. So what exactly happens if someone tries to write to a read-only bit or someone tries to read from a write only bit?

The following is a simple example -

Bits 7, 6, 4 and 3 in TIFR1 (Timer/Counter1 Interrupt Flag Register) of ATmega169 are designated as read-only bits and are supposed to have the initial value of 0.
What would happen if someone were to write a value to those bits as following?

TIFR1 = 0xFF;

Please let me know.

Thanks a lot in advance !

Warm regards,
Sandeep Chaudhary.

Thanks and regards,
Sandeep K Chaudhary,
University of Waterloo, Canada.

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

Quote:
So what exactly happens if someone tries to write to a read-only bit?
Nothing.
Quote:
So what exactly happens if someone tries to read from a write only bit?
Nothing (or rather, you read whatever the datasheet says you will read).

Regards,
Steve A.

The Board helps those that help themselves.

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

It will vary depending on the register.

In THAT case, I would expect a write to have no effect. But, why chance it? Do 4 bit-sets on the other 4.

A different example shows why you cannot say, categorically. The USART has two data registers at the same address. One is write only and is the transmitter register. One is read only and is the receiver register. So, if you read, you get the receiver register; you CANNOT read the transmit register. Likewise, if you write, you write to the transmitter register; you CANNOT write to the receiver register.

Thus, there is enough difference that one cannot give a generic statement.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

As far as the UDR register is concerned, it is irrelevant to the question since it is neither read-only or write-only. It only behaves oddly as to what is written or read.

The only exception that I can think of to what I wrote above is the PINx registers on newer AVRs. They are marked as read-only, but do have an effect if you write to them.

Regards,
Steve A.

The Board helps those that help themselves.

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

On the Mega48, one is shown as read and one, write. But, not read-only or write-only. Interestingly, a search on the Mega48 spec sheet only turns up "read only" associated with the PIN register.

Actually, the 4 bits asked about are do-nothing bits.

So, I suspect that the OP is wondering whether or not those places can be used for bit/flag storage. The answer to that is a resounding NO.

And Steve A. is correct.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Quote:

I suspect that the OP is wondering whether or not those places can be used for bit/flag storage

If that were the case perhaps someone should mention GPIORn ? GPIOR0 in particular is great for 8 individual bit variables accessed using SBI/CBI.

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

https://www.avrfreaks.net/index.p...

https://www.avrfreaks.net/index.p...

These two links should give enough info for bit storage in unused/auxiliary registers.

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

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

Thanks a lot for quick responses, guys. My intention was to find out if writes to a read-only bit could cause any type of issues. The answer to which as I now know is NO(from Steve's and Jim's replies). I am a student and am involved in a project where we are interested in knowing this particular thing.

Warm regards,
Sandeep.

Thanks and regards,
Sandeep K Chaudhary,
University of Waterloo, Canada.

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

Quote:

I am a student and am involved in a project where we are interested in knowing this particular thing.

So, to keep our interest: Why is it of interest to know that?

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

In general, something might break if you write R/O bits. It is not uncommon to have some bits reserved for testing or some functionality that did not work as expected, so it is not advertised, for example.

NOTE: I no longer actively read this forum. Please ask your question on www.eevblog.com/forum if you want my answer.

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

It would be poor design to allow writing to R/O bits to actually affect the operation of the chip. From a datasheet:

Quote:
For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written.
So the concern of writing to reserved bits is for compatibility between versions of the chip. Writing to reserved I/O addresses however, may affect chip behavior in the way that you state.

Regards,
Steve A.

The Board helps those that help themselves.

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

Although probably rare, it is not impossible that some function was implemented, but did not work properly. If function is minor, there is no reason to re-spin another silicon revision. I don't have actual examples of this happening.

But given how popular AVRs are we'd probably know by now about any such bits.

NOTE: I no longer actively read this forum. Please ask your question on www.eevblog.com/forum if you want my answer.

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

Quote:
So, to keep our interest: Why is it of interest to know that?

This is part of my research.

Quote:
In general, something might break if you write R/O bits. It is not uncommon to have some bits reserved for testing or some functionality that did not work as expected, so it is not advertised, for example.

Yeah, if the read-only bits happen to be reserved then they should ideally not be written[for future compatibility etc]. But in a lot of specifications, I see that there are many bits in registers that are marked as 'R' but are not specifically described as 'reserved'. Could they be considered as reserved? Can I say that it could be problematic if someone tries to write to them?

Quote:
So the concern of writing to reserved bits is for compatibility between versions of the chip. Writing to reserved I/O addresses however, may affect chip behavior in the way that you state.

Yeah, ok.

I am actually trying to make a case if catching such violations(write to read-only bits) can help improve the quality of embedded software. Therefore, I am interested in knowing if things work any differently when this is the case.

Thanks and regards,
Sandeep K Chaudhary,
University of Waterloo, Canada.

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

Quote:

Quote:
So, to keep our interest: Why is it of interest to know that?

This is part of my research.


Then make it original research, and carry out some tests. Or ask Atmel support to query the chip designers. Or if I'd make a statement "If you write to the 4th bit of I/O register 14, an Easter egg is triggered that writes 0x55 to all SRAM locations" would you then report that as the result of your research?

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

This is an example of purely academic research. It is very unlikely that it will be helpful in any kind of practical situations.

NOTE: I no longer actively read this forum. Please ask your question on www.eevblog.com/forum if you want my answer.

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

:-) Yeah, ok. Thanks !

Thanks and regards,
Sandeep K Chaudhary,
University of Waterloo, Canada.

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

Well, if you could do the error catching in very few cycles, then "reliability" might be improved. Here, I mean reliability in the MISRA sense.

But, then the question becomes whether the error checking improves or hinders code reliability. After all, the code size is now larger, and that increases the odds of an error, one might think.

I would argue that checking for these errors is in about the same category as array bounds checking.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

A little more than 6 years late on this one. But that's why I'm here in this thread. I was trying to find out the 'rules' associated with writing to registers (such as interrupt mask registers) that have bits that are marked 'R' (I assume read-only) and other bits marked R/W (read-write). The life story of many manuals or documents is that they appear to leave out or hide particular details.

 

A register - such as timer interrupt mask register (eg. TMSK5) ..... has some 'R' bits and some 'R/W' bits. So most people should wonder what that actually means. And most people should wonder how you're supposed to write to an 8 bit register that has 'R' bits and 'R/W' bits. The first-guess assumption would be that you can write to that particular register, and the bits that are marked 'R' would be ignored. That's the guess.

 

While experienced users of micro-controllers and micro-processors know what to do, and what to expect ----- the details provided in many data-sheets or 'user manuals' will often leave people scratching their heads ---- because important details that should be mentioned are left out.

 

Last Edited: Thu. Jul 11, 2019 - 01:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


e.g.:

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

joeymorin wrote:
joeymorin e.g.:

 

Thanks Joey. Nice information. That's very handy information. When I was looking at registers for 2560, like TMSK5, it didn't even have information on some bits, like bit 4, bit 6 and bit 7. So a general rule might be ---- for an 'R' bit, if the default/initial value is '0', then write a '0' to those 'R' bits for cases when we write to the register.

 

One interesting thing is --- for the UCSR register, bit 5 is an 'R' bit, and its initial value is '1'. The instructions don't indicate what value we should write to this 'R' bit (bit 5) when we write to this register. That's what I mean about life-story of holes and gaps in user manuals.

 

Thanks again Joey. Much appreciated.

Last Edited: Thu. Jul 11, 2019 - 01:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Without ruling out the possibility of a datasheet errata, in general you should follow the advice of the datasheet.  When advice is absent, then similarly and in general, it shouldn't matter one way or the other.

 

So in the case of UDREn, it shouldn't matter.  In practice, it doesn't either.

 

I generally choose to write '0' where no advice is given.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Thu. Jul 11, 2019 - 02:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:

Without ruling out the possibility of a datasheet errata, in general you should follow the advice of the datasheet.  When advice is absent, then similarly and in general, it shouldn't matter one way or the other.

I generally choose to write '0' where no advice is given.

 

Nice recommendations Joey. I'll follow that same line of thinking that you taught above. Sounds good. Thanks again for your help. It definitely helped me a lot.

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

If you were updating TIMSK5 would you not simply do something like:

TIMSK |= (1 << OCIE5C) | (1 << TOIE5);

if (for example) you were enabling those two bits. As such the "unused bits" would just be written back with whatever is read from them. The datasheet says those unused bit default to 0 so presumably this would just write 0 back to them.

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

clawson wrote:

If you were updating TIMSK5 would you not simply do something like:

 

Yes ..... maybe. That's what I would probably do. The interesting thing is ----- whatever we do, we certainly have to write to that register. That's what made me think about ----- since we need to write to that register, then is there any general procedure we need to follow? Your approach makes really good sense.

Last Edited: Fri. Jul 12, 2019 - 01:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Just note that it doesn't work in all circumstances.  For example, TIFRn registers contain interrupt flags.  When read as '1', this signifies that an interrupt has occurred, but has not been serviced.  The catch is that writing that bit to '1' will clear the flag.  So, in general, you >>don't<< want to use |= with interrupt flag registers.  Rather, use = by itself.

 

Bad:

TIFR5 |= (1 << TOV5);

Good:

TIFR5 = (1 << TOV5);

This is a peculiarity of interrupt flags and the registers in which they are found.  This has been discussed at length elsewhere.  Search out those threads if you're interested.

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Great recommendation and information Joey. That's why I had to ask about all this kind of thing. Immensely appreciated. That was a good question from clawson too.

So the important thing here is ------ the documentation needs to be clear and complete. And unfortunately, a lot of documents out there --- are incomplete and are missing important details, which is the life story of lots of books and manuals out there.

Last Edited: Fri. Jul 12, 2019 - 04:56 AM