spi write and read registers

05 Mar 2012

Hi, I have questions about "read and write" a short address via Serial Protocol Interface. I do not really understand this code:

<<code>>

uint8_t MRF24J40::ReadShort (uint8_t address)

{

uint8_t value;

mCs = 0;

wait_us(1);

mSpi.write((address<<1) & 0x7E);

wait_us(1);

value = mSpi.write(0xFF);

wait_us(1);

mCs = 1;

wait_us(1);

return value;

}

<<code: Copyright (c) 2011 Jeroen Hilgers >>

a unsigned char of 8 bit is selected

Write function SPI:

- value Data to be sent to the SPI slave

- returns Response from the SPI slave

the slave select (cs has to be set on 0), then we wait 1 micro second. First bit of a short address is 0 and last address is 0 for the command read (0x7E: 01111110)

Here is the deal, I do not understand theses sentences:

- mSpi.write((address<<1) & 0x7E)

- value = mSpi.write(0xFF);

The operator <<1 means a shift of 1 on the left and & return 1 if bit have the same value.

I do not really know what it means. I'm not sure to be clear. I'm a bit confuse.

Thanks a lot

05 Mar 2012

See operator & and &&, you described the logical AND, while & is the bitwise AND operator, so 0x7E is used as a bitmask, just to be sure the last bit is really zero. I currently dont know why the shift ... this is some logic you didnt post

05 Mar 2012

Well ok, I think the address is given as a 6bit value, where the lower 6bits describe it. To send it over SPI, it must be surrounded with an upper and lower 0, so the address is shifted up and masked with 0x7E. Because the shift up automatically creates a 0 in LSB, the mask 0x7F would also work, because it should only be assured that the MSB is 0.

05 Mar 2012

Rene is right, the code is applying a bit mask operation to make sure that the most significant and least significant bits are both zero. The msb must be zero when you send a 'short' address to the MRF24J40, the lsb must be zero to indicate you want to read from that address. See the datasheet. After sending the address and read command you write a dummy byte (0xFF) to the device to read back the value from that address.

The library code by Jeroen Hilgers also has methods to Read/Write a register using the 'long address'.

06 Mar 2012

Thanks for your replies I'm well understanding why the bit mask is (0x7E), the thing is that I do not undertand "<<" and "&" or '"|"operators. I search informations about these but I did not find.

06 Mar 2012

Sorry, I did not understand. Now it is clear.

Thanks a lor