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

Working NTP and timezonegit statusgit status

parent e3070e4f
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include "UDP_protocol.h" #include "UDP_protocol.h"
USART uart(SERCOM1->USART, 500000); USART uart(SERCOM1->USART, 1000000);
Debug debug(uart); Debug debug(uart);
...@@ -115,6 +115,19 @@ void Debug::frame(const ethernet_frame* frame) ...@@ -115,6 +115,19 @@ void Debug::frame(const ethernet_frame* frame)
uart.send_new_line(); uart.send_new_line();
} }
void Debug::frame_hex(const ethernet_frame* frame)
{
uint8_t* p = (uint8_t*) frame;
for (uint16_t i=0; i<(frame->payload.length + 18); i++)
{
uart.send_hex8(p[i]);
uart.send(' ');
}
uart.send_new_line();
}
void Debug::frame_type(const ethernet_frame* frame) void Debug::frame_type(const ethernet_frame* frame)
{ {
uart.send("[INFO] "); uart.send("[INFO] ");
...@@ -217,9 +230,9 @@ void Debug::icmp_packet(const ICMP_packet* packet) ...@@ -217,9 +230,9 @@ void Debug::icmp_packet(const ICMP_packet* packet)
void Debug::sntp(const NTP_packet* packet) void Debug::sntp(const NTP_packet* packet)
{ {
uart.send("[SNTP] Get timestamp: "); uart.send("[SNTP] Get timestamp: ");
uart.send_dec(packet->transmit_timestamp.seconds); uart.send_hex32(__ntohl(packet->transmit_timestamp.seconds) & ~(1UL<<31));
uart.send('.'); uart.send('.');
uart.send_dec(packet->transmit_timestamp.seconds_fraction); uart.send_hex32(__ntohl(packet->transmit_timestamp.seconds_fraction));
uart.send_new_line(); uart.send_new_line();
} }
......
...@@ -48,6 +48,7 @@ class Debug ...@@ -48,6 +48,7 @@ class Debug
void reg(const char* name, const uint8_t value); void reg(const char* name, const uint8_t value);
void reg(const char* name, const uint16_t value); void reg(const char* name, const uint16_t value);
void frame(const ethernet_frame* frame); void frame(const ethernet_frame* frame);
void frame_hex(const ethernet_frame* frame);
void frame_type(const ethernet_frame* frame); void frame_type(const ethernet_frame* frame);
void ip_packet(const IP_packet* packet); void ip_packet(const IP_packet* packet);
void icmp_packet(const ICMP_packet* packet); void icmp_packet(const ICMP_packet* packet);
......
...@@ -67,16 +67,14 @@ bool IP_protocol::decode(const ethernet_payload& rx_payload, ethernet_payload& t ...@@ -67,16 +67,14 @@ bool IP_protocol::decode(const ethernet_payload& rx_payload, ethernet_payload& t
return false; return false;
} }
void IP_protocol::assembly(ethernet_payload& payload, IP dst_ip, uint16_t& length, uint8_t protocol) void IP_protocol::assembly(IP_packet* packet, const IP& dst_ip, uint16_t& length, uint8_t protocol)
{ {
IP_packet* packet = (IP_packet*) payload.data;
packet->ihl = 5; packet->ihl = 5;
packet->version = 4; packet->version = 4;
packet->tos = 0; packet->tos = 0;
packet->tl = __htons((4*packet->ihl) + length); packet->tl = __htons((4*packet->ihl) + length);
packet->identification = packet->identification; packet->identification = 0;
packet->flags_fragment_offset = packet->flags_fragment_offset; packet->flags_fragment_offset = 0;
packet->ttl = 64; packet->ttl = 64;
packet->protocol = protocol; packet->protocol = protocol;
packet->checksum = 0; packet->checksum = 0;
...@@ -84,9 +82,9 @@ void IP_protocol::assembly(ethernet_payload& payload, IP dst_ip, uint16_t& lengt ...@@ -84,9 +82,9 @@ void IP_protocol::assembly(ethernet_payload& payload, IP dst_ip, uint16_t& lengt
packet->src_address = local_ip.ip.value; packet->src_address = local_ip.ip.value;
packet->dst_address = dst_ip.ip.value; packet->dst_address = dst_ip.ip.value;
packet->checksum = checksum((uint8_t*)packet, __ntohs(packet->tl)); packet->checksum = checksum((uint8_t*)packet, packet->ihl*4);
payload.length = __ntohs(packet->tl); length = __ntohs(packet->tl);
} }
uint16_t IP_protocol::get_data_length(uint8_t ihl, uint16_t tl) uint16_t IP_protocol::get_data_length(uint8_t ihl, uint16_t tl)
......
...@@ -68,7 +68,7 @@ class IP_protocol ...@@ -68,7 +68,7 @@ class IP_protocol
IP_protocol(const IP& local_ip); IP_protocol(const IP& local_ip);
bool decode(const ethernet_payload& rx_payload, ethernet_payload& tx_payload); bool decode(const ethernet_payload& rx_payload, ethernet_payload& tx_payload);
void assembly(ethernet_payload& payload, IP dst_ip, uint16_t& length, uint8_t protocol); void assembly(IP_packet* packet, const IP& dst_ip, uint16_t& length, uint8_t protocol);
uint16_t get_data_length(uint8_t ihl, uint16_t tl); uint16_t get_data_length(uint8_t ihl, uint16_t tl);
uint16_t checksum(const uint8_t* vdata, uint16_t length); uint16_t checksum(const uint8_t* vdata, uint16_t length);
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include <cstdint> #include <cstdint>
#include <ctime> #include <ctime>
#include <cstdio> #include <cstdio>
#include <clocale>
#include <cstdlib>
#include "ADC_class.h" #include "ADC_class.h"
#include "BoostConverter.h" #include "BoostConverter.h"
...@@ -33,12 +35,18 @@ NixieClock::NixieClock(): display(), ...@@ -33,12 +35,18 @@ NixieClock::NixieClock(): display(),
old_time(0), old_time(0),
state(DISPLAYING_TEMPERATURE) state(DISPLAYING_TEMPERATURE)
{ {
setlocale(LC_TIME, "cs_CZ");
setenv("TZ","CET-1CEST-2,M3.5.0/+2,M10.5.0/+1",1);
tzset();
time_date.tm_hour = 9; time_date.tm_hour = 9;
time_date.tm_min = 25; time_date.tm_min = 25;
time_date.tm_sec = 0; time_date.tm_sec = 0;
time_date.tm_year = 117; time_date.tm_year = 117;
time_date.tm_mon = 4; time_date.tm_mon = 4;
time_date.tm_mday = 9; time_date.tm_mday = 9;
seconds = mktime(&time_date); seconds = mktime(&time_date);
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "ENC28J60.h" #include "ENC28J60.h"
#include "Debug.h" #include "Debug.h"
#include "NixieClock.h" #include "NixieClock.h"
#include "System.h"
using namespace ENC28J60_namespace; using namespace ENC28J60_namespace;
...@@ -150,6 +151,7 @@ void NixieStack::packet_handler() ...@@ -150,6 +151,7 @@ void NixieStack::packet_handler()
case IPV4: case IPV4:
{ {
//In tx_frame->payload is packet to send //In tx_frame->payload is packet to send
debug.frame_hex(frame);
if (ipv4.decode(frame->payload, tx_frame->payload)) if (ipv4.decode(frame->payload, tx_frame->payload))
{ {
memcpy(tx_frame->dest_address, frame->src_address, 6); memcpy(tx_frame->dest_address, frame->src_address, 6);
...@@ -213,7 +215,6 @@ void NixieStack::send_sntp_request(const IP ntp_server_ip) ...@@ -213,7 +215,6 @@ void NixieStack::send_sntp_request(const IP ntp_server_ip)
if ((ntp_server_ip.ip.value & mask.ip.value) != (gateway.ip.value & mask.ip.value)) if ((ntp_server_ip.ip.value & mask.ip.value) != (gateway.ip.value & mask.ip.value))
{ {
ip_to_search = gateway; ip_to_search = gateway;
debug.message("Looking for gateway MAC");
} }
else ip_to_search = ntp_server_ip; else ip_to_search = ntp_server_ip;
...@@ -222,29 +223,36 @@ void NixieStack::send_sntp_request(const IP ntp_server_ip) ...@@ -222,29 +223,36 @@ void NixieStack::send_sntp_request(const IP ntp_server_ip)
{ {
//TODO: Method for sending UDP packets //TODO: Method for sending UDP packets
ethernet_frame* frame = (ethernet_frame*) tx_buffer.write(); ethernet_frame* frame = (ethernet_frame*) tx_buffer.write();
IP_packet* ip_packet = (IP_packet*) frame->payload.data;
UDP_packet* udp_packet = (UDP_packet*) ip_packet->data;
uint16_t length = 0;
memcpy(frame->dest_address, dst_mac, 6); memcpy(frame->dest_address, dst_mac, 6);
memcpy(frame->src_address, mac, 6); memcpy(frame->src_address, mac, 6);
frame->type_length = __htons(IPV4); frame->type_length = __htons(IPV4);
UDP_packet* udp_packet = (UDP_packet*) frame->payload.data; sntp.assembly_request(udp_packet->data, length);
length += UDP_HEADER_SIZE;
sntp.assembly_request(udp_packet->data, frame->payload.length);
frame->payload.length += UDP_HEADER_SIZE;
udp_packet->length = __htons(frame->payload.length); //TODO: udp.assembly()
udp_packet->length = __htons(length);
udp_packet->dst_port = __htons(NTP_PORT); udp_packet->dst_port = __htons(NTP_PORT);
udp_packet->src_port = __htons(NTP_PORT); udp_packet->src_port = __htons(NTP_PORT);
//TODO: UDP checksum //TODO: UDP checksum
udp_packet->checksum = 0;
ipv4.assembly(ip_packet, pending_sntp_server_ip, length, UDP);
frame->payload.length = length;
tx_buffer.write_advance(); tx_buffer.write_advance();
} }
//Ask for MAC otherwise //Ask for MAC otherwise
else else
{ {
debug.message("Sending ARP request");
send_arp_request(ip_to_search); send_arp_request(ip_to_search);
pending_ip = ip_to_search; pending_ip = ip_to_search;
...@@ -261,6 +269,7 @@ void NixieStack::sntp_arp_handler() ...@@ -261,6 +269,7 @@ void NixieStack::sntp_arp_handler()
MAC dst_mac; MAC dst_mac;
if (arp.search(pending_ip, dst_mac)) if (arp.search(pending_ip, dst_mac))
{ {
//TODO: Method for sending UDP packets
ethernet_frame* frame = (ethernet_frame*) tx_buffer.write(); ethernet_frame* frame = (ethernet_frame*) tx_buffer.write();
IP_packet* ip_packet = (IP_packet*) frame->payload.data; IP_packet* ip_packet = (IP_packet*) frame->payload.data;
UDP_packet* udp_packet = (UDP_packet*) ip_packet->data; UDP_packet* udp_packet = (UDP_packet*) ip_packet->data;
...@@ -275,14 +284,17 @@ void NixieStack::sntp_arp_handler() ...@@ -275,14 +284,17 @@ void NixieStack::sntp_arp_handler()
length += UDP_HEADER_SIZE; length += UDP_HEADER_SIZE;
//TODO: udp.assembly() //TODO: udp.assembly()
udp_packet->length = __htons(length); udp_packet->length = __htons(length);
udp_packet->dst_port = __htons(NTP_PORT); udp_packet->dst_port = __htons(NTP_PORT);
udp_packet->src_port = __htons(NTP_PORT); udp_packet->src_port = __htons(NTP_PORT);
//TODO: UDP checksum //TODO: UDP checksum
udp_packet->checksum = 0;
ipv4.assembly(frame->payload, pending_sntp_server_ip, length, UDP); ipv4.assembly(ip_packet, pending_sntp_server_ip, length, UDP);
frame->payload.length = length;
tx_buffer.write_advance(); tx_buffer.write_advance();
pending_sntp_request = false; pending_sntp_request = false;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <ctime>
#include "Time.h" #include "Time.h"
#include "Debug.h" #include "Debug.h"
...@@ -18,7 +19,14 @@ void SNTP_client::decode(uint8_t* data) ...@@ -18,7 +19,14 @@ void SNTP_client::decode(uint8_t* data)
debug.sntp(packet); debug.sntp(packet);
seconds = packet->transmit_timestamp.seconds; seconds = __ntohl(packet->transmit_timestamp.seconds) - 2208988800;
tm time_date;
time_t timee = time(NULL);
localtime_r(&timee, &time_date);
uart.send(asctime(&time_date));uart.send_new_line();
//TODO: Set milliseconds to match timestamp fraction part //TODO: Set milliseconds to match timestamp fraction part
} }
......
...@@ -16,7 +16,7 @@ System::System() ...@@ -16,7 +16,7 @@ System::System()
void System::init_clock() void System::init_clock()
{ {
//Enable external 32kHz crystal, 125 092us start-up //Enable external 32kHz crystal, 125 092us start-up
SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_STARTUP(0x05) | SYSCTRL_XOSC32K_XTALEN | SYSCTRL_XOSC32K_EN32K; SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_STARTUP(0x06) | SYSCTRL_XOSC32K_XTALEN | SYSCTRL_XOSC32K_EN32K;
SYSCTRL->XOSC32K.bit.ENABLE = 1; SYSCTRL->XOSC32K.bit.ENABLE = 1;
while (!SYSCTRL->PCLKSR.bit.XOSC32KRDY) ;; while (!SYSCTRL->PCLKSR.bit.XOSC32KRDY) ;;
......
#include "UDP_protocol.h" #include "UDP_protocol.h"
#include <cstdint>
#include <machine/endian.h>
#include "SNTP_client.h" #include "SNTP_client.h"
#include "USART.h" #include "USART.h"
#include "Debug.h" #include "Debug.h"
...@@ -11,8 +14,9 @@ UDP_protocol::UDP_protocol(): sntp() ...@@ -11,8 +14,9 @@ UDP_protocol::UDP_protocol(): sntp()
void UDP_protocol::decode(const uint8_t* udp_data, const uint16_t length) void UDP_protocol::decode(const uint8_t* udp_data, const uint16_t length)
{ {
UDP_packet* packet = (UDP_packet*) udp_data; UDP_packet* packet = (UDP_packet*) udp_data;
debug.message("UDP packet arrived");
switch (packet->src_port) switch (__ntohs(packet->src_port))
{ {
case NTP_PORT: sntp.decode(packet->data); case NTP_PORT: sntp.decode(packet->data);
break; break;
......
...@@ -115,7 +115,10 @@ int main(void) ...@@ -115,7 +115,10 @@ int main(void)
stack.init(); stack.init();
//Send SNTP request on clock1.zcu.cz //Send SNTP request on clock1.zcu.cz
stack.send_sntp_request(IP({147, 228, 57, 10 })); //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 }));
time_t old = 0; time_t old = 0;
......
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