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.
User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: Teensy 4.0 & 4.1

Post by rdagger » Sat Feb 05, 2022 9:51 pm

That sounds great! I'd be very interested how you are wiring and clocking the Teensy Audio board. I went ahead and wrote a MicroPython driver to configure the SGTL5000. It implements all the commands and is well commented (about 1100 lines of code). I also wrote a program to dump and document all the contents of the SGTL5000 registers. Please let me know if you would like to use the code. I haven't been able to get any audio yet but I'm still trying to get my head around all the configuration options.

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Teensy 4.0 & 4.1

Post by Mike Teachman » Sun Feb 06, 2022 12:06 am

rdagger wrote:
Sat Feb 05, 2022 9:51 pm
That sounds great! I'd be very interested how you are wiring and clocking the Teensy Audio board.
For wiring: There really is no wiring. The Teensy Audio board plugs onto the Teensy (or visa-versa). The MicroPython I2S class uses the signals that connect these two boards.
For clocking: The Teensy is always a controller. The SGTL5000 device is expected to be configured as a peripheral, for both DAC and ADC applications. For writing a sample stream to the SGTL5000 device, the I2S class produces 4 I2S signals: MCK, SCK, WS, and SD (signal naming convention follows the 1986 Phillips I2S standard). MCK support is new in the mimxrt implementation of I2S.
The i.MX RT1062 processor PLL is used to create the clocks MCK, SCK, and WS. Accuracy: A 44100Hz output stream will have an actual frequency of 44100.03Hz.

I do not know if it is possible to configure the MicroPython I2S class to simultaneously read and write from the SGTL5000 device. The Teensy will support two I2S instances, but I do not know if the clock signals between the two instances (particularly the WS signal) will be synchronized together. I might work, "by accident".

The MicroPython I2S API is identical across all ports (stm32, esp32, pico), described here. As mentioned, the I2S implementation on the mimxrt port will also support MCK, which will likely be added to the other ports supporting I2S.
rdagger wrote:
Sat Feb 05, 2022 9:51 pm
I went ahead and wrote a MicroPython driver to configure the SGTL5000. It implements all the commands and is well commented (about 1100 lines of code). I also wrote a program to dump and document all the contents of the SGTL5000 registers. Please let me know if you would like to use the code. I haven't been able to get any audio yet but I'm still trying to get my head around all the configuration options.
Would I like to use your driver? A huge YES ! I was just about to start writing a MicroPython driver for the SGTL5000 device. With your driver I can test the mimxrt I2S class with the Teensy audio shield. If it works, I can share my Teensy micropython binary and you can test at your end.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: Teensy 4.0 & 4.1

Post by rdagger » Sun Feb 06, 2022 1:25 am

Mike Teachman wrote:
Sun Feb 06, 2022 12:06 am
Would I like to use your driver? A huge YES ! I was just about to start writing a MicroPython driver for the SGTL5000 device. With your driver I can test the mimxrt I2S class with the Teensy audio shield. If it works, I can share my Teensy micropython binary and you can test at your end.
I created a GitHub repo: https://github.com/rdagger/micropython-sgtl5000
The code is untested but it is mostly ported so it should work. The __init__ method probably needs to be refined.

sgtl5000.py is the main library
sgtl5000_dump.py provides easy to read dumps of all the registers for debugging
demo_01.py is a demo template

Please let me know how it goes and I would really appreciate a .hex file so I can test the Teensy 4.0.

Thanks for all the great contributions!

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Teensy 4.0 & 4.1

Post by Mike Teachman » Sun Feb 06, 2022 6:14 am

I cloned the repo and made some progress.
- the module imports without error
- the module is easy to follow and the comment blocks are golden !
- configured I2S to play a wav file
- verified all I2S signals to the audio board using a scope
- incrementally added codec methods to the I2S program
- the mute_dac() method created an error AttributeError: 'CODEC' object has no attribute 'adc_dac_ctrl'
- I initialized self.adc_dac_ctrl = 0 to overcome the above error, but wondered if adc_dac_ctrl needs to be initialized to something other than zero?
- I wondered if some init() config needs to be done related to CHIP_I2S_CTRL? (maybe it is and I missed it)

Here is what I have so far. I'm guessing a bit on this configuration. So far no sound is coming out the headphones, but I feel I am close.

Code: Select all

i2c = I2C(0, freq=400000)
codec = CODEC(0x0A, i2c)
codec.mute_headphone(False)
codec.headphone_select(0)
codec.mute_dac(False)
codec.dac_volume(0.3, 0.3)
codec.volume(0.3, 0.3)
- that is all for today ....

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: Teensy 4.0 & 4.1

Post by rdagger » Sun Feb 06, 2022 6:46 am

