Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ATmega 16 general programming in c language

Status
Not open for further replies.
RMW in any system is when you read a value , modify it and then write it back

For example

Code:
PORTB=0x01;   // this is a write operation

PORTB= PORTB | 0x01; // this is a RMW operation , it reads PORTB value, ORes it with 1 and then writes the result back

In order to keep the existing values of the bits and modify only a specific bit there is a need for RMW operation
 

Code:
PORTB= PORTB | 0x01; // this is a RMW operation , it reads PORTB value, ORes it with 1 and then writes the result back
Actually this is not an RMW operation, at least it shouldn't regardless of compiler. This is an atomic operation, uninterruptible by other ISRs.
On the contrary, if more than one bits are to be ORed, then this would be done through a register, making it an RMW operation.

https://obrazki.elektroda.pl/2397357600_1355434507.jpg

Image from IAR compiler, no optimization.
 

So you define as RMW anything that is not atomic and can be interrupted?

Your code shows the SBI used for the code I have provided.

This is from the mega8 datasheet page 151

Do not use Read-Modify-Write instructions (SBI and CBI) to set or clear the MPCM bit.
The MPCM bit shares the same I/O location as the TXC Flag and this might accidentally
be cleared when using SBI or CBI instructions.
 

You are right to say that anything that is atomic is not necessarily RMW, but unless you disable interrupts, RMW is not atomic. OR with a single bit (sbi) is atomic.
As for the datasheet's quote, it is a part of describing the MPCM, UART section. I can't see how it is related with bit manipulation of port registers.
Let us not forget that code provided is from IAR compiler. I bet gcc will produce the same assembly code. Is it possible that a compiler will do such a silly mistake?
 

My point was this
Do not use Read-Modify-Write instructions (SBI and CBI) to set or clear the MPCM bit.

SBI and CBI are both Read-Modify-Write instructions so when PORTB= PORTB | 0x01; generates the following ASM code SBI 0x5,0 how is that not RMW ?
 

In SBI 0x5,0 read and write operations are missing. Only modify is there. On the contrary, PORTB= PORTB | 0x39;, forces the code to move the value to a register, make the OR in there, and then return this value to its original position. Read-modify-write.
 

So the datasheeet says that SBI and CBI are both Read-Modify-Write instructions but you say that although SBI is used it is not RWM because the read and write operations are missing.
Why do you have to see a register loaded with the value to be modified in order for RMW to apply?
Why can't this be applied internally in hardware as an atomic operation?

Refer to https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=227590#227590
But on most AVR's (the devices which are exceptions to this rule have notes in the Register Summary section of the datasheet), even these so-called bitwise operations actually operate on the whole register, even if only one bit is being changed.
In a single instruction cycle (two clocks in this case), the whole I/O register is read into a scratch space, a single bit is modified, and the whole scratch register is written out to the I/O register again.
 

Compiler's disassembler speaks for itself.

A post from another forum is not exactly what we were looking for to clear this issue. We both have good knowledge on microcontrollers, but we disagree on that. I wouldn't have problems to disagree with him either. There are posts in AVRFreaks from more well known members, that claim the opposite, but I wouldn't refer to those posts as well.

On the contrary, I would only trust references like the following.
http://www.atmel.com/images/doc0856.pdf

As well as the two files provided by this post.
https://www.edaboard.com/threads/220546/#post937681

Please feel free to search and find anything there that will clear this issue. I will refer to page 123 of the instruction set pdf, where it is written that for tiny (which is an AVR as well), the sbi command is executed in 1 cycle. Is it possible for an RMW command to be executed in 1 cycle?
 

A post from another forum is not exactly what we were looking for to clear this issue
Actually that forum is a valuable source of info when it comes to AVR, any wrong answer in that forum would be confronted very fast from other members especially when it comes to things like that which are black or white.

Compiler's disassembler speaks for itself.
The compiler just shows the SBI instruction used, the claim that this is not a RMW instruction is made by you so the compiler has nothing to do as proof of what you say.

My previous quote is from an Atmel datasheet (mega 16 in particular in which this thread refers to) and clearly states that SBI and CBI are both Read-Modify-Write instructions so why do you disagree with that?

I will refer to page 123 of the instruction set pdf, where it is written that for tiny (which is an AVR as well), the sbi command is executed in 1 cycle. Is it possible for an RMW command to be executed in 1 cycle?

I refer to the same page too but it says 2 cycles for the device we talk about (m16)
Words : 1 (2 bytes)
Cycles : 2
Cycles XMEGA: 1
Cycles Reduced Core tinyAVR:1

This is also from the mega16 datasheet in the I/O memory section
Some of the Status Flags are cleared by writing a logical one to them. Note that the CBI and SBI
instructions will operate on all bits in the I/O Register
, writing a one back into any flag read as
set, thus clearing the flag. The CBI and SBI instructions work with registers $00 to $1F only.

How do these instruction write to all bits and don't use RMW operation?
SBI and CBI are RMW in AVR mega 16 like it or not.

