Skip to content

Commit 1d0d3f5

Browse files
committed
Record the NTP request arrival time - without interrupts
1 parent 2ea71d4 commit 1d0d3f5

File tree

5 files changed

+25
-34
lines changed

5 files changed

+25
-34
lines changed

Firmware/RTK_Everywhere/Begin.ino

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,6 @@ void beginFS()
778778
}
779779

780780
// Check if configureViaEthernet.txt exists
781-
// Used to indicate if SparkFun_WebServer_ESP32_W5500 needs _exclusive_ access to SPI and interrupts
782781
bool checkConfigureViaEthernet()
783782
{
784783
if (online.fs == false)
@@ -796,7 +795,6 @@ bool checkConfigureViaEthernet()
796795
}
797796

798797
// Force configure-via-ethernet mode by creating configureViaEthernet.txt in LittleFS
799-
// Used to indicate if SparkFun_WebServer_ESP32_W5500 needs _exclusive_ access to SPI and interrupts
800798
bool forceConfigureViaEthernet()
801799
{
802800
if (online.fs == false)
@@ -836,8 +834,6 @@ void beginInterrupts()
836834
{
837835
DMW_if systemPrintf("pin_Ethernet_Interrupt: %d\r\n", pin_Ethernet_Interrupt);
838836
pinMode(pin_Ethernet_Interrupt, INPUT); // Prepare the interrupt pin
839-
// TODO: figure out how to handle NTP mode and timestamp the arrival of UDP NTP requests
840-
// attachInterrupt(pin_Ethernet_Interrupt, ethernetISR, FALLING); // Attach the interrupt
841837
}
842838
#endif // COMPILE_ETHERNET
843839
}

Firmware/RTK_Everywhere/Ethernet.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ bool ethernetIsNeeded()
208208
return needed;
209209
}
210210

211+
/*
211212
// Ethernet (W5500) ISR
212213
// Triggered by the falling edge of the W5500 interrupt signal - indicates the arrival of a packet
213214
// Record the time the packet arrived
@@ -218,6 +219,7 @@ void ethernetISR()
218219
// Do it in updateEthernet
219220
gettimeofday((timeval *)&ethernetNtpTv, nullptr); // Record the time of the NTP interrupt
220221
}
222+
*/
221223

222224
// Restart the Ethernet controller
223225
void ethernetRestart()
@@ -393,7 +395,7 @@ void onEthernetEvent(arduino_event_id_t event, arduino_event_info_t info)
393395
// Web server routines
394396
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
395397

396-
// Start Ethernet WebServer ESP32 W5500 - needs exclusive access to WiFi, SPI and Interrupts
398+
// Start Ethernet for the web server
397399
void ethernetWebServerStartESP32W5500()
398400
{
399401
Network.onEvent(onEthernetEvent);
@@ -405,10 +407,10 @@ void ethernetWebServerStartESP32W5500()
405407
ETH.config(settings.ethernetIP, settings.ethernetGateway, settings.ethernetSubnet, settings.ethernetDNS);
406408
}
407409

408-
// Stop the Ethernet web server
410+
// Stop Ethernet for the web server
409411
void ethernetWebServerStopESP32W5500()
410412
{
411-
ETH.end(); // This is _really_ important. It undoes the low-level changes to SPI and interrupts
413+
ETH.end();
412414
}
413415

414416
#endif // COMPILE_ETHERNET

Firmware/RTK_Everywhere/NTP.ino

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ const RtkMode_t ntpServerMode = RTK_MODE_NTP;
7878

7979
static NetworkUDP *ntpServer; // This will be instantiated when we know the NTP port
8080
static uint8_t ntpServerState;
81-
static volatile uint8_t
82-
ntpSockIndex; // The W5500 socket index for NTP - so we can enable and read the correct interrupt
8381
static uint32_t lastLoggedNTPRequest;
8482

8583
//----------------------------------------
@@ -474,7 +472,7 @@ struct NTPpacket
474472

