1

I've seen people use code like PORTB |= 0x1 << 6 to set the sixth bit of PORTB in arduino. I understand that this is significantly faster than using digitalWrite. How can this be done using a modern Teensy (i.e. 3.6 or 4.0) if at all?

  • 2
    Step one: Read the datasheet for the chip on the board. Step two: implement what you have learned from step 1. Step three: profit. – Majenko Feb 10 at 22:15
  • @Majenko one slight problem with that: how do I know which pins correspond to which ports? Also, I find the datasheets like that very confusing so there's no guarantee that I'd be able to figure out exactly what's going on. – Jachdich Feb 10 at 22:21
  • 1
    You need to relate the information in the datasheet to the source code for the core and board definition. – Majenko Feb 10 at 22:21
  • For the Teensy 3.6 you need to get to grips with section 63.3 of nxp.com/docs/en/reference-manual/K66P144M180SF5RMV2.pdf – Majenko Feb 10 at 22:25
  • DDRA is equivalent to GPIOA_PDDR. PORTA is GPIOA_PDOR, PINA is GPIOA_PDIR – Majenko Feb 10 at 22:28
4

All Teensies support digitalWriteFast out of the box. digitalWriteFast compiles to just setting the bit in the right port register. You can not do that faster by direct register manipulation.

However, if for some reason you do want to manipulate the registers directly here the pin/gpio relation for the T4. (Generated by the sketch described here: https://github.com/luni64/TeensyTimerTool/wiki/Avoid-PWM-timer-clashes")

Pin |  GPIO Reg  |  PWM timer
----|------------|-------------
00  |  GPIO6_03  |  FLEX_PWM1 
01  |  GPIO6_02  |  FLEX_PWM1 
02  |  GPIO9_04  |  FLEX_PWM4 
03  |  GPIO9_05  |  FLEX_PWM4 
04  |  GPIO9_06  |  FLEX_PWM2 
05  |  GPIO9_08  |  FLEX_PWM2 
06  |  GPIO7_10  |  FLEX_PWM2 
07  |  GPIO7_17  |  FLEX_PWM1 
08  |  GPIO7_16  |  FLEX_PWM1 
09  |  GPIO7_11  |  FLEX_PWM2 
10  |  GPIO7_00  |  QUAD1     
11  |  GPIO7_02  |  QUAD1     
12  |  GPIO7_01  |  QUAD1     
13  |  GPIO7_03  |  QUAD2     
14  |  GPIO6_18  |  QUAD3     
15  |  GPIO6_19  |  QUAD3     
16  |  GPIO6_23  |  no pwm    
17  |  GPIO6_22  |  no pwm    
18  |  GPIO6_17  |  QUAD3     
19  |  GPIO6_16  |  QUAD3     
20  |  GPIO6_26  |  no pwm    
21  |  GPIO6_27  |  no pwm    
22  |  GPIO6_24  |  FLEX_PWM4 
23  |  GPIO6_25  |  FLEX_PWM4 
24  |  GPIO6_12  |  FLEX_PWM1 
25  |  GPIO6_13  |  FLEX_PWM1 
26  |  GPIO6_30  |  no pwm    
27  |  GPIO6_31  |  no pwm    
28  |  GPIO8_18  |  FLEX_PWM3 
29  |  GPIO9_31  |  FLEX_PWM3 
30  |  GPIO8_23  |  no pwm    
31  |  GPIO8_22  |  no pwm    
32  |  GPIO7_12  |  no pwm    
33  |  GPIO9_07  |  FLEX_PWM2 
34  |  GPIO8_15  |  FLEX_PWM1 
35  |  GPIO8_14  |  FLEX_PWM1 
36  |  GPIO8_13  |  FLEX_PWM1 
37  |  GPIO8_12  |  FLEX_PWM1 
38  |  GPIO8_17  |  FLEX_PWM1 
39  |  GPIO8_16  |  FLEX_PWM1 

#### Sorted by PWM timer
Pin |  GPIO Reg  |  PWM timer
----|------------|-------------
12  |  GPIO7_01  |  QUAD1     
11  |  GPIO7_02  |  QUAD1     
10  |  GPIO7_00  |  QUAD1     
13  |  GPIO7_03  |  QUAD2     
18  |  GPIO6_17  |  QUAD3     
15  |  GPIO6_19  |  QUAD3     
14  |  GPIO6_18  |  QUAD3     
19  |  GPIO6_16  |  QUAD3     
24  |  GPIO6_12  |  FLEX_PWM1 
01  |  GPIO6_02  |  FLEX_PWM1 
25  |  GPIO6_13  |  FLEX_PWM1 
34  |  GPIO8_15  |  FLEX_PWM1 
35  |  GPIO8_14  |  FLEX_PWM1 
36  |  GPIO8_13  |  FLEX_PWM1 
37  |  GPIO8_12  |  FLEX_PWM1 
38  |  GPIO8_17  |  FLEX_PWM1 
39  |  GPIO8_16  |  FLEX_PWM1 
00  |  GPIO6_03  |  FLEX_PWM1 
08  |  GPIO7_16  |  FLEX_PWM1 
07  |  GPIO7_17  |  FLEX_PWM1 
09  |  GPIO7_11  |  FLEX_PWM2 
04  |  GPIO9_06  |  FLEX_PWM2 
05  |  GPIO9_08  |  FLEX_PWM2 
06  |  GPIO7_10  |  FLEX_PWM2 
33  |  GPIO9_07  |  FLEX_PWM2 
28  |  GPIO8_18  |  FLEX_PWM3 
29  |  GPIO9_31  |  FLEX_PWM3 
23  |  GPIO6_25  |  FLEX_PWM4 
22  |  GPIO6_24  |  FLEX_PWM4 
03  |  GPIO9_05  |  FLEX_PWM4 
02  |  GPIO9_04  |  FLEX_PWM4 
27  |  GPIO6_31  |  no pwm    
17  |  GPIO6_22  |  no pwm    
26  |  GPIO6_30  |  no pwm    
30  |  GPIO8_23  |  no pwm    
31  |  GPIO8_22  |  no pwm    
32  |  GPIO7_12  |  no pwm    
21  |  GPIO6_27  |  no pwm    
20  |  GPIO6_26  |  no pwm    
16  |  GPIO6_23  |  no pwm    

#### Sorted by GPIO register:        
Pin |  GPIO Reg  |  PWM timer
----|------------|-------------
01  |  GPIO6_02  |  FLEX_PWM1 
00  |  GPIO6_03  |  FLEX_PWM1 
24  |  GPIO6_12  |  FLEX_PWM1 
25  |  GPIO6_13  |  FLEX_PWM1 
19  |  GPIO6_16  |  QUAD3     
18  |  GPIO6_17  |  QUAD3     
14  |  GPIO6_18  |  QUAD3     
15  |  GPIO6_19  |  QUAD3     
17  |  GPIO6_22  |  no pwm    
16  |  GPIO6_23  |  no pwm    
22  |  GPIO6_24  |  FLEX_PWM4 
23  |  GPIO6_25  |  FLEX_PWM4 
20  |  GPIO6_26  |  no pwm    
21  |  GPIO6_27  |  no pwm    
26  |  GPIO6_30  |  no pwm    
27  |  GPIO6_31  |  no pwm    
10  |  GPIO7_00  |  QUAD1     
12  |  GPIO7_01  |  QUAD1     
11  |  GPIO7_02  |  QUAD1     
13  |  GPIO7_03  |  QUAD2     
06  |  GPIO7_10  |  FLEX_PWM2 
09  |  GPIO7_11  |  FLEX_PWM2 
32  |  GPIO7_12  |  no pwm    
08  |  GPIO7_16  |  FLEX_PWM1 
07  |  GPIO7_17  |  FLEX_PWM1 
37  |  GPIO8_12  |  FLEX_PWM1 
36  |  GPIO8_13  |  FLEX_PWM1 
35  |  GPIO8_14  |  FLEX_PWM1 
34  |  GPIO8_15  |  FLEX_PWM1 
39  |  GPIO8_16  |  FLEX_PWM1 
38  |  GPIO8_17  |  FLEX_PWM1 
28  |  GPIO8_18  |  FLEX_PWM3 
31  |  GPIO8_22  |  no pwm    
30  |  GPIO8_23  |  no pwm    
02  |  GPIO9_04  |  FLEX_PWM4 
03  |  GPIO9_05  |  FLEX_PWM4 
04  |  GPIO9_06  |  FLEX_PWM2 
33  |  GPIO9_07  |  FLEX_PWM2 
05  |  GPIO9_08  |  FLEX_PWM2 
29  |  GPIO9_31  |  FLEX_PWM3 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.