They are not RMW instructions in newer devices like m48/88/168 , the datasheet in that case states
Some of the Status Flags are cleared by writing a logical one to them. Note that, unlike most
other AVRs, the CBI and SBI instructions will only operate on the specified bit
, and can therefore
be used on registers containing such Status Flags. The CBI and SBI instructions work with reg-
isters 0x00 to 0x1F only.
 

I get your point, you are talking about what is happening in the hardware background of ATmega16, which is transparent to the programmer. You said it in previous posts as well, no need to repeat yourself.
But you don't seem to get MY point. So let us remember how this all started.

RMW in any system is when you read a value , modify it and then write it back

PORTB= PORTB | 0x01; // this is a RMW operation , it reads PORTB value, ORes it with 1 and then writes the result back

Now I suppose that the 'you' part went to the OP, unless you were talking to ATmega16 in second person. If assumed that you were talking to the OP, then the example clearly implied that this was an explanation of what is happening in commands level, which is completely wrong. Because RMW is also a more general term as you may know. And I still wonder what that "it" would be... The OP could easilly be driven to wrong conclusions reading this vague "explanation".

As for AVRfreaks, there are in deed many respectful members and clawson is one of them. Please read again post 2 in the thread you asked this very question last night. His answer is more general as well.

Now please, read again this thread's post #2 and then your last post. Maybe you meant all this from the beginning, who knows? I answered on specific words in a specific post.


PS: Please post the link for each document you use as reference. Users don't have to search in the web, you could make it more convenient by posting the web location of those very documents.
 

The OP asked this
what is called read/modify/write in atmega 16 operation ?

I have replied this
PORTB= PORTB | 0x01; // this is a RMW operation , it reads PORTB value, ORes it with 1 and then writes the result back

But you said
Actually this is not an RMW operation
Using an image that showed this code compiled to an SBI instruction

I replied that
SBI is a Read-Modify-Write instruction

But you said that it isn't
In SBI 0x5,0 read and write operations are missing. Only modify is there

You even justified it using this which doesn't apply in m16
for tiny (which is an AVR as well), the sbi command is executed in 1 cycle. Is it possible for an RMW command to be executed in 1 cycle

In my last reply I think I proved beyond doubt that SBI is a RMW instruction in mega16 so now you try to justify your claim by saying that according to your definition a RMW operation can only be executed in memory and if it is executed in hardware like with the SBI in m16 then it is not considered that.

If that makes sense to you then it simply doesn't make sense to me.
A RMW can create problems by clearing register flags unintentionally no matter how it is executed.

Please post the link for each document you use as reference. Users don't have to search in the web, you could make it more convenient by posting the web location of those very documents.
You mean the mega16 datasheet?
Here www.atmel.com/Images/doc2466.pdf
It is the only document I have referred to , the AVR instruction PDF link was already provided in your post.
 

Nonsense!

By thread #10 you proved that the original answer you gave was wrong. Why didn’t you gave the correct answer from the beginning and gave it after 10 posts? You were talking to the OP as if this was a command sequence and not hardware operation (by saying "when you read a value"). It was also misleading, this is clear from my reference to atomic operations and compiler results. No need to quote all posts, it is obvious. Of course you quote anything that is convenient for you, skipping other parts, like the part I wonder why post #2 was so poor as answer compared to the last one, or clawson's answer in AVRFreaks that was close to my understanding. Futhermore, even now you said nothing on optimization level and how that could affect. Again it is described in clawson's post. So you seem to be satisfied by your original answer, and that’s fine by me. I wouldn't. Maybe you knew the answer but you were lazy to give more info, or didn't know more info at that time who knows?

After I said something different in post #3, you are since struggling to prove that the original answer was OK, by web digging and asking in other forums (if you were so sure, then why post in AVRFreaks?). This in not bad, but you could just comeback with a second post and fill the gaps of the first one, it happens all the time to many posters. But no, you are pulling it and trying to stand for yourself by insulting other members with expressions like “like it or not”, “according to your definition”, or by trying to twist my words on atomic operations.

Finally you posted a quote from mega88 datasheet as well, not only from mega16, but even then you change the truth by saying that it was one datasheet and the other datasheets were posted by me (as if I didn’t know it). At least I posted them. You just care to use emphatic phrases like "here". Here where? After 2 posts? I had already found them, thanks for nothing.

I think that this thread has nothing more to offer to members, I am bowing out. OP took a clear picture.
 

I think that this thread has nothing more to offer to members, I am bowing out. OP took a clear picture.

It is interesting when you say that after accusing me for everything that comes in your mind from references I didn't provide to optimization levels to replies posted in other forums (I thought you said you didn't care for posts in other forums but you use them when it suits you) and all this to cover the fact that in no point in this thread you admit that SBI is a RMW operation unlike what you were saying in the first replies in order to prove that my code was wrong.

Thats fine by me, I have already posted all the references that support my case and refer to the actual device discussed in this thread which is mega 16 (and not tiny like you referred to in order to make your point about SBI), anyone reading the thread can decide for himself.

insulting other members with expressions like “like it or not”,
:shock:
 

all this to cover the fact that in no point in this thread you admit that SBI is a RMW operation
Sorry, couldn't leave that unanswered. SBI in ATmega16 is an RMW operation. Nothing to do with other stuff I said in the last two posts.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top