Why do we need to do write a data in to a register using this sequence of read-modify-write order?
In reply to Arun_Rajha:
/*Let's assume 32bit ctrl_reg register with 2 different bit-field.
data_ctrl(22bit), clk_ctrl(10bit)*/
_______________________
| 31 : 10 | 9 : 0 |
| data_ctrl | clk_ctrl |
|___________|__________|
// you want to modify data_ctrl bit-field and don't want to change the clk_ctrl bit-field value.
// But when you write the register, it will modify all 32bit value.
// so first read the register data, modify the read data then write the register
tmp_data = *ctlr_reg; // ctrl_reg read operation
tmp_data = data_ctrl | (tmp_data & 0x0000_003FF); // modify the data
*ctlr_reg = tmp_data; // ctrl_reg write operation
In reply to Rahulkumar Patel:
@Rahulkumar
Thanks you for answering,
I have been pondering this in my head for long time.
In reply to Rahulkumar Patel:
In reply to Arun_Rajha:
/*Let's assume 32bit ctrl_reg register with 2 different bit-field.
data_ctrl(22bit), clk_ctrl(10bit)*/
_______________________
| 31 : 10 | 9 : 0 |
| data_ctrl | clk_ctrl |
|___________|__________|
// you want to modify data_ctrl bit-field and don't want to change the clk_ctrl bit-field value.
// But when you write the register, it will modify all 32bit value.
// so first read the register data, modify the read data then write the register
tmp_data = *ctlr_reg; // ctrl_reg read operation
tmp_data = data_ctrl | (tmp_data && 0x0000_003FF); // modify the data
*ctlr_reg = tmp_data; // ctrl_reg write operation
Be careful with logical operation “&&”. It wont work as your expectation with this statement. It should be bitwise operation “&”.
tmp_data = data_ctrl | (tmp_data & 0x0000_003FF);
In reply to chris90:
It was typo. Thanks for correcting it.
Why left shift operation is not performed here on data_ctrl variable?
It should be like as below right?
tmp_data = (data_ctrl << 10) | (tmp_data & 0x0000_003FF);
If data_ctrl is of 32bits then left shift must be performed. I am confused how data_ctrl is declared above and how it’s working?