Audio and Ethernet

Status
Not open for further replies.

David_B

Active member
Hi, hoping somebody can help. I think that my issue may be similar to that experienced in this recent thread:
https://forum.pjrc.com/threads/45689-Problem-with-sending-audio-data-over-ethernet, although my application is different.

I'm making an audio player that will download a file over FTP, save it to an SD card and subsequently play it when triggered. I'm using a Teensy 3.2 with a WIZ820io module and audio shield.

Against the odds, perhaps, this is more or less working. I can pick up an IP address on my LAN using DHCP, connect and download a file. I can also play audio apart until the Ethernet.begin, after which once can hear something, but audio is corrupted.

I'm struggling to find out where the problem is. I don't know whether the Teensy is struggling to process both the ethernet and audio data at the same time, or whether there's something else going on. I have thought about using a 3.6 instead should that be the case.

To make the WIZ820io work with the audio shield, I have, of course had to switch to using differing pins. This seems to be working fine. I've also tried resetting and powering down the WIZ before trying audio again and this doesn't help.

My test code is below. For completeness, 1.wav is a 126kB file at 1411kbps.

Any input or thoughts would be greatly appreciated.

Thanks in advance
David

Code:
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_MISO_PIN  12
#define SDCARD_SCK_PIN   14

#define W5100_RESET_PIN 1
#define W5100_PWDN_PIN 2
#define W5100_SS_PIN 21

#include <Audio.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0xDD };  
IPAddress ip(192, 168, 1, 105);

EthernetClient client;
EthernetClient dclient;

char ftp_server[] = "aaa.co.uk";
String fileName = "public_html/styles.css";
#define ftp_username aaa
#define ftp_password aaa
char outBuf[128];
char outCount;

File fh;

AudioPlaySdWav           sd_card;                         
AudioOutputI2S           soundcard;

AudioConnection         patchcords[]={
                          AudioConnection(sd_card,0,soundcard,0),
                          AudioConnection(sd_card,1,soundcard,1)                        
                        };

AudioControlSGTL5000     sgtl5000_1;

void setup() {  
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  
  Serial.begin(57600);
  Serial.println("hello");

  pinMode(W5100_RESET_PIN, OUTPUT);
  pinMode(W5100_PWDN_PIN, OUTPUT);
  digitalWrite(W5100_RESET_PIN, LOW);    // begin reset the WIZ820io
  delay(1000);
  
  pinMode(W5100_SS_PIN, OUTPUT);
  digitalWrite(W5100_SS_PIN, HIGH);  // de-select WIZ820io
  pinMode(SDCARD_SCK_PIN, OUTPUT);
  digitalWrite(SDCARD_SCK_PIN, HIGH);   // de-select the SD Card

  delay(1000); 
  digitalWrite(W5100_PWDN_PIN, LOW); 
  digitalWrite(W5100_RESET_PIN, HIGH);   // end reset pulse

  AudioMemory(100);

  // Enable the first audio shield, select input, and enable output
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
  sgtl5000_1.volume(0.5);
  //sgtl5000_1.adcHighPassFilterDisable();
  sgtl5000_1.lineInLevel(0);

  Serial.println("W5100 restarted");
  delay(3000);

  if (!(SD.begin(SDCARD_CS_PIN))) Serial.println("Unable to access the SD card");
  else Serial.println("accessed sd card");

  sd_card.play("1.wav");
  delay(5);  
  
  while(sd_card.isPlaying()){
    //
  }

  Ethernet.init(W5100_SS_PIN);
  
  Serial.println("Attempting to get an IP address using DHCP:");
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Serial.println("failed to get an IP address using DHCP, trying manually");
    Ethernet.begin(mac, ip);
  }
  
  //Ethernet.begin(mac, ip, dns, gateway, subnet);
  Serial.print("My address:");
  Serial.println(Ethernet.localIP()); 
 
  delay(2000);

  //digitalWrite(W5100_RESET_PIN, LOW);    // begin reset the WIZ820io
  digitalWrite(W5100_PWDN_PIN, HIGH);  

  delay(2000);
  
  sd_card.play("1.wav");
  delay(5);    
  while(sd_card.isPlaying()){
    //
  }

  Serial.println("READY");
  
  

  //fDoFTP();
}

