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

Thread: Teensy with 2 Wiz820io units

  1. #1
    Junior Member
    Join Date
    Oct 2016
    Posts
    17

    Teensy with 2 Wiz820io units

    Hi,

    Anyone know if it is possible to have 2 of the Wiz820io units on a teensy.

    I have tried and I can get one or the other to work but not both at the same time. If I enable both only one works.

    Possible to do with the chip select line. (Or a code error?)

    /*
    Dual EtherNET

    C0nnect up 2 wiznet chips and use the same MISO MOSI CLK lines and even the same reset, but
    use a different chip select and maybe we can poll them?

    Simon Wakley Jan 2021

    This code is in the public domain.
    */

    #define WIZ1 1
    #define WIZ2 1

    #include <Ethernet.h>
    #include <EthernetUdp.h>

    #define GREEN_LED 20 // Just a green LED on Pin 20 to ground
    #define WIZNET_RESET 6 // Wired to both Resets

    #define WIZNET1_CS 17 // Wiznet1 Chip Select nSS
    #define WIZNET2_CS 18 // Wiznet2

    #ifdef WIZ1
    byte mac1[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress ip1(192, 168, 1, 179);
    unsigned int localPort1 = 8899; // local port to listen on
    #endif

    #ifdef WIZ2
    byte mac2[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };
    IPAddress ip2(192, 168, 1, 177);
    unsigned int localPort2 = 8877; // local port to listen on
    #endif

    // buffers for receiving and sending data
    char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
    char ReplyBuffer[] = "acknowledged"; // a string to send back

    // An EthernetUDP instance to let us send and receive packets over UDP
    #ifdef WIZ1
    EthernetUDP Udp1;
    #endif

    #ifdef WIZ2
    EthernetUDP Udp2;
    #endif

    void EthSelect(int id)
    {

    if (id == WIZNET1_CS)
    {
    digitalWrite(WIZNET2_CS, HIGH); // Turn it off
    Ethernet.init(WIZNET1_CS);
    }
    else
    {
    digitalWrite(WIZNET1_CS, HIGH); // Turn it off
    Ethernet.init(WIZNET2_CS);
    }
    }


    void setup()
    {

    pinMode(GREEN_LED, OUTPUT);

    // This is to disable the Wiz until ready to go
    pinMode(WIZNET_RESET, OUTPUT);
    delay(100);
    digitalWrite(WIZNET_RESET, LOW);
    delay(10);
    digitalWrite(WIZNET_RESET, HIGH);
    delay(100);

    pinMode(WIZNET1_CS, OUTPUT);
    pinMode(WIZNET2_CS, OUTPUT);

    #ifdef WIZ1
    // Wiznet Chip Select Line


    //Ethernet.init(WIZNET1_CS); // Most Arduino shields

    EthSelect(WIZNET1_CS);
    // start the Ethernet
    Ethernet.begin(mac1, ip1);
    #endif

    #ifdef WIZ2
    EthSelect(WIZNET1_CS);

    // start the second Ethernet
    Ethernet.begin(mac2, ip2);
    #endif

    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
    }


    #ifdef WIZ1
    // Check for Ethernet hardware present
    Ethernet.init(WIZNET1_CS); // Most Arduino shields

    if (Ethernet.hardwareStatus() == EthernetNoHardware)
    Serial.println("Ethernet shield 1 was not found. Sorry, can't run without hardware. ");


    if (Ethernet.linkStatus() == LinkOFF)
    {
    Serial.println("Ethernet cable 1 is not connected.");
    }
    #endif

    #ifdef WIZ2

    EthSelect(WIZNET2_CS);
    //Ethernet.init(WIZNET2_CS); // Most Arduino shields
    if (Ethernet.hardwareStatus() == EthernetNoHardware)
    Serial.println("Ethernet shield 2 was not found. Sorry, can't run without hardware. ");


    if (Ethernet.linkStatus() == LinkOFF)
    {
    Serial.println("Ethernet cable 2 is not connected.");
    }
    #endif

    // start UDP
    #ifdef WIZ1
    Udp1.begin(localPort1);
    #endif

    #ifdef WIZ2
    Udp2.begin(localPort2);
    #endif
    }


    int count = 0;
    void loop()
    {
    // if there's data available, read a packet
    int packetSize;

    #ifdef WIZ1
    //Ethernet.init(WIZNET1_CS); // Device 1
    EthSelect(WIZNET1_CS);

    packetSize= Udp1.parsePacket();

    if (packetSize)
    {
    Serial.print("Received packet from 1 of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp1.remoteIP();
    for (int i=0; i < 4; i++) {
    Serial.print(remote[i], DEC);
    if (i < 3) {
    Serial.print(".");
    }
    }
    Serial.print(", port ");
    Serial.println(Udp1.remotePort());

    // read the packet into packetBufffer
    Udp1.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply to the IP address and port that sent us the packet we received
    Udp1.beginPacket(Udp1.remoteIP(), Udp1.remotePort());
    Udp1.write(ReplyBuffer);
    Udp1.endPacket();
    }
    #endif

    #ifdef WIZ2

    //Ethernet.init(WIZNET2_CS); // Device 2
    EthSelect(WIZNET2_CS);

    packetSize= Udp2.parsePacket();
    if (packetSize)
    {
    Serial.print("Received packet from 2 of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp2.remoteIP();
    for (int i=0; i < 4; i++) {
    Serial.print(remote[i], DEC);
    if (i < 3) {
    Serial.print(".");
    }
    }
    Serial.print(", port ");
    Serial.println(Udp2.remotePort());

    // read the packet into packetBufffer
    Udp2.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply to the IP address and port that sent us the packet we received
    Udp2.beginPacket(Udp2.remoteIP(), Udp2.remotePort());
    Udp2.write(ReplyBuffer);
    Udp2.endPacket();
    }
    #endif

    delay(10);

    count++;

    // Always nice to see a flickering light to know it's running
    if ((count % 10) < 5)
    digitalWrite(GREEN_LED, HIGH);
    else
    digitalWrite(GREEN_LED, LOW);
    }Click image for larger version. 

Name:	IMG_2219.jpg 
Views:	10 
Size:	152.8 KB 
ID:	23098Click image for larger version. 

Name:	IMG_2219.jpg 
Views:	10 
Size:	152.8 KB 
ID:	23098
    Attached Files Attached Files

Posting Permissions

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