arduino / arduino/ArduinoCore-samd

Speed up digitalWrite() and etc by using PORT_IOBUS

Open
#354 0 comments 3 reactions 1 assignee Claimed by @Rocketct View on GitHub
Dominant language
C
Stars
502
Forks
740
PR merge metrics
No merged PRs in 30d

Description

The SAMD21 has a separate single-cycle "IOBUS" between the CPU and the PORT peripherals that is significantly faster than accessing the PORTs via the usual AHB/APB buses. digialWrite() on SAMD21 can be sped up approximately 14% (measured on a scope using a tight loop) just by accessing the PORT via PORT_IOBUS instead of via PORT.

This rather trivial patch (also attached) accomplishes this...
The only complication I found is that this requires enabling continuous sampling on input, since the IOBUS will not wait the ~2 cycles needed to synchronize the bus with the pin.

~~~
--- original/cores/arduino/WVariant.h 2016-09-28 06:51:40.000000000 -0700
+++ new/arduino/WVariant.h 2018-08-31 23:00:15.000000000 -0700
@@ -21,6 +21,16 @@
#include
#include "sam.h"

+/*
+ * if our chip has the high-speed IOBUS for GPIO, we can use
+ * that instead of of the normal AHB bus, which would instert
+ * extra wait states.
+ */
+#ifdef PORT_IOBUS
+#undef PORT
+#define PORT PORT_IOBUS
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
--- original/cores/arduino/wiring_digital.c 2017-02-02 06:46:38.000000000 -0800
+++ new/cores//arduino/wiring_digital.c 2018-08-31 22:59:20.000000000 -0700
@@ -30,6 +30,10 @@
return ;
}

+ // enable continuous pin scanning on all pins, so that we can read from
+ // the high speed PORT_IOBUS without getting stale data.
+ PORT->Group[g_APinDescription[ulPin].ulPort].CTRL.reg = 0xFFFFFFFF;
+
// Set pin mode according to chapter '22.6.3 I/O Pin Configuration'
switch ( ulMode )
{
~~~
[PORT_IOBUS.diff.txt](https://github.com/arduino/ArduinoCore-samd/files/2342145/PORT_IOBUS.diff.txt)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.