Teensy 4.0 & 4.1

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
mjs513
Posts: 11
Joined: Wed May 10, 2017 12:01 am

Re: Teensy 4.0 & 4.1

Post by mjs513 » Fri Mar 25, 2022 1:59 pm

Been checking this thread on and off for a while and was able to load Micropython on a T4.1 using instructions from here https://micropython.org/download/TEENSY41/.

Pretty new to micropython so fairly confused on getting things set up. So if you can indulge me I have a few questions.

It seems I can use REPL using putty to connect to the T4.1 but it only shows up as a com port. Doesn't appear as a file system is started like with CircuitPython with /Flash. Is there something I am suppose to set up to be able to make it appear in windows as a drive?

Guess since you are talking about SD card access how would I access say the builtin sd card on the T4.1 or even the extra PSRAM or FLASH that you could put on the bottom of the drive as QSPI?

Thanks
Mike

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Teensy 4.0 & 4.1

Post by Roberthh » Fri Mar 25, 2022 2:33 pm

Hello @mjs513, MSC support is not included. That's why you just see a serial port, but no Mass Storage Device. This is a prominent aspect of discussion, and MicroPython tends not to support MSC. It is supported for PyBoards and since recently available as a recent build option for RP2 Pico boards. Having that is either inconvenient or prone to file system corruption or both. You can uses tools like mpremote, rshell or IDE's like Thonny to transfer files. If the device is connected to Ethernet, you can as well use ftp.
If at boot time a FAT formatted SDCard is in the socket, it will be mounted by the embedded boot script _boot.py (copy below). There is nothing special with the boot script beside being embedded into the firmware.
Support for additional Flash and SPIRAM is on my ToDo list. Note however that SPIRAM is slow. There are boards with additional SDRAM, like the Seeed Arch Mix board, which is used, giving you 32MByte of fast RAM for the heap. But Teensy give ~750k of heap, which is not bad.

Boot script: The first part mounts the flash file system with a littlefs file system, the second part a FAT formatted SDCard. For MSC support, the internal file system would have for be FAT as well, which is definitely not designed for flash.

Code: Select all

# _boot.py
# Try to mount the filesystem, and format the flash if it doesn't exist.
# Note: the flash requires the programming size to be aligned to 256 bytes.

import os
import sys
import mimxrt
from machine import Pin

bdev = mimxrt.Flash()
try:
    vfs = os.VfsLfs2(bdev, progsize=256)
except:
    os.VfsLfs2.mkfs(bdev, progsize=256)
    vfs = os.VfsLfs2(bdev, progsize=256)
os.mount(vfs, "/flash")
os.chdir("/flash")
sys.path.append("/flash")

# do not mount the SD card if SKIPSD exists.
try:
    os.stat("SKIPSD")
except:
    try:
        from machine import SDCard

        sdcard = SDCard(1)

        fat = os.VfsFat(sdcard)
        os.mount(fat, "/sdcard")
        os.chdir("/sdcard")
        sys.path.append("/sdcard")
    except:
        pass  # Fail silently

mjs513
Posts: 11
Joined: Wed May 10, 2017 12:01 am

Re: Teensy 4.0 & 4.1

Post by mjs513 » Fri Mar 25, 2022 3:22 pm

Hi @Roberthh

Thanks for the info and I did see something about mpremote early on in the discussion but a bit dense (a lot more to learn I guess). Too bad though.

Yes SPIRAM is fairly slow, have tested it on the T4 and T4.1 since we managed to get LittleFS operational on the T4x for NOR and NAND flash. Works a bit better if using the QSPI pads on the bottom of the T4.1. Oh if interested we did get MSC/MTP working on the T4 (though MTP still needs work) so we were able to access external drives (SDD, HDD and USB Drives) using usbhost. This is just for info.

Have to play a bit more with SD Card and drivers for micropython. So thanks for the starting tips. also have to read through this 27 page thread :)

Mike

EDIT: Forgot. We were playing with our MTP driver so we attached a T41 with circuitpython on it and found that if actual uses FAT12 on flash - not optimal but it does work. Crazy - even esp32 is moving to littlefs

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Teensy 4.0 & 4.1

Post by Roberthh » Fri Mar 25, 2022 4:13 pm

Having support for MTP on Teensy/MIMXRT and other MicroPyhton devices is highly interesting. That would not have the synchronisation and reliability problems of MSC. I started to look into that, but did not find a good sample implementation yet.

mjs513
Posts: 11
Joined: Wed May 10, 2017 12:01 am

Re: Teensy 4.0 & 4.1

Post by mjs513 » Sun Mar 27, 2022 7:18 pm

Just by way of an update. We attached a T4.1 with micropython to USBHost adapter on a second T4.1. We then did some tinkering in the sketch (using userial) to put the T4.1MP (Micropython) into repl mode and its working. But we took it one step further. The Normal T4.1 using MTP to store a file with micropython commands to send via REPL to the T4.1MP and we have that working now as well. Still looking at making some improvements.

Now for the question. I did get the Builtin SD card working but looking like its only support FAT formatted sd cards and not exFat cards. Been doing some searches but can't really find home to reconfigure use exFat.

Code: Select all

$$USerial:import os
$$USerial:>>> 111
$$USerial:os.chdir('/sdcard')
$$USerial:>>> 
$$USerial:os.getcwd()
$$USerial:'/sdcard'
>>> 
$$USerial:os.listdir()
$$USerial:['System Volume Information', 'D435 Obs-slam Links.txt', 'teensy41_card.png']
>>>


