Was having trouble with multiple SPI devices. So I wrote a simple SPI test routine below. It seems that the SPI.h functions hold the enable line for 16 bit periods even if only sending 8 bits? This has been proven with a scope.
Any way to set the SPI function to jump back after only 8 SPI clock cycles?
Note: It's not the delay function in my example, does the same thing without delay();
You are sending an int value. On the Arduino an int type is two bytes or 16 bits long. If you only want to send 8 bits at a time, try using an 8-bit variable such as a char, byte or uint8_t instead.
What MCU are we talking about?
I think it has nothing to do with 16bit or 8Bit transfers.
SPI.transfer always only transfers 8 bits and only creates 8 clock pulses.
And the delay to the SS-edge is independent of the clockspeed. Setting/clearing the SS pin with digitalWrite needs some µs time ( at least on AVR ). That's what you see.
Normally for SPI the SS (slave select) is brought low to activate/enable the device for the transfer and returned to high after the transfer is complete. This may not matter if your test bed is only to show the elapsed time of an SPI.transfer statement on a scope, but could exlain the original problem that this was intended to analyse.
Now keep in mind the enable line (green) cannot go low until the SPI.transfer() function is done. It's the SPI.transfer function that is causing the delay. Any you can see the extra time is exactly 8 more bit periods.
The trace clearly shows SPI is only transferring 8 bits - not 16 bits. The delay in the trace 4 changing is more likely to be due to the digitalWrite function IMHO.
Just an observation, but as I'm sure you already know, the SPI device CS signal is normally low during the transfer.
The polarity of the enable is dependent on the receiving device, in this case my FPGA design, I can throw an inverter into the FPGA if the absolute polarity is important to the Arduino.
Interesting though that the delay is exactly 8 but cells long. And note there is no delay with the enable going high. So I would expect the same thing going low.
I remember another discussion here about delays in these "digitalWrite" and "digitalRead" functions. Why? This is a very simply thing in .asm.
It looks like the default SPI.write() is a 16bit function and if you send only 8bits, it just leaves the other 8 bit periods blank but still holds the function to 16 bit tines long?
I'll try basic bit-banging on that port and pin.
P.S. The actual application works fine. I just don't like that delay eating into my loop daylight time.
That is a coincidence. You will see that if you change the SPI speed - the delay stays the same. Did you read and try what I supposed in #5 and #6?
There is, but you don't see it at your scope, because the delay created by digitalWrite() is before actually changing the output.
That's what I suggested in #6, direct port manipulation. Then you will see no delay. But digitalWrite does a lot checking and has to calculate the port and bit number from the pin number.
Believe it or believe it not - the delay you see is simply the delay created by digitalWrite.
digitalWrite is doing this translation every time it's called. So it seem this function is a convenience where time is not critical or a tool for beginners. If you want speed or efficiency, use the old method of direct port manipulation.
As we know you have to preserve the state of the port byte if other functions are using other bits so it's a bit tricky for new comers.
Yes of course. But it is completely board specific.
The Arduino IDE supports a lot of boards, and the idea is to support all the boards with the standard functionality. If you use only standard functions, one sketch can be compiled for nearly all the supported boards wothout any change.
That's it.
But you are always free to dive deeper - The Arduino IDE does not prevent you from doing this. But maybe something special to your MCU - e.g. Atmel Studio - is a better choice then.