Wednesday, June 14, 2017

My Hi-Fi Raspberry Pi DAC project. The Design Phase

    I'm in no way an audiophool audiophile, but I do like to listen to good music coming from a nice bit of quality engineered speaker and amp.
I've had a Sony CD player ( CDP-X303 ES ) for a while now and while it's working nice, it has a huge drawback.
I have to buy a truck load of CDs just so that I can listen to my favorite songs. I tend to have a variety of preferences, mostly based on my mood, that's why I tend to listen to anything from Michael Jackson to  Linkin Park and from Run DMC to Eminem. And anything in between.

     It's much more convenient to download ahem.... BUY my favorite tracks and store them on a drive and listen to them from some kind of network DAC. It's also easy on the mind to know that if a track has some kind of artefacts in it, (Sone CDs I bought have these) you just delete it and downl.... buy another. Unlike CDs, where you just have to live with the thought that something you paid money on  has random shitty, thunderous noises on it. 

PS: Don't buy the Michael Jackson CD albun "The Essential Michael Jackson" from Sony Music. You'll be trully regret it if you do. At least, I did.


A Networked DAC


    So, first of all, I need a board that links whatever DAC I'll make, to my LAN at home, so I can access the songs. And  it'd be nice to also play them.
   It'd be even better if I don't have to spin a whole board for this, so I'm thinking something along the lines of an Arduino or Raspberry Pi.

    I want to be able to play stuff like FLAC or WAV at something like 192 to 380 Kbps, 24 or 32 bits. Also, the few DACs that can handle these bit rates have an I2S (no, it's not a typo) interface, and conveniently enough, a few platforms have an I2S  bus available.
  One such platform is the Raspberry Pi. It's  cheap, has a ton of processing power and best of all, there are a lot of debians and ready-made software for it, covering almost anything you can think of.
  Another option would be the Odroid., which also has enough umph for the task at hand.

  For those cringing at the sound of using platforms like these for Hi-Fi, please keep your composure.  Your finely-tuned ears can't really tell the difference between a 1000 $ DAC and an embedded platform with a good DAC on it (except if the latter is poorly designed). 
  Now, having said this, please excuse me while I go put on my flame suit.

    My decision went in favor of the Raspberry Pi platform just because I happened to have one around.

A quick search for a Raspberry debian that can handle all you music via a network interface will produce the following results:

 - Volumio
 - Rune Audio
 - Etc.

    There are a lot of web players out there that do  the same thing. My personal choice was Volumio. Why did I choose this over the others I hear you ask ? Well, because this was the first result that popped up on Google.

Which one... which..one... Hmm...

    So, having solved the networking part, now all I need is a DAC that can do everything I want i.e. up to 382 Kbps at 32 bits.
A friend of mine did a similar project and he used the PCM5242 from Texas Instruments. It has a very impressive spec sheet, both in performance and in size. But the size is mostly due to its internal DSP. Yep, this DAC also has an  mini DSP inside. Wicked! That opens up a whole new world of opportunities.

Where's the rest of it?

    Anyone looking at the datasheet for this can plainly see the banner on the front page, where it says it has  a DSP inside it. But if you go on further, you start  to see that the datasheet is incomplete. It's missing some of the data on the DSP registers. Bummer. Plus, if you want to program it via an interface, you need TI's Pure Path Studio software...which can not be downloaded from anywhere. You have to send the guys at TI a mail with a request for it.

    Also, take note that there are a few commercial Raspberry shields out there that have the PCM5242. These claim it has a DSP inside  it, which is correct. Unfortunately, if you want to use it or program it, or do anything with it, other than just convert bits to sound, you're on your own. You basically have to create your own library for the PCM's DSP so you configure every bit of it.

    But let's not let this small drawback keep us from implementing what would be a very capable DAC
  I'm going to build one to see how it sounds and how it behaves and if I like it, I'm going to design another version where I try to use the internal DSP and actually put the whole thing to use.

Actually designing stuff

    Ok, so let's see how we're going to do this.
First thing's first: a normal I2S interface uses 4 wires.
 - A master clock, to time the whole thing - MCLK or SCK;
 - A bit-rate clock, BCK, that tells the DAC what bitrate the song is being played at, i.e. you sampling frequency;
 - The data line, DIN
 -  And a signaling line  - LRCK - to tell the DAC which frame of  data is for the right channel and which is for the left one.


Fig.1  I2S Bus with all 4 signals.BCK is derived from the MCK




Fig.2  I2S Bus implementing only 3 signals. MCK is 
internally reconstructed from the BCK

    Normally, the master clock would have to be set so that it is a multiple of the BCK. This means that with most network  DACs (I'm talking about the system as a whole, not the DAC chip itself) you need to have an external clock feeding the DAC directly, or an interface IC, (bridging something like USB - I2S) when an interface conversion is necessary. This is kind of cumbersome and adds a lot of parts to your BOM.
  If you are curious, you can have a look at XMOS's line-up of chips. There are some that go up to 192 k.

    But the PCM5242 only needs one clock - the BCK. It can take this and rebuild the master clock with an internal PLL, thus saving a lot of board space and simplifying the design.

   The 5242 can work either in Hardware mode, or Software mode.
 Hardware mode is kind of a stand-alone thing. You just feed it 1s and 0s from an I2S bus, then it converts them to sound. Easy. You also have some pins with which to control signal attenuation, and that's about it. I'm over-simplifying, but you get the point.

    Software mode is where the magic really happens. You still use the I2S bus, but you can also do soft muting on the DAC, you can control the volume in much finer steps, set PLL frequencies, write internal registers and also, my favorite, control the DSP. All this through an I2C or SPI interface. Your choice. Or, rather my choice, to be exact.

  The prototype is going to just run in HW mode. I don't want to deal with writing any internal registers and doing power ON/OFF sequences and stuff like that. At least, not just yet. I just want to see it work....or to be more precise, to hear it work.


Down to the nitty-gritty


    Ok, so having settled on hooking the DAC so it works in Hardware Mode, what other egineering stuff do we need, to make this thing work?

    My Sony amp has 1 Vpp inputs, but from what I've seen, it can tolerate higher peak-to-peak voltages. Up to 4 or 5 Vpp. I actually scoped a Sony 339ES CD player and got as much as 7Vpp.
 But for safety's sake, let's choose a maximum output voltage of 2 Vpp for the DAC hat.

  The DAC itself can output either 4.2 Vpp or 2.1 Vpp levels. So, let's go with the 2.1 Vpp.
Now, I'm kind of weary about powering the amp's input directly from the DAC, so  I want to put a buffer between them.
 Because I'm lazy,  and don't want to mess around with op-amps possibly oscillating like crazy, I'm going to go with the TPA6120A2 audio amp as the buffer. That's the one in the DAC's data sheet.

  Thus, the final output will have a level of about 4 Vpp (the amp's minimum gain is x2) but we can solve that with some extra attenuation from the DAC. For this, you just implement the table below so that your signal has the needed level.




Analog and digital power supplies design

3.3V PSUs

    The DAC needs both an analog power supply and a digital one, both 3.3V. This isn't such a big deal. But I can sure as hell try and make one out of it.

    One can easily fit a small SMPS or two on a Pi hat. The problem is however the noise these will generate. Ok, this, and the fact that some audiophiles will cringe at hearing the words "SMPS" and "Hi-Fi" in the same sentence.
    I want everything to be as clean as possible, noise wise, so my solution was to split the power supply from the main DAC board. Not only that, I went a step further and split the analog and digital supply, each having its own board.

    Having this configuration however can be more detrimental than helpful. One might be tempted to think that a current's return path would be that of least resistance to ground. And that is correct.. But where that ground is, that can be a bit tricky to find.
  In the set-up I proposed, the ground is not the one on the DAC hat i.e. the PCM's GND connections. It's the one way over on the PSU. So, the actual path would be DAC → about 5 cm of wires (2 inches for the non-MKS lovers) → PSU GND
With such a large loop area,  it's an open invitation to a whole lot of noise pick up, no matter how quiet the actual PSU is.

 I tried to mitigate this by having the PSUs regulate down to 5V for the PCM's power rails then have an additional LDO on the Pi hat itself, to do the rest of the work down to 3.3V. This  (I'm hoping) will "bring back" the GND return path to the hat. The current loop this set-up creates is the shortest possible.

To split o not to split, that is the question

   A recurring issue in this Analog-meets-Digital world appears: how to manage the digital and  analog ground planes for the DAC?
Having both an analog and a digital rail, how exactly do you route the grounds on the board so that you get the lowest possible EMI and the least amount of noise pick-up on you signal lines? Do you split them clear through the middle and place the DAC right on the center line? Or do yo just make on continuous plane?

  There are lots of articles  dealing with this exact issue, and depending on when it was written, you might get some diverging answers.
I have come across a couple of articles that explain the issue very well.
One is this article, which kind of gets you on the right path. If you need to go deeper down the rabbit hole, I recommend reading "Grounding in mixed-signal systems demystified", parts 1 and 2, from Texas Instruments.

  Following the general direction from these articles, I decided to only join my grounds under (or close to) the PCM DAC and exercise the right routing technique so that I have no analog signal wires crossing the digital ground or vice versa.
  Now, for this design, it's pretty easy to get things right, it's only going to be a two layer board and there are only 4 ICs and some passives on it, but for other designs, it can get messy when routing signal lines across the PCB, and keeping  the traces in their right ground plane can cause you some head aches

Cap selection


  No, I don't mean electrolytics. That's a discussion for another time.  I mean SMD ceramic caps. As in MLCC caps.


In this design, there are a few places where caps really matter, despite being used mostly for bypassing.

    The 3.3V regulators on the DAC board are LP2992 LDOs. Yes, Texas Instruments again. Now, these are advertised as having very low noise, and to achieve this, the datasheet recommends using some very low ESR caps on the input and output  and NP0 or C0G caps on the Bypass pin.
   My personal choice was to go with X7R caps for the 5V input and 3.3V output caps. The rest of the bulk capacitance will be on the PSU boards.
These have much lower ESR than electrolytics and behave much better under DC bias than X5R caps. Tantalums are on my naughty list and try to avoid them as much as possible. They don't take voltage spikes that well and their ESR isn't really that low.
    Also, the X7R material is less sensitive to changes in capacitance due to temperature and/or DC voltage (compared to X5R, that is).

  NP0 and C0G dielectrics are literally in a higher class than X7R / X5R. While one might use the latter in normal bypassing applications, the NP0/C0G can be used in filters, or for setting precise time constants. If you were to plot a frequency vs capacitance chart, you'd get pretty much a straight line from DC to about 10 MHz and a somewhat linear temperature coefficient.
This makes them very useful for, say.... some nice RC filters that go on the output of a DAC. And it just so happens that we have some of these between the DAC's differential output and the op-amp's input.

  You can find a lot of info about this on the net, so I won't bore you with too many details. You can check out this document I found in haste, while deciding which type of cap I should use. It's got graphs and tables in it , so it must be good.



Hardware Mode configuration pins


  Like I mentioned earlier, the PCM5242 can be used in Hardware Mode or in Software Mode, making it a very flexible design.


  First off, to put the DAC in Hardware mode, pins 24 and 23 need to be pulled low.
Now, there aren't many control options left. You can choose between a 2VRMS output or a 1VRMS output by making pin 21 Low or High, respectively (i.e. 1V out for AGNS high).

  I've already talked about the attenuation output pins so that's the important bits taken care of.
What's left is to set pin 16 Low, because we don't want any de-emphasis, and if you don't want to hear any hissing in your speakers when nothing's playing, then you'd want to set pin 1 Low.
There, that about covers it.


Schematic and PCB Layout

    If you're interested in building something similar or just want to have a look over the schematic, you can go to my GitHub  project page. There I have all the documents relating to this project. Feel free to download and/or use the desing as you might see fit.

  The laying out of the PCB went pretty much as I'd expect it for this simple design. Like I wrote, I opted to do the joined AGND and DGND sollution.
Unfortunately, because I wanted the bypass caps for the DAC as close to the pins as possible, I didn't have ena clear way to split the ground right beneath the DAC, so they're joined in two locations, either side of the DAC


     This may look weird, but I don't think it impacts the noise performance of this thing too much. But, if you believe otherwise and have more experience than me, then please leave a comment below, I'd really like to hear some nice explanations.


    So, basically, this is what I'm expecting to get from the PCB manufacturer. Except in black. It's going to be gorgeous.
Oh, and for those of you wondering what those holes on the bottom of the picture are.... wel,, I figured I'll just solder the audio output cable straight to the PCB. And to firmly hold the cable there and not put pressure on the solder joints, I figured I'd zip tie the cable to the PCB. A bit flakey? Maybe. But I  didn't want too many metal to metal interface between the output of the DAC and the input of my speaker amp. I may possibly be dellusional here, but hey... I like to experiment.

  The PSU layout also went snoothly and looking back, I could have squeezed both PSUs onto a single board or two smaller boards than the ones that came out. Next time I'll know better.



    Now, all that's left is to wait for the boards and parts to arrive.

  If you would like  to read on about how I put everything together, tested this and how it all turned out, the second part of this build is here.


Disclaimer: The stuff from TI I bought myself. I'm in no way affiliated with them. It just happened that they have the kind of parts I  needed














0 comments:

Post a Comment

 
google-site-verification: googlee8a41f51abde7545.html