Commit f606849f authored by Martin Vítek's avatar Martin Vítek

Add regular SNTP update

parent 49b78b51
......@@ -54,8 +54,8 @@ namespace ENC28J60_namespace
sys.delay_ms(2); //Fix for issue 2, viz errata, page 3
//Set-up RX & TX buffer - RX=6001B, TX=2190B
set_eth_rx_buffer_size(6001);
//Set-up RX & TX buffer - RX=~6599B, TX=~1580B
set_eth_rx_buffer_size(6599);
//Set-up receive filters - accept only if CRC is OK, unicast, match pattern
write_control_register(ERXFCON, UCEN_bm | CRCEN_bm | PMEN_bm);
......@@ -107,18 +107,18 @@ namespace ENC28J60_namespace
bool reenable = false;
//Fixed: errata issue 5 - RX buffer should start at 0x0000
//Fixed: errata issue 14 - ERXRDPT must be odd
//Fixed: errata issue 14 - ERXND must be odd
//if size is even (-> ERXND is even) make it odd
if ((size-1)%2 == 0) size += 1;
if (size%2 == 0) size += 1;
if (size > 0x1FFF) return;
//TODO: this variables aren't necessary, values can be readed from ERXST and ERXND
eth_rx_buffer_start = 0x0000;
eth_rx_buffer_end = size-1;
eth_rx_buffer_end = size;
eth_tx_buffer_start = size;
eth_tx_buffer_start = size+1;
eth_tx_buffer_end = 0x1FFF;
/*
......@@ -136,17 +136,17 @@ namespace ENC28J60_namespace
reenable = true;
}
//Start of RX buffer - by size parameter
//Start of RX buffer - 0
write_control_register(ERXSTL, eth_rx_buffer_start&0xFF);
write_control_register(ERXSTH, eth_rx_buffer_start>>8);
//End of RX buffer - 0x1FFF
//End of RX buffer - by size parameter
write_control_register(ERXNDL, eth_rx_buffer_end&0xFF);
write_control_register(ERXNDH, eth_rx_buffer_end>>8);
//For tracking purposes
write_control_register(ERXRDPTL, eth_rx_buffer_end&0xFF);
write_control_register(ERXRDPTL, eth_rx_buffer_end>>8);
write_control_register(ERXRDPTL, eth_rx_buffer_start&0xFF);
write_control_register(ERXRDPTH, eth_rx_buffer_start>>8);
//Start address of first packet
eth_rx_packet_start = eth_rx_buffer_start;
......@@ -537,7 +537,7 @@ namespace ENC28J60_namespace
return false;
}
frame->payload.length = (status.bit.rx_byte_count) - 18; //Payload length including padding
frame->payload.length = (status.bit.rx_byte_count) - 18; //Payload length including padding, excluding ethernet header and CRC
//Destination address
read_buffer_memory(frame->dest_address, 6);
......
......@@ -30,6 +30,9 @@ bool IP_protocol::decode(const ethernet_payload& rx_payload, ethernet_payload& t
//Trash not mine IP destinations
if (IP(rx_packet->dst_address) != local_ip) return false;
//Check if total length is bigger than header
if (__ntohs(rx_packet->tl) < (rx_packet->ihl*4)) return false;
uint16_t data_length = get_data_length(rx_packet->ihl, rx_packet->tl);
switch (rx_packet->protocol)
......@@ -45,14 +48,16 @@ bool IP_protocol::decode(const ethernet_payload& rx_payload, ethernet_payload& t
tx_packet->ttl = rx_packet->ttl;
tx_packet->protocol = ICMP;
tx_packet->checksum = 0;
tx_packet->src_address = local_ip.ip.value;
tx_packet->dst_address = rx_packet->src_address;
tx_packet->checksum = checksum((uint8_t*)tx_packet, __ntohs(tx_packet->tl));
tx_packet->checksum = checksum((uint8_t*)tx_packet, tx_packet->ihl*4);
tx_payload.length = __ntohs(tx_packet->tl);
debug.message("Sending ICMP echo reply");
return true;
}
......
......@@ -15,6 +15,9 @@
#include "ENC28J60.h"
#include "EEPROM_25AA02E48.h"
#include "StatusLED.h"
#include "NetworkTypes.h"
#include "NixieStack.h"
#include "System.h"
using namespace ENC28J60_namespace;
......@@ -35,7 +38,8 @@ NixieClock nixie;
NixieClock::NixieClock(): display(),
separator(),
old_time(0),
state(DISPLAYING_TEMPERATURE)
state(DISPLAYING_TEMPERATURE),
sntp_server_ip({147, 228, 57, 10}) //clock1.zcu.cz
{
setlocale(LC_TIME, "cs_CZ");
......@@ -43,6 +47,7 @@ NixieClock::NixieClock(): display(),
tzset();
/*
time_date.tm_hour = 9;
time_date.tm_min = 25;
time_date.tm_sec = 0;
......@@ -51,6 +56,7 @@ NixieClock::NixieClock(): display(),
time_date.tm_mday = 9;
seconds = mktime(&time_date);
*/
}
void NixieClock::display_time()
......@@ -102,6 +108,24 @@ void NixieClock::display_handler()
}
}
void NixieClock::sntp_handler()
{
if (time(NULL) > (stack.sntp.last_update + SNTP_UPDATE_INTERVAL))
{
//Give SNTP 60seconds, then retry
stack.sntp.last_update += SNTP_UPDATE_INTERVAL + SNTP_RETRY_INTERVAL;
stack.send_sntp_request(sntp_server_ip);
debug.message("SNTP request");
sys.delay_ms(500);
sys.delay_ms(500);
sys.delay_ms(500);
sys.delay_ms(500);
}
}
void NixieClock::get_mac()
{
eeprom.read_EUI(mac);
......
......@@ -14,6 +14,7 @@
#include "ENC28J60.h"
#include "EEPROM_25AA02E48.h"
#include "StatusLED.h"
#include "NetworkTypes.h"
using namespace ENC28J60_namespace;
......@@ -28,6 +29,12 @@ class NixieClock
DISPLAYING_DATE,
DISPLAYING_TEMPERATURE,
};
enum
{
SNTP_UPDATE_INTERVAL = 100, //Update interval in seconds (15min)
SNTP_RETRY_INTERVAL = 0,
};
public:
NixieDisplay display;
......@@ -36,10 +43,11 @@ class NixieClock
public:
time_t old_time;
tm time_date;
time_t sntp_last_update;
display_states state;
//uint8_t mac[6];
IP sntp_server_ip;
public:
NixieClock();
......@@ -49,6 +57,7 @@ class NixieClock
void display_temperature();
void display_handler();
void sntp_handler();
void get_mac();
void print_eeprom();
......
......@@ -31,6 +31,7 @@ class NixieStack
IP pending_ip;
IP pending_sntp_server_ip;
public:
ARP_protocol arp;
SNTP_client sntp;
IP_protocol ipv4;
......
......@@ -22,10 +22,11 @@ void SNTP_client::decode(uint8_t* data)
seconds = __ntohl(packet->transmit_timestamp.seconds) - 2208988800;
tm time_date;
time_t timee = time(NULL);
last_update = seconds;
localtime_r(&timee, &time_date);
uart.send(asctime(&time_date));uart.send_new_line();
localtime_r(&last_update, &time_date);
debug.info(asctime(&time_date));
//TODO: Set milliseconds to match timestamp fraction part
}
......
#pragma once
#include <cstdint>
#include <ctime>
enum
{
......@@ -61,6 +61,9 @@ struct NTP_packet
class SNTP_client
{
public:
time_t last_update;
public:
SNTP_client();
......
......@@ -113,10 +113,7 @@ int main(void)
stack.init();
//Send SNTP request on clock1.zcu.cz
stack.send_sntp_request(IP({147, 228, 57, 10 }));
//Send SNTP request on ntp.nic.cz
//stack.send_sntp_request(IP({217, 31, 202, 100 }));
stack.send_sntp_request(nixie.sntp_server_ip);
time_t old = 0;
......@@ -124,6 +121,7 @@ int main(void)
{
nixie.display_handler();
nixie.separator.pulsing_handler();
nixie.sntp_handler();
eth.get_interrupt_source();
stack.check_link();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment