Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 2 1 2 LastLast
Results 1 to 25 of 47

Thread: CircuitPython on Teensy 4!

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    15

    CircuitPython on Teensy 4!

    Thanks to @arturo182's work on the iMX RT series in Python, we can now easily support Teensy 4!

    There are many issues still to work out but a bunch of stuff does work. Check the GitHub label for relevant issues here: https://github.com/adafruit/circuitp...l%3Amimxrt10xx The main one currently is that after loading CircuitPython with the Teensy loader you have to unplug and plug the Teensy back in. That process is only needed to update CircuitPython though. (Expect to see lots of bug fixes in the coming weeks and months.) Changing the Python code is as easy as changing the files on the drive.

    For an intro to CircuitPython check out this guide: https://learn.adafruit.com/welcome-to-circuitpython
    It also has a troubleshooting section that can be helpful: https://learn.adafruit.com/welcome-t...roubleshooting
    The essentials guide covers all of the core APIs: https://learn.adafruit.com/circuitpython-essentials
    The API reference is here: https://circuitpython.readthedocs.io/en/latest/

    Most other guides also have CircuitPython examples as well.
    For real-time help we have a Discord chat (see #help-with-circuitpython and #circuitpython): https://adafru.it/discord
    Adafruit's CircuitPython tech support forum is here: https://forums.adafruit.com/viewforum.php?f=60
    We'll try to follow up here as well.

    As I said, this is all very new code. My working branch is here: https://github.com/tannewt/circuitpython/tree/teensy4
    Once it is merged into mainline (this week most likely) per-commit releases are available on S3: https://adafruit-circuit-python.s3.a...ml?prefix=bin/
    Proper releases are available here: https://circuitpython.org/downloads
    All of our development is done in the open. Mainline is here: https://github.com/adafruit/circuitpython
    We have a weekly meeting about CircuitPython on our Discord. Past meetings are available here: https://www.youtube.com/watch?v=F7YW...5xjWp0ht8yNgg0
    We also have a call for thoughts on CircuitPython in 2020 here: https://blog.adafruit.com/2020/01/01...circuitpython/ See what other's posted here: https://blog.adafruit.com/tag/circuitpython2020/

    Anyway, welcome to the CircuitPython community! We're excited to support Teensy 4 and will answer questions and give help below.


    Moderator Edit: newer code is available on msg #42.
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    May 2015
    Posts
    393
    Thanks! I was wondering how it worked. I didn't think drag and drop would be possible on a Teensy 3.x/4 so I thought the only choice might have been microphyton.

  3. #3
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,495
    Thanks. I posted as a comment on the video how it was done so this answers that question. Did you see Paul's comment on the last 4K of memory: https://forum.pjrc.com/threads/59028...l=1#post226156. Does the current version take this into account?

    Changing the Python code is as easy as changing the files on the drive.
    Ok changing files on the drive? I am assuming that after CircuitPython is installed it appears as a drive in windows. Forgive my ignorance - never used circuit python before.

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,297
    Quote Originally Posted by mjs513 View Post
    Thanks. I posted as a comment on the video how it was done so this answers that question. Did you see Paul's comment on the last 4K of memory: https://forum.pjrc.com/threads/59028...l=1#post226156. Does the current version take this into account?

    Ok changing files on the drive? I am assuming that after CircuitPython is installed it appears as a drive in windows. Forgive my ignorance - never used circuit python before.
    Yep, when you power up an MCU running circuitpython, a disk drive pops up. you can drop/edit .py files on the disk and then execute the .py files.

    you should get an adafruit circuit playground express and/or the M4 feather (SAMD51). You can either run cirucitpython, or use the Arudino IDE to upload your usual sketches. The adafruit SMT32F405 can also run micropython and has microSD.

    another link for the NXP1062 feather (WIP)
    https://circuitpython.org/board/feather_mimxrt1062/
    It has LCD connector, sort of like on NXP EVK boards, and a microSD slot on the bottom side. But the feather footprint doesn't bring out very many pins (ADC, digital, SPI, I2C,UART). it's a proof-of-concept, not an Adafruit product (yet?)
    Last edited by manitou; 01-13-2020 at 10:52 AM.

  5. #5
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,495
    @manitou
    Thanks for the explanation. Will probably have to pick one of the up at some point - too many things on the to do list right now to start playing with that one. Still not sure which way to go - circuitpython or micropython? Not sure if you saw what I posted on another thread about someone ported mircopython at nxp to their 1062 sdk:

    Interesting thing is that they already have a port of micropython for Teensy 3.x series: https://github.com/micropython/micro...sy-3.1-3.5-3.6. Haven't tried it.

    As for the Teensy 4: https://forum.micropython.org/viewtopic.php?t=6783 and this: https://github.com/RockySong/micropython-rocky. RockySong is from NXP and ported it to the 1050 and 1060. So if anyone is interested

  6. #6
    Junior Member
    Join Date
    Jul 2019
    Posts
    15
    Quote Originally Posted by mjs513 View Post
    Thanks. I posted as a comment on the video how it was done so this answers that question. Did you see Paul's comment on the last 4K of memory: https://forum.pjrc.com/threads/59028...l=1#post226156. Does the current version take this into account?

    Ok changing files on the drive? I am assuming that after CircuitPython is installed it appears as a drive in windows. Forgive my ignorance - never used circuit python before.
    No, this version doesn't account for the 4k block. It's probably not an issue though because FAT likely won't use it until it's filled up. I'll keep working on the linker scripts for it tomorrow and account for it then.

    Quote Originally Posted by mjs513 View Post
    @manitou
    Thanks for the explanation. Will probably have to pick one of the up at some point - too many things on the to do list right now to start playing with that one. Still not sure which way to go - circuitpython or micropython? Not sure if you saw what I posted on another thread about someone ported mircopython at nxp to their 1062 sdk:
    I'd recommend trying both MicroPython and CircuitPython. They both have their strengths. :-) You don't have to pick just one.

  7. #7
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,297
    OK, I fetched the zip file and loaded the .hex on to one of my T4's. CIRCUITPY disk popped up as expected. terminal/REPL on /dev/ttyACM0. copied my blink.py on to CIRCUITPY disk and import blink.py from REPL ... and sure enough, i have a blinking LED on my T4. here's blink.py
    Code:
    import board
    import digitalio
    import time
    
    led = digitalio.DigitalInOut(board.D13)
    led.direction = digitalio.Direction.OUTPUT
    
    while True:
        led.value = True
        time.sleep(0.5)
        led.value = False
        time.sleep(0.5)
    and thankfully i was still able to run IDE and load up a regular sketch on the T4

    nice work

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,077
    Quote Originally Posted by manitou View Post
    it's a proof-of-concept, not an Adafruit product (yet?)
    I'm really curious to see the bottom side.

  9. #9
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,297
    Quote Originally Posted by PaulStoffregen View Post
    I'm really curious to see the bottom side.
    more photos on arturo's twitter feed https://twitter.com/arturo182/status...41134253682690

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,077
    Wow, he did put all the capacitors on the top side!

  11. #11
    Junior Member
    Join Date
    Nov 2019
    Posts
    4
    Thanks for this 1st image of CircuitPython for Teensy 4.0 !

    First tests :
    Code:
    $ screen /dev/ttyACM0
    Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
    
    Press any key to enter the REPL. Use CTRL-D to reload.
    Adafruit CircuitPython 5.0.0-beta.3-6-g926375d99-dirty on 2020-01-10; Teensy 4.0 with IMXRT1062DVJ6A
    >>> import gc
    >>> gc.collect()
    >>> gc.mem_free()
    746976
    >>> import os
    >>> os.uname()
    (sysname='mimxrt10xx', nodename='mimxrt10xx', release='5.0.0', version='5.0.0-beta.3-6-g926375d99-dirty on 2020-01-10', machine='Teensy 4.0 with IMXRT1062DVJ6A')
    >>> help('modules')
    __main__          busio             microcontroller   struct
    _os               collections       micropython       supervisor
    _pixelbuf         digitalio         neopixel_write    sys
    _time             errno             os                time
    analogio          gamepad           pulseio           touchio
    array             gc                random            usb_hid
    bitbangio         io                re                usb_midi
    board             json              rtc
    builtins          math              storage
    Plus any modules on the filesystem
    A USB meter shows 88 mA (at 5.06V) when using REPL on Teensy 4.0 connected to microUSB cable, without any running program.

  12. #12
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,077
    I'm playing with python a bit. Looks like the time.sleep() function may be off by 1 millisecond.

    When I run this I'm expecting to see ~100 Hz on pin 13.

    Code:
    import board
    import digitalio
    import time
    
    led = digitalio.DigitalInOut(board.D13)
    led.switch_to_output()
    
    while True:
            led.value = True
            time.sleep(0.005)
            led.value = False
            time.sleep(0.005)
    But the actual frequency is 125 Hz.

    Click image for larger version. 

Name:	file.png 
Views:	9 
Size:	22.9 KB 
ID:	18734

    I tried several other tests. Looks like the actual delay is 1 millisecond less than it should be.


    I also tried to recreate the USB serial lines/sec benchmark in python code. Not sure if this is really the best way?

    Code:
    import board
    import time
    
    count = 0;
    prior_count = 0;
    count_per_second = 0
    prior_msec = time.monotonic_ns() / 1000000
    
    while True:
            print("count=", count, "lines/sec=", count_per_second)
            count = count + 1
            msec = time.monotonic_ns() / 1000000
            if (msec - prior_msec > 1000):
                    prior_msec = prior_msec + 1000
                    count_per_second = count - prior_count
                    prior_count = count

  13. #13
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,297
    Quote Originally Posted by PaulStoffregen View Post

    I tried several other tests. Looks like the actual delay is 1 millisecond less than it should be.
    that might be a "feature" -- i saw the same thing and using .00501 gets you closer to 5 ms
    Code:
    >>> import time
    >>> t1 = time.monotonic(); time.sleep(0.001); t2 = time.monotonic(); t2-t1
    0.0
    >>> t1 = time.monotonic(); time.sleep(0.00101); t2 = time.monotonic(); t2-t1
    0.000976563
    >>> t1 = time.monotonic(); time.sleep(0.005); t2 = time.monotonic(); t2-t1
    0.00390625
    >>> t1 = time.monotonic(); time.sleep(0.00501); t2 = time.monotonic(); t2-t1
    0.00488281
    >>> t1 = time.monotonic_ns(); time.sleep(0.00501); t2 = time.monotonic_ns(); t2-t1
    5000000
    ? round up?

  14. #14
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,297
    blink.py with no sleep, e.g. circuitypython toggle, use scope on pin 13 to measure period.
    Code:
    circuit playground express  100 us   SAMD21
    M4  21.4 us   SAMD51
    STM32F405  38.4 us
    T4  43.6 us

  15. #15
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,495
    @tannewt

    One of the things I like about the T4 is the ability to overclock if I want directly from the sketch.

    Poking around your repository, there's a lot there so probably missed it, it there a way to change clock speeds for the T4. Also didn't see where you were setting the clock so begs me to ask what's your default clock speed for the T4 right now.

    You did a great job on getting this ported over the T4 so please hope you don't take anything as criticism especially since I haven't even tried it yet.

  16. #16
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,297
    don't take my word for it but i found
    Code:
    ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/clocks.c
                #define BOARD_BOOTCLOCKRUN_CORE_CLOCK             600000000
    so i think circuitpython is trying to run at 600mhz. micropython actually has pyb.freq() to change clock from python, though i don't know if T4 implements that?

    I guess we're drifting away from Teensy topics ...

  17. #17
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,495
    Thanks for pointing me to it - still on first cup of coffee and eyes have closed. Will take a look. Maybe a improvement that can be made to circuit python in the future.

  18. #18
    Junior Member
    Join Date
    Nov 2019
    Posts
    4
    Bugs so far :
    - CircuitPython halts after :
    Code:
    import microcontroller
    microcontroller.cpu.temperature
    - REPL disconnects after pasting more than approx. 4 lines in paste mode (CTRL+E). I have to disconnect from the USB port and connect to REPL work again.

  19. #19
    Junior Member
    Join Date
    Jul 2019
    Posts
    15
    Quote Originally Posted by PaulStoffregen View Post
    I'm playing with python a bit. Looks like the time.sleep() function may be off by 1 millisecond.

    When I run this I'm expecting to see ~100 Hz on pin 13.

    Code:
    import board
    import digitalio
    import time
    
    led = digitalio.DigitalInOut(board.D13)
    led.switch_to_output()
    
    while True:
            led.value = True
            time.sleep(0.005)
            led.value = False
            time.sleep(0.005)
    But the actual frequency is 125 Hz.

    Click image for larger version. 

Name:	file.png 
Views:	9 
Size:	22.9 KB 
ID:	18734

    I tried several other tests. Looks like the actual delay is 1 millisecond less than it should be.


    I also tried to recreate the USB serial lines/sec benchmark in python code. Not sure if this is really the best way?

    Code:
    import board
    import time
    
    count = 0;
    prior_count = 0;
    count_per_second = 0
    prior_msec = time.monotonic_ns() / 1000000
    
    while True:
            print("count=", count, "lines/sec=", count_per_second)
            count = count + 1
            msec = time.monotonic_ns() / 1000000
            if (msec - prior_msec > 1000):
                    prior_msec = prior_msec + 1000
                    count_per_second = count - prior_count
                    prior_count = count
    Weird! I would expect it to be slower than 100Hz because time.sleep() should be pessimistic. For timing sensitive functions we usually use the peripherals via C code instead. For example, PWM will be more accurate through `pulseio.PWMOut`.

    I'm actually impressed that these are the issues you have found so far. It's all very early still. :-)

    Quote Originally Posted by mjs513 View Post
    @tannewt

    One of the things I like about the T4 is the ability to overclock if I want directly from the sketch.

    Poking around your repository, there's a lot there so probably missed it, it there a way to change clock speeds for the T4. Also didn't see where you were setting the clock so begs me to ask what's your default clock speed for the T4 right now.

    You did a great job on getting this ported over the T4 so please hope you don't take anything as criticism especially since I haven't even tried it yet.
    Adding the ability to overclock will come at some point. We'd like it for the SAMD51 as well. (Clocked at 120MHz now but can probably do 180-200MHz.) It's not a high priority though because I want to verify we're being smart about using CPU cache and TCM first. Supporting most of our hardware APIs is a higher priority too because it will enable more of the existing examples.

    If you'd like to take a crack at adding it, let me know. :-) I'm always happy to help folks dig into the code.

    Quote Originally Posted by rcolistete View Post
    Bugs so far :
    - CircuitPython halts after :
    Code:
    import microcontroller
    microcontroller.cpu.temperature
    - REPL disconnects after pasting more than approx. 4 lines in paste mode (CTRL+E). I have to disconnect from the USB port and connect to REPL work again.
    Thanks for finding those! Would you mind creating GitHub issues for them? That is how we keep track of things. Here is the url to do it: https://github.com/adafruit/circuitpython/issues/new

  20. #20
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,077
    Quote Originally Posted by tannewt View Post
    Weird! I would expect it to be slower than 100Hz because time.sleep() should be pessimistic.
    I ran the same code on a Grand Central M4, same 125 Hz waveform.

    Looks like a bug in Circuit Python. Seems the delay seem to be limited to an integer number of milliseconds, but the conversion rounds down to the next lower integer.

    FWIW, Arduino had this exact same bug in delay() about 12 years ago (long before they used Google Code to track bugs, later migrating to github).

    Click image for larger version. 