void loop() {
  // put your main code here, to run repeatedly:

}
 
In further investigations - I've found that sending data out over the internal DAC does work - although it's rather noisy (perhaps better electrical layout would help alleviate this) which I guess must mean that something in the Ethernet library is interfering with writing to the audio shield.

Any ideas what this might be, anyone?

David
 
I'm using a Teensy 3.2 with a WIZ820io module and audio shield.

You're going to need special wiring for pins 11 and 13 of the WIZ820io to pins 7 and 14 on the audio + Teensy board. Normally MOSI is on pin 11 and SCK is on pin 13. But the audio board needs those pins for digital audio signals, so they can't be used for MOSI and SPI. You need to use the alternate pins, 7 and 14. In hardware, this is a matter of not connecting pins straight through from the other boards to the WIZ820io adaptor. In software, the SPI.setMOSI(7) and SPI.setSCK(14) need to be used before Ethernet.begin().
 
Hi Paul,

Thank you for taking the time to reply. Much appreciated.

Think that I've already done that wiring and set the SPI which I do before resetting the WIZ820io and the SD card - unless I've misunderstood

In hardware, this is a matter of not connecting pins straight through from the other boards to the WIZ820io adaptor.

I'm not using the WIZ820io shield for Teensy but wiring separately to pins. I don't think that I've got any conflicts between those used for the audio shield (although I could be wrong)

With everything wired, the audio shield works until I call Ethernet.begin. I also call Ethernet.init(W5100_SS_PIN); [21] to remove any conflict between that and the SD card. The ethernet and SD card both seem to work OK with the audio shield connected.

Have I missed something obvious?

Pins that I think are in use now are:

Audio:
9: BCLK
11: MCLK
13: RX
15: VOL (not in use)
18: SDA
19: SCL
22: TX
23: LRCLK

SD: (slot on audio shield)
7: MOSI
10: SDCS
12: MISO
14: SCLK

WIZ820io
1: RESET
2: PWDN
7: MOSI
12: MISO
14: SCLK
21: SS

Thanks in advance of any further thoughts.

Very best
David
 
Thanks for the confirmation, Paul. Great to rule that out. Any ideas about what else might be causing the issue? I've had a look through the Ethernet library to see whether any other pins were being allocated but haven't spotted anything. It happens whether I use a sine or sd card input so I don't think it's a card reading issue.

Could there be an issue communicating with the wiz and audio board at the same time?

Thanks again for your help.
David
 
Hi Paul,

I've made some progress with this. Using a sine input, I was able to trace things through until I located where it stopped working.

