Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 4:c186ef1dfa99, committed 2021-01-17
- Comitter:
- timo_k2
- Date:
- Sun Jan 17 18:28:05 2021 +0000
- Parent:
- 3:fb39c8c13b34
- Commit message:
- The cousin for the code UDP Round Trip Delay OS6 K64F.; The code is cleaned and comments completed.
Changed in this revision
--- a/README.md Mon Nov 23 12:48:48 2020 +0000 +++ b/README.md Sun Jan 17 18:28:05 2021 +0000 @@ -1,5 +1,6 @@ -Round trip delay meter. Date and time is taken from a NTP server. +Round trip delay meter. Mbed OS Timer with microsecond precision used for a stopwatch. -A microcontroller board with an Ethernet interface. +A microcontroller board with an Ethernet interface is needed. +For measuring documentation the date and time is taken from a NTP server. An other microcontroller with "Round trip delay echo" will be needed. -NXP FRDM-K64F used for testing. \ No newline at end of file +ST NUCLEO H743ZI used for testing. \ No newline at end of file
--- a/main.cpp Mon Nov 23 12:48:48 2020 +0000
+++ b/main.cpp Sun Jan 17 18:28:05 2021 +0000
@@ -5,9 +5,9 @@
* Round trip delay meter. Date and time is taken from a NTP server.
* A microcontroller board with an Ethernet interface.
* An other microcontroller with "Round trip echo" will be needed.
- * NXP FRDM-K64F used for testing.
+ * ST NUCLEO H743Zi used for testing.
*
- * Timo Karppinen 20.11.2020
+ * Timo Karppinen 17.1.2021
***********************************/
#include "mbed.h"
#include "EthernetInterface.h"
@@ -15,50 +15,50 @@
#define REMOTE_PORT 5000
#define LOCAL_PORT 5001
-#define BUFF_SIZE 128
+#define BUFF_SIZE 128 // test with 32, 128, 512, etc
//Network interface
EthernetInterface net;
//Threads
- Thread ntp_thread;
Thread recv_thread;
- Thread send_thread;
-
+
// UDP
SocketAddress clientUDP; // Client on remote device
UDPSocket serverUDP; // UDP server in this device
-
//NTP server is a time server used for delivering timing information for networks.
-// Returns 32 bits for seconds and 32 bits for fraction of seconds.
+//Returns 32 bits for seconds and 32 bits for fraction of seconds.
//#define ntpAddress "2.pool.ntp.org"
#define ntpAddress "time.mikes.fi" // The VTT Mikes in Helsinki
#define ntpPort 123
+// The address and port number can be replaced with the ones for the local
+// network NTP server.
// Functions
-time_t getNTP();
+//time_t getNTP();
void udpReceive( void );
void udpSend( void );
-DigitalIn sw2(SW2); // sw2 on K64F, button pressed = FALSE
-DigitalOut led2(LED2); // RGB LED green on K64F
+DigitalIn sw2(PC_13); // Blue button on H743ZI, button pressed = TRUE
+DigitalOut led2(PE_1); // Yellow LED on H743ZI, TRUE = LED on.
int sw2state = 0;
int sw2old = 1;
char in_data[BUFF_SIZE];
int newDatagram = 0;
int newDatagramOld = 0;
-time_t timeNTP = 0;
+//time_t timeNTP = 0;
-//using namespace std::chrono;
-Timer stopwatch;
+Timer stopwatch; // Timer is an operating system class since OS 6.0
Timer armedFor;
int main() {
printf("\nRound trip delay in UDP messaging (using Ethernet)\n");
//Bring up the network interface
+ //eth.set_network(IP_Adress,GATEWAY,MASK); // leave out if using DHCP
+ net.set_network("192.168.1.10","192.168.1.1","255.255.252.0");
net.connect();
// Show network address
@@ -66,8 +66,12 @@
net.get_ip_address(&netAddress);
printf("\n\n NTPClient - UDPServer IP Address: %s\n", netAddress.get_ip_address() ? netAddress.get_ip_address():"None");
- // NTP client at ntp thread
- ntp_thread.start(getNTP);
+
+ // NTP client
+
+ //printf("Message to NTP time server...\n");
+ //timeNTP = getNTP();
+ //printf("Current time in day month hour.min.sec year is %s\r\n", ctime(&timeNTP));
// UDP server
@@ -77,56 +81,50 @@
recv_thread.start(udpReceive);
printf("Listening has been started at port number %d\n", LOCAL_PORT);
-
- send_thread.start(udpSend);
- printf("Sending out demo data to port number %d", REMOTE_PORT);
- printf(" has been started.\n");
+ printf("The operator for the \"Echo board\" should send a message!\n ");
printf("The IP will be taken from the incoming message\n");
+ printf("Press the blue switch after receiving \"Echo server listening\"\n");
-
while(1) {
sw2state = sw2.read();
- printf( "\nsw2state is %d\n", sw2state);
+ printf( "\nsw2state is %d\n", sw2state); // Printing showing "I am alive".
- if((sw2state == 0)&&(sw2state != sw2old)) {
+ if((sw2state == 1)&&(sw2state != sw2old)) {
led2.write(0);
armedFor.reset(); // reset timer to zero
stopwatch.reset(); // reset stopwatch timer to zero
- timeNTP = getNTP();
+ //timeNTP = getNTP();
armedFor.start();
- stopwatch.start();
+ //stopwatch.start(); // moved to udpSend subroutine
udpSend();
- // Start polling for the incomening "Echo" UDP datagram
+ // Start polling for the incoming "Echo" UDP datagram
while ( armedFor.elapsed_time().count() < 8000000 ){
if((newDatagram == 1)&&(newDatagram != newDatagramOld)){
+ stopwatch.stop();
char firstChar;
firstChar = in_data[0];
- if (firstChar == 69){ // ASCII symbol 69 = "E" for the Echo
- stopwatch.stop();
- printf( "firstChar: %s\n", &firstChar);
- }
+ printf( "firstChar: %s\n", &firstChar);
for (int k =0; k < BUFF_SIZE; k++){
in_data[k] = 0;
- }
+ }
}
- newDatagramOld = newDatagram; //Reading the stopwatch once only
-
+ newDatagramOld = newDatagram; //Reading the stopwatch once only
+ newDatagram = 0;
}
// printing for testing. Replace with writing once to a SD memory card.
- printf("Current time in day month hour.min.sec year is %s\r\n", ctime(&timeNTP));
+ //printf("Measured at ( day month hour.min.sec year ) %s\r\n", ctime(&timeNTP));
printf("The time taken was %llu microseconds\n", stopwatch.elapsed_time().count());
+
}
- newDatagram = 0;
sw2old = sw2state; // Once only with pushing the button as long as you like.
led2.write(1);
armedFor.stop();
stopwatch.stop(); // Stop the stopwatch if we did not receive the echo.
- printf("\nWe stopped sending more UDP packets to the server.\nYou can unplug your device!\n");
- ThisThread::sleep_for(4000ms);
+ ThisThread::sleep_for(1000ms);
}
}
@@ -140,10 +138,10 @@
printf("An error occurred when getting the time. Code: %u\r\n", timestamp);
}
else { // the printings for testing only!
- //printf("The timestamp seconds from the NTP server in\r\n 32 bit hexadecimal number is %X\r\n", timestamp);
- //printf(" decimal number is %u\r\n", timestamp);
+ printf("The timestamp seconds from the NTP server in\r\n 32 bit hexadecimal number is %X\r\n", timestamp);
+ printf(" decimal number is %u\r\n", timestamp);
timestamp += (60*60*2); // GMT +2 for Finland for the winter time.
- //printf("Current time is %s\r\n", ctime(×tamp));
+ printf("Current time is %s\r\n", ctime(×tamp));
}
return timestamp;
}
@@ -154,6 +152,7 @@
while(1) {
bytes = serverUDP.recvfrom(&clientUDP, &in_data, BUFF_SIZE);
newDatagram = 1; // set this before using time for printing
+ ThisThread::sleep_for(400ms); // waiting for the print buffer
printf("\n");
printf("bytes received: %d\n",bytes);
printf("string: %s\n",in_data);
@@ -168,10 +167,10 @@
char out_data[BUFF_SIZE];
snprintf(out_data, BUFF_SIZE, "UDP message for getting the Echo" );
- clientUDP.set_port(REMOTE_PORT);
- serverUDP.sendto(clientUDP, out_data, sizeof(out_data));
printf("Sending out: %s\n", out_data);
printf("with %d" , sizeof(out_data));
printf(" data bytes in UDP datagram\n");
-
+ clientUDP.set_port(REMOTE_PORT);
+ stopwatch.start(); // starting the stopwatch after preparations are done
+ serverUDP.sendto(clientUDP, out_data, sizeof(out_data));
}
--- a/mbed-os.lib Mon Nov 23 12:48:48 2020 +0000 +++ b/mbed-os.lib Sun Jan 17 18:28:05 2021 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os.git/#f2278567d09b9ae9f4843e1d9d393526b9462783 +https://github.com/ARMmbed/mbed-os.git/#eff0d4c8b93c5a165af9f2e5e42c321a7f83eed9