Name:	DSC_0684_web.jpg 
Views:	6 
Size:	170.8 KB 
ID:	18735

    Click image for larger version. 

Name:	file.png 
Views:	4 
Size:	23.8 KB 
ID:	18736

  21. #21
    Junior Member
    Join Date
    Jul 2019
    Posts
    15
    Interesting! We do a float to int conversion here: https://github.com/adafruit/circuitp...__init__.c#L80

    I bet that is the issue. Please file an issue here: https://github.com/adafruit/circuitpython/issues/new

  22. #22
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,077
    Though why Python can't give microsecond precision delay is also a good question. Arduino supports delayMicroseconds() on all boards. The slower boards usually have a minimum delay of several microseconds, so it's far from perfect, but certainly a lot better than 1 millisecond resolution.

  23. #23
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,077

  24. #24
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,297
    Quote Originally Posted by PaulStoffregen View Post
    Though why Python can't give microsecond precision delay is also a good question. Arduino supports delayMicroseconds() on all boards. The slower boards usually have a minimum delay of several microseconds, so it's far from perfect, but certainly a lot better than 1 millisecond resolution.
    there is a microcontroller.delay_us() in micropython
    Code:
    >>> import time
    >>> import microcontroller
    >>> t1 = time.monotonic_ns(); microcontroller.delay_us(3000); t2 = time.monotonic_ns(); t2-t1
    3000000
    I don't know how robust or accurate delay_us() is.

  25. #25
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,345
    Just a post for historical reference info on @tannewt and adafruit/circuitpython this bug fix date:
    Characters dropped with picocom #2 by tannewt was closed on Sep 21, 2016
    Will be interesting to see it evolve for Teensy.

Posting Permissions

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