Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Teensy 3.1 - fastest digitalWrite? Is port manipulation still the way to go?

  1. #1

    Teensy 3.1 - fastest digitalWrite? Is port manipulation still the way to go?

    On the Arduino, for fast I/O you write to the port directly, bypassing the friendly/safe/slow digitalWrite - does this still apply to the Teensy 3.1 / LC?

    I gather Paul's made the PORTn stuff work with Teensys, but is it still fastest?

    h

  2. #2
    Senior Member mortonkopf's Avatar
    Join Date
    Apr 2013
    Location
    London, uk
    Posts
    934
    There is also the digitalWriteFast() function as well as port control. See the discussion https://forum.pjrc.com/threads/23431...-using-IO-pins

  3. #3
    Thanks! (That post should be stickied or stuck in the tips thread

  4. #4
    Senior Member
    Join Date
    Dec 2014
    Posts
    310
    My understanding is that you'll always do best with digitalWriteFast. If possible, I believe it compiles to a macro for that port (without you having to do the register legwork and reading). If the pin is variable, it'll compile to a regular digitalWrite.

    I believe there are also other 'fast' functions available, such as digitalReadFast.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,452
    Quote Originally Posted by howiemnet View Post
    I gather Paul's made the PORTn stuff work with Teensys, but is it still fastest?
    Yes, there's code which replaces AVR PORTn single-bit logical and / or operations with native digitalWriteFast(), which is the fastest way.

  6. #6
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    4,071
    Just to be clear, I believe digitalWriteFast is only fast when the pin number is constant. If it isn't constant, then it falls back to use the general library interface. Unfortunately if you doing abstractions and such, the pin number is often an argument to the constructor, and it becomes non-constant within the abstraction. You should declare pins using the const keyword to make sure the compiler knows they are constant.

  7. #7
    Senior Member Jp3141's Avatar
    Join Date
    Nov 2012
    Posts
    486
    Is there any difference between using #define or const for pin definitions ?

  8. #8
    Senior Member mortonkopf's Avatar
    Join Date
    Apr 2013
    Location
    London, uk
    Posts
    934
    Quote Originally Posted by howiemnet View Post
    On the Arduino, for fast I/O you write to the port directly, bypassing the friendly/safe/slow digitalWrite - does this still apply to the Teensy 3.1 / LC?

    I gather Paul's made the PORTn stuff work with Teensys, but is it still fastest?

    h
    Either way, you can easily access port manipulation such as for PORT B: #define RGB_OUT GPIOB_PDOR, and then RGB_OUT = value; where value will be the binary that corresponds to your PORT pins, this could also be given a Hex description for example 0x16 which would be B10000 meaning only the PORT pin at 4 would go High. (counting zero as a value).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •