Despite the tutorials I'm concerned about how SPI works in arduino

I kinda know how SPI.transfer() work but I don't understand how will my slave device know when I'm passing a register adress or some data that has to be written.

Since I'm using an MPU9250 I know that the MSB of the reg address has to be 0 to open the register in write mode, but after this how does my slave know that the next value I'm passing it's data and not a new reg address or viceversa?

Thank you

Have you looked at the data sheet.

Are you using a library or handling the interface in your code?

If your "slave device" is a standard IC or module, the manufacturer data sheet provides a clear description of how data transfer is to take place. You are required to follow the rules, or it won't work as expected, so study it carefully.

It knows because it was designed to know.

Usually there will be several bytes worth of "SPI.transfer()" for each "interaction." The first bytes will contain addresses and control info, with the data being sent in the subsequent bytes.

For the MPU9250, you'd usually use an existing library that hides all of that for you, like GitHub - hideakitai/MPU9250: Arduino library for MPU9250 Nine-Axis (Gyro + Accelerometer + Compass) MEMS MotionTracking™ Device

1 Like

Consider a letter address format that is explicitly defined with no allowable variations.

  • Name
  • Number
  • Street
  • City
  • State
  • Country

You make a automated sorting machine.
First it reads Country
Then State
etc

If it receives a letter with the state and country reversed, it will try to send it to someplace wrong, that may not exist.
In some cases the device may be able to identify that the state is not a country, but not in every case.

So if your MPU9250 receives a sequence of data that is wrong, it will ever generate an error or do something you didn't want to do.

Simply stated, if you don't provide the command and data in the correct format you will not get what you want. It must be in the required format.

1 Like

because it expects address first then data

I'm using the SPI library to make a code by myself

Ik it's just that in libraries for mpu9250 that I find on github I don't see that many operations.
The write/read and communication functions have just some lines and they're way more simple than what I can image reading the communication instructions I see on the datasheet (which tells me to send a start signal then wait for a determined clock state, send acknowledgements and so on), this is why I think I'm missing something.

I must add that the start/stop conditions signals are cited just in the I2C section and I don't know if they apply to SPI as well.

They don't.

so what do I do

You ignore anything in the I2C interface and just use the information in the SPI section of the data sheet.

It is not entirely clear what you want to do and why you want to do it.

Is your library for a specific device? In which case you follow the data transfer sequence for that device. You can still use the existing SPI library inside your library. It is a waste writing your own as it will only be the same as the standard one.

Yeah maybe, still, pretty anything is written in SPI sections of the datasheet, almost if they forgot to write the instructions.
And the library i'm writing is for communicating with the device while for SPI basic operations like SPI.transfer() I'm using the one from arduino.

Edit: *anything useful

The datasheet is getting batted around a lot.  Have you looked at the Register Map?

Do you mean not
"while for SPI basic"
but
"while four SPI basic"
Because you can use them all with the same library.

I don't think I understand, but i mean "for"

Yeah i read that while defining the variables I need in my code, there's just the descriptions of the registers in that file

There are usually the correct number of operations, in the correct order, to make the device work as required. Study those operations carefully, line by line, and you should learn a lot.

1 Like

Yeah I'm starting to think it's like you guys said, I can try once I write enough code.