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

Thread: Multithreading on Teensy 3.2?

  1. #1

    Multithreading on Teensy 3.2?

    I am new to Teensy, and started with a 3.2 board, thinking it should offer similar capabilities of STM32 Nucleo-L476 as they are both facilitating the Cortex M4.
    The divide however seems to be large?

    On STM32 I am used to using the HAL library, where it is easy to connect to peripherals and also getting callbacks that will run in independent threads.
    Unfortunately I am not able to find out how to do that with the Teensy 3.2, so if someone could please tell me to find such info?

    In short, I have a main loop that continuously makes decisions dependent on the values from various sensors (temperature, humidity, Hall, light, etc.), and adjust various timer's PWM duty cycles.
    Those sensors may be read during the main loop, but also on external interrupts that calls independent functions.
    Then, I use a UART Rx/Tx to access a running system, and this is also interrupt driven executing in its own threads.

    As all Nucleo boards I have seen are rather large in physical size, I found the Teensy 3.2, which is impressively compact in size, and I thought it would be rather similar to the Nucleo to program.

    Multi-thread wise it seems not so?
    Can anyone please tell me where to look?

  2. #2
    Junior Member
    Join Date
    Jul 2016
    Posts
    12
    There is a multi threading library posted somewhere here on the forum if you really want to go down that route.

  3. #3
    I did look into the "Teensy 3.x multithreading library first release", but it seems very raw and I would prefer not to have to debug that as well as my own code.

    What other route would you advice?

    If I only have one thread, I have to serially poll my sensors and then do my decisions on their values, and adjust my PWM timers, controlling whatever devices I have, and always in the same order.

    Is that how the Arduino platform is normally expected to be used?

  4. #4
    Junior Member
    Join Date
    Jan 2017
    Posts
    17
    Quote Originally Posted by Peter Rasmussen View Post
    Is that how the Arduino platform is normally expected to be used?
    Standard Arduino is coming from 8 bit controllers, where multithreading is a large overhead. So you can't expect that everything is based on multithreading by default.

    The multi threading library is included in the Teensy software. Look under "Teensy Threads" in the library folder. Just look at the examples.

    Maybe this is helpful, too
    https://www.pjrc.com/teensy/td_timin...pedMillis.html
    https://www.pjrc.com/teensy/td_timin...rvalTimer.html
    https://forum.pjrc.com/threads/44723-Arduino-Events

  5. #5
    Thank you for the links, I will look into those.

    I didn't know Arduino from before, but searched for smaller sized boards with Cortex M4 MPU, and when also seeing the Teensy 4.0 with a 600MHz Cortex M7, I would expect the software to have moved on significantly since 8 bit controllers.

  6. #6
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,591
    Moving on significantly since 8 bit controllers is unfortunately often inhibited from the well-meant but inefficient intention to maintain backward compatibility.

  7. #7
    Yes, a pity.
    I started programming in 1981 and has been around since the Z80, and I wouldn't attempt to make software for the newest CPUs backward compatible, which would just underline 'backward'.
    Moving on and cutting old stuff is what Major revision numbers are for, isn't it?

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,068
    Indeed the main focus has been compatibility with Arduino and the large ecosystem of Arduino libraries and examples.

  9. #9
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,909
    Quote Originally Posted by Peter Rasmussen View Post
    What other route would you advice?
    I decided, instead of full-blown multithreading, to exploit the Arm multi-priority interrupt system.
    As my sensor systems lead to a data driven interrupt based processing chain, a lot of work is done in parallel. high speed DMA based acquisition, low-speed (I2C type) data capture and very low priority data archiving (done when no interrupts are served).

  10. #10
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,096
    +1 the interrupt system is really good and easy to use.

  11. #11
    Sounds like the systems I am making.
    Could you point me to a place to start looking at it, with support for a Teensy 3.2?

  12. #12
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,096
    I guess the simplest - as begin - is the intervaltimer.
    You might look at the NVIC macros (NVIC_ENABLE_IRQ etc), vector table, attachInterrupt, pin irqs and software triggered interrupts (every interrupt can be triggered by software)
    For the details, the referencs manual is useful, and a look at the core (search for NVIC and _VectorsRam[] <- vector table in RAM)

    Should be all pretty similiar to the STM as both run with ARM-Cortex - the syntax is different, that's all.
    Somebody ported RTOS - I don't remember who it was.

    And take a look at the Teensy 4 (600MHz, overclock to 1GHz (needs cooling), much RAM and Flash), same size as the Teensy 3.2

  13. #13
    Thank you Frank, I'll have a look.

    I didn't get a reference manual as I just bought the Teensy 3.2 barebones for testing and seeing how easy it is to port my code from using STM32 HAL on Nucleo boards.
    The reason 3.2 looks interesting (for me now) is that it can handle high currents, up to 250mA, but the 4.0 will get my attention later, if I can make an acceptable environment for it.
    Or I might go for a Rasperry Pi to do complex things, and then Teensy to do the peripheral stuff.

    On MCU level, I come from using STM32 CubeMX that takes a load off you, doing all the nitty gritty stuff, so you can concentrate on what you want to do in your code.
    Even on a Nucleo F031K6 with just 4KB RAM and 32KB Flash, you can do some pretty advanced stuff.
    Right now I need something more though, which is why I am looking for a Cortex M4.

  14. #14
    Junior Member
    Join Date
    May 2020
    Posts
    2
    Quote Originally Posted by PaulStoffregen View Post
    Indeed the main focus has been compatibility with Arduino and the large ecosystem of Arduino libraries and examples.
    On one end I agree with that. Yet as you continue to develop with faster and faster processors it starts to become a major hinderance.
    Imagine your PC that could only run arduino. No multitasking. It would be just a total waste.
    With the ARM Cortex-M7 processor at 600 MHz, I cannot see why anyone would not be using threading with that. Even the ESP32 has freeRTOS.

  15. #15
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,096
    Feel free to port it

  16. #16

  17. #17
    "Even the ESP32 has freeRTOS."

    An ESP32-WROOM-32D also has dual-core CPU at 240MHz, 520KB RAM, 448KB ROM for the program, an external 4MB Flash chip, WiFi and Bluetooth, all mounted in one module.
    All for just EUR 3.5, so what's not to like?

Posting Permissions

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