See: https://github.com/adafruit/Adafruit_FRAM_SPI/blob/master/Adafruit_FRAM_SPI.cpp The following method reads and converts the input to 8 bit. I have several questions concerning its function.
Using addr = 0xFFFF as an example, the method converts the variable to 0xFF. I understand that bits are removed from the end, but surely 65535 != 255. Perhaps relatively, to their max size, but how is this a useful conversion? I might be missing something about bitwise here..
uint8_t Adafruit_FRAM_SPI::read8 (uint16_t addr)
{
digitalWrite(_cs, LOW);
SPItransfer(OPCODE_READ); //read memory from fram array (2-byte)
SPItransfer((uint8_t)(addr >> 8));
SPItransfer((uint8_t)(addr & 0xFF));
uint8_t x = SPItransfer(0);
digitalWrite(_cs, HIGH);
return x;
}
Correct me if I'm wrong, the casting is to keep sign extension from occurring.
The masking I understand in theory, but do not at all understand its application here.
Finally, it always returns the same value x = 0, why?
I also see there's a write method but no read method. Only read8. Reasoning?
addr>>8
), and then the lowest 8 bits. Not sure why they do both masking and casting. After that it sends 8 zero bits. During the sending of those bits the FRAM send the value in the address to the arduinos MISO pin. The 8-bit value is stored in the `x variable. Note that SPI is bi-directional, and you can send and receive data at the same time. – Gerben Mar 16 '16 at 20:15