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

Thread: Audio and Ethernet

  1. #1

    Audio and Ethernet

    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...-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:
    
    }

  2. #2
    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

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    15,144
    Quote Originally Posted by David_B View Post
    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().

  4. #4
    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

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    15,144
    Yes, those connects look ok.

  6. #6
    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

  7. #7
    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:
            /*
            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)
            }
            */
            CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // DOUT/MOSI = 7 (PTD2)
    
    ...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

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    15,144
    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?

  9. #9
    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

  10. #10
    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 no overwriting the audio data!!

  11. #11
    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

  12. #12
    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

  13. #13
    How did you get on?
    Quote Originally Posted by sourceforge807 View Post
    sorry for my bad englisch
    Seems fine to me! ☺️

  14. #14
    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

  15. #15
    it seems to be working fine in my network
    Click image for larger version. 

Name:	Screen RTP Audio.jpg 
Views:	7 
Size:	80.2 KB 
ID:	11539

  16. #16
    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

  17. #17
    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:
    Click image for larger version. 

Name:	Pinbelegung.jpg 
Views:	7 
Size:	99.6 KB 
ID:	11540
    the colored are the used pins wich i have connected. i hope you can see this. i cant´t upload this exel sheet

  18. #18
    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

  19. #19
    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

  20. #20
    Quote Originally Posted by David_B View Post
    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"

  21. #21
    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!

  22. #22
    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 by sourceforge807; 09-19-2017 at 11:43 AM.

Posting Permissions

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