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

Working NTP and timezonegit statusgit status

parent e3070e4f
......@@ -13,7 +13,7 @@
#include "UDP_protocol.h"
USART uart(SERCOM1->USART, 500000);
USART uart(SERCOM1->USART, 1000000);
Debug debug(uart);
......@@ -115,6 +115,19 @@ void Debug::frame(const ethernet_frame* frame)
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)
{
uart.send("[INFO] ");
......@@ -217,9 +230,9 @@ void Debug::icmp_packet(const ICMP_packet* packet)
void Debug::sntp(const NTP_packet* packet)
{
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_dec(packet->transmit_timestamp.seconds_fraction);
uart.send_hex32(__ntohl(packet->transmit_timestamp.seconds_fraction));
uart.send_new_line();
}
......
......@@ -48,6 +48,7 @@ class Debug
void reg(const char* name, const uint8_t value);
void reg(const char* name, const uint16_t value);
void frame(const ethernet_frame* frame);
void frame_hex(const ethernet_frame* frame);
void frame_type(const ethernet_frame* frame);
void ip_packet(const IP_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
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->version = 4;
packet->tos = 0;
packet->tl = __htons((4*packet->ihl) + length);
packet->identification = packet->identification;
packet->flags_fragment_offset = packet->flags_fragment_offset;
packet->identification = 0;
packet->flags_fragment_offset = 0;
packet->ttl = 64;
packet->protocol = protocol;
packet->checksum = 0;
......@@ -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->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)
......
......@@ -68,7 +68,7 @@ class IP_protocol
IP_protocol(const IP& local_ip);
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 checksum(const uint8_t* vdata, uint16_t length);
......
......@@ -3,6 +3,8 @@
#include <cstdint>
#include <ctime>
#include <cstdio>
#include <clocale>
#include <cstdlib>
#include "ADC_class.h"
#include "BoostConverter.h"
......@@ -33,12 +35,18 @@ NixieClock::NixieClock(): display(),
old_time(0),
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_min = 25;
time_date.tm_sec = 0;
time_date.tm_year = 117;
time_date.tm_mon = 4;
time_date.tm_mday = 9;
seconds = mktime(&time_date);
}
......
......@@ -14,6 +14,7 @@
#include "ENC28J60.h"
#include "Debug.h"
#include "NixieClock.h"
#include "System.h"
using namespace ENC28J60_namespace;
......@@ -150,6 +151,7 @@ void NixieStack::packet_handler()
case IPV4:
{
//In tx_frame->payload is packet to send
debug.frame_hex(frame);
if (ipv4.decode(frame->payload, tx_frame->payload))
{
memcpy(tx_frame->dest_address, frame->src_address, 6);
......@@ -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))
{
ip_to_search = gateway;
debug.message("Looking for gateway MAC");
}
else ip_to_search = ntp_server_ip;
......@@ -222,29 +223,36 @@ void NixieStack::send_sntp_request(const IP ntp_server_ip)
{
//TODO: Method for sending UDP packets
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->src_address, mac, 6);
frame->type_length = __htons(IPV4);
UDP_packet* udp_packet = (UDP_packet*) frame->payload.data;
sntp.assembly_request(udp_packet->data, frame->payload.length);
frame->payload.length += UDP_HEADER_SIZE;
sntp.assembly_request(udp_packet->data, length);
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->src_port = __htons(NTP_PORT);
//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();
}
//Ask for MAC otherwise
else
{
debug.message("Sending ARP request");
send_arp_request(ip_to_search);
pending_ip = ip_to_search;
......@@ -261,6 +269,7 @@ void NixieStack::sntp_arp_handler()
MAC dst_mac;
if (arp.search(pending_ip, dst_mac))
{
//TODO: Method for sending UDP packets
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;
......@@ -275,14 +284,17 @@ void NixieStack::sntp_arp_handler()
length += UDP_HEADER_SIZE;
//TODO: udp.assembly()
udp_packet->length = __htons(length);
udp_packet->length = __htons(length);
udp_packet->dst_port = __htons(NTP_PORT);
udp_packet->src_port = __htons(NTP_PORT);
//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();
pending_sntp_request = false;
......
......@@ -2,6 +2,7 @@
#include <cstdint>
#include <cstring>
#include <ctime>
#include "Time.h"
#include "Debug.h"
......@@ -18,7 +19,14 @@ void SNTP_client::decode(uint8_t* data)
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
}
......
......@@ -16,7 +16,7 @@ System::System()
void System::init_clock()
{
//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;
while (!SYSCTRL->PCLKSR.bit.XOSC32KRDY) ;;
......
#include "UDP_protocol.h"
#include <cstdint>
#include <machine/endian.h>
#include "SNTP_client.h"
#include "USART.h"
#include "Debug.h"
......@@ -11,8 +14,9 @@ UDP_protocol::UDP_protocol(): sntp()
void UDP_protocol::decode(const uint8_t* udp_data, const uint16_t length)
{
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);
break;
......
......@@ -115,7 +115,10 @@ int main(void)
stack.init();
//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;
......
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