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

Thread: Teensy 4.0 TimerOne Issues

  1. #1
    Junior Member
    Join Date
    Mar 2016
    Posts
    6

    Teensy 4.0 TimerOne Issues

    i've been using the TimerOne Library for awhile now
    I am able to use it on Teensy 3.2, Attiny85 and Atmega328 without a hitch.

    I am just testing out my new Teensy 4.0 and I am having all sorts of weird behavior with the TimerOne library

    is this someting that is being worked on? i know theres a lot going on right now with the 4.0 stuff
    but i figured i'd ask in case there was something i'm not aware of

    I downloaded the latest TimerOne library today. and the latest Teensyduino as well
    both which helped me get further but not all the way

    the function that i attached to TimerOne sort of seems to work
    my DAC chip (MCP4921) outputs sound and values
    but I can't setPeriod and change the timing of the timer
    and nothing else inside my attached function does anything

    the Serial Monitor also stops working
    (it works if i remove TimerOne from my program)
    and I have to push the program button on my Teensy 4.0 everytime I upload
    (this also automatically works like u would expect when I remove TimerOne from my program)

    sorta seems like the Teensy freezes up or something

    anyway just looking for some insight or info that I may not be hip too

    also i've tried a few different things, including IntervalTimer with similar results.
    the only way i could get it to work was to scrap the Timer Routine and just run the function straight from loop()
    but that is not ideal.
    really want to get the Timer Routine working or any type of Interrupt Service Routine for that matter

    thanks yall!
    <#

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,332
    Can a demonstrative sample be posted - minimal and complete to work without extra hardware? Details on timing frequency and such may be an issue.

    Changing the period only happens at the end of a period - but that should work in theory on T4 if it works on T_3.2.

    T_4 does have unique and faster behavior and may need attention to an _isr detail - having a repro sketch would be needed to observe/confirm/correct.

  3. #3
    Junior Member
    Join Date
    Mar 2016
    Posts
    6
    yea i think i can do that except the DAC chip is kinda central to the function but i can try to take that out if thats what u mean by without extra hardware

    now that i've had a lil more time to marinate
    i wonder if this has anything to do w/ a similar problem i had initially with Teensy 3.2 and frequent updates of setPeriod()

    here are some links
    https://forum.pjrc.com/threads/34936...ion-Teensy-3-2
    and i actually used this forked TimerOne library to fix it
    https://github.com/oyeb/TimerOne

    this library contains a different setPeriod() function that has an extra argument of 0 or 1 (i believe)

    i'm kinda looking at the two different libraries now and the new Teensy 4.0 part of the TimerOne library
    guessing its the parts after this line
    #elif defined(__arm__) && defined(TEENSYDUINO) && defined(__IMXRT1062__)

    everything looks way different than the previous teensy so not sureeeee if this new setPeriod function will work as is for T4.0 *shrug*

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,332
    Indeed the " __IMXRT1062__ " signifies the Teensy_4.0 processor.

    As for extra hardware - if it can repro in a simple case just dropped into IDE and run on a nearly bare T_4 that would allow anyone with time/interest to give it a run - so yeah external DAC should be stubbed out if possible.

    It is possible 'seeing' the code would be enough to catch the problem depending on its nature. For USB Serial to stop or freeze the whole T4 seems like an _isr() may be entering fast enough to eat up cycles? Can a limit be added on the change of the setPeriod()?

  5. #5
    Junior Member
    Join Date
    Mar 2016
    Posts
    6
    i'm going to try to limit the speed of updating setPeriod()

    also just messing around quick and it seems like removing an analogRead(A9) line brings back Serial Monitor and automatic uploading

    okay so if i move my analogRead stuff to the loop() everything works as u would expect

    so seems like maybe my issue was with calling analogRead from my TimerOne function ???

    why would that be?

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,332
    analogRead() blocks for some short time doing the read - not sure if it has any other side effects - but apparently as used it is bad to have in an _isr() and that isn't surprising.

    For fast and regular background Analog data reads the ADC library was just updated to support Teensy 4.

  7. #7
    Junior Member
    Join Date
    Mar 2016
    Posts
    6
    yea i just sussed out the new ADC library!
    improved my sample rate immensely

    pretty happy with the results

    thanks so much for your help man
    sometimes i just need to talk it out

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,073
    If you still have that code which didn't work with TimerOne, any chance you could give it a try with the just-released Teensydinno 1.49? TimerOne and TimerThree were both improved for this new release.

    If there's still a problem with 1.49, could post it here? Even if you've moved on, if I can reproduce the problem, maybe these libs can be improved for others who come later...

  9. #9
    Junior Member
    Join Date
    Mar 2016
    Posts
    6
    hey Paul yes i was using 1.49 the whole time

    i'm not at my home computer right now i can post the details when i get home

    but the issue was calling analogRead from within the function that was attached to TimerOne

    a simple line like : value = analogRead(A9);

    once I removed analogRead from the function and placed it in the regular loop()
    everything went back to normal

    i'm going to investigate a little more with the ADC library

    i believe that last night I was able to get analogReadContinuous to work from within the function attached to TimerOne
    but i want to mess around with it a little more today now that i understand a bit more whats going on

  10. #10
    Junior Member
    Join Date
    Mar 2016
    Posts
    6
    okay here is a stripped down version of my code
    i tested it and it is giving me the same behavior as i had with my full program

    the function DDS() is attached to TimerOne
    with the "RAW_AUDIO = analogRead(AUDIO_IN1);" in the DDS() function it freezes up.
    serial monitor stops working. automatic code upload stops working. DDS() function itself doesn't really work

    if u comment out "RAW_AUDIO = analogRead(AUDIO_IN1);" from the DDS() function
    and uncomment the same line in the loop()
    everything functions as normal.

    hope this helps! lemme know if u need anything else from me
    Code:
    //TEENSY 4.0 *************************** *************************** ***************************
    /////////////////////////////////////////////////////////////////////////////////////////////////
    
    //**NEED THIS LIBRARY//FILES (TimerOne & SPI & ADC)
    #include <TimerOne.h>
    
    #define DEBUG
    
    #define AUDIO_IN1 A8
    #define AUDIO_IN2 A9
    #define POT1 A0
    #define CLOCK 12
    
    int adc_offset = 512; //512 10-BIT
    int dac_offset = 2048; //2048 12-BIT
    byte nr = 6; //4 //3
    
    int mix = 0;
    int AUDIO_IN = 0;
    int SIDE_IN = 0;
    int FLTR_AUDIO = 0;
    int MSTR_OUT = 2048; //2048 12-BIT
    int RAW_AUDIO = 512; //512 10-BIT
    int SIDE_AUDIO = 512; //512 10-BIT
    
    unsigned long CLOCK_SPEED = CLOCK;
    
    elapsedMillis lastDebugPrint = 0;
    byte led = 13;
    
    unsigned long SR_KNOB;
    
    
    /////SETUP*********************************************************************************
    void setup(void) {
    //INITIALIZE ALL PINs
    //pinMode(led,OUTPUT);
    
      Timer1.initialize(CLOCK); //65 =15.4 70=14.29 //75=13.33
      Timer1.attachInterrupt(DDS); 
      
    //*************************** SERIAL MONITOR ***************************
    #ifdef DEBUG
      Serial.begin(9600);
    #endif
    }
    /////END OF SETUP*********************************************************************************
    
    /////TIMER INTERUPPT*********************************************************************************
    void DDS() {
    // AUDIO INPUT *************************** ***************************
      Timer1.setPeriod(CLOCK_SPEED); //4.0
      RAW_AUDIO = analogRead(AUDIO_IN1); // #1 10-BIT 0 - 1023
      //SIDE_AUDIO = analogRead(AUDIO_IN2); // #2 10-BIT 0 - 1023
      AUDIO_IN=RAW_AUDIO-adc_offset;
      SIDE_IN=SIDE_AUDIO-adc_offset;
      if (AUDIO_IN <= nr && AUDIO_IN >= -nr) AUDIO_IN = 0; //cleaner audio input
      if (SIDE_IN <= nr && SIDE_IN >= -nr) SIDE_IN = 0;
        mix = (AUDIO_IN+SIDE_IN)<<2;
    // END OF (DDS) *********************************************************************************
    }
    
    void loop(void){
      /////SERIAL PRINT ****DEBUG****** ////////////////////////////////////////////
     #ifdef DEBUG
      if (lastDebugPrint >= 200) {
        lastDebugPrint = 0;
        Serial.print("RAW = ");
        Serial.print(RAW_AUDIO);
        Serial.print("  SIDE = ");
        Serial.print(SIDE_AUDIO);
        Serial.print("  MIX = ");
        Serial.print(mix);
        Serial.print("  CLOCK = ");
        Serial.print(CLOCK_SPEED);
        Serial.println();
      }
      #endif
    //*****AUDIO INPUT // ADC*****  
      //RAW_AUDIO = analogRead(AUDIO_IN1); // #1 10-BIT 0 - 1023
      //SIDE_AUDIO = analogRead(AUDIO_IN2); // #2 10-BIT 0 - 1023
    
    //****KNOBS*****
      SR_KNOB = analogRead(POT1) >> 1; // 0 - 511 SAMPLE RATE
      
      CLOCK_SPEED = (SR_KNOB+CLOCK);
      //Timer1.setPeriod(CLOCK_SPEED); //4.0
      
    ////////////////////  
    }
    // END OF LOOP ////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////
    Last edited by Frank B; 01-15-2020 at 07:46 PM. Reason: add code tags

Posting Permissions

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