T3 beta 8 regression?

el_supremo

Well-known member
This Teensy3 code uses the Periodic Interrupt Timer to flash the onboard LED. It works if I run it from beta7 but it doesn't do anything when run from beta8.

Code:
// Pin for LED
#define LEDPIN 13

// Frequency to run the interrupt at
#define FREQ 2 // in Hertz

volatile int ledVal;

// Constants for bitvalues within the TCTRL1 register
#define TIE 2
#define TEN 1
void timer_setup()
{
  // Teensy 3.0 version
  SIM_SCGC6 |= SIM_SCGC6_PIT; // Activates the clock for PIT
  // Turn on PIT
  PIT_MCR = 0x00;
  // Set the period of the timer.  The µC runs at 50MHz
  // So interrupt length can be determined by 50Mhz/FREQ.
  PIT_LDVAL1 = 50000000/FREQ;
  // Enable interrupts on timer1
  PIT_TCTRL1 = TIE;

  NVIC_ENABLE_IRQ(IRQ_PIT_CH1); // Another step to enable PIT channel 1 interrupts
  // Start the timer
  PIT_TCTRL1 |= TEN;
}

void setup()
{
  pinMode(LEDPIN, OUTPUT);
  ledVal = 0;
  digitalWrite(LEDPIN, ledVal);
  timer_setup();
}

void pit1_isr(void)
{
  PIT_TFLG1 = 1; 
  digitalWrite(LEDPIN, ledVal ^= 1);
}

void loop()
{
}

Pete
 
If it's within a .ino or .cpp file, you need this:

Code:
extern "C" void pit1_isr(void)
{
  PIT_TFLG1 = 1; 
  digitalWrite(LEDPIN, ledVal ^= 1);
}

Just add the 'extern "C"' part in your file. No need to edit mk20dx128.h.
 
That generates these errors (with the mod removed from mk20dx128.h):
Code:
interrupt_test_1.ino: In function 'void pit1_isr()':
interrupt_test_1:10: error: previous declaration of 'void pit1_isr()' with 'C++' linkage
interrupt_test_1:39: error: conflicts with new declaration with 'C' linkage

Pete
 
Pete,

I copied this in order to see if I could figure out what was going on. I copied and pasted the code and get nothing blinking. LED stays off. (yes, I did edit the mk20dx128.c file). It appears to complete the routine where you set the timers up (timer_setup), but then nothing happens and it now requires a button reboot to load another sketch. I am running beta 8 on linux x64. When I try to load your sketch, it will not auto-reboot. I have to press the button. To load another sketch, I have to do this again. Once it loads a known running sketch, it will then auto reboot...

Paul,

I tried the suggestion you made before I edited the mk20dx128.c file and got the same error as Pete.

Keith
 
Apologies,

I am a true noob, here. I un-edited the *.c file and edited the *.h file by adding the one line:
void pit1_isr(void); // added by kg
at line 31 and it now blinks *and* the halfkay does not hang.

Now more learning.... Is someone that knows more than I working on porting the timerone library to teensy3? I know it is a 'needs to be done' item and that Paul is extremely busy. I just wonder if anyone else could attempt it?

Keith
 
That generates these errors (with the mod removed from mk20dx128.h):
Code:
interrupt_test_1.ino: In function 'void pit1_isr()':
interrupt_test_1:10: error: previous declaration of 'void pit1_isr()' with 'C++' linkage
interrupt_test_1:39: error: conflicts with new declaration with 'C' linkage

Pete

This has to be in a .cpp or in a .h. If it's in the .ino the Arduino IDE magic preprocessor will pick the function definition up and add an early declaration which lacks the extern "C".
 
I've put the pit1_isr function in its own .cpp file. It now compiles without error but the sketch fails (i.e. doesn't run properly) if the declaration is removed from mk20dx128.h but works correctly when the declaration is there.

Pete
 
For me it works without any change to mk20dx128.h.

pit_isr_simple.ino:
Code:
#include "pit.h"

void setup() {
  Serial.begin(0);
  pit_setup();
}

void loop() {
  Serial.println(count);
  delay(1000);
}
pit.h:
Code:
#include <Arduino.h>

extern volatile uint32_t count;

void pit_setup ();
pit.cpp:
Code:
#include "pit.h"

volatile uint32_t count;

extern "C" void pit0_isr (void) {
  PIT_TFLG0 = 1;
  count++;
}

#define PIT_TCTRL_TIE 2
#define PIT_TCTRL_TEN 1

void pit_setup () {
  count = 0;
  SIM_SCGC6 |= SIM_SCGC6_PIT;
  PIT_MCR = 0;
  NVIC_ENABLE_IRQ(IRQ_PIT_CH0);
  PIT_LDVAL0 = F_BUS/1000; // 1 kHz
  PIT_TCTRL0 = PIT_TCTRL_TIE | PIT_TCTRL_TEN;
}

Kind regards,
Sebastian
 
Back
Top