STM32CubeMx LWIP tutorial (I) - PHY hardware layer construction

Publisher:悠然自在Latest update time:2018-04-22 Source: eefocusKeywords:STM32CubeMx Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Regarding the PHY construction of LWIP, the PHY chip used is LAN8720A, RMII mode. 
Regarding the hardware connection part, the Atomic board F407 is used, and the hardware connection is as follows: 
Write the picture description here 
Note: LAN8720A can be configured through the PHYAD0 pin, which is multiplexed with the RXER pin. The chip has a pull-down resistor inside. After the hard reset is over, LAN8720A will read the pin level as the SMI address of the device. When the pull-down resistor is connected (it can also be floating because the chip has a pull-down resistor inside), the SMI address is set to 0. When the pull-up resistor is connected externally, it can be set to 1. The RXER pin of this hardware is floating, and its PHY chip address is 0

Regarding the configuration of SMT32CubeMx pin clock and other configurations, you can configure them according to the actual situation, so I will not go into details here; 
click the Parameter Setting tab in the ETH configuration and configure it as follows: 
Write the picture description here 
In this tab, just fill in the PHY address according to the actual situation. The PHY chip address I use is 0, so just enter 0 here; 
click the Advanced Parameters tab in the ETH configuration and select as follows: 
Write the picture description here 
Since the STM32CubeMx system defaults to LAN8742A, we choose user PHY configuration LAN8720A; 
its configuration is as follows: 
Write the picture description here 
There are not many configuration items about the PHY chip, most of which use the default parameters. Those that need to be changed are marked with red lines, such as: 
PHY Address Value: This is the PHY chip address we set before; 
PHY Name: This is just an identifier and can be set at will. Here I set it to LAN8720; 
PHY Special Control/Status Register Offset: You can find it according to the chip manual, as follows: 
Write the picture description here 
Note: The address of this register is decimal 31. 
Similarly, the settings of the PHY Speed ​​mask and PHY Duplex mask registers are as follows: 
Write the picture description here 
Bits4:2 includes the PHY Speed ​​mask and PHY Duplex mask, 
Bits2 represents two speeds: 10MB/s and 100MB/s; PHY Speed ​​mask (0x0004) 
Bits4 represents two working modes: half-duplex and full-duplex; PHY Duplex mask (0x0010)

Then enable ETH interrupt and use the default priority;

The LWIP configuration is as follows: 
Write the picture description here 
The rest can be kept as default

Finally, in the generated code, add the code for resetting the PHY hardware in the ETH hardware initialization section: (Note: This code is added here because of hardware design, add it according to the actual situation) 
Write the picture description here

Add the following code:

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "stm32f4xx_hal.h"

#include "lwip.h"

#include "gpio.h"


/* USER CODE BEGIN Includes */

#include "tcp.h"

#include "stats.h"

/* USER CODE END Includes */


static struct tcp_pcb *tcp_echoserver_pcb;


void tcp_echoserver_init(void);

static err_t tcp_echoserver_accept(void *arg,struct tcp_pcb *newpcb,err_t err);

static err_t tcp_echoserver_recv(void *arg,struct tcp_pcb *tpcb,struct pbuf *p,err_t err);


void tcp_echoserver_init(void)

{

    err_t err;


    tcp_echoserver_pcb=tcp_new();


    if(tcp_echoserver_pcb!=NULL)

    {

        err=tcp_bind(tcp_echoserver_pcb,IP_ADDR_ANY,7);


        if(err==ERR_OK)

        {

            tcp_echoserver_pcb=tcp_listen(tcp_echoserver_pcb);

            tcp_accept(tcp_echoserver_pcb,tcp_echoserver_accept);

        }


        else

        {

            memp_free(MEMP_TCP_PCB,tcp_echoserver_pcb);

        }

    }

}


static err_t tcp_echoserver_accept(void *arg,struct tcp_pcb *newpcb,err_t err)

{

    tcp_recv(newpcb,tcp_echoserver_recv);


    return ERR_OK;

}


static err_t tcp_echoserver_recv(void *arg,struct tcp_pcb *tpcb,struct pbuf *p,err_t err)

{

    tcp_write(tpcb,p->payload,p->len,1);

    pbuf_free(p);


    return ERR_OK;

}

/* USER CODE END 0 */


int main(void)

{


  /* USER CODE BEGIN 1 */


  /* USER CODE END 1 */


  /* MCU Configuration----------------------------------------------------------*/


  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();


  /* USER CODE BEGIN Init */


  /* USER CODE END Init */


  /* Configure the system clock */

  SystemClock_Config();


  /* USER CODE BEGIN SysInit */


  /* USER CODE END SysInit */


  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_LWIP_Init();


  /* USER CODE BEGIN 2 */

    tcp_echoserver_init();

  /* USER CODE END 2 */


  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  /* USER CODE END WHILE */


  /* USER CODE BEGIN 3 */

    MX_LWIP_Process();

  }

  /* USER CODE END 3 */


}

Download the program to the target board and connect the network cable to the computer; 
change the properties of the computer's IPV4 as follows: 
Write the picture description here 
Finally, use the command line to ping the target board as follows: 
Write the picture description here 
At this point, the test of the PHY chip is completed, and LWIP works normally;

As a side note, the configuration of other PHY chips is similar to this, but please note the differences in the register addresses of each chip;


Keywords:STM32CubeMx Reference address:STM32CubeMx LWIP tutorial (I) - PHY hardware layer construction

Previous article:Implementing microsecond delay of general timer under STM32Cubemx
Next article:Stm32cubeMX generates configuration project examples and process records

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号