Mike Teachman wrote:
Sun Feb 06, 2022 6:14 am
- I wondered if some init() config needs to be done related to CHIP_I2S_CTRL? (maybe it is and I missed it)
I thought it might be more efficient to create variables instead of reading registers to create bit masks for some of the more common registers such as adc_dac_ctrl. I truncated some of the init section before I posted it because it was a mess. I accidently cut some variables.

I think at least the following registers should be configured in the init section:
CHIP_CLK_CTRL
CHIP_I2S_CTRL
CHIP_SSS_CTRL


Here's an example that could be appended to the init method:

Code: Select all

# 44.1 kHz, 256*Fs
self.write(self.CHIP_CLK_CTRL, 0x04)
# SCLK=64*Fs, 16bit, I2S format (data length)
self.write(self.CHIP_I2S_CTRL, 0x30)
# ADC->I2S, I2S->DAC
self.write(self.CHIP_SSS_CTRL, 0x10)
# Unmute DAC, ADC normal operations, disable volume ramp
self.adc_dac_ctrl = 0x00
self.write(self.CHIP_ADCDAC_CTRL, self.adc_dac_ctrl)
# Digital gain, 0dB
self.write(self.CHIP_DAC_VOL, 0x3C3C)
# Set volume (lowest level)
self.write(self.CHIP_ANA_HP_CTRL, 0x7F7F)
# Enable & mute headphone output, select & unmute line in & enable ZCD
self.analog_ctrl = 0x36
self.write_byte(self.CHIP_ANA_CTRL, self.analog_ctrl)

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Teensy 4.0 & 4.1

Post by Mike Teachman » Sun Feb 06, 2022 6:27 pm

Thanks! I updated the driver init and will continue working on the goal of playing a wav file through headphones. I will report back on any progress. The next update might be in a couple of days.

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Teensy 4.0 & 4.1

Post by Mike Teachman » Mon Feb 07, 2022 5:49 am

@rdagger Some good news. I was able to use your driver to make a prototype of the Teensy playing through the audio shield. I committed a new folder "teensy_audio_shield" to this branch:
https://github.com/miketeachman/micropy ... ree/mimxrt
The folder contains a micropython binary for the Teensy4.1 that supports the I2S class, an example program to play a wav file, and your driver with a couple of minor changes (most important change is to the headphone_select() method). The example program uses the SD card slot on the Teensy, not on the audio shield. You should be able to load the binary and run the example.
Next step: try out the audio shield recording capabilities.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: Teensy 4.0 & 4.1

Post by rdagger » Mon Feb 07, 2022 7:05 pm

Mike Teachman wrote:
Mon Feb 07, 2022 5:49 am
@rdagger Some good news. I was able to use your driver to make a prototype of the Teensy playing through the audio shield. I committed a new folder "teensy_audio_shield" to this branch:
https://github.com/miketeachman/micropy ... ree/mimxrt
The folder contains a micropython binary for the Teensy4.1 that supports the I2S class, an example program to play a wav file, and your driver with a couple of minor changes (most important change is to the headphone_select() method). The example program uses the SD card slot on the Teensy, not on the audio shield. You should be able to load the binary and run the example.
Next step: try out the audio shield recording capabilities.
Great job, I loaded your firmware to my Teensy 4.0 and it works!

Since my Teensy 4.0 doesn't have an SD card slot, I used the one on the Teensy audio adapter. However, I wasn't able to get the machine.SDCard class to work. Maybe my syntax is incorrect. Is it implemented for an external SD card? I ended up using this sdcard.py driver which worked on SPI0.

I'll merge your changes and continue to test the functionality. I'm also interested in recording. I would like to record from the line in and would like to output to an external speaker while recording.

Thanks!

alphaFred
Posts: 31
Joined: Wed Apr 15, 2020 6:47 pm

Re: Teensy 4.0 & 4.1

Post by alphaFred » Mon Feb 07, 2022 8:14 pm

Since my Teensy 4.0 doesn't have an SD card slot, I used the one on the Teensy audio adapter. However, I wasn't able to get the machine.SDCard class to work. Maybe my syntax is incorrect. Is it implemented for an external SD card?
Hi @rdagger.
The machine.SDCard works with the uSDHC peripheral of the i.MXRT controller. It does not support SD Card access via SPI so far.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: Teensy 4.0 & 4.1

Post by rdagger » Mon Feb 07, 2022 8:43 pm

alphaFred wrote:
Mon Feb 07, 2022 8:14 pm
The machine.SDCard works with the uSDHC peripheral of the i.MXRT controller. It does not support SD Card access via SPI so far.
Appreciate the guidance, thanks!

Post Reply