#include <Arduino.h>
#include <QNEthernet.h>
#include <avr/io.h>
#include <avr/interrupt.h>
using namespace qindesign::network;
void GetSample() {
// Read the timestamp of the last transmitted frame and clear the interrupt.
//struct timespec timestamp;
Serial.println("Interrupt triggered!");
/*
if (EthernetIEEE1588.readAndClearTxTimestamp(timestamp)) {
// Do something with the timestamp, e.g., print it to the serial monitor.
Serial.print("Timestamp: ");
Serial.print(timestamp.tv_sec);
Serial.print(" seconds and ");
Serial.print(timestamp.tv_nsec);
Serial.println(" nanoseconds");
} else {
Serial.println("Failed to read timestamp.");
}
*/
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Starting Ethernet IEEE 1588 example...");
qindesign::network::Ethernet.begin();
qindesign::network::EthernetIEEE1588.begin();
qindesign::network::EthernetIEEE1588.setChannelMode(0, qindesign::network::EthernetIEEE1588Class::TimerChannelModes::kClearOnCompare);
//qindesign::network::EthernetIEEE1588.setChannelCompareValue(0, 20000); // 20 us
qindesign::network::EthernetIEEE1588.setChannelCompareValue(0, 50000000); // 500 ms
qindesign::network::EthernetIEEE1588.setChannelInterruptEnable(0, true);
IRQ_NUMBER_t irqNumber = IRQ_ENET_TIMER;
attachInterruptVector(irqNumber, GetSample);
NVIC_ENABLE_IRQ(IRQ_ENET_TIMER);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
Serial.println("Main loop running...");
Serial.print("Timer value: ");
struct timespec timestamp;
Serial.println(qindesign::network::EthernetIEEE1588.readTimer(timestamp));
Serial.print(timestamp.tv_sec);
Serial.print(" seconds and ");
Serial.print(timestamp.tv_nsec);
Serial.println(" nanoseconds");
}