@@ -52,6 +52,7 @@ Wiznet5500lwIP eth(GIZMO_HW_ENET_CS, SPI, GIZMO_HW_ENET_INT);
5252WiFiUDP udp;
5353IPAddress dsIP;
5454IPAddress gizmoIP;
55+ byte mac[6 ];
5556
5657bool enetAvailable = false ;
5758
@@ -190,7 +191,6 @@ void GizmoSetup() {
190191 WiFi.setHostname (cfg.hostname );
191192 WiFi.noLowPowerMode ();
192193 WiFi.setTimeout (500 );
193- WiFi.config (gizmoIP);
194194
195195 // Enable the hardware watchdog. If we've gotten stuck for more
196196 // than a few seconds something has broken - badly.
@@ -202,11 +202,17 @@ void GizmoSetup() {
202202 byte d3 = (cfg.teamNumber % 1000 ) % 100 / 10 * 16 ;
203203 byte d4 = (cfg.teamNumber % 1000 ) % 100 % 10 ;
204204
205- byte mac[] = {0x02 , 0x00 , 0x00 , byte (d1+d2), byte (d3+d4), 0x00 };
205+ mac[0 ] = 0x02 ;
206+ mac[1 ] = 0x00 ;
207+ mac[2 ] = 0x00 ;
208+ mac[3 ] = byte (d1+d2);
209+ mac[4 ] = byte (d3+d4);
210+ mac[5 ] = 0x00 ;
206211 netLinkResetWiznet ();
207212 // Necessary to wait here to allow the Wiznet time to come back.
208213 delay (50 );
209214 eth.setSPISpeed (30000000 );
215+
210216 if (!eth.begin (mac)) {
211217 Serial.println (" GIZMO_ENET_INIT_FAILED" );
212218 } else {
@@ -217,7 +223,6 @@ void GizmoSetup() {
217223
218224 statusUpdate ();
219225 status.Update ();
220- netState = NET_SEARCH;
221226}
222227
223228// Tick runs all the functions that need to happen on every loop
@@ -313,7 +318,7 @@ bool loadConfig(String path) {
313318
314319 // This works because we can assert that the driver's station is at
315320 // a fixed address.
316- dsIP = IPAddress (10 , byte (cfg.teamNumber /100 ), byte (cfg.teamNumber %100 ), 2 );
321+ dsIP = IPAddress (10 , byte (cfg.teamNumber /100 ), byte (cfg.teamNumber %100 ), 2 );
317322 gizmoIP = IPAddress (10 , byte (cfg.teamNumber /100 ), byte (cfg.teamNumber %100 ), 3 );
318323
319324 Serial.print (" GIZMO_CFG_TEAM " );
@@ -447,11 +452,17 @@ void netStateLinkSearch() {
447452 netState = NET_CONNECT_WAIT_ENET;
448453 Serial.println (" GIZMO_NET_ENET_ATTEMPT" );
449454 netStateConnectTimeout = millis () + 5000 ;
455+ WiFi.config (0 );
456+ eth.end ();
457+ eth.config (gizmoIP);
458+ eth.begin (mac);
450459 WiFi.disconnect ();
451460 WiFi.end ();
452461 return ;
453462 }
454463
464+ eth.config (0 );
465+ WiFi.config (gizmoIP);
455466 Serial.println (" GIZMO_NET_WIFI_ATTEMPT" );
456467 netState = NET_CONNECT_WIFI;
457468 netStateConnectTimeout = millis () + 30000 ;
@@ -476,14 +487,14 @@ void netStateConnectWaitEnet() {
476487 netState = NET_BIND;
477488 netLink = NET_WIRED;
478489 nextControlPacketDueBy = millis () + 2000 ;
479- Serial.println (" GIZMO_NET_IP " + eth.localIP ().toString ());
490+ Serial.println (" GIZMO_NET_IP_WIFI " + WiFi.localIP ().toString ());
491+ Serial.println (" GIZMO_NET_IP_ENET " + eth.localIP ().toString ());
480492 Serial.println (" GIZMO_NET_WIRED_RUN" );
481493}
482494
483495void netStateWifiConnect () {
484496 Serial.println (" GIZMO_NET_WIFI_START" );
485497 WiFi.mode (WIFI_STA);
486- WiFi.config (gizmoIP);
487498 WiFi.beginNoBlock (cfg.netSSID .c_str (), cfg.netPSK .c_str ());
488499
489500 netState = NET_CONNECT_WAIT_WIFI;
@@ -510,11 +521,13 @@ void netStateConnectWaitWifi() {
510521 netState = NET_BIND;
511522 netLink = NET_WIRELESS;
512523 nextControlPacketDueBy = millis () + 2000 ;
513- Serial.println (" GIZMO_NET_IP " + WiFi.localIP ().toString ());
524+ Serial.println (" GIZMO_NET_IP_WIFI " + WiFi.localIP ().toString ());
525+ Serial.println (" GIZMO_NET_IP_ENET " + eth.localIP ().toString ());
514526 Serial.println (" GIZMO_NET_WIFI_RUN" );
515527}
516528
517529void netStateBind () {
530+ udp.stopAll ();
518531 if (!udp.begin (1729 )) {
519532 Serial.println (" GIZMO_NET_BIND_FAIL" );
520533 delay (100 );
@@ -539,7 +552,6 @@ void netStateRun() {
539552
540553 if (nextControlPacketDueBy < millis ()) {
541554 status.SetControlConnected (false );
542- Serial.printf (" GIZMO_CTRL_TIMEOUT %d\r\n " , (millis () - nextControlPacketDueBy));
543555 zeroizeCState ();
544556 return ;
545557 }
@@ -548,7 +560,7 @@ void netStateRun() {
548560 // this is only necessary when running the polling mode ethernet
549561 // lwIP interface.
550562 if (netLink == NET_WIRED) {
551- delay (1 );
563+ delay (5 );
552564 }
553565}
554566
0 commit comments