Direct port access in Arduino IDE

Hello,

just received my first XIAO board and I’m quite impressed with its performance so far.

As I want to use it to manipulate an incoming 800 Hz PWM signal (connected to an input pin set up for trggering pin change interrupts) I’m looking for a way to access (i.e. read from and write to) the digital IO pins directly without using digitalRead/digitalWrite.

Is there a similar approach as for Arduinos where one can use
for example PORTB = PINB | B00100000 to set a bit?

Thanks and best regards,
Stefan

Something like this:

EPortType port = g_APinDescription[ulPin].ulPort; 
 uint32_t pin = g_APinDescription[ulPin].ulPin; 
 uint32_t pinMask = (1ul << pin); 
PORT->Group[port].OUTCLR.reg = pinMask;

PORT->Group[port].OUTCLR.reg = pinMask; //LOW

PORT->Group[port].OUTSET.reg = pinMask; //HIGH

Hello ansonhe97,

thanks for the feedback, just tested the proposed code and it executes ~ 1 us faster than digitalWrite.
Going to explore the speed up potential some more.

All the best,
Stefan

1 Like

Hello Stefan_69,

Can you present here piece of your code where you are using this approach in order to make your code faster?
The proposed code is somehow unclear for me to understand…

regards,
s.micic

Hello s.micic,

sorry for the late response, was quite busy at work … Anyhow, I’ll paste my code below, as mentioned in the original post I want to modify the duty cylce of an incoming PWM signal while keeping its frequency constant. You will find both methods (“Arduino style” and “fast style”) for setting the GPIO pin.

If you are using the Arduino IDE you can also have a look at “wiring_digital.c” which can be found under “AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.7.9\cores\arduino”

Hope this helps,
Stefan

… sorry, forgot to include the option for “fast reading” the GPIO pin (used in the if statement):