/******************************************************************************* * Includes ******************************************************************************/ #include "lwip/opt.h" #include "fsl_debug_console.h" #include "tcpecho.h" #include "lwip/netifapi.h" #include "lwip/netif.h" #include "lwip/tcpip.h" #include "netif/ethernet.h" #include "ethernetif.h" #include "socket_task.h" #include "shell_task_mode.h" #include "lwip/sys.h" #include "lwip/netif.h" #include "pin_mux.h" #include "board.h" #ifndef configMAC_ADDR #include "fsl_silicon_id.h" #endif #include "fsl_phy.h" #include "fsl_component_serial_manager.h" #include "fsl_shell.h" #include "lwip/sockets.h" #include "shell_task.h" #include"socket_task.h" #include "fsl_phylan8720a.h" #include "fsl_enet.h" #include "pin_mux.h" #include /******************************************************************************* * Definitions ******************************************************************************/ /* @TEST_ANCHOR */ /* IP address configuration. */ #ifndef configIP_ADDR0 #define configIP_ADDR0 192 #endif #ifndef configIP_ADDR1 #define configIP_ADDR1 168 #endif #ifndef configIP_ADDR2 #define configIP_ADDR2 0 #endif #ifndef configIP_ADDR3 #define configIP_ADDR3 150 #endif /* Netmask configuration. */ #ifndef configNET_MASK0 #define configNET_MASK0 255 #endif #ifndef configNET_MASK1 #define configNET_MASK1 255 #endif #ifndef configNET_MASK2 #define configNET_MASK2 255 #endif #ifndef configNET_MASK3 #define configNET_MASK3 0 #endif /* Gateway address configuration. */ #ifndef configGW_ADDR0 #define configGW_ADDR0 192 #endif #ifndef configGW_ADDR1 #define configGW_ADDR1 168 #endif #ifndef configGW_ADDR2 #define configGW_ADDR2 0 #endif #ifndef configGW_ADDR3 #define configGW_ADDR3 100 #endif /* Ethernet configuration. */ extern phy_lan8720a_resource_t g_phy_resource; #define EXAMPLE_ENET_BASE ENET #define EXAMPLE_PHY_ADDRESS BOARD_ENET0_PHY_ADDRESS #define EXAMPLE_PHY_OPS &phylan8720a_ops #define EXAMPLE_PHY_RESOURCE &g_phy_resource #define EXAMPLE_CLOCK_FREQ CLOCK_GetFreq(kCLOCK_CoreSysClk) #ifndef EXAMPLE_NETIF_INIT_FN /*! @brief Network interface initialization function. */ #define EXAMPLE_NETIF_INIT_FN ethernetif0_init #endif /* EXAMPLE_NETIF_INIT_FN */ /*! @brief Stack size of the temporary lwIP initialization thread. */ #define INIT_THREAD_STACKSIZE 1024 /*! @brief Priority of the temporary lwIP initialization thread. */ #define INIT_THREAD_PRIO DEFAULT_THREAD_PRIO /******************************************************************************* * Prototypes ******************************************************************************/ /******************************************************************************* * Variables ******************************************************************************/ phy_lan8720a_resource_t g_phy_resource; static phy_handle_t phyHandle; /******************************************************************************* * Code ******************************************************************************/ static void MDIO_Init(void) { (void)CLOCK_EnableClock(s_enetClock[ENET_GetInstance(EXAMPLE_ENET_BASE)]); ENET_SetSMI(EXAMPLE_ENET_BASE); } static status_t MDIO_Write(uint8_t phyAddr, uint8_t regAddr, uint16_t data) { return ENET_MDIOWrite(EXAMPLE_ENET_BASE, phyAddr, regAddr, data); } static status_t MDIO_Read(uint8_t phyAddr, uint8_t regAddr, uint16_t *pData) { return ENET_MDIORead(EXAMPLE_ENET_BASE, phyAddr, regAddr, pData); } static int s_af; static int s_sck_type; #define SERVER_IP_ADDRESS "192.168.1.150" #define SERVER_PORT 9000 // Define the size of the receive buffer #define BUFFER_SIZE 1024 int server_fd, new_socket, valread; struct sockaddr_in server_addr; int opt = 1; // int addrlen = sizeof(address); char buffer[1024] = { 0 }; char* hello = "Hello from server"; /*! * @brief Initializes lwIP stack. * * @param arg unused */ static void stack_init(void *arg) { ip4_addr_t netif_ipaddr, netif_netmask, netif_gw; ethernetif_config_t enet_config = {.phyHandle = &phyHandle, .phyAddr = EXAMPLE_PHY_ADDRESS, .phyOps = EXAMPLE_PHY_OPS, .phyResource = EXAMPLE_PHY_RESOURCE, .srcClockHz = EXAMPLE_CLOCK_FREQ, #ifdef configMAC_ADDR .macAddress = configMAC_ADDR #endif }; static struct netif s_netif; LWIP_UNUSED_ARG(arg); /* Set MAC address. */ #ifndef configMAC_ADDR (void)SILICONID_ConvertToMacAddr(&enet_config.macAddress); #endif IP4_ADDR(&netif_ipaddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3); IP4_ADDR(&netif_netmask, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3); IP4_ADDR(&netif_gw, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3); tcpip_init(NULL, NULL); netifapi_netif_add(&s_netif, &netif_ipaddr, &netif_netmask, &netif_gw, &enet_config, EXAMPLE_NETIF_INIT_FN, tcpip_input); netifapi_netif_set_default(&s_netif); netifapi_netif_set_up(netif_default); while (ethernetif_wait_linkup(&s_netif, 5000) != ERR_OK) { PRINTF("PHY Auto-negotiation failed. Please check the cable connection and link partner setting.\r\n"); } shell_task_init(NULL, 0); vTaskDelete(NULL); } /*! * @brief Main function */ int main() { /* Init hardware*/ /* attach 12 MHz clock to FLEXCOMM0 (debug console) */ CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH); /* Enable the asynchronous bridge */ SYSCON->ASYNCAPBCTRL = 1; CLOCK_EnableClock(kCLOCK_InputMux); BOARD_InitBootPins(); BOARD_BootClockFRO12M(); BOARD_InitBootClocks(); BOARD_InitDebugConsole(); tcpip_init(NULL, NULL); /* Initialize lwIP from thread */ if (sys_thread_new("main", stack_init, NULL, INIT_THREAD_STACKSIZE, INIT_THREAD_PRIO) == NULL) { LWIP_ASSERT("main(): Task creation failed.", 0); } // Creating socket file descriptor if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { LWIP_ASSERT("server_fd >= 0 ", server_fd >= 0); perror("socket failed"); exit(EXIT_FAILURE); } // Forcefully attaching socket to the port 8080 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { perror("setsockopt"); exit(EXIT_FAILURE); } server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(SERVER_PORT); // Forcefully attaching socket to the port 900 if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { perror("bind failed"); exit(EXIT_FAILURE); } if (listen(server_fd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } if ((new_socket = accept(server_fd, (struct sockaddr*)&server_addr, (socklen_t*)&server_addr)) < 0) { perror("accept"); exit(EXIT_FAILURE); } valread = read(new_socket, buffer, 1024); PRINTF("%s\n", buffer); send(new_socket, hello, strlen(hello), 0); PRINTF("Hello message sent\n"); // closing the connected socket close(new_socket); // closing the listening socket shutdown(server_fd, SHUT_RDWR); // MDIO_Init(); // g_phy_resource.read = MDIO_Read; // g_phy_resource.write = MDIO_Write; #if 0 // Client side C/C++ program to demonstrate Socket #include #define PORT 8080 int main(int argc, char const* argv[]) { int status, valread, client_fd; struct sockaddr_in serv_addr; char* hello = "Hello from client"; char buffer[1024] = { 0 }; if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("\n Socket creation error \n"); return -1; } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // Convert IPv4 and IPv6 addresses from text to binary // form if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { printf( "\nInvalid address/ Address not supported \n"); return -1; } if ((status = connect(client_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0) { printf("\nConnection Failed \n"); return -1; } send(client_fd, hello, strlen(hello), 0); printf("Hello message sent\n"); valread = read(client_fd, buffer, 1024); printf("%s\n", buffer); // closing the connected socket close(client_fd); return 0; } #endif vTaskStartScheduler(); /* Will not get here unless a task calls vTaskEndScheduler ()*/ return 0; }