Go Down

Topic: Reading MOSI data from Bosch ASIC (Read 3 times) previous topic - next topic

Nick Gammon


SPI.transfer (4) would send the value of 4, of course - and writing an a = SPI.transfer(4) would capture the data returned as a result of sending a value of 4 out over SPI?


No, as my page tries to explain.

As each bit is sent and received simultaneously it isn't possible for a single transfer to send data and receive a response (to that same data).

Quote
... because the appropriate data is sent back is sent back at the same time as the data going out, let's check I understand this right.


It is possible to receive valid data as you send stuff, but it won't be in direct response to the byte being sent.

jtw11

#11
Jan 25, 2013, 11:38 pm Last Edit: Jan 25, 2013, 11:40 pm by jtw11 Reason: 1
Quote
As each bit is sent and received simultaneously it isn't possible for a single transfer to send data and receive a response (to that same data).


Ah, okay. Maybe i've misunderstood the following from your web page, perhaps you could steer me on the correct track?

Quote
Basically, while the master hardware is clocking out bits on the MOSI line (master out, slave in) it is also clocking in bits on the MISO (master in, slave out). Effectively, during one character time, it both sends and receives one byte. Hence the name of the function SPI.transfer.


Code: [Select]
char a, b;

a = SPI.transfer (4);

// a is now 1

b = SPI.transfer (3);

// b is now 2


...as I have replicated this in my code, for example - to read the contents of the identification register, IDENT_REG, at address B01001000 - I have done as you have, declared storage for the received data, I have then sent out the address B01001000, then sent a second transfer B00000000. The second transfer being the dummy bits required to retrieve the contents of the register.

Code: [Select]
byte ident_reg_contents = B00000000;

SPI.transfer(B01001000);
ident_reg_contents = SPI.transfer(B00000000);


Certainly that's how this page is implying to do so - http://www.breitband-lambda.de/pages/lambda---english/controlling-the-cj125.php?lang=EN

The 'Read Access' bit of the datasheet shows that too, send the address - then send 8 dummy bits, and the return of each dummy bit is the corresponding register entry, MSB first.

Perhaps i'm missing something fundamental? Do you mean my methodology is wrong, or my syntax to save the returned data is wrong? If it's of any help, I have no compiler errors at all.

Many thanks so far!

Nick Gammon

Code: [Select]

SPI.transfer(B01001000);


How about:

Code: [Select]

SPI.transfer(0x48);


That's easier to read, and matches the spec.

Their page seems to suggest the data is in 16-bit lots:



So without the device to hand it is hard to be sure. You could try the 16-bit approach:

Code: [Select]

SPI.transfer(0);  // MS byte
SPI.transfer(0x48);  // LS byte

byte result1 = SPI.transfer(0);
byte result2 = SPI.transfer(0);


And then try printing those.

jtw11

I went over to binary to make dealing with setting individual bits on or off, as I can simply line them up with their graphical display in the datasheet. On that note, may one mix hex & binary in the same code?

Quote
Their page seems to suggest the data is in 16-bit lots


Yes, page 5 specifies a 16-bit data frame.

However, sorry - I'm not with you on your 16 bit approach example?

Nick Gammon

Each transfer sends 8 bits, so 2 x transfer does 16 bits.

Go Up