Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "xparameters.h"
  4. #include "platform_config.h"
  5. #include "xil_printf.h"
  6. #include "lwip/sockets.h"
  7. #include "netif/xadapter.h"
  8. #include "lwipopts.h"
  9. #include "xil_printf.h"
  10. #include "VGA_zonderClock.h"
  11. #include "xil_io.h"
  12.  
  13. /* FreeRTOS includes */
  14. #include "FreeRTOS.h"
  15. #include "task.h"
  16. #include "queue.h"
  17.  
  18. #define THREAD_STACKSIZE 1024
  19.  
  20. // receive port for echo packets
  21. u16_t echo_port = 7;
  22.  
  23. int main_thread();
  24. void print_echo_app_header();
  25. void process_echo_request(void *p);
  26. void echo_application_thread();
  27. void network_thread(void *p);
  28. int main_thread();
  29. void lwip_init();
  30. static void prvRxTask( void*pvParameters);
  31. static TaskHandle_t xRxTask;
  32. static QueueHandle_t xQueue= NULL;
  33. char HWstring[2048] = "\0";
  34. int moveX = 0;
  35.  
  36. #define THREAD_STACKSIZE 1024
  37.  
  38. //Generic data structure used for all lwIP network interfaces.
  39. static struct netif server_netif;
  40. struct netif *echo_netif;
  41.  
  42.  
  43. void process_echo_request(void *p)
  44. {
  45.     int sd = (int)p;
  46.     int RECV_BUF_SIZE = 2048;
  47.     char recv_buf[RECV_BUF_SIZE];
  48.     int n, nwrote;
  49.  
  50.     while (1) {
  51.         /* read a max of RECV_BUF_SIZE bytes from socket */
  52.         if ((n = read(sd, recv_buf, RECV_BUF_SIZE)) < 0) {
  53.             xil_printf("%s: error reading from socket %d, closing socket\r\n", __FUNCTION__, sd);
  54.             break;
  55.         }
  56.         else
  57.         {
  58.             strncpy(HWstring, recv_buf, 1);
  59.             xil_printf(HWstring);
  60.             xQueueSend( xQueue,HWstring,0UL );
  61.         }
  62.  
  63.  
  64.         /* break if the received message = "quit" */
  65.         if (!strncmp(recv_buf, "quit", 4))
  66.             break;
  67.  
  68.         /* break if client closed connection */
  69.         if (n <= 0)
  70.             break;
  71.  
  72.         /* handle request */
  73.         if ((nwrote = write(sd, recv_buf, n)) < 0) {
  74.             xil_printf("%s: ERROR responding to client echo request. received = %d, written = %d\r\n",
  75.                     __FUNCTION__, n, nwrote);
  76.             xil_printf("Closing socket %d\r\n", sd);
  77.             break;
  78.         }
  79.  
  80.  
  81.     }
  82.  
  83.     /* close connection */
  84.     close(sd);
  85.     vTaskDelete(NULL);
  86. }
  87.  
  88. void echo_application_thread()
  89. {
  90.     xil_printf("-----in echo application thread ------\r\n");
  91.     int sock, new_sd;
  92.     struct sockaddr_in address, remote;
  93.     int size;
  94.  
  95.     if ((sock = lwip_socket(AF_INET, SOCK_STREAM, 0)) < 0)
  96.         return;
  97.  
  98.     address.sin_family = AF_INET;
  99.     address.sin_port = htons(echo_port);
  100.     address.sin_addr.s_addr = INADDR_ANY;
  101.  
  102.     if (lwip_bind(sock, (struct sockaddr *)&address, sizeof (address)) < 0)
  103.         return;
  104.  
  105.     //  Set a socket into listen mode.
  106.     lwip_listen(sock, 0);
  107.  
  108.     size = sizeof(remote);
  109.  
  110.     while (1) {
  111.         if ((new_sd = lwip_accept(sock, (struct sockaddr *)&remote, (socklen_t *)&size)) > 0) {
  112.             /*---------------------------------------------------------------------------*
  113.              * Routine:  sys_thread_new
  114.              *---------------------------------------------------------------------------*
  115.              * Description:
  116.              *      Starts a new thread with priority "prio" that will begin its
  117.              *      execution in the function "thread()". The "arg" argument will be
  118.              *      passed as an argument to the thread() function. The id of the new
  119.              *      thread is returned. Both the id and the priority are system
  120.              *      dependent.
  121.              * Inputs:
  122.              *      char *name              -- Name of thread
  123.              *      void (* thread)(void *arg) -- Pointer to function to run.
  124.              *      void *arg               -- Argument passed into function
  125.              *      int stacksize           -- Required stack amount in bytes
  126.              *      int prio                -- Thread priority
  127.              * Outputs:
  128.              *      sys_thread_t            -- Pointer to per-thread timeouts.
  129.              *---------------------------------------------------------------------------*/
  130.             sys_thread_new("echos", process_echo_request, (void*)new_sd,4096,DEFAULT_THREAD_PRIO);
  131.         }
  132.     }
  133. }
  134. static void prvRxTask( void*pvParameters)
  135. {
  136. char Recdstring[2048] = "";
  137.     for( ;; )
  138.     {
  139.         xQueueReceive( xQueue,Recdstring,portMAX_DELAY);
  140.         xil_printf( "Received string from UDP network: %d\r\n", Recdstring[0]);
  141.         //VGA_AXI_mWriteReg(XPAR_VGA_AXI_0_S00_AXI_BASEADDR, 0, ((int)Recdstring[0]-48)<<28);
  142.         if(strcmp(Recdstring, "o") == 0)
  143.         {
  144.             moveX = moveX + 50;
  145.                 xil_printf("%d\n\r", moveX);
  146.             VGA_ZONDERCLOCK_mWriteReg(XPAR_VGA_ZONDERCLOCK_0_S_AXI_BASEADDR, VGA_ZONDERCLOCK_S_AXI_SLV_REG2_OFFSET, moveX);
  147.             VGA_ZONDERCLOCK_mWriteReg(XPAR_VGA_ZONDERCLOCK_0_S_AXI_BASEADDR, 0, 0x7E0);         //nog geen limietenss
  148.         }
  149.         //VGA_ZONDERCLOCK_mWriteReg(XPAR_VGA_ZONDERCLOCK_0_S_AXI_BASEADDR, 0, Recdstring[0]);
  150.         VGA_ZONDERCLOCK_mWriteReg(XPAR_VGA_ZONDERCLOCK_0_S_AXI_BASEADDR, 4, Recdstring[0]);
  151.     }
  152. }
  153.  
  154. int main()
  155. {
  156.     xil_printf("-----in Main ------\r\n");
  157.     sys_thread_new("main_thrd", (void(*)(void*))main_thread, 0,THREAD_STACKSIZE,DEFAULT_THREAD_PRIO);
  158.     xTaskCreate( prvRxTask, ( const char* ) "Rx", 2048, NULL, tskIDLE_PRIORITY, &xRxTask);
  159.     xQueue= xQueueCreate(1,sizeof( HWstring) );
  160.     configASSERT(xQueue);
  161.     vTaskStartScheduler();
  162.     while(1);
  163.     return 0;
  164. }
  165.  
  166. void network_thread(void *p)
  167. {
  168.     xil_printf("-----in network_thread ------\r\n");
  169.     struct netif *netif;
  170.     struct ip_addr ipaddr, netmask, gw;
  171.     /* the mac address of the board. this should be unique per board */
  172.     unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };
  173.  
  174.     netif = &server_netif;
  175.  
  176.     /* initiliaze IP addresses to be used */
  177.     IP4_ADDR(&ipaddr,  192, 168, 1, 10);
  178.     IP4_ADDR(&netmask, 255, 255, 255,  0);
  179.     IP4_ADDR(&gw,      192, 168, 1, 1);
  180.  
  181.     /* Add network interface to the netif_list, and set it as default */
  182.     if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, PLATFORM_EMAC_BASEADDR)) {
  183.         xil_printf("Error adding N/W interface\r\n");
  184.         return;
  185.     }
  186.     // Set a network interface as the default network interface
  187.     netif_set_default(netif);
  188.  
  189.     /* specify that the network if is up */
  190.     netif_set_up(netif);
  191.  
  192.     /* start packet receive thread - required for lwIP operation */
  193.     sys_thread_new("xemacif_input_thread", (void(*)(void*))xemacif_input_thread, netif, THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  194.     xil_printf("\r\n");
  195.     sys_thread_new("echod", echo_application_thread, 0,THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  196.     vTaskDelete(NULL);
  197.  
  198.     return;
  199. }
  200.  
  201. int main_thread()
  202. {
  203.     lwip_init();
  204.     xil_printf("-----in Main_Thread ------\r\n");
  205.     /* initialize lwIP before calling sys_thread_new */
  206.     /* any thread using lwIP should be created using sys_thread_new */
  207.     sys_thread_new("NW_THRD", network_thread, NULL,THREAD_STACKSIZE,DEFAULT_THREAD_PRIO);
  208.     vTaskDelete(NULL);
  209.     return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement