Port interrupt programming in Teensy 3.5

Status
Not open for further replies.

t-bird

New member
Hi All
I am trying to get this propeller balancer https://github.com/QuadTinnakon/Arduino-Balance-a-Brushless-Motor-and-Propeller-Balancing-

Working in Teensy 3.5.

The following lines are written for the Mega which need to be converted to Teensy speak.

//DDRK &= ~(1<<0); PORTK |= (1<<0); PCMSK2 |= (1<<0); PCICR |= (1<<2);

//ISR(PCINT2_vect){if(PINK&(1<<0))ISRspeed();}

PJRC have explanations of the Interrupts on https://www.pjrc.com/teensy/interrupts.html

But I can't get it to work. There are only a few examples of Port manipulation for interrupts on the net.

Can someone explain step by step how this is converted ?
 
No need to struggle with direct port manipulation since the T3.5 is so much quicker than the Mega. Rewrite the code using attachInterrupt() and corresponding callback functions.
 
Yes I did that
pinMode(3, INPUT_PULLDOWN);
attachInterrupt(digitalPinToInterrupt(3),ISRspeed, RISING);
NVIC_SET_PRIORITY(IRQ_PORTA, 0);

But I need warp speed
 
But I need warp speed

Ok then.... (though I'm skeptical it really matters when the hardware is a brushless motor that can't possibly change its magnetic fields anywhere near this ~100 nanoseconds time scale)

You can get the fastest possible using digitalReadFast(pin), so at least that part is easy. If the pin number is a constant, this compiles to the native register access.

For the interrupt, you can get it with ordinary attachInterrupt(), than then after attachInterrupt has done the setup work, you can directly commandeer the interrupt vector with attachInterruptVector(interrupt, function). This is defined in kinetis.h, so look there for details. You'll need to figure out which of the 5 actual native port interrupts your pin uses. The easiest way to find the native port/pin name is the schematic.

Your native interrupt function MUST manually clear the interrupt flag in the port config register (chapter 10 in the manual). If you don't use the normal attachInterrupt function which does this for you, then your code is responsible. This flag isn't cleared automatically by hardware. If the interrupt code doesn't clear it before returning, your interrupt code will become an infinite loop, blocking all other lower priority interrupts.

If you want the absolute fastest response time, also use NVIC_SET_PRIORITY(interrupt, 0) to give your interrupt the highest priority. Lower numbers are higher priority. By default, nothing is higher than 32, so if you give yours 0, it will interrupt all other interrupts, except of course when code has disabled all interrupts.

Also use FASTRUN on your interrupt function, so the compiler puts it into the zero wait state RAM. By default, functions go into the flash memory.

If you *really* want to dive into extreme ARM optimizations, I highly recommend Joseph Yiu's Definitive Guide book. It covers many important details of ARM Cortex-M that matter greatly for maximum optimization. You can find a link on the datasheets page.

https://www.pjrc.com/teensy/datasheets.html
 
Last edited:
Thank you Paul.

I have already used NVIC_SET_PRIORITY(IRQ_PORTA, 0); which I got from one of your previous post.

I will look at the FASTRUN function.
 
Status
Not open for further replies.
Back
Top