I have some misunderstanding of the processor register concepts. There is a register with address, let’s say, 0x0345678. This register is 32 bits wide/long(doesn’t matter right now, which word is the right one). As written in manual there is some kind of table/array:
Position Value 0 111111...10b 1 111111...11b 2 7A ....................... 7 3F7C
I have to access the value with position 2.
The first thing what I’ve done was:
#define REG 0x0345678
void somereadfunction()
{
volatile unsigned int *pval = (volatile unsigned int *)REG;
printf("%x", *(pval | 0x02));
}
And as you already guessed, it was the wrong assumption.
Another endeavor was this one:
for(unsigned int i = 0; i < 3; i++)
{
printf("i: %d, res: 0x%08X", i, *((volatile unsigned int *)REG));
}
And it works. So, my question is, why and how? Does processor just switch the register value with some magic algorithm inside, written by another developer? I’m a bit confused about that. I know how to access the third bit of the register using some simple bit-wise operations, but I don’t understand, how, just by calling three times the register, we will get the right value?
Thank you beforehand for your answers.
ADDED: Processor is ARM 7. Used the i2c device.
Why?
Because the people who built your hardware device (that is connected to your ARM processor) probably declared just a few addresses that “belong” to the device. Then, they noticed there was more data in the hardware than registers, and it was too late to change the manual, so they decided to have some registers hold multiple values.
How?
The hardware has an internal counter that counts read commands from the processor. Each time the processor wants to read from the register, hardware sends it another piece of data, indexed by the invisible internal counter, and increases the counter. The counter is 3-bit, so it counts 0, 1, …, 7, 0, 1, … etc.
So if you want to read the data with index 2, you also have to read all other data elements (3, 4, 5, 6, and 7) to reset the invisible counter.