Can you point me to where to look or any hints would be appreciated

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Teensy 4.0 & 4.1

Post by Roberthh » Sun Mar 27, 2022 7:56 pm

The part of MTP sounds very interesting.
The setting for the vfs file system just supports FAT. I do no even know if it can be configured to use exFAT. Looking at the files in micropython/extmod, I see no trace of exFAT.

mjs513
Posts: 11
Joined: Wed May 10, 2017 12:01 am

Re: Teensy 4.0 & 4.1

Post by mjs513 » Sun Mar 27, 2022 8:07 pm

Too bad on exFAT sd cards especially with the cards getting so large now.

If you are interested you can check this thread out on the PJRC forum from this post on which concerns micropython/circuitpython: https://forum.pjrc.com/threads/68139-Te ... post302978

Oh just maybe to help the Teensy 4 has a flexcan library that works well if you need a reference to implement. There were a few gotchas when we implemented it. Check for FLexcan_t4

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Teensy 4.0 & 4.1

Post by Roberthh » Mon Mar 28, 2022 6:32 am

Thanks for the link. I know the PJRC forum and libraries. The latter is sometimes helpful. Besides that, we use the NXP libraries for development. They cover the whole product range, are documented and work well. And we usually use a MIMXRT_DEV board for the first implementation step with it's support for source level debugging. In any case is the integration into the MicroPython framework and testing for all supported hardware variants the larger part of the work.

Still, having MTP support at the MP Device on top of the TinyUSB library which can be used by a PC is most interesting. A python script variant would serve as well. So you Teensys to Teensy work seems to point onto that direction.

KurtE
Posts: 11
Joined: Mon Mar 28, 2022 1:52 pm

Re: Teensy 4.0 & 4.1

Post by KurtE » Mon Mar 28, 2022 2:45 pm

Sorry, I know I am late to this topic. Sorry in advance if some of this is slightly off topic.

And I really green when it comes to Python! I have done a lot more doing stuff in C/C++ (decades). And I am reasonably active up on the Teensy.

Recently have spent some time trying to learn at least some of the basics of Python. Recently In particular how to write a High Level Analyzer(HLA) for Saleae Logic Analyzer.

Over the last couple of days I (we including @mjs513) started playing some with CircuitPython and MicroPython through the back door.
That is saw a few forum threads up on the PJRC forum about issues of plugging in a CircuitPython board into the USB Host port of a Teensy 4.1. So I was curious (https://forum.pjrc.com/threads/69820-US ... Keybow2040)

So we started hacking up a USB Host test sketch to see how their USB device is configured, and found as @mjs513 mentioned, they expose an MSC drive, as well as a CDC Serial port, and Keyboard and Mouse and probably more like Audio.... And as we are having some fun the test code keeps expanding :) For example it is setup, with MTP support, such that we expose the MSC drive on the CircuitPython board as an MTP drive to the host, so we can download new version of the python code...

So we wondered about the MicroPython, so configured a T4.1 as one and plugged it into the USBHost and see that it was configured as a USB Serial class object. So through forwarders in our test sketch we can talk to the board, including typing stuff directly, or by having the sketch download file...

So again having fun. Now I just need to learn how to do things, for example when running a script, like:

Code: Select all

import machine
>>> import time
>>> loop_count = 0
>>> led = machine.Pin(13, machine.Pin.OUT)
>>> while True:
...     led.high()
...     time.sleep(0.2)
...     led.low()
...     time.sleep(0.2)
...     led.high()
...     time.sleep(0.2)
...     led.low()
...     time.sleep(0.2)
...     led.high()
...     time.sleep(0.2)
...     led.low()
...     time.sleep(0.2)
...     led.high()
...     time.sleep(0.2)
...     led.low()
...     time.sleep(0.8)
...     loop_count += 1
...     if (loop_count & 0x1F) == 0:
...         print(loop_count)
...
It blinks correctly, but I can not figure how to break out of it, like if I wish to run something different.
with CircuitPython, I could hit ctrl+c, but that did not work, For some other stuff I read ctrl+z ... Tried that... Actually tried
ctrl+a -> ctrl+z and no luck.

Next up, try to figure out if the sketch is getting the input... I know print(loop_count) is working, but now to figure out how to get the USB
Serial input data. Is it the class UART? I would assume this for the Hardware Serial ports like Serial1...

Time to experiment.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Teensy 4.0 & 4.1

Post by Roberthh » Mon Mar 28, 2022 3:04 pm

The intention is to break a running script with Ctrl-C. At the moment there is a bug in all ports using TinyUSB in that Ctrl_C is not the first character waiting in the queue to be processed. Si it is ignored if some other characters were typed between the last character used by Python and Ctrl-C. A fix exists and the PR waiting for approval.

The class for USB serial input data is NOT UART, because it is no UART. You can use the standard Python input() method for reading from the "console", or you can use sys.stdin.read() for reading and sys.stdout.write() for writing, and more of that. If you want to use a UART, you can do so, but it is not linked to the USB port.
Microypthon offers a tools called mpremote to talk to the device, upload and download files and mount a directory of your PC as local directory of the T41 board. It is a design decision of MicroPython to not support MSC mode. It might look comfortable at first glance, but comes with a lot of drawbacks.

The T41 port supports Ethernet. If you hook up the device to Ethernet, you can use a ftp or telnet server script to connect to your local.

Post Reply