SAMD51 Feather M4 Express / Speed up DigitalWrite()

I have speed issues with DigitalWrite() for SAMD51.
I'm reading data from the SPI port and use DigitalWrite() pin10 for Chip Select.

It appear that toggling my Chip Select pin take forever to complete and my SPI data stream timeout for that reason.

How can I bypass DigitalWrite() to speed up pin toggling ?

Hi QcTechno,

On the Adafruit Feather M4 digital pin D10 is allocated to the SAMD51's port pin PA20.

To configure the port pin PA20 as an output:

PORT->Group[PORTA].DIRSET.reg = PORT_PA20;

To output a HIGH on the pin:

PORT->Group[PORTA].OUTSET.reg = PORT_PA20;

To output a LOW on the pin:

PORT->Group[PORTA].OUTCLR.reg = PORT_PA20;

To toggle the output pin:

PORT->Group[PORTA].OUTTGL.reg = PORT_PA20;

Thank you for the quick reply.
I removed pinMode() and DigitalWrite() in setup loop.
I replaced DigitalWrite() with your code with no effect.
Pin didn't toggle as expected.

Code compile without issues.

Pin didn't toggle as expected.

Are you sure? I just tested the lines of code in a small sketch on my Feather M4 and it toggles D10:

// D10 Output Test
void setup() {
  PORT->Group[PORTA].DIRSET.reg = PORT_PA20;        // Set D10 to an output
}

void loop() {
  PORT->Group[PORTA].OUTSET.reg = PORT_PA20;    // Toggle D10 HIGH
  delay(500);                                   // Wait for half a second
  PORT->Group[PORTA].OUTCLR.reg = PORT_PA20;    // Toggle D10 LOW
  delay(500);                                   // Wait for half a second
}

Hi QcTechno,

My apologies, one of the lines read PORTB rather than PORTA for the OUTCLR register. I've now corrected that above. Probably the reason why your code didn't toggle the pin.

How long is “forever”? I did some test with a metro M4, and digitalWrite() took “about” 500ns.
(The direct port io can be done in about 30ns)

https://forums.adafruit.com/viewtopic.php?f=57&t=133497#p668379

MartinL:
Hi QcTechno,

My apologies, one of the lines read PORTB rather than PORTA for the OUTCLR register. I've now corrected that above. Probably the reason why your code didn't toggle the pin.

Ok I will try this next monday back at my bench.
I think I find my problem, mostly not related to I/O speed issue but good to know how to do this.

I appreciate your ( super quick ) support