I got as far as enable_pins in the SPCRemulation class (around line 1121 of avr_emulation.h - depending, I guess on which version you're using)

Around line 1141, if I change:
Code:
        if ((pinout & 1) == 0) {
            CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // DOUT/MOSI = 11 (PTC6)
        } else {
            CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // DOUT/MOSI = 7 (PTD2)
        }

to
Code:
[COLOR=#000000][FONT=Consolas]        [COLOR=#008000]/*[/COLOR]
[COLOR=#008000]        if ((pinout & 1) == 0) {[/COLOR]
[COLOR=#008000]            CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // DOUT/MOSI = 11 (PTC6)[/COLOR]
[COLOR=#008000]        } else {[/COLOR]
[COLOR=#008000]            CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // DOUT/MOSI = 7 (PTD2)[/COLOR]
[COLOR=#008000]        }[/COLOR]
[COLOR=#008000]        */[/COLOR]
        CORE_PIN7_CONFIG = [COLOR=#795e26]PORT_PCR_MUX[/COLOR]([COLOR=#09885a]2[/COLOR]); [COLOR=#008000]// DOUT/MOSI = 7 (PTD2)[/COLOR]
[/FONT][/COLOR]

...things seem to work.

Could this mean that the pinout variable either isn't being set or read as expected in setMOSI and enable_pins, or overwritten somewhere, or do you think that this is just a workaround to a fringe case? I'll make the changes to the library for the time being, but would be nice to be able to eventually code around the problem without having to do this. Are you able to make any suggestions.

Thanks as always
David
 
Oh, maybe this problem is related to the recent changes it the SPI library? Perhaps the old SPIFIFO class now conflicts if using alternate pins?

Kurt, any ideas?

As a quick test, look in w5100.cpp. There's a place where you can edit defines whether Ethernet will use SPIFIFO, or fall back to the regular old SPI library. Try disabling use of SPIFIFO. Does the help?
 
Thanks for the quick reply. I did try that earlier without success but have just again by commenting the line that defines USE_SPIFIFO at the top of the file (doing it properly, I guess) and that seems to work OK :)
 
Now i tried my code with uncommenting
Code:
#if defined(__arm__) && defined(TEENSYDUINO)
#include "SPIFIFO.h"
#ifdef  HAS_SPIFIFO
#define USE_SPIFIFO
#endif
#endif
these block in the w5100.cpp. and i see: it thems working :cool: no overwriting the audio data!!
 
Glad that you've got it working. Do you mean that you commented the block to effectively remove it and force the library to *not* use SPIFIFPO

i.e. change:
Code:
#if defined(__arm__) && defined(TEENSYDUINO)
#include "SPIFIFO.h"
#ifdef  HAS_SPIFIFO
#define USE_SPIFIFO
#endif
#endif

to something along the lines of:
Code:
#if defined(__arm__) && defined(TEENSYDUINO)
#include "SPIFIFO.h"
#ifdef  HAS_SPIFIFO
//#define USE_SPIFIFO
#endif
#endif
 
it looks like:
Code:
/*
#if defined(__arm__) && defined(TEENSYDUINO)
#include "SPIFIFO.h"
#ifdef  HAS_SPIFIFO
#define USE_SPIFIFO
#endif
#endif
*/
but i don´t have checkt the sended data with wireshark. i will do it tomorrow an write here my result.

p.s.

sorry for my bad englisch :rolleyes:
 
I'm now trying to run this on a Teensy 3.6. I'd assumed that it would more or less translate straight over but it doesn't seem so simple.

I think that problem is with pin assignment again when audio objects are declared.

Paul, are things compiled differently on the 3.2 and 3.6. Might this issue be expected?

Very best
David
 
it seems to be working fine in my network :D
Screen RTP Audio.jpg
 
Hmm, that's interesting. Can I ask how you've connected the WIZ820io and audio shield? Any chance you be able to post the code you're using to initialise everything and set up the audio shield. What clock speed are you using?

Cheers
David
 
oh i see. wrong multicast ip :-D so what. i´am playing a little bit around with it :-D
the code i use is on github. i´ve changed the ethernet init from the loop to setup and delete some serial.print();
maybe there can be some errors inside the code. but i hope that will help you.
the pin connections are:
Pinbelegung.jpg
the colored are the used pins wich i have connected. i hope you can see this. i cant´t upload this exel sheet :(
 
Thanks loads for the info. It's a really useful sanity check. We've wired things up in more or less the same way. I'm still having some really weird problems. I see that you're not using any audio objects and that seems to be where the issues I'm finding are coming up. Can I ask what clock speed you're using when you compile? (I assume that you're using the Arduino IDE?)

Mit freundlichen grüßen
David
 
I thought that it would be useful to post some test code on the offchance that someone is able to give me any pointers.

This seems to work fine on a Teensy 3.2. These issues are happening on a Teensy 3.6.

Code:
#define _MOSI_PIN 7
#define _MISO_PIN 12
#define _SCK_PIN 14

#define W5100_RESET_PIN 1
#define W5100_SS_PIN 20

#include "Audio.h"
#include "SD.h"
#include "SPI.h"
#include "Ethernet.h"
#include "MPR121.h"
#include "Wire.h"


byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0xBB };  
IPAddress ip(192, 168, 1, 105);

//AudioPlaySdWav           sd_card;                         
//AudioOutputI2S           soundcard;
//AudioSynthWaveformSine   sine;
/*
AudioConnection         patchcords[]={
                          AudioConnection(sine,0,soundcard,0),
                          AudioConnection(sine,0,soundcard,1)                        
                        };
*/
//AudioControlSGTL5000     sgtl5000_1;

EthernetClient client;

void setup() {
  SPI.setMOSI(_MOSI_PIN);
  SPI.setMISO(_MISO_PIN);
  SPI.setSCK(_SCK_PIN);
  
  Serial.begin(57600);
  Serial.println("hello");

  pinMode(W5100_RESET_PIN, OUTPUT);
  digitalWrite(W5100_RESET_PIN, LOW);    // begin reset the WIZ820io
  delay(1000);
  
  pinMode(W5100_SS_PIN, OUTPUT);
  digitalWrite(W5100_SS_PIN, HIGH);  // de-select WIZ820io

  delay(1000); 
  digitalWrite(W5100_RESET_PIN, HIGH);   // end reset pulse

  AudioMemory(60);

  Serial.println("W5100 restarted");
  delay(3000);

  if (!(SD.begin(BUILTIN_SDCARD))) Serial.println("Unable to access the SD card");
  else Serial.println("accessed sd card");

  Ethernet.init(W5100_SS_PIN);

  Serial.println("Attempting to get an IP address using DHCP:");
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Serial.println("failed to get an IP address using DHCP, trying manually");
    Ethernet.begin(mac, ip);
  }

  Serial.print("My address:");
  Serial.println(Ethernet.localIP()); 

  delay(100);

  Serial.println("READY");

  if (client.connect("breadart.co.uk",80)) {
    Serial.println("ETHERNET: Command connected");
    client.println("GET styles.css");

    File fhFileHandle;
    SD.remove("TESTFILE.TXT");
    fhFileHandle=SD.open("TESTFILE.TXT", FILE_WRITE);
    Serial.print("FTP: SD: attempting to open handle to file **");
    Serial.print("TESTFILE.TXT");
    if(!fhFileHandle) Serial.println("** -> FAILED");
    else {    
      Serial.println("** -> SUCCESS\nDOWNLOADING...");

      for(int i=0; i<20; i++) Serial.print("         |");
      Serial.print('\n');

      int iByteCount=0;
      
      while(client.connected()) {
        while(client.available()){
          byte by=client.read();
          fhFileHandle.write(by);
          iByteCount++;
          if(iByteCount%100 == 0) Serial.print("-"); 
        }
      }

      fhFileHandle.close();
      Serial.println();
      Serial.println("SUCCESS!!");
    }
  }
  else Serial.println("ETHERNET: Command connection failed");

}

void loop() {
  // put your main code here, to run repeatedly:

}

At 180MHz, with the AudioOutputI2s line commented out, the code will obtain an IP address, go online and download the test file from my server.
with the line uncommented, it's unable to get an IP address.

At 96MHz, it can sometimes get an IP address but is unable to connect and download the file. Again, with the line commented out, things work as expected.

At 72MHz, everything seems to work.

Any thoughts, anyone?

Very best
David
 
Mit freundlichen grüßen
David
Do you speak german?

I use the Arduino IDE with the teensyduino. The speed from teensy is 180mhz. no extra compile options.

somewhere i read that some trouble can be with the sequence of the included files.
try:
Code:
#include "Ethernet.h"
#include "Audio.h"
#include "SD.h"
#include "SPI.h"
#include "MPR121.h"
#include "Wire.h"
 
Do you speak german?
Ja, etwas. Nicht so gut wie ich moechte!

Thank you for the suggestion. It doesn't solve the problem, unfortunately! I'll keep trying. In the meantime, I think that I'll stick with the 3.2 which seems to be working quite well!
 
Klingt doch bisher ganz gut :)

On wich frequenz does The 3.2 works? When they are different it could be a timing problem.
 
Last edited:
Help!

Having a very similar problem to OP. Just updated some hardware that I've been using for a few years now, has worked fine for a long time, but after uploading my small changes to the code, Audio has now stopped working.

Similar setup to OP, using Audio and Ethernet. When I run the example FFT sketch it runs fine, but when I simply add Ethernet.begin(mac,ip) to the sketch, I get all zeros back from the FFT.

I've changed the proper SPI pins before calling anything (plus setting pin 6 high, this used to be used for resetting the WIznet chip, but is no longer used in current libraries). Here's a stripped down version of my sketch, when Ethernet.begin() is commented out, it works, when it's not, it doesn't.

Code:
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioInputI2S          audioInput;         // audio shield: mic or line-in
AudioAnalyzeFFT256     myFFT;
AudioConnection        patchCord1(audioInput, 0, myFFT, 0);
AudioControlSGTL5000   audioShield;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x6E, 0x44 };
IPAddress ip(192, 168, 0, 105);

void setup() {

  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);

  SPI.setMOSI(7);
  SPI.setSCK(14);

  AudioMemory(12);
  audioShield.enable();
  audioShield.inputSelect(AUDIO_INPUT_LINEIN);
  myFFT.windowFunction(AudioWindowHanning256);

  Ethernet.begin(mac, ip);

}

void loop() {
  float n;
  int i;

  if (myFFT.available()) {
    // each time new FFT data is available
    // print it all to the Arduino Serial Monitor
    Serial.print("FFT: ");
    for (i=0; i<40; i++) {
      n = myFFT.read(i);
      if (n >= 0.01) {
        Serial.print(n);
        Serial.print(" ");
      } else {
        Serial.print("  -  "); // don't print "0.00"
      }
    }
    Serial.println();
  }
}

The loop runs, but returns all zeros when Ethernet is enabled :( Any help here would be greatly appreciated. Made one small change to my code, have an event tomorrow that I need to bring this to, and now I've got nothing! Maybe I'll go back to an old version of teensyduino to get a working project, but for now it seems like Ethernet + Audio is broken.

Edit: For reference, running Teensyduino 1.40 with Arduino 1.8.5 on Teensy 3.2 with Teensy Audioshield and Wiznet820io. I tried changing the CPU frequency to 72MHz but didn't fix it, currently running at 96Mhz. Also, Ethernet works properly whether or not Audio is included or not, no problems there. Seems like the Ethernet library is overwriting some the Audio pin configurations, I'm digging through the core now, but haven't found anything yet...
 
Last edited:
Glad that you've got it working. Do you mean that you commented the block to effectively remove it and force the library to *not* use SPIFIFPO

i.e. change:
Code:
#if defined(__arm__) && defined(TEENSYDUINO)
#include "SPIFIFO.h"
#ifdef  HAS_SPIFIFO
#define USE_SPIFIFO
#endif
#endif

to something along the lines of:
Code:
#if defined(__arm__) && defined(TEENSYDUINO)
#include "SPIFIFO.h"
#ifdef  HAS_SPIFIFO
//#define USE_SPIFIFO
#endif
#endif

This is the fix for now!! My IDE wasn't showing me the right compiler definitions, so didn't think that was even active, but sure enough it was. So, something wrong with the way SPIFIFO works, don't have time to dig into it right now, but I'm sure someone smarter will! Thank you David_B for the preemptive fix!
 
Status
Not open for further replies.
Back
Top