43 #include <FlowScanner.h>
51 static const uint8_t
CS_PIN = 10;
57 static IPAddress_P NTPServerIP = { 206, 246, 122, 250 };
96 uint8_t versionNumber : 3;
97 uint8_t leapIndicator : 2;
99 uint8_t pollingInterval;
102 NTPShort rootDispersion;
105 uint32_t referenceId;
106 char referenceStr[4];
108 NTPTimestamp reference;
110 NTPTimestamp receive;
111 NTPTimestamp transmit;
118 static const int timeZone = 1;
119 static const uint16_t NTP_UPDATE_INTERVAL_TICKS = NTICKS(10 * 60 * 1000);
122 static const unsigned long secs1900_1970 = (17UL * 366UL + (70UL - 17UL) * 365UL) * 24UL * 60UL * 60UL;
126 uint32_t millis_offset;
128 uint16_t updateTimer;
130 bool isLeapYear(uint16_t y)
132 return !((1970 + y) % 4) && (((1970 + y) % 100) || !((1970 + y) % 400));
149 remoteIP = NTPServerIP;
159 msg.leapIndicator = 3;
160 msg.versionNumber = 3;
163 msg.pollingInterval = 17;
164 msg.precision = 0xFA;
165 msg.rootDelay.fraction.zero();
166 msg.rootDelay.seconds.zero();
167 msg.rootDispersion.seconds = 1;
168 msg.rootDispersion.fraction = 0;
170 msg.reference.zero();
171 msg.reference.fraction = 0;
183 ACASSERT(len == 48,
"Incorrect frame length %d", len);
185 millis_offset = millis();
187 NTPMessage& msg = *(NTPMessage*)packet.udpData;
189 epoch = msg.transmit.seconds - secs1900_1970;
196 const uint8_t daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
198 uint32_t ep = epoch + (millis() - millis_offset) / 1000;
204 hour = ((ep % 24) + timeZone) % 24;
206 weekDay = ((ep + 4) % 7);
210 while ((uint32_t)(days += (isLeapYear(year) ? 366 : 365)) <= ep)
215 days -= isLeapYear(year) ? 366 : 365;
223 for (uint8_t m = 0; m < 12; m++)
225 dm = (m == 1) ? (isLeapYear(year) ? 29 : 28) : daysInMonth[m];
251 if (updateTimer == 0 || !inSync())
253 updateTimer = NTP_UPDATE_INTERVAL_TICKS;
274 Serial.begin(115200);
276 #ifdef ACROSS_ARDUINO
277 ACross::printf_serial_init();
280 printf(PSTR(
"NTP/UDP Client EtherDune sample\n"));
281 Serial.print(F(
"Free RAM: ")); Serial.println(ACross::getFreeRam());
282 Serial.println(F(
"Press any key to start..."));
284 while (!Serial.available());
294 ACERROR(
"failed to start EtherDune");
296 ACINFO(
"waiting for link...");
300 ACINFO(
"link is up");
314 if ((int32_t)(millis() - timer) >= 0)
323 printf_P(PSTR(
"Current date/time: %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n"), ntp.year, ntp.month, ntp.day, ntp.hour, ntp.minute, ntp.second);
328 Serial.println(F(
"Waiting for time sync..."));
void zero()
sets the variable to 0.
virtual void onReceive(uint16_t len)
Called when a datagram has arrived.
#define IPAddress_P
helper macro to store an IP address in PROGMEM
Implements the UDP protocol.
uint16_t write(uint16_t len, const byte *data)
In the case of TCP, writes the given data buffer to the socket.
static IPAddress_P gatewayIP
static bool begin(uint8_t cspin)
Initializes EtherDune and the underlying hardware
void tick()
This is a timer function that is called every NETWORK_TIMER_RESOLUTION milliseconds on every service...
bool send()
Sends the packet currently in the outgoing buffer
static IPAddress netmask
Subnet mask.
static IPAddress gatewayIP
IP address of the gateway in this network.
static const uint8_t CS_PIN
Represents a network byte order 32 bit integer.
static MACAddress_P mymac
Represents a network byte order 16 bit integer.
static bool isLinkUp()
Determines whether the network link is ready
static MACAddress localMAC
Ethernet MAC address.
Base data structures for Internet communication.
static void loop()
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets...
static IPAddress localIP
IP address of this application.
static IPAddress_P netmask