SPI clock on Arduino DUE

Hi All,

I am running a test program to test out the SPI programming to test my ADC circuit, however I could not get the SPI clock to run on my desired speed. The code is as follows, I am using the suggested way to perform a SPI transfer. The same method to call SPI is working perfectly on UNO R3, but on DUE the clock speed still staying at the default 4MHz. Is there any specific way to control the clock speed on DUE?

#include <SPI.h>

const int speedmax = 48000000; //set clock speed at 48MHz

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  SPI.begin(10);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
   for(byte wiper_value = 0; wiper_value <= 65535; wiper_value++) {
    Serial.println(wiper_value);
    SPI.beginTransaction(SPISettings(speedmax, MSBFIRST, SPI_MODE3));
    SPI.transfer16(10, wiper_value);      // send a new wiper value    
    SPI.endTransaction();
    Serial.println(wiper_value); // read the value from analog pin A0 and send it to serial
    
    delay(500); 
  }

}

PS: I tried using the old, phase out method and it works, but I would like to use the suggested method to build my program.

Did you read this?

If you are looking for speed, try a serial baud rate higher than 9600.

Edit: Try this:
speedMaximum: The maximum speed of communication. For a SPI chip rated up to 20 MHz, use 20000000.

Is the baud rate affecting the SPI clock speed? I thought the serial included in my code is just for serial monitoring on the PC use.

For the code.divider function, I am aware of this but seems Arduino already phased out this function and not recommending us to use in new projects.

Unless things have changed, the Serial.print() function is blocking once the serial tx buffer is full.
On SPI, try a speed lower than 48MHz but higher than 4MHz. Maybe 24?

Edit: ...and the code will stay in the Serial.print() function until there is enough room in the tx buffer for the entire string.

I had tried SPI.setClockDivider() and it works at 48MHz without removing the Serial functions, but there is a problem, between each 8bits transferred there's a relatively huge gap despite I am using SPI.transfer16. Do you have any suggestions to reduce the gap between each byte?
This was already removed the serial communication parts.

I need an effective clock rate of 42MHz or higher for my applications, so I could not reduce the clock rate further.

Is that the output of the code in the OP?
If so, why is this in your loop?

   delay(500); 

Edit: That was a hint. Post your new code.

The new code is just the same code on the post omitted all the serial related codes(was away from PC at the moment), nothing was changed.

The 500ms delay is just to let it repeat every 0.5 seconds such that it won't be too fast to check the output on my scope.

My new code is as below, the SPI waveform is still similar to the photo above, there's a 1us gap between each bytes in a single transaction.

#include <SPI.h>

const int speedmax = 48000000; //set clock speed at 48MHz

void setup() {
  // put your setup code here, to run once:
  
  SPI.begin(10);
  SPI.setClockDivider(10,2);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
   for(byte wiper_value = 0; wiper_value <= 65535; wiper_value++) {
    SPI.beginTransaction(SPISettings(speedmax, MSBFIRST, SPI_MODE3));
    SPI.transfer16(10, wiper_value);      // send a new wiper value    
    SPI.endTransaction();

    
  }

}

Could be the loop and for() processing. Try this as a test:

void loop() {
   int wiper_value = 65535;

   while(1) {
      SPI.beginTransaction(SPISettings(speedmax,MSBFIRST,SPI_MODE3));
      SPI.transfer16(10,wiper_value);
      SPI.endTransaction();
   }
)

Edit: Why are you using a byte type rather than int?

The actual data to be sent to the system will be in byte form. However, I would try to code with your suggestions and come back to you.

If the data is a byte, why are you sending 16 bits?

It should be a 16 bit binary data, I don't know what to define it in the code.

In your for() loop, change "byte wiper_value" to "int wiper_value".
How did the test go?

Theres no change on the output, same that there's a 1us gap between the first and second 8 bits, same for changing the code to both int and replace the for loop with a while(1) loop, there is still no sign of the 16bits are transferred without a gap.

Take a look here. Since you are using the 16 transfer, pay attention to lines 225 and 226, 229 and 230, 234 and 235, and again lines 238 and 239. Those are delays.