475473
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
476474
// NTP process one request
477-
// recTv contains the timeval the NTP packet was received - from the W5500 interrupt
475+
// recTv contains the timeval the NTP packet was received
478476
// syncTv contains the timeval when the RTC was last sync'd
479477
// ntpDiag will contain useful diagnostics
480478
bool ntpProcessOneRequest(bool process, const timeval *recTv, const timeval *syncTv, char *ntpDiag = nullptr,
@@ -486,19 +484,24 @@ bool ntpProcessOneRequest(bool process, const timeval *recTv, const timeval *syn
486484
if (ntpDiag != nullptr)
487485
*ntpDiag = 0; // Clear any existing diagnostics
488486

489-
int packetDataSize = ntpServer->parsePacket();
487+
gettimeofday((timeval *)&ethernetNtpTv, nullptr); // Record the possible time of the NTP request
490488

491-
IPAddress remoteIP = ntpServer->remoteIP();
492-
uint16_t remotePort = ntpServer->remotePort();
489+
int packetDataSize = 0;
493490

494-
if (ntpDiag != nullptr) // Add the packet size and remote IP/Port to the diagnostics
495-
{
496-
snprintf(ntpDiag, ntpDiagSize, "NTP request from: Remote IP: %d.%d.%d.%d Remote Port: %d\r\n", remoteIP[0],
497-
remoteIP[1], remoteIP[2], remoteIP[3], remotePort);
498-
}
491+
if (ntpServer->available() > 0)
492+
packetDataSize = ntpServer->parsePacket();
499493

500-
if (packetDataSize && (packetDataSize >= NTPpacket::NTPpacketSize))
494+
if (packetDataSize >= NTPpacket::NTPpacketSize)
501495
{
496+
IPAddress remoteIP = ntpServer->remoteIP();
497+
uint16_t remotePort = ntpServer->remotePort();
498+
499+
if (ntpDiag != nullptr) // Add the packet size and remote IP/Port to the diagnostics
500+
{
501+
snprintf(ntpDiag, ntpDiagSize, "NTP request from: Remote IP: %d.%d.%d.%d Remote Port: %d\r\n", remoteIP[0],
502+
remoteIP[1], remoteIP[2], remoteIP[3], remotePort);
503+
}
504+
502505
// Read the NTP packet
503506
NTPpacket packet;
504507

@@ -774,7 +777,6 @@ void ntpServerStop()
774777
// Release the NTP server memory
775778
if (ntpServer)
776779
{
777-
w5500DisableSocketInterrupt(ntpSockIndex); // Disable the receive interrupt
778780
ntpServer->stop();
779781
delete ntpServer;
780782
ntpServer = nullptr;
@@ -850,12 +852,7 @@ void ntpServerUpdate()
850852
ntpServerStop();
851853
else
852854
{
853-
// Start the NTP server
854-
// TODO
855-
//ntpServer->begin(settings.ethernetNtpPort);
856-
//ntpSockIndex = ntpServer->getSockIndex(); // Get the socket index
857-
//w5500ClearSocketInterrupts(); // Clear all interrupts
858-
//w5500EnableSocketInterrupt(ntpSockIndex); // Enable the RECV interrupt for the desired socket index
855+
ntpServer->begin(settings.ethernetNtpPort); // Start the NTP server
859856
online.ethernetNTPServer = true;
860857
if (!inMainMenu)
861858
reportHeapNow(settings.debugNtp);
@@ -872,9 +869,6 @@ void ntpServerUpdate()
872869

873870
else
874871
{
875-
if (w5500CheckSocketInterrupt(ntpSockIndex))
876-
w5500ClearSocketInterrupt(ntpSockIndex); // Clear the socket interrupt here
877-
878872
// Check for new NTP requests - if the time has been sync'd
879873
bool processed = ntpProcessOneRequest(systemState == STATE_NTPSERVER_SYNC, (const timeval *)&ethernetNtpTv,
880874
(const timeval *)&gnssSyncTv, ntpDiag, sizeof(ntpDiag));

Firmware/RTK_Everywhere/RTK_Everywhere.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,9 @@ correctionsSource pplCorrectionsSource = CORR_NUM; // Record which source is fee
788788

789789
// configureViaEthernet:
790790
// Set to true if configureViaEthernet.txt exists in LittleFS.
791-
// Causes setup and loop to skip any code which would cause SPI or interrupts to be initialized.
792-
// This is to allow SparkFun_WebServer_ESP32_W5500 to have _exclusive_ access to WiFi, SPI and Interrupts.
791+
// Previously, the SparkFun_WebServer_ESP32_W5500 needed _exclusive_ access to SPI and Interrupts.
792+
// That's no longer true - thanks to Espressif adding full support for the W5500 within the
793+
// arduino-esp32 core (v3.0.0+). But it's easier to leave the code as it is.
793794
bool configureViaEthernet;
794795

795796
int floatLockRestarts;
@@ -1106,7 +1107,7 @@ void setup()
11061107
gnssBeginPPS(); // Configure the time pulse output
11071108

11081109
DMW_b("beginInterrupts");
1109-
beginInterrupts(); // Begin the TP and W5500 interrupts
1110+
beginInterrupts(); // Begin the TP interrupts
11101111

11111112
DMW_b("beginButtons");
11121113
beginButtons(); // Start task for button monitoring.

Firmware/RTK_Everywhere/States.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,6 @@ void stateUpdate()
617617
RTK_MODE(RTK_MODE_ETHERNET_CONFIG);
618618
// The code should only be able to enter this state if configureViaEthernet is true.
619619
// If configureViaEthernet is not true, we need to restart again.
620-
//(If we continue, startEthernerWebServerESP32W5500 will fail as it won't have exclusive access to SPI and
621-
// ints).
622620
if (!configureViaEthernet)
623621
{
624622
displayConfigViaEthStarting(1500);

0 commit comments

Comments
 (0)