From fd45ecf1cc8c4e02e3d2b0527e30a8212d1ceebd Mon Sep 17 00:00:00 2001
From: Local Nuc Development <nuc1@tuwien.ac.at>
Date: Fri, 22 Dec 2023 15:43:28 +0100
Subject: [PATCH] updatd demo program

---
 examples/patmos_tsn_udp_tcp_hello.c | 71 +++++++++++++++++++----------
 1 file changed, 47 insertions(+), 24 deletions(-)

diff --git a/examples/patmos_tsn_udp_tcp_hello.c b/examples/patmos_tsn_udp_tcp_hello.c
index 90e0236..78cc826 100644
--- a/examples/patmos_tsn_udp_tcp_hello.c
+++ b/examples/patmos_tsn_udp_tcp_hello.c
@@ -25,7 +25,7 @@ uint8_t network_stack_buffer[NETWORK_STACK_MEMORY_POOL_SIZE];
 
 // Network interface config
 char eth0_if_name[] = "eth0";
-uint8_t eth0_if_ipv4[4] = {172, 16, 0, 201};
+uint8_t eth0_if_ipv4[4] = {172, 16, 100, 201};
 uint8_t eth0_if_mac[6] = {0x00, 0x80, 0x6e, 0xF0, 0xDA, 0x42};
 uint16_t eth0_interface_id= 0xFFFF;
 
@@ -41,7 +41,7 @@ unsigned short udp_src_port_isr = 55555;
 // destination config
 socket_address_t eth0_udp_dest_addr;
 //unsigned char dst_mac[6] ={0x00, 0x0e, 0xc6, 0xc9, 0x48, 0x59};
-unsigned char eth0_udp_dst_ipv4[4] = {172, 16, 0, 200};
+unsigned char eth0_udp_dst_ipv4[4] = {172, 16, 100, 200};
 unsigned short udp_dst_port = 12345;
 
 // CONFIGURATION FOR TCP SOCKET (server)
@@ -78,6 +78,7 @@ volatile uint32_t packets_received = 0;
 volatile uint8_t isr_fired=0;
 #define TICK_INTERVALL_US 10000
 #define LEDS (*((volatile _IODEV unsigned *)  PATMOS_IO_LED))
+#define GPIOS (*((volatile _IODEV unsigned *)  PATMOS_IO_GPIO))
 
 int init_network_and_socket(void)
 {
@@ -190,8 +191,8 @@ int populate_tsn_schedule(void)
     gcl[3].gate_state=0x00;
     gcl[3].slot_duration= 1000000; // ns
 
-    tsn_config.gcl_length = 4;
-    tsn_config.cycle_duration = 50000000; //1000000*(tsn_config.gcl_length+1); //ns
+    tsn_config.gcl_length = 4;  
+    tsn_config.cycle_duration = 5000000; //1000000*(tsn_config.gcl_length+1); //ns
     
     tsn_config.admin_cycle_time.seconds=0;
     tsn_config.admin_cycle_time.nanoseconds=0;
@@ -342,17 +343,21 @@ void fault_handler(void) {
 
 void isr_routine(void)
 {
+    
     SEI();
     static uint8_t cnt=0;
-    cnt = (cnt+1) >=10 ? 0 : cnt+1;
-    
+    cnt = (cnt+1) >=5 ? 0 : cnt+1;
+
     if(cnt==0)
     {
         // Only relevant for high prioriy socket
         uint8_t*data = (uint8_t*) &hello_short;
         uint16_t data_len =  strlen( (char*) &hello_short);
-        network_error_t ret = socket_send(eth0_udp_isr_socket_id, &eth0_udp_dest_addr, data, data_len );
         
+        GPIOS |=  (1 << 0);
+        network_error_t ret = socket_send(eth0_udp_isr_socket_id, &eth0_udp_dest_addr, data, data_len );
+        GPIOS &= ~(1 << 0);
+
         if( ret != NET_ERR_OK && ret != NET_ERR_ARP_UNKNOWN_ADDR && 
             ret != NET_ERR_DRVR_FRAME_QUEUED && ret != NET_ERR_DRVR_TIMESLOT_TO_SHORT && ret != NET_ERR_DRVR_TSN_QUEUES_CLOSED)
         {
@@ -368,16 +373,17 @@ void isr_routine(void)
             packets_sent++;
         }
     }
-    // general for all sockets/interfaces
-    else if(cnt == 5)
-    {
-        network_update();
-    }
-    // general for all sockets/interfaces
-    else if(cnt == 7)
-    {
-        network_read_interface(eth0_interface_id); 
-    }
+    // // general for all sockets/interfaces
+    // else if(cnt == 5)
+    // {
+    //     network_update();
+    // }
+    // // general for all sockets/interfaces
+    // else if(cnt == 7)
+    // {
+    //     network_read_interface(eth0_interface_id); 
+    // }
+
     CLI();
 }
 
@@ -478,7 +484,7 @@ int main(void)
     init_tcp_socket_eth0();
     if(eth0_tcp_socket_id == 0xFFFF)
     {
-        return -1;
+       return -1;
     }
 
 #ifdef WCET_ANALYSIS
@@ -524,14 +530,28 @@ int main(void)
     packets_sent =0;
     while (1)
     {
-        unsigned long long int start_time = get_cpu_usecs();
-        while (get_cpu_usecs()-start_time < 500000){}
+        
+        for (uint8_t i=0; i<10; i++)
+        {
+            unsigned long long int start_time = get_cpu_usecs();
+            //while (get_cpu_usecs()-start_time < 100000){}
+
+            while (get_cpu_usecs()-start_time < 10000){}
+            //GPIOS |=  (1 << 0);
+            network_update();
+            network_read_interface(eth0_interface_id);
+            network_low_priority_update();
+            //GPIOS &= ~(1 << 0);
+        }
 
         // Handle TCP handshakes and possibly other low-priority management tasks
-        network_low_priority_update();
 
-        LEDS ^= 0x1;
+        //LEDS ^= 0x01; // LEDS ^= 0x1;
+        // GPIOS ^= 0x01;
+
+        GPIOS |=  (1 << 1);
         ret = socket_send(eth0_udp_socket_id, &eth0_udp_dest_addr, data, data_len );
+        GPIOS &= ~(1 << 1);
         if( ret != NET_ERR_OK && ret != NET_ERR_ARP_UNKNOWN_ADDR && 
             ret != NET_ERR_DRVR_FRAME_QUEUED && ret != NET_ERR_DRVR_TIMESLOT_TO_SHORT && ret != NET_ERR_DRVR_TSN_QUEUES_CLOSED)
         {
@@ -593,7 +613,7 @@ int main(void)
             }
             else if(ret == NET_ERR_OK)
             {
-                tcp_state_t conn_state = socket_tcp_get_connection_state(eth0_tcp_socket_id, &msg_src_addr.ip_address, &msg_src_addr.port);
+                tcp_state_t conn_state = socket_tcp_get_connection_state(eth0_tcp_socket_id, &msg_src_addr.ip_address, msg_src_addr.port);
                 if (conn_state != TCP_STATE_ESTABLISHED) {
                     // if we received a message, but the connection is not yet fully established, we should trigger the tcp stack to complete the three-way handshake
                     network_low_priority_update(); // check if the final ACK of the three-way handshake has been received and set the connection state accordingly
@@ -611,8 +631,11 @@ int main(void)
                 network_error_t ret_send;
                 if(compare_request(tcp_request_buffer, tcp_request_length, request_hello, (uint16_t)strlen(request_hello)))
                 {
+
                     printf("received give me hello request\n");
+                    GPIOS |= (1<<2);
                     ret_send = socket_tcp_write(eth0_tcp_socket_id, &msg_src_addr, (uint8_t*)hello_world, tcp_response_hello_length);
+                    GPIOS &= ~(1<<2);
                     if(ret_send != NET_ERR_OK) utils_log("prb to send response : 0x%04x\n", ret_send);
                     /*
                     if(ret_send == NET_ERR_TCP_CONNECTION_STATE) {
@@ -644,7 +667,7 @@ int main(void)
         //LEDS ^= 0x10;
         packets_received += cnt;
 
-        ////LEDS = packets_sent - packets_received;
+        LEDS = packets_sent - packets_received;
         packets_sent_last = packets_sent;
     }
 
-- 
GitLab