stm32f412 Application Development Note V: combining W5500 for Ethernet communication

Source: Internet
Author: User

Because of the actual use of the requirements we test the network communication, on the NUCLEO-F412ZG Test board does not have an Ethernet section, we choose to add a W5500 experimental board. W5500 supports SPI interface communication, dc3.3v feed. And the NUCLEO-F412ZG Test board has SPI1 each pin Spi1_nss (cn7_17), Spi1_sck (cn7_10), Spi1_miso (Cn7_12), Spi1_mosi (cn7_14) to the CN7, The 3.3VDC and GND also guide the CN8, which can be easily implemented for testing. As shown in the red box:

1 , the preparation of the test

In order to achieve Ethernet communication first need to download W5500 driver source, can be downloaded to Wiznet's official website: http://www.iwiznet.cn/

Currently the latest version of V1.1, I chose to V1.03 download the compressed package after decompression, such as:

You need to copy the Ethernet folder to our project directory:

And in the IAR project to add the relevant files and paths, mainly socket.c, w5500.c, wizchip_.conf.c three files. These three files respectively implement socket, hardware driver and related communication configuration function, can see the corresponding source level manual.

2 , writing test code

After completing the above work, you can begin to actually transplant the work. The steps are as follows:

    • hardware configuration and initialization.
    • Initialization of Ethernet communication configuration.
    • Implement a specific communication process.

2.1. Configuration and initialization of hardware

Since W5500 communicates with STM32 via the SPI interface, hardware configuration and initialization is very simple, and W5500 actually does not matter, making some common operations. In fact, the process of initializing the SPI interface of STM32F412ZG requires the initialization of the RCC, Gpio, and SPI to be implemented. For this section you can view the ST's routines.

2.2. Initialization of Ethernet communication configuration

The initialization of Ethernet communication configuration has three main contents:

    • Registers the TCP communication related callback function registerfunction ();
    • Initialize chip parameter chipparametersconfiguration ();
    • Initializing network communication Parameters Networkparameterconfiguration ()

The specific implementation of the three functions is as follows:

function registration, first of all, the SPI registration callback function should be implemented by the user to access the Wizchip

void Registerfunction (void)

{

Critical section callback function

Reg_wizchip_cris_cbfunc (Spi_crisenter, spi_crisexit); Register critical section function

Chip Selection callback function

#if _wizchip_io_mode_ = = _wizchip_io_mode_spi_vdm_

Reg_wizchip_cs_cbfunc (Spi_cs_select, spi_cs_deselect);//register SPI chip SELECT Signal function

#elif _wizchip_io_mode_ = = _wizchip_io_mode_spi_fdm_

Reg_wizchip_cs_cbfunc (Spi_cs_select, spi_cs_deselect); CS must be low-level.

#else

#if (_wizchip_io_mode_ & _wizchip_io_mode_sip_)! = _wizchip_io_mode_sip_

#error "Unknown _wizchip_io_mode_"

#else

Reg_wizchip_cs_cbfunc (Wizchip_select, wizchip_deselect);

#endif

#endif

SPI read-Write callback function

Reg_wizchip_spi_cbfunc (Spi_readbyte, spi_writebyte); Registering read-write functions

}

The registration function is actually a function pointer call, can refer to the C function pointer part of the content. For the above registered functions, spi_writebyte need to explain, whether using a function or direct operation Register, after the completion of the need to read (red), or the client will be connected TCPServer timeout alarm, do not understand what the reason.

Write 1 bytes of data to the SPI bus

void Spi_writebyte (uint8_t txdata)

{

while ((SPI2->SR&SPI_I2S_FLAG_TXE) ==0); Wait for Send area empty

spi2->dr=txdata; Send a Byte

while ((Spi2->sr&spi_i2s_flag_rxne) ==0); Wait for a byte to finish receiving

spi2->dr;

while (Spi_i2s_getflagstatus (SPI2, spi_i2s_flag_txe) = = RESET); Wait for Send area empty

Spi_i2s_senddata (Spi2,txdata); Send a Byte

while (Spi_i2s_getflagstatus (spi2,spi_i2s_flag_rxne) = = RESET); Wait for a byte to finish receiving

Spi_i2s_receivedata (SPI2); Returns the data received

}

Initialize chip parameters

void Chipparametersconfiguration (void)

{

uint8_t tmp;

uint8_t Memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};

Wizchip Socket Buffer Initialization

if (Ctlwizchip (Cw_init_wizchip, (void*) memsize) = =-1) {

printf ("Wizchip Initialized fail.\r\n");

while (1);

}

PHY Physical Layer Connection Status check

do{

if (Ctlwizchip (Cw_get_phylink, (void*) &tmp) = =-1) {

printf ("Unknown PHY Link stauts.\r\n");

}

}while (tmp = = Phy_link_off);

}

The above implementation of the network physical layer configuration.

Initializing network parameter information in the Wizchip

void Networkparameterconfiguration (void)

{

uint8_t Tmpstr[6];

Ctlnetwork (Cn_set_netinfo, (void*) &gwiznetinfo);

Ctlnetwork (Cn_get_netinfo, (void*) &gwiznetinfo);

Ctlwizchip (cw_get_id, (void*) tmpstr);

}

Where Gwiznetinfo is a struct variable of type wiz_netinfo, which is defined in wizchip_conf.h to set the MAC address, IP address and other network parameters, as follows:

typedef struct WIZ_NETINFO_T

{

uint8_t Mac[6]; < Source Mac Address

uint8_t Ip[4]; < Source IP Address

uint8_t Sn[4]; < Subnet Mask

uint8_t Gw[4]; < Gateway IP Address

uint8_t Dns[4]; < DNS server IP Address

Dhcp_mode DHCP; < 1-static, 2-dhcp

}wiz_netinfo;

The initialization of the network section is now complete.

2.3, the realization of the specific communication process

After the previous configuration network has been able to ping, the following can be implemented in specific applications. For me this project is the implementation of Modbus TCP has been written.

Write TCP Server, this section has a lot of information, directly attached to the code:

TCP Server Data communication

int32_t TCPServer (uint8_t sn, uint16_t Port)

{

int32_t ret;

uint8_t SOCKETSTATUS=GETSN_SR (SN);

Switch (socketstatus)

{

Case sock_established:

{

if (Getsn_ir (SN) & Sn_ir_con)

{

Setsn_ir (Sn,sn_ir_con);

}

uint16_t size=0;

if (size = GETSN_RX_RSR (sn)) > 0)

{

if (Size > Data_buffer_size)

{

size = Data_buffer_size;

}

uint8_t Rxbuffer[data_buffer_size];

ret = recv (sn,rxbuffer,size);

if (ret <= 0)

{

return ret;

}

Functions to add data parsing and response

uint8_t Txbuffer[data_buffer_size];

uint16_t length=receiveddataparsing (Rxbuffer,txbuffer);

uint16_t sentsize=0;

while (length! = sentsize)

{

ret = Send (sn,txbuffer+sentsize,length-sentsize);

if (Ret < 0)

{

Close (SN);

return ret;

}

Sentsize + = ret; No tube sockerr_busy, because it is 0.

}

}

Break

}

Case SOCK_CLOSE_WAIT:

if ((Ret=disconnect (sn))! = SOCK_OK)

{

return ret;

}

Break

Case Sock_init:

if (ret = listen (SN))! = SOCK_OK)

{

return ret;

}

Break

Case sock_closed:

if ((Ret=socket (sn,sn_mr_tcp,port,0x00))! = SN)

{

return ret;

}

Break

Default

Break

}

return 1;

}

where Receiveddataparsing (Rxbuffer,txbuffer) implements specific application protocols, depending on the specific requirements, we have implemented a simple Modbus TCP protocol.

3 , test results

Finished writing, debugging error, the program is downloaded to the target board, first of all, since it is Ethernet communication, we are on the upper computer computer Simple network test. On the target board we set the IP address as: 192.168.1.100, in CMD using the simple ping command test as follows:

As can be seen, the network is not a problem. Next we use the TCP&UDP test tool for further testing and monitor the packets using Microsoft Network Monitor. Open the TCP&UDP test tool, create a TCP client, target IP or target board IP, port with 502 (we set in the software).

After the creation is complete, the connection is correct, send a command in the Send field with 16, you can see the data received in the receiving column below.

Let's look at the packets captured in Microsoft Network Monitor to check if the data is correct.

Since we have implemented the simple Modbus TCP protocol, we are using MODSCAN32 to test whether the communication is correct. Start ModScan32 first, and do the following configuration:

The data is displayed after the connection, and there is no problem after a period of continuous updating.

Again, let's look at the data updates captured in Microsoft Network Monitor in contrast to the MODSCAN32, and the packets are complete and correct.

Complete the test, communication is no problem stm32f412 and W5500 Ethernet communication is simple and convenient.

stm32f412 Application Development Note V: combining W5500 for Ethernet communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.