bit shifting and pinout for teensy 3.1

Status
Not open for further replies.

Agge89

New member
I bought a teensy 3.1 so that I could learn a little arm programming. What I would like to do is to program a teensy in a similar way as you could program an AVR. For example to turn on a pin you would write PORTC |= 1<<3; and so on. Is this type of procedure (register shifting) possible on a teensy 3.1? In which case, is there a pinout available (to convert teensys' pins to actual pins of the CPU) and a manual for the CPU?

Best regards
Agge
 
Yes.

First, Teensyduino has some limited emulation of AVR registers with Teensy 3.1. PORTB, PORTC and PORTD work, affecting the same pin numbers as they would on Arduino Uno. The bitwise manipulation of these registers by constants is automatically translated into the equivalent native ARM code. If you want to see the internal details, look in hardware/teensy/cores/teensy3/avr_emulation.h. Normally this stuff is only used when people compile older Arduino sketches with those AVR register names hard-coded.

Second, Teensyduino has digitalWriteFast() and digitalReadFast(), which use the native registers when the inputs are constants. These result in the most optimal code, and you still get the ease of use and readable code of Arduino style sketches.

Third, Teensyduino defines a number of more convenient names for the hardware registers, based on the Arduino pin numbers. Using these isn't as readable as digitalWriteFast(), but still more portable for future boards than hard coding the registers. They're defined in core_pins.h.

The actual hardware registers are documented in chapters 11 and 49 of the reference manual. Chapter 49 covers the actual GPIO registers, which work pretty much the same as AVR's registers, except there's 6 of them instead of just 3. The other 3 give you toggle, set and clear using a direct write, which slightly faster and much safer if an interrupt might also access the register.

Chapter 11 documents the port config stuff. This is where things differ from AVR. In the 8 bit world, usually when you turn on a peripheral, it automatically takes over certain pins. Sometimes the GPIO registers sort-of control the pins, like direction, when using SPI. On ARM, you explicitly control what controls each pin. This adds an extra step, but it gives you far more control, and it's consistent for all peripherals. Many peripherals can have certain signals connected to more than 1 pin. There's a big table at the end of chapter 10 that gives the full list of pin and what stuff each can use. In Teensyduino, calling pinMode() always assigns the pin to GPIO. Serial1.begin(), SPI.begin() and Wire.begin() write to config registers to reassign the pins to those peripherals. It's not automatic, like AVR.

The other big difference from AVR is the pin default to disabled, not to input mode. This has the advantage of preventing the pins from consuming any power, especially if their input voltage "float" to near the logic switching threshold. But it does mean you must configure the pin before you can read.
 
Status
Not open for further replies.
Back
Top