STM32F407 Ethernet and USB OTG rapid development

Publisher:雅致书香Latest update time:2018-06-27 Source: eefocusKeywords:stm32f407  Ethernet  USB  OTG Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

introduction

If you want to quickly complete the network protocol stack and USB OTG functions, it is best to use ST's own tool STM32CubMx. If you don't know how to use it yet, don't worry. I will tell you how to do it step by step with pictures.

Software platform: windows, STM32CubMx, keil v5

Hardware platform: Atom's stm32f407ZGT6 development board,

STM32CubMx configuration

1. Mapping configuration of Ethernet pins

First: Read the schematic diagram and use STM32CubMx to complete the configuration. The schematic diagram is not explained here. Go directly to STM32CubMx configuration.


(1) Open the STM32CubMx software, select New Project, select the corresponding chip in the pop-up menu, and click OK. As shown below:


(2) Set the clock. The board uses an 8MHz crystal oscillator, and the software corresponds to Peripheral -> RCC. As shown below:



(3) Configure Ethernet according to the schematic diagram. Select the RMII interface. Note that due to pin reuse, after selecting RMII, the software automatically may not correspond to the principle. We should configure the pins according to the schematic diagram! If they correspond, there is no need to change them. If they do not correspond, they must be changed. As shown below:


The Ethernet PHY chip pin connection diagram in the original picture is the RMII interface, as shown below:


Corresponding pins, software corresponding pin configuration


The corresponding positions of the pins RMII_REF_CLK, ETH_MDIO, RMII_CRS_DV, ETH_RESET, RMII_TX_EN, RMII_TXD0, and RMII_TXD1, as well as the configuration on the software, are shown in the following figure:






After the above configuration, the Ethernet pins are basically mapped. To modify the pin configuration, click a key and select the corresponding function in the pop-up function menu!

2. USB OTG pin mapping


OTG configuration! Use FS for OTG, because the schematic diagram uses FS.


The pins use the default ones and do not need to be changed.


3. UART1 as the printer port mapping


The pins use the default ones and do not need to be changed.


4. Configuration of third-party libraries


(1) The configuration of the third-party library mainly includes the file system (required for USB OTG), the network protocol stack (required for Ethernet), and the RT OS (the OS is selected this time to facilitate the scheduling of Ethernet and USB OTG tasks later).

As follows: Since OS is selected, and the tick of OS is based on Systick, we have to select another timer for software tcik. Here we select TIM1. As shown in the figure below:

Configure sys tick, system timer, in ms


Optional third-party libraries:

Select USB Disk as the file system

Enable freeOS (embedded real-time operating system) and LWIP (TCP/IP protocol stack)



Details:

System clock configuration

The system clock configuration is set in the Clock configuration tab, as follows: Use a 168MHz clock, and the USB clock must be 48MHz. The specific configuration is as follows:


 The function is configured in the Configuration tab. To facilitate web page development, I configure the httpd function of LWIP and set the GPIO output to High. As shown below:


The overall detailed framework is as follows:



Steps to generate code framework:

  Generate code framework, menu project->setting to set the compilation platform and directory, Generator code generates code framework!



Code configuration

1. Enter the software and code platform writing!


(1) Writing PHY driver


Since the code generated according to the above steps is basically usable, but the code is based on the PHY configured by the LAN8742A chip, the schematic uses LAN8720. Note that the PHY chips are basically the same. The main chip and PHY communicate through the MDIO interface. This is standardized, so the framework program is also like this. These basically do not need to be changed. In addition, PHY is a standard module defined in IEEE802.3. The register address 0-31 of the PHY chip is also standardized in IEEE802.3, so it is very convenient when transplanting. The difference is the PHY reset time and the PHY chip address. The address LAN8720 has the following description:

The PHYAD0 bit is driven high or low to give each PHY a unique address.

PHYAD 0 bit is set to 1 or 0 to give the PHY a unique address, which is the address of the LAN7820A. This address is latched into an internal register at the end of a hardware reset (default = 0b).

In a multi-PHY application (such as a repeater), the controller is able to manage each PHY via the unique address.

Each PHY checks each management data frame for a matching address in the relevant bits.

When a match is recognized, the PHY responds to that particular frame.

The PHY address is also used to seed the scrambler.

In a multi-PHYapplication, this ensures that the scramblers are out of synchronization and dispersesthe electromagneticradiation across the frequency spectrum.

According to the above statement, the address of LAN7820 here should be 0, so find the definition of LAN8742A_PHY_ADDRESS and change it to 0.

Location: Stm32f4xx_hal_conf.h


Reset time


IP Modification


Modify your own IP. Note that the framework turns on DHCP by default. With the DHCP function, the device can request the DHCP server to dynamically configure the device's IP in the network. We want to set it to a fixed IP here, so disable DHCP! As follows:

Header file: lwipopts.h



lwip.c file modification



Fixed IP modification, for ease of use, I define a union type to store IP addresses, as shown below:


Modify the HAL_ETH_Init function and add a hard reset as follows:



LWIP Test Results

After this modification, it can be completed! Only the host can ping. Note that the printer port mapping code is not written out. This is very simple. You can find it by searching Baidu. The specific experimental diagram is as follows:



You must use the serial port tool of Zhengdian Atom. Note that you need to turn on some debugging information to see these print information.



 USB OTG can be used directly, allowing USB monitoring status. The experimental effect is as follows:

USB Test Results



Insert the USB, the host detects that the USB is connected, and you can call the mount and fopen functions to read the file normally. This is basically done!

Summarize

1. After completing the functions of LWIP and USB, the remaining part is network communication, which are all applications and are relatively simple. Network communication is nothing more than SOCKET, and USB is the fopen function calling write and read functions, etc.

2. Regarding the printing port, I saw httpd_init. That’s because I added a web page design, and I can modify the IP address through the web page.

3. Use STM's STM32CubMx software to quickly develop the framework you want. Most of the underlying configurations do not need to be calculated manually, such as the clock. If you use your own calculations to allocate clock sources to each bus, it will be quite troublesome. I have experienced this when I was working on STM32F103. If you don't know how to use this software, it is recommended that you learn it, because it can help us a lot. As someone who is engaged in STM32 and Linux development, it saves a lot of time.


Keywords:stm32f407  Ethernet  USB  OTG Reference address:STM32F407 Ethernet and USB OTG rapid development

Previous article:STM32 Driver DP83848 Notes
Next article:STM32F407VGT6+LAN8720 debugging notes

Recommended ReadingLatest update time:2024-11-23 15:06

Design of VSAT baseband data acquisition system based on USB interface
0 Introduction Among many satellite communication systems, VSAT (very small aperture satellite terminal) satellite communication system has developed rapidly in recent years due to its many characteristics, especially in the field of professional communication network, it has a broad market. The use of this system
[Microcontroller]
Design of VSAT baseband data acquisition system based on USB interface
Change USB device to USB host on 2440/2410
In previous development, we had to use two USB host ports on the 2440, one for the camera and one for the wireless network card. However, the Friendly Arm mini2440 board only has one USB host port. We tried to solve this problem by connecting an external USB hub. There was no problem connecting the wireless network car
[Microcontroller]
Apple Beats Studio Buds revealed: In-ear handleless design, USB-C interface
Jiwei.com reported on June 10 that according to foreign media MacRumors, photos and specific details of the rumored Apple new headphones Beats Studio Buds appeared in the Taiwan Communications Database recently. Unlike previous Beats and AirPods, Beats Studio Buds adopts an in-ear handleless design with the iconic B
[Mobile phone portable]
iPadOS 14.7 official version released: fixes the USB-C to 3.5mm headphone jack converter bug, etc.
      After pushing the iOS 14.7 update to iPhone users yesterday, Apple pushed the official version of iPadOS 14.7 to iPad users early this morning.   IT Home learned that iPadOS 14.7 has made improvements and bug fixes for iPad, including the lack of a "Share Playlist" menu option in Apple Music, and an issue where
[Mobile phone portable]
Application of Mixed Signal Digital Oscilloscope in the Development of Universal Serial Bus
  In the process of developing embedded systems, serial bus protocol analysis has always been a headache for engineers. In the case of a logic analyzer, engineers need to set complex trigger conditions to capture the required data, which requires expensive equipment investment and a lot of time to set up the instrument
[Test Measurement]
Application of Mixed Signal Digital Oscilloscope in the Development of Universal Serial Bus
STM32F407 outputs multiple PWM waves
Main control, black STM32F407 board from Taobao Compiler IAR Target requirement Output four PWM waves Here are the H and C files for you. You only need to call the function in main to achieve the output of four PWM channels. Add in main  PWM_Init();  Update_PWM_(20,200,500,600); //You can get 2%, 20%, 50%, 60%
[Microcontroller]
USB communication program development and design using CY7C68013A
0 Introduction CY7C68013 integrates an enhanced 8051 microcontroller and an I2C compatible controller, and its transmission rate can reach 100 kHz or 400kHz. LabVIEW is a software for designing virtual instruments based on graphical language. LabVIEW uses structural block diagrams in graphical mode to construct progr
[Microcontroller]
USB communication program development and design using CY7C68013A
USB in Automobiles
The convergence of consumer and automotive electronics, including the adoption of the USB protocol as an in-system serial bus, will continue. USB provides a cost-effective and well-understood interface that bridges consumer and automotive systems. At the end of the 20th century, the most successful and widely u
[Automotive Electronics]
USB in Automobiles
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号