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

Thread: Teensy 4 + HiFiBerry DAC

  1. #1

    Teensy 4 + HiFiBerry DAC

    Hi all,

    Like many Teensy 4 users, I've been eager to funnel all of that computing power into some slick HD audio.
    This past year, HiFiBerry released two next-gen audio cards for Raspberry Pi - DAC2 Pro and DAC2 HD.
    DAC2 Pro builds on their PCM-5122 powered DAC+ Pro board. It can play 192kHz, 24-bit audio with claimed quality metrics of 112dB SNR and -93db THD+N. It sports an onboard headphone amp, and costs $45 USD.
    DAC2 HD is a totally new card using an older DAC, PCM-1796. The DAC is far less integrated, so the board is packed with supporting components - for an improved 123db SNR and -108db THD +N. MSRP: $99 USD.

    Both cards have precision clock sources and ultra low noise regulators to filter the DAC power supply.

    How great would it be if Teensy 4.X could talk to the world via these monsters?
    I developed a simple breakout board to do just that:

    Click image for larger version. 

Name:	T4.jpg 
Views:	95 
Size:	74.0 KB 
ID:	23552

    The board is available in two versions - one mounting above Teensy, and one below.

    Design files and Arduino code are available on Github here, and the boards can be ordered from OSHPark (here and here). The only additional components are raspberry pi pin headers, with Adafruit part numbers given in the repository.

    The Arduino example code is a simple sine wave sweep, using Paul's audio library.
    By setting a macro, it can be configured to run in I2S master mode, where Teensy generates clock signals, or I2S slave mode using HiFiBerry's precision clocks.
    The example code also configures the headphone amp on DAC2 Pro.
    The example code runs on DAC+ Pro and DAC2 Pro, but not on DAC2 HD. The breakout board has the necessary connections for the HD, but I haven't been able to get the code working yet. The working examples have a few remaining quality issues, especially with Teensy in I2S slave mode - but I hope to iron these out in the next few weeks. Issues are noted in the repository, and I'll discuss them in depth in future posts.

    Full disclosure - In parallel, I'm developing an audio board using these components for Bpod, a behavior measurement platform used in Neuroscience research. Using code heavily inspired by Paul's audio library, the board plays a library of 192kHz stereo waveforms from microSD on trigger with only 1 sample of latency (using preloaded psRAM buffers), and outputs an isolated TTL signal to precisely indicate playback start and end. This project will also be open source, once it meets the necessary specs to be useful in its application.

    I'll be glad to share more technical info with anyone who's interested (HiFiBerry I2C sniff logs captured with PulseView, etc)!

    -J

  2. #2
    Good news - the board now works with HiFiBerry DAC2 HD!
    I also worked out the remaining issues with I2S slave mode on DAC2 Pro.
    The updated code is on Github, here.

    The signal quality looks great if both channels have the same sine waveform generated by the audio library.
    Unfortunately I'm still unable to get separate waveforms to play cleanly on the left and right channels.
    For instance, this works:
    Code:
    AudioSynthWaveformSine   sine1;
    AudioSynthWaveformSine   sine2;
    AudioConnection          patchCord1(sine1, 0, i2s1, 0);
    AudioConnection          patchCord2(sine1, 0, i2s1, 1);
    But this produces a chopped overtone (at the speed of audio library packets?):
    Code:
    AudioSynthWaveformSine   sine1;
    AudioSynthWaveformSine   sine2;
    AudioConnection          patchCord1(sine1, 0, i2s1, 0);
    AudioConnection          patchCord2(sine2, 0, i2s1, 1);
    Using custom code derived from the audio library (i.e. synthesizing the sine directly in the I2S DMA channel's ISR) I can get clean signals out of both channels simultaneously, so it's not a limitation of the hardware. I'll post some example code later on when I have time to mark it up nicely - with sampling rates up to 192kHz.

    -J

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,452
    Quote Originally Posted by J_Sanders View Post
    But this produces a chopped overtone (at the speed of audio library packets?):

    Code:
    AudioSynthWaveformSine   sine1;
    AudioSynthWaveformSine   sine2;
    AudioConnection          patchCord1(sine1, 0, i2s1, 0);
    AudioConnection          patchCord2(sine2, 0, i2s1, 1);
    Would you like me to investigate? I need a complete program I can copy into Arduino, even if the rest seems "trivial".

  4. #4
    Hi Paul!

    The overtone was fixed by increasing AudioMemory to 50.
    With AudioMemory as high as 250, I'm still getting crackling on the right audio channel on the HiFiBerry boards.
    The original sketch was the example code, here.
    This is the same sketch with the HiFiBerry code stripped out, and set up for the SGTL5000 with Teensy in I2S master mode:

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <utility/imxrt_hw.h>
    
    #define SINE_FREQ_MAX 20000
    #define SINE_FREQ_MIN 20
    
    uint32_t sineFrequency = 1000;
    
    AudioOutputI2S i2s1; 
    
    AudioSynthWaveformSine   sine1;
    AudioSynthWaveformSine   sine2;
    AudioConnection          patchCord1(sine1, 0, i2s1, 0);
    AudioConnection          patchCord2(sine2, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;  
    
    void setup() {
      AudioMemory(50);
      sine1.amplitude(0.5);
      sine2.amplitude(0.5);   
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
    }
    
    void loop() {
      sineFrequency += 1;
      if (sineFrequency > SINE_FREQ_MAX) {
        sineFrequency = SINE_FREQ_MIN;
      }
      sine1.frequency(sineFrequency);
      sine2.frequency(sineFrequency);
      delayMicroseconds(500);
    }
    I'm not sure how to configure SGTL5000 as master for use with AudioOutputI2Sslave (looks like a new call to enable() with a clock argument? clock = fs didn't work for me)

    Thanks!

    -J

  5. #5
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,554
    Is that now about the SGLT or HiFiBerry?

    If you have crackling noise other effects with the Audio Board, there is something wrong with your hardware...

    Increasing the Audiomemory does not help... the additional memory is not used.
    If you have the newest Audio Library from GitHub, you can use the SGTL master mode this way (on T4):

    Edit: Code removed, there were some parts missing. Best is to look here:

    An example where this is used, is here: https://github.com/FrankBoesing/Teen...ayer_pitch.ino

  6. #6
    Hi Frank B,

    You were correct - it was a hardware issue!

    The last code I posted was a stab at adapting the problematic code from the HiFiBerry to the SGLT, albeit in Teensy-as-I2S-master mode.
    Using your code, I was able to run the code in Teensy-as-slave mode with no issue.
    I also discovered that the crackling issue only exists for the top-mount version of my HiFiBerry breakout board - the bottom mount version plays with no issue.
    I'm not sure yet whether it's the board design or the particular Teensy that's soldered into it. For now, I added a warning to Github + OSHPark.

    One thing I didn't pay much attention to in designing this breakout board, is trace impedance matching on the I2S bus lines. In any case, it will take some debugging.

    On a separate topic, here's a spectrogram of a 20Hz-80kHz frequency sweep from Teensy 4.1 --> HiFiBerry DAC2 HD running at 192kHz and synthesizing the wave directly in the I2S ISR (code here):
    Click image for larger version. 

Name:	FreqSweep_HD.jpg 
Views:	37 
Size:	159.9 KB 
ID:	23778

    The data was acquired directly from the DAC output with a 16-bit NI USB-6211 DAQ board - so of course I'll need to capture it with a high-end 24-bit audio interface at some point to see the nuances. The noise at ~65kHz exists even with the DAC board unplugged, so it can be disregarded. The ultrasonic range looks pretty clean, so with a bit of tweaking this may be one way for Teensy to talk back to the bats.

    Thank you for helping with this!
    Last edited by J_Sanders; 02-20-2021 at 09:22 PM.

  7. #7
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,554
    The Audio Board has a 100 Ohm series resistor in the MCLK line.
    Maybe this would help?

  8. #8
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,554
    So, you're using the DAC for ultrasonic sounds?
    Have you tried the PT8211? It's capable of 384khz (according to the datasheet) and cheap.
    The bats will not notice that, I think..... (otherwise...RUN!

  9. #9
    I'm not sure that a 100-ohm resistor could account for the difference - the two (working and buggy) boards have identical wiring, with the only difference being trace length. I used thick traces, so I expect the difference to be very minor - but I'll definitely have to measure it. It's definitely not an issue with the master clock line though - Raspberry Pi doesn't have an MCK pin on its GPIO header. The DAC has to generate MCK from another clock line with clock dividers.

    Personally I'm not after bats (though it sounds like a lot of fun from the various threads on here!) I'm prototyping a low latency sound server for Neuroscience experiments. The device has to produce sound with high SNR and low distortion across the rat and mouse hearing ranges (~500Hz-80kHz). So far it looks like T4.1 + HiFiBerry DAC2 HD fits the bill, if I could only solve that pesky sdFAT/SDIO issue.

    That being said, I'll have to check out PT8211. How's its sound quality vs. SGTL? It seems to be markedly absent from major distributors...

  10. #10
    Senior Member manicksan's Avatar
    Join Date
    Jun 2020
    Location
    Sweden
    Posts
    288
    I think $45 and specially $99 is alot overpriced for a IC that only costs $5 each.
    Even with all the components and PCB it would never reach the prices. No its because its made specially for raspberry pi I think.

  11. #11
    Senior Member
    Join Date
    Jul 2020
    Posts
    1,101
    Well the $45 seems fairly plausible - here's a competitor: https://uk.farnell.com/mikroelektron...ay4/dp/2521708

    Not sure whats on the $99 board, but a low volume board like these costs quite a bit to develop - the
    test equipment you need to measure performance at these levels is not cheap for a start.

  12. #12
    My ten cents: you're paying for the peripherals and proprietary board layout, not the DAC IC. There are plenty of breakout boards for audio DACs, but they don't do high end power supply filtering, analog output filtering, precision clocking, etc. Implementing that stuff properly is art. In my hands, performance of HiFiBerry DAC2 HD looks qualitatively similar to similarly priced sound cards for PC. The $45 DAC2 Pro is much less impressive (I'm actually in a dialogue with TI to sanity check my measurements), but the clock ICs and headphone amp provide a convenient package for $15 more than their DAC+ Standard (incidentally also compatible with the Teensy HiFiBerry breakout board).

  13. #13
    I added example USB Passthrough sketches for DAC2 Pro and DAC2 HD.
    This effectively turns either HiFiBerry board into a 44.1kHz 16-bit USB sound card for the PC. To load these example sketches, you must select 'Audio' from Tools > USB Type in Arduino IDE.
    https://github.com/sanworks/Teensy4_...20Sound%20Card
    Enjoy!

  14. #14
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,554
    I've added it here, with a link to this thread.

  15. #15
    Awesome, thanks Frank!

    A small update - I discovered that the crackling issue on the top-mount version of the board only affects DAC2 HD - not DAC2 Pro or DAC+.
    I've updated OSHpark and Github notes to reflect this.

    The solution turned out to be power supply decoupling - a 10uf ceramic capacitor between 5V and GND where power leaves the breakout board to the HiFiBerry board.
    Not sure why the bottom-mount version of the board didn't need this, or whether 10uf is the optimal value here. Anyhow it works - and updated boards are coming soon!

    Updated guidance: until a revised PCB is released, if you're using DAC2 HD, use the bottom-mount version (or solder in a 1206 cap as shown below).

    From the experienced EE people in the crowd - any opinion on 10uf vs some other value, and ceramic vs. other types for decoupling a whole board (which presumably has its own appropriate decoupling at each IC)?

    Click image for larger version. 

Name:	CapSolved.jpg 
Views:	27 
Size:	112.3 KB 
ID:	23873
    Last edited by J_Sanders; 02-28-2021 at 06:07 PM.

  16. #16
    I just posted v1.1 of this board in the repository, solving the previous stability issue with DAC2 HD.
    The fix was to add a 4-component USB power supply filter borrowed from an FTDI circuit note, figure 2.5 here.
    As of now, there are no known issues with the interface - and the audio quality is +++

  17. #17
    the boards can be ordered from OSHPark (here and here).
    IMPORTANT: Please use the updated OSHPark links in the PCB Readme, here.

Posting Permissions

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