Sending 256 bits over SPI, easier way than mine?

Hi

I need to send an OP code (1 byte) followed by a data field of 32 bytes over SPI.
My first thought was to make it all into an array and have a for loop send each byte after the other.

My question is, with all you bright minds out there, is there a simpler way?

byte lblArr[] = {0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x02, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

Im looking to set the bits corresponding to label 0xD0 (208th bit from last) and 0xB9 (185th bit)

Is there a way to make the for loop insert "1" instead of "0"at specific bits?

Attached the datasheet page showing the OP codes and setup.

SPI sends whole bytes. You can't send partial bytes. You can pad bytes with zeroes or ones if it helps... if there is a bit offset, you have to bit shift the array contents to align the 256 bits with byte boundaries.

ajakobsson:
Hi

I need to send an OP code (1 byte) followed by a data field of 32 bytes over SPI.
My first thought was to make it all into an array and have a for loop send each byte after the other.

My question is, with all you bright minds out there, is there a simpler way?

There are 3 serial data transmission protocols in Arduino, which are all byte oriented protocols (handle data byte-by-byte) and these are: UART, SPI, and I2C. They are are neither simple nor complicated; they are with their known characteristics. Our job is to to use them to do our tasks.

By saying 'simple way' -- what have you wanted to mean? Code optimization or space optimization or execution time optimization?

aarg:
SPI sends whole bytes. You can't send partial bytes. You can pad bytes with zeroes or ones if it helps... if there is a bit offset, you have to bit shift the array contents to align the 256 bits with byte boundaries.

Thanks for your input.

I might have worded the heading wrong, I'm sending 32 bytes (256 bits) after the "op code" (so 33 bytes in total).

Each of the bits in the 32 bytes tells the chip which Arinc label to filter, I'm looking to read 2 different Arinc labels, so only 2 of those 256 bits needs to be 1, the rest of them should be 0. But the location of the high bits are critical, which is why I set up the array in the example in my first post as I did.

GolamMostafa:
There are 3 serial data transmission protocols in Arduino, which are all byte oriented protocols (handle data byte-by-byte) and these are: UART, SPI, and I2C. They are are neither simple nor complicated; they are with their known characteristics. Our job is to to use them to do our tasks.

By saying 'simple way' -- what have you wanted to mean? Code optimization or space optimization or execution time optimization?

Thanks for your input.

By simpleR way, I mean simpleR than the example of using a for loop to send bytes from a byte array predefined with all 256 bits (32 bytes) i suggested in my first post. Time efficiency is what I'm after

The spec of the chip i'm sending this to requires SPI, so I don't care about UART or I2C.

Indexing a byte array is at least an order of magnitude faster than actually transmitting the data. It's already as efficient as it can usefully be. I think if you investigate, you will find that the SPI bytes are already transmitted back to back with no pauses.

If you think about it, on an 8 bit processor there is no place where 256 contiguous bits can exist. So any method you come up with, will always have to index bytes somehow. The compiler translates indexed arrays very efficiently. That fact is what makes C so distinctive since 1968. :slight_smile:

@OP
Hope, the following diagram (Fig-1) will help you to visualize (at conceptual level) the byte oriented simultaneous data exchange mechanism between Master and Slave in a SPI Network. SPI is the most speedy network among the 3 serial protocols supported by Arduino.
spi328x.png
Figure-1:

spi328x.png

I think the OP is well aware of the data exchange mechanism. I have been proven wrong at times.

aarg:
Indexing a byte array is at least an order of magnitude faster than actually transmitting the data. It's already as efficient as it can usefully be. I think if you investigate, you will find that the SPI bytes are already transmitted back to back with no pauses.

If you think about it, on an 8 bit processor there is no place where 256 contiguous bits can exist. So any method you come up with, will always have to index bytes somehow. The compiler translates indexed arrays very efficiently. That fact is what makes C so distinctive since 1968. :slight_smile:

Thanks, I'll stick with the "for loop" then :wink:

aarg:
I think the OP is well aware of the data exchange mechanism. I have been proven wrong at times.

You are correct, the end product here will be a device to translate from Arinc 429 bus (aviation standard) to arduino for presenting certain values on an LCD (for testing purposes).

Thanks for you inputs guys! Much appreciated.

aarg:
I think if you investigate, you will find that the SPI bytes are already transmitted back to back with no pauses.

GolamMostafa:
@OP
Hope, the following diagram (Fig-1) will help you to visualize (at conceptual level) the byte oriented simultaneous data exchange mechanism between Master and Slave in a SPI Network.

aarg:
I think the OP is well aware of the data exchange mechanism. I have been proven wrong at times.

ajakobsson:
You are correct, [...]

In this Forum, the Forum Members are all living in the air; they just share whatever information/message they have created/re-created/collected/compiled.