Forum Rule: Always post complete source code & details to reproduce any issue!
Page 19 of 19 FirstFirst ... 9 17 18 19
Results 451 to 465 of 465

Thread: Teensy 3.x multithreading library first release

  1. #451
    Junior Member
    Join Date
    Apr 2020
    Location
    Germany
    Posts
    16
    Thats a great idea... But i don't know how to do this.
    The only line i know is the one for the interuppt priority (NVIC_SET_PRIORITY).

  2. #452
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    Add
    Code:
    NVIC_SET_PRIORITY(IRQ_SOFTWARE, 16);
    to setup(). 208 is the default.


    If that does not work, can you please post a short example that I can copy&paste to my arduino? I want to find&fix the problem, if it is not the interrupt-priority.

    @Paul we should rename IRQ_SOFTWARE... it's misleading.
    or add a #define IRQ_AUDIO IRQ_SOFTWARE

  3. #453
    Junior Member
    Join Date
    Apr 2020
    Location
    Germany
    Posts
    16
    At a priority of 16 it still clicks like before. I increased the priority all the way to 1,where it only clicks around two times per play.
    But at a priority of 11 my SK6812 led strips startet to freak out (i use them with the ws2812Serial Library from Paul).

    And sending is kind of a problem because the code has around 1800 lines... I could try to send it here or upload it to github.

  4. #454
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,154
    Quote Originally Posted by SomeoneFromGermany View Post
    But at a priority of 11 my SK6812 led strips startet to freak out (i use them with the ws2812Serial Library from Paul).
    I assume you keep in mind that the priority level are in steps of 16 (0 to 15 are the same, followed by 16 to 31, etc.)
    IOW, the lowest 4 bits of the priority number are ignored (or better reserved for future chips)

  5. #455
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    No.. sry, but i'm not going to debug 1800 lines.

  6. #456
    Junior Member
    Join Date
    Apr 2020
    Location
    Germany
    Posts
    16
    No i didn't knew this... But either way the leds freak out.

  7. #457
    Junior Member
    Join Date
    Apr 2020
    Location
    Germany
    Posts
    16
    @Frank B understandable.

  8. #458
    Junior Member
    Join Date
    Apr 2020
    Location
    Germany
    Posts
    16
    Code:
    AudioInputUSB               usb1;
    AudioInputI2S               i2s1;
    AudioMixer4                 LEFTC;
    AudioMixer4                 RIGHTC;
    AudioPlaySdWav              SDwav;
    AudioOutputI2S              i2sout;
    AudioMixer4                 mixer1;
    AudioMixer4                 mixer2;
    AudioAnalyzePeak            peak_L;
    AudioAnalyzePeak            peak_R;
    AudioConnection          patchCord1(usb1,    0, LEFTC,  0);
    AudioConnection          patchCord2(usb1,    1, RIGHTC, 0);
    AudioConnection          patchCord3(i2s1,    0, LEFTC,  1);
    AudioConnection          patchCord4(i2s1,    1, RIGHTC, 1);
    AudioConnection          patchCord5(LEFTC,   0, mixer1, 0);
    AudioConnection          patchCord6(RIGHTC,  0, mixer2, 0);
    AudioConnection          patchCord7(SDwav,   0, mixer1, 1);
    AudioConnection          patchCord8(SDwav,   1, mixer2, 1);
    AudioConnection          patchCord9(mixer1,  0, i2sout, 0);
    AudioConnection          patchCord10(mixer2,  0, i2sout, 1);
    AudioConnection          patchCord11(LEFTC,   0, peak_L, 0);
    AudioConnection          patchCord12(RIGHTC,  0, peak_R, 0);
    
    AudioControlSGTL5000     CHIP;
    
    IntervalTimer FadeSDin;
    IntervalTimer FadeSDout;
    
    //void DebugSermon() {}
    
    class FADECLASS {
      public:
        void in(volatile int ch, volatile float Gain) {
          mixer1.gain(ch, Gain);
          mixer2.gain(ch, Gain);
        }
        void out(volatile int ch, volatile float Gain) {
          mixer1.gain(ch, 1 - Gain);
          mixer2.gain(ch, 1 - Gain);
        }
    };
    FADECLASS Fade;
    
    void SDin() {
      noInterrupts();
      lockT();
      Fade.in(1, G);
      Fade.out(0, G);
      G += 0.1;
      if (G > 1.1) {
        FadeSDin.end();
      }
      unlockT();
      interrupts();
    }
    
    void SDout() {
      noInterrupts();
      lockT();
      Fade.in(0, G2);
      Fade.out(1, G2);
      G2 += 0.1;
      if (G2 > 1.1) {
        delayofchance = millis() - 50;
        delaybegun = true;
        FadeSDout.end();
        SDwav.stop();
      }
      unlockT();
      interrupts();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    void HandleAudio() {
      //Serial.print("Audio: ");
      //Serial.println(threads.id());
      //while (1) {
      unsigned long curr = millis();
      if (SDwav.isPlaying() == false && plays == true && FDOUT == false) {
        noInterrupts();
        lockT();
        SDwav.play(currPlay);
        delay(5);
        plays = false;
        FDOUT = true;
        waitSonder = false;
        G = 0;
        pre = curr;
        FadeSDin.begin(SDin, 500000);
        unlockT();
        interrupts();
      }
    
      if (curr - pre > AudioTime + FadeOffset && FDOUT == true) {
        G2 = 0;
        FadeSDout.begin(SDout, 500000);
        FDOUT = false;
      }
      if (DebugBoot && Debugdelay >= Debugwait) {
        Debugdelay = 0;
        //DebugSermon();
      }
      threads.yield();
      //}
    }
    This is the code that handles the audio (There are quite a few unnecessary things but i wanted to try something with classes)
    Maybe this helps....

    This is for locking the threads:
    Code:
    void lockT() {
      for (int i = 0; i <= 4; i++) {
        if (i != threads.id()) {
          threads.suspend(i);
        }
      }
    }
    
    void unlockT() {
      for (int i = 0; i <= 7; i++) {
        if (i != threads.id()) {
          threads.restart(i);
        }
      }
    }

  9. #459
    Junior Member
    Join Date
    Apr 2020
    Location
    Germany
    Posts
    16
    Is there an other thing i could try beside changing the interrupt priority?

  10. #460
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,154
    Quote Originally Posted by SomeoneFromGermany View Post
    Is there an other thing i could try beside changing the interrupt priority?
    Restart from basics
    get audio playback example to work, which, by design, will work without threads (All is interrupt driven)
    Now, introduce threads, that must run on a lower interrupt priority (higher number)
    Obviously the treads are not allowed to interfere with audio (e.g. disable interrupts, reading, writing to microSD, etc)
    etc.etc

  11. #461
    Junior Member
    Join Date
    Apr 2020
    Location
    Germany
    Posts
    16
    Ok, Thanks.
    I will try this in the next few days. If i find an other bug that i can not solve by myself i will snitch your time again .

  12. #462
    Junior Member
    Join Date
    Apr 2020
    Location
    Germany
    Posts
    16
    Oh, well sorry for disturbing... I'm just stupid.
    I had an sd card extension in use. I took it out and it worked like a charm. XD

  13. #463
    Junior Member
    Join Date
    Mar 2021
    Posts
    1
    Hi,

    what is the correct way of terminating a thread from inside the thread? Is a "return" adequate?
    The doku says:
    All threads start immediately and run until the function terminates (usually with a return).
    Once a thread ends because the function returns, then the thread will be reused by a new function.
    What does "reused by a new function" mean? Which new function? And what if there is no new function?

    And my second question: Instead of rebooting the entire microcontroller I would like to restart one thread only. What is the best way of doing that?

    Kind regards
    Wolfgang

  14. #464
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,700
    just make sure the thread exits the scope, return or not.
    you know the thread needs a while loop to remain always running, the moment that while loop exits, the thread exits, and the thread stops as it exits scope. so if you wanted to restart a thread, exit it first then restart it again later on in your code

    you can use a volatile variable to break the while loop as well, if your code decides to end the thread, you can always if ( var ) return; (or break the loop so the thread exits. then you can start it again later on (have the thread unset the volatile variable right before the while loop begins)

  15. #465
    Junior Member
    Join Date
    Dec 2020
    Posts
    11

    Class method thread

    When I compile the following code (platformIO) I get the following error message:
    Code:
    lib/Test/test.cpp: In member function 'void Test::begin(HardwareSerial*)':
    lib/Test/test.cpp:20:46: error: invalid use of non-static member function
       readThreadID = threads.addThread(readSerial);
                                                  ^
    lib/Test/test.cpp:21:46: error: invalid use of non-static member function
       processThreadID = threads.addThread(process);
                                                  ^
    Compiling .pio/build/teensy41/FrameworkArduino/yield.cpp.o
    *** [.pio/build/teensy41/libeec/Test/test.cpp.o] Error 1
    In post # 292 it appears to me that the methods BaseControl::readSerialData and BaseControl:rocessBaseCommand are not static methods they both access class variables.

    Any insight would be helpful. Iím just starting to try and understand how to use threads

    Code:
    main.cpp
    
    #include <Arduino.h>
    #include <test.h>
    
    #define UART Serial5
    #define BAUD 9600
    
    HardwareSerial myUART = HardwareSerial(UART);
    
    Test myTest;
    
    void setup() {
      delay(5000);
      Serial.begin(115200);
      while (!Serial); //Wait for user to open terminal
      Serial.println("Setup:");
      
      myUART.begin(BAUD);  // void return
      delay(1000);
      myTest.begin(&myUART);
      
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    }
    Code:
    test.h
    
    #pragma once
    
    #include <Arduino.h>
    #include "circular_buffer.h"
    
    
    // Test -----------------------------------------------------------------------
    class Test{
    public:
    	Test(void);
      
      // Methods ------------------------------------------------------------------
      void begin(HardwareSerial *hws);
    
    private:
      void readSerial();
      void process();
      Circular_Buffer<uint8_t, 256> readCB;   // Size must be power of 2
      HardwareSerial *hwsPort;
     
      uint8_t readThreadID;
      uint8_t processThreadID;
      uint64_t nBytesRead = 0;  
    };
    Code:
    test.cpp
    #include <Arduino.h>
    
    #include <test.h>
    #include <TeensyThreads.h>
    
    Threads::Mutex hwSerial5;
    
    // ----------------------------------------------------------------------------
    Test::Test(void){
      pinMode(LED_BUILTIN, OUTPUT);
      digitalWriteFast(LED_BUILTIN, HIGH);
      delay(1000);
      digitalWriteFast(LED_BUILTIN, LOW);
    }
    
    // Initialize the Serial port -------------------------------------------------
    void Test::begin(HardwareSerial *serial){
      hwsPort  = serial; 
      readThreadID = threads.addThread(readSerial);
      processThreadID = threads.addThread(process);
      
    } // begin(serial)
    
    
    void Test::readSerial(){
      uint8_t chr;
      digitalWriteFast(LED_BUILTIN, LOW);
      
      while (true){  
        {Threads::Scope scope(hwSerial5);
        while (hwsPort->available() > 0){
          chr = hwsPort->read();
          readCB.write(chr);
          nBytesRead++;
          if (readCB.available() == 0)
            digitalWriteFast(LED_BUILTIN, HIGH);
        } // available
        } // scope 
      } // true  
    } // readSerial
    
    
    void Test::process(){
      uint8_t chr;
      
      {Threads::Scope scope(hwSerial5);  // protecting readCB
      while (readCB.available() > 0){
        chr = readCB.read();
        Serial.printf("%02X \n", chr);  // place holder for someting more intresting
      } // available
      } // scope   
    } //process
    thanks
    dk23

Posting Permissions

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