API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
wiznet_tcp_server.c

This is an example about how to create a TCP server using the wiznet device.

/* @file main.c
* @author TST
* @version 0.1
* @date 18/11/2013
* @brief This is an example of how to create a TCP server using the
* wiznet device.
*
* The Wiznet chip is a Hardwired TCP/IP embedded Ethernet controller that
* enables easier internet connection using SPI (Serial Peripheral
* Interface).
*
* This example shows the user how to create and manage a TCP server
* performing an active listenning.
*
* @note To run this application it's necessary to set the following parameters:
* - MAC address.
* - IP device.
* - Subnet mask.
* - IP gateway.
* - DNS address.
*
* Users shall change the specific defines located below: MACx, IP_ADDRx, NET_MASK_x, IPGWx
* and DNSx.
*
*
* A TCP client is necessary to run this application. User can use a Telnet
* protocol for this application, mainly because it's very easy to find one.
* Telnet is a network protocol used on the Internet or local area networks
* to provide a bidirectional interactive text-oriented communications facility
* using a virtual terminal connection. Telnet runs over the Transmission
* Control Protocol (TCP). So user can send TCP data to wiznet in a simple
* way. The TCP port used is 1236.
* Example:
* telnet <your IP board> 1236
*
* @note This application uses the debug UART to display debug information.
* To use the debug UART, users should set the next configuration in their
* serial communication program:
* - Baud rate -----> 115200
* - Word length ---> 8 bits
* - Parity --------> none
* - Stop bit ------> 1
* - Flow control --> none
*
*/
/* Platform headers */
#include "tsmart.h"
#include "wiznet\W5200\include\w5200.h"
#include "wiznet\W5200\include\socket.h"
#include "wiznet\W5200\include\spi.h"
#include "wiznet\W5200\include\config.h"
/* This is the specific configuration for Wiznet, Set them */
/* Set MAC address */
#define MAC0 0x00
#define MAC1 0x11
#define MAC2 0x22
#define MAC3 0x33
#define MAC4 0x44
#define MAC5 0x55
/* Set IP address */
#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 1
#define IP_ADDR3 158
/* Set Netmask */
#define NET_MASK0 255
#define NET_MASK1 255
#define NET_MASK2 255
#define NET_MASK3 0
/* Set IP Gateway */
#define IP_GW0 192
#define IP_GW1 168
#define IP_GW2 1
#define IP_GW3 1
/* Set DNS */
#define DNS0 8
#define DNS1 8
#define DNS2 8
#define DNS3 8
/* Number of socket for the TCP server */
#define TCP_S_SOCKET 5
/* Maximum reception buffer size */
#define MAX_BUF_SIZE (2*1024)
/* @brief Wiznet config
*
* This function configures wiznet the user network
* parameters
*
*/
void WIZ_Config(void){
/* Variables */
wiz_NetInfo netinfo;
/* HW Reset */
/* Initialize Wiznet */
/* Enable IRQ socket to work in IRQ mode */
/* Set MAC address */
netinfo.Mac[0] = MAC0;
netinfo.Mac[1] = MAC1;
netinfo.Mac[2] = MAC2;
netinfo.Mac[3] = MAC3;
netinfo.Mac[4] = MAC4;
netinfo.Mac[5] = MAC5;
/* Set IP address */
netinfo.IP[0] = IP_ADDR0;
netinfo.IP[1] = IP_ADDR1;
netinfo.IP[2] = IP_ADDR2;
netinfo.IP[3] = IP_ADDR3;
/* Set Subnet mask */
netinfo.Subnet[0] = NET_MASK0;
netinfo.Subnet[1] = NET_MASK1;
netinfo.Subnet[2] = NET_MASK2;
netinfo.Subnet[3] = NET_MASK3;
/* Set IP Gateway */
netinfo.Gateway[0] = IP_GW0;
netinfo.Gateway[1] = IP_GW1;
netinfo.Gateway[2] = IP_GW2;
netinfo.Gateway[3] = IP_GW3;
/* Set DNS */
netinfo.DNSServerIP[0] = DNS0;
netinfo.DNSServerIP[1] = DNS1;
netinfo.DNSServerIP[2] = DNS2;
netinfo.DNSServerIP[3] = DNS3;
/* Set network info */
SetNetInfo(&netinfo);
/* Show netowrk info */
ts_printf("\r\n--------------------------------------- \r\n");
ts_printf("Network Configuration Information \r\n");
ts_printf("--------------------------------------- ");
ts_printf("\r\nMAC : %02X.%02X.%02X.%02X.%02X.%02X", netinfo.Mac[0],netinfo.Mac[1],netinfo.Mac[2],netinfo.Mac[3],netinfo.Mac[4],netinfo.Mac[5]);
ts_printf("\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]);
ts_printf("\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]);
ts_printf("\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]);
ts_printf("\r\nDNS server : %d.%d.%d.%d \r\n", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]);
}
/* @brief vW_TCP_Server task
*
* This task turns on the wiznet device and it sets the network parameters.
* After that, it creates a TCP server and waits for new TCP data form a
* remote TCP client.
*
* @param pvParameters
*/
void vW_TCP_Server(void *pvParameters) {
/* Variables */
uint16_t local_port = 1236; /* Local port */
int16_t data_len; /* Data length received */
tsmart_w5200_socket_events_t event; /* Event */
static uint8_t data[MAX_BUF_SIZE]; /* Data buffer */
/* Debug message */
ts_printf("---> WIZNET: TCPSERVERT EXAMPLE <---\r\n");
/* Configure network */
WIZ_Config();
/* Time to stabilize */
vTaskDelay(1000/portTICK_RATE_MS);
/* Open server */
if (TCPServerOpen(TCP_S_SOCKET, local_port) != 1){
for(;;);
}
ts_printf("Waiting for the first connection\r\n");
while(1){
xQueueReceive(socket_queue[TCP_S_SOCKET], &event, portMAX_DELAY/portTICK_RATE_MS);
switch(event){
/* Nothing to do */
break;
/* Connected */
ts_printf("< Remote TCP client connected >\r\n\r\n");
TCPSend(TCP_S_SOCKET, (u_char *)"WIZNET OK, press '*' to disconnect\r\n", strlen("WIZNET OK, press '*' to disconnect\r\n"));
break;
/* Data received */
/* Reset data buffer */
memset(data, 0x00, sizeof(data));
/* Get data lenght received */
data_len = GetSocketRxRecvBufferSize(TCP_S_SOCKET);
/* Receive TCP data */
data_len = TCPRecv(TCP_S_SOCKET, (u_char*)data, data_len);
if(data[0] == '*'){
/* Disconnection request */
ts_printf("\r\n< Disconnection request: closing connection... >\r\n");
TCPClose(TCP_S_SOCKET);
}else{
ts_printf("%s", data);
}
break;
/* Disconnected */
/* Open server */
if (TCPServerOpen(TCP_S_SOCKET, local_port) != 1){
for(;;);
}
ts_printf("Waiting for a new connection\r\n");
break;
/* Timeout */
ts_printf("< Timeout >\r\n\r\n");
break;
/* Sending ACK */
ts_printf("< Welcome message Sending- OK >\r\n");
break;
default:
/* Unknow state */
break;
}
}
/* End task */
while(1);
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
tsmart_uart_config_t uart_config;
uint8_t uart_buf[1];
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: Wiznet and UART
*/
/* Initialize wiznet */
/* UART configuration */
uart_config.baud_rate = 115200;
uart_config.flow_control = TSMART_UART_FC_NONE;
uart_config.parity_bit = TSMART_UART_PARITY_NONE;
uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
uart_config.stop_bit = TSMART_UART_STOPBITS_1;
uart_config.rx_buf_sz = 1;
uart_config.rx_buf = uart_buf;
uart_config.use_dma = TSMART_UART_DMA_RX_TX;
uart_config.use_tim = TSMART_UART_TIM_SW;
/* Initialize UART */
TSMART_UART_Init(&tsmart_uart1, &uart_config);
/* Use ts_printf for debugging. This utility is vinculated to an UART. To use it,
* it is necessary to initilialize an UART */
ts_printf_init(&tsmart_uart1);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need one task:
*
* - W_TCP_Server task: This task turns on the wiznet, configures the device
* and creates a TCP server. Finally it is waiting for
* new data from a remote tcp client.
*/
/* Create W_TCP_Server Task */
if (xTaskCreate(vW_TCP_Server, "W_TCP_Server", 512, NULL, 7, NULL) != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}