digitalWriteFast - Teensy 2 and 3.2...

Status
Not open for further replies.

KurtE

Senior Member+
Sorry, this has probably been asked before, but was playing around with a Teensy 2 and found that digitalWriteFast is not defined for a Teensy 2. Windows 10, Arduino 1.6.9, Beta Teensyduino...

Example:

Code:
void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWriteFast(13, HIGH);
  delay(500);
  digitalWriteFast(13, LOW);
  delay(500);
}
Compiles fine for 3.2, but change board type to Teensy 2 and it fails...

Tried including a version of digitalWriteFast I had on my machine and it fails to compile as well:
#include <digitalWriteFast.h>

Code:
Arduino: 1.6.9 (Windows 10), TD: 1.29-beta1, Board: "Teensy 2.0, Serial, 16 MHz, US English"

Build options changed, rebuilding all
In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy/WProgram.h:19:0,

                 from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy/Arduino.h:1,

                 from C:\Users\Kurt\AppData\Local\Temp\build76549ddd49c526a7c849f11ae1a19cc0.tmp\sketch\foo.ino.cpp:1:

foo: In function 'void loop()':
C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:124:59: error: '__digitalPinToBit' was not declared in this scope

 if ( (int)(A) < 0x40) { bitWrite(*(A), __digitalPinToBit(P), (V) );}  \

                                                           ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy/wiring.h:88:49: note: in definition of macro 'bitSet'

 #define bitSet(value, bit) ((value) |= (1UL << (bit)))

                                                 ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:124:25: note: in expansion of macro 'bitWrite'

 if ( (int)(A) < 0x40) { bitWrite(*(A), __digitalPinToBit(P), (V) );}  \

                         ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:136:59: note: in expansion of macro '__atomicWrite__'

 if (__builtin_constant_p(P) && __builtin_constant_p(V))   __atomicWrite__((uint8_t*) digitalPinToPortReg(P),P,V) \

                                                           ^

C:\Users\Kurt\Documents\Arduino\foo\foo\foo.ino:10:3: note: in expansion of macro 'digitalWriteFast'

   digitalWriteFast(13, HIGH);

   ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:128:35: error: '__digitalPinToBit' was not declared in this scope

 bitWrite(*(A), __digitalPinToBit(P), (V) );                   \

                                   ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy/wiring.h:88:49: note: in definition of macro 'bitSet'

 #define bitSet(value, bit) ((value) |= (1UL << (bit)))

                                                 ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:128:1: note: in expansion of macro 'bitWrite'

 bitWrite(*(A), __digitalPinToBit(P), (V) );                   \

 ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:136:59: note: in expansion of macro '__atomicWrite__'

 if (__builtin_constant_p(P) && __builtin_constant_p(V))   __atomicWrite__((uint8_t*) digitalPinToPortReg(P),P,V) \

                                                           ^

C:\Users\Kurt\Documents\Arduino\foo\foo\foo.ino:10:3: note: in expansion of macro 'digitalWriteFast'

   digitalWriteFast(13, HIGH);

   ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:124:59: error: '__digitalPinToBit' was not declared in this scope

 if ( (int)(A) < 0x40) { bitWrite(*(A), __digitalPinToBit(P), (V) );}  \

                                                           ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy/wiring.h:88:49: note: in definition of macro 'bitSet'

 #define bitSet(value, bit) ((value) |= (1UL << (bit)))

                                                 ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:124:25: note: in expansion of macro 'bitWrite'

 if ( (int)(A) < 0x40) { bitWrite(*(A), __digitalPinToBit(P), (V) );}  \

                         ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:136:59: note: in expansion of macro '__atomicWrite__'

 if (__builtin_constant_p(P) && __builtin_constant_p(V))   __atomicWrite__((uint8_t*) digitalPinToPortReg(P),P,V) \

                                                           ^

C:\Users\Kurt\Documents\Arduino\foo\foo\foo.ino:12:3: note: in expansion of macro 'digitalWriteFast'

   digitalWriteFast(13, LOW);

   ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:128:35: error: '__digitalPinToBit' was not declared in this scope

 bitWrite(*(A), __digitalPinToBit(P), (V) );                   \

                                   ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy/wiring.h:88:49: note: in definition of macro 'bitSet'

 #define bitSet(value, bit) ((value) |= (1UL << (bit)))

                                                 ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:128:1: note: in expansion of macro 'bitWrite'

 bitWrite(*(A), __digitalPinToBit(P), (V) );                   \

 ^

C:\Users\Kurt\Documents\Arduino\libraries\digitalWriteFast/digitalWriteFast.h:136:59: note: in expansion of macro '__atomicWrite__'

 if (__builtin_constant_p(P) && __builtin_constant_p(V))   __atomicWrite__((uint8_t*) digitalPinToPortReg(P),P,V) \

                                                           ^

C:\Users\Kurt\Documents\Arduino\foo\foo\foo.ino:12:3: note: in expansion of macro 'digitalWriteFast'

   digitalWriteFast(13, LOW);

   ^

Error compiling for board Teensy 2.0.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Not a big deal, I was simply using digitalWriteFast for debug code to watch things using Logic Analyzer.

Can look for newer version of this header file to see if that works, and/or simply use digitalWrite instead, but was wondering if there is preferred way to handle this between these chips...

Edit: I believe my old copy came directly from the old google code: https://code.google.com/archive/p/digitalwritefast/downloads
 
Last edited:
These 8bit AVR processors are so old fashioned. If you aren't a 1990ies retro fan, you should abandon these and catch up with the 32bit world. :)
 
Actually I have maybe 20/30 of the T3.1/3.2s...

But was curious to try compiling some of my Dynamixel AX Buss code for T3.2s to a T2 and try it out, to see how the timing of this using PJRC's code compares to the USB2AX device by Xevellabs that uses an Atmega32u2 with lufa
 
Thanks Paul,

After writing this thread, I looked again at the cores\teensy\core_pins.h file where digitalWrite is defined for the Teensy 2 and it looks to me like you have digitalWriteFast functionality built into digitalWriteFast. So was/am tempted to do add a definition for digitalWriteFast in this file, either like:

Code:
#define digitalWriteFast(pin, val) digitalWrite(pin, val)

or
Code:
static inline void digitalWriteFast(uint8_t, uint8_t) __attribute__((always_inline, unused));
static inline void digitalWriteFast(uint8_t pin, uint8_t val)
{
    digitalWrite(pin, val);
}

Can do and do pull request if that makes sense?
 
Last edited:
Can do and do pull request if that makes sense?

Yeah, the inline function makes sense.

Really, on Teensy 2.0 the digitalWriteFast() function should probably skip the timer stuff on the PWM pins. Not sure if I really want to put that much work into the old code base.
 
I know what you mean, but decided to give it a quick try since I mentioned it...

Did a pull request in case you are interested.
For digitalWriteFast, I made source copy of digitalWrite as for the built in const.. like pin numbers, else it calls off to digitalWrite. So this avoids the timer and PWM stuff.

Also added digitalReadFast, which just calls digitalRead...

Used test sketch:
Code:
void setup()
{
  pinMode(1, OUTPUT);
  pinMode(11, OUTPUT);
  
}

void loop()
{
  digitalWriteFast(1, HIGH);
  delay(200);
  digitalWriteFast(1, LOW);
  delay(200);
  digitalWriteFast(11, !digitalReadFast(11));
}

And looked at generated code:
Code:
000001b8 <loop>:
[B] 1b8:	29 9a       	sbi	0x05, 1	; 5[/B]
 1ba:	68 ec       	ldi	r22, 0xC8	; 200
 1bc:	70 e0       	ldi	r23, 0x00	; 0
 1be:	80 e0       	ldi	r24, 0x00	; 0
 1c0:	90 e0       	ldi	r25, 0x00	; 0
 1c2:	c2 d0       	rcall	.+388    	; 0x348 <delay>
[B] 1c4:	29 98       	cbi	0x05, 1	; 5[/B]
 1c6:	68 ec       	ldi	r22, 0xC8	; 200
 1c8:	70 e0       	ldi	r23, 0x00	; 0
 1ca:	80 e0       	ldi	r24, 0x00	; 0
 1cc:	90 e0       	ldi	r25, 0x00	; 0
 1ce:	bc d0       	rcall	.+376    	; 0x348 <delay>
 [B]1d0:	4e 99       	sbic	0x09, 6	; 9
 1d2:	02 c0       	rjmp	.+4      	; 0x1d8 <loop+0x20>
 1d4:	5e 9a       	sbi	0x0b, 6	; 11
 1d6:	08 95       	ret
 1d8:	5e 98       	cbi	0x0b, 6	; 11
 1[/B]da:	08 95       	ret
Which I think looks OK...

Kurt
 
Status
Not open for further replies.
Back
Top