SPI question

Hi All,

I hope you can help me. Forgive me for a little piece of C# code:

                SpiCom = new SPI(new SPI.Configuration(Cpu.Pin.GPIO_NONE,
                    false, 0, 0, false, false, 1000, SPI.SPI_module.SPI1));
                
                byte[] ReadBuffer = new byte[64];
                // Create and fill write buffer
                byte[] WriteBuffer = new byte[64];
                for (byte c = 0; c < 64; c++) { WriteBuffer[c] = c; }

                SpiCom.WriteRead(WriteBuffer, ReadBuffer);

This would fire 64 bytes over the SPI line and expects 64 bytes back from the slave. Now I have a Mongoose IMU that has a Arduino Pro mini Atmega 328 that allows me to change the firmware. I'd like to have it listen to SPI commands (bytes written) and answer by sending SPI bytes back. I expect one byte in return for every byte written.

The example above is ignoring the slave select pin and is running at 100Mhz.

I'm a complete aruino noob and this is the first time I ever worked on it, so please don't blaim me for any extremely stupid questions. This is what I figured out so far:

I should insert the SPI library:
#include <SPI.h>

I should initialize the library:
SPI.begin()

And now I'm lost XD

I know there is a SPI.transfer(byte) that writes a byte and reads a result, but that seems to be more like the master behaviour. As I client I would like it to read a byte and write a byte back later on. Is there any way of doing that or am I making another kind of mistake here?

Also, how should I set the clock speed and wait delay time of the SPI?

Thanks a lot for the time to read this and hopefully answer these questions :slight_smile:

Read this and get back to us if there are more questions:

Hi Nick,

Thank you for your extremely quick response and article! I just read it and it makes a lot more sense now. I hope I can get it to work now because your article is so clear that I would feel a bit dumb if I still could not get it to work, but if I can't I hope I can spam you with some more questions in this thread.

Thanks a lot for taking the time!