I have been working with Arduino Uno R4 Wifi and SPI by making a quick SPI speed test and figured out I can't go beyond 5MHz when configuring SPISettings:
Thanks, @Rintin, for stepping in! It was a sampling frequency issue, indeed.
It looks like my Logic 2 software lost its settings after the updates. I was working before with signals in the range of 20MHz without issues. I moved the sampling frequency from 10MS/s to 250MS/s, and now I can exercise the full SPI speed range.
Thanks KurtE, I'll try it.
Yes, I read that thread, but it seems to have no outcome: the Arduino team still hasn't released a new version of the SPI library. However, something is fundamentally wrong with me: the clock frequency is not even 24 MHz, as I expected, but only 4!
I don't have UNO R3, but I don't need it. I'm sure the speed would increase significantly if the clock was at least 24 MHz.
#include <SPI.h>
void setup() {
// put your setup code here, to run once:
SPI.begin();
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
SPI.beginTransaction(SPISettings(20000000, LSBFIRST, SPI_MODE0));
}
uint8_t loop_count = 0;
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(10, LOW);
SPI.transfer(loop_count++);
digitalWrite(10, HIGH);
}
And it is showing me that the clock is bumped up to about 12mhz...
Note: if I changed the main loop to output a few bytes at a time
uint8_t loop_count = 0;
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(10, LOW);
SPI.transfer(loop_count++);
SPI.transfer(loop_count++);
SPI.transfer(loop_count++);
SPI.transfer(loop_count++);
digitalWrite(10, HIGH);
}
You still don't get great throughput with their API
Thank you. It turns out it was enough to write if (!SD.begin(24000000, PIN_SPI_CS))
instead of if (!SD.begin(PIN_SPI_CS))
to increase SCK from 4MHz to 24MHz. By the way, if I wrote the number 20000000, then I also got a frequency of 12 MHz.
However, this did not affect the recording speed in any way, precisely because, as you noticed, there are huge gaps between the packets, and in the case of working with SD they are even larger.
Right now I don't have an alternative to the SD library, at least not one that I can master quickly. I read that I need to use a ring buffer and also allocate space for the file on the memory card in advance, but I'm not ready for that yet...
Where from the SPISetting page you will see: speedMaximum : The maximum speed of communication. For a SPI chip rated up to 20 MHz, use 20000000.
The SPI software will use the number you passed in, to find the best setting of it's internal clock, such that the generated speed does not exceed the value you passed in.
My guess from the above is the SPI clock can run up to 24 MHZ (maybe more), but the possible dividers are that if you ask for anything from 12mhz to under 24, it will give you 12MHZ. 24 to ??? will give you 24... Again maybe 36 works, maybe 48... not sure...