Wiznet makers

Benjamin

Published June 27, 2024 ©

43 UCC

11 WCC

4 VAR

0 Contests

0 Followers

1 Following

Original Link

stm32G0-w5500

This project connects an STM32G0 microcontroller with the WIZnet W5500 Ethernet module to obtain an IP address via DHCP.

COMPONENTS Hardware components

STMicroelectronics - STM32G0 microcontroller

x 1


WIZnet - W5500

x 1


PROJECT DESCRIPTION

stm32G0-w5500

This repository provides an implementation for connecting an STM32G0 microcontroller to a network using the Wiznet W5500 Ethernet module and obtaining an IP address via DHCP (Dynamic Host Configuration Protocol).

Features

  • Implements a DHCP client
  • Connects to a network router and obtains IP address automatically
  • Includes example code for basic network communication

Hardware Supported

  • STM32G0 microcontroller
  • Wiznet W5500 Ethernet module

Pin configuration

The W5500 Ethernet module is connected to the following pins of the STM32G0 microcontroller:

W5500 pinSTM32G0 pin
MISOPA6
MOSIPA7
SCKPB3
CSPA5
GNDGND
VCC3.3V/5v

This example code runs without the interupt pin.

Debug Output

The library uses the standard printf() function for debugging output. The default configuration uses the USART2 peripheral on the STM32G0 microcontroller.

The default configuration of the serial output uses 115200 baud, 8 data bits, no parity bit and 1 stop bit (8N1).

After running the example, you can check the serial output in the serial monitor of the STM32G0 microcontroller.

Registering SPI callback...
Calling DHCP_init()...
Registering DHCP callbacks...
Calling DHCP_run()...
> Send DHCP_DISCOVER
DHCP message : 10.42.0.1(67) 300 received.
> Receive DHCP_OFFER
> Send DHCP_REQUEST
DHCP message : 10.42.0.1(67) 300 received.
> Receive DHCP_ACK

> Check leased IP - OK
Callback: IP assigned! Leased time: 10 sec
IP:  10.42.0.128
GW:  10.42.0.1
Net: 255.255.255.0
DNS: 10.42.0.1
Calling wizchip_setnetinfo()...

Connecting STM32G0 to a Network Using WIZnet W5500 Ethernet Module

The STM32G0-W5500 GitHub repository provides a comprehensive guide and implementation for interfacing an STM32G0 microcontroller with a WIZnet W5500 Ethernet module. This integration allows the microcontroller to connect to a network and obtain an IP address using DHCP (Dynamic Host Configuration Protocol). This guide aims to delve into the key components of this implementation, explaining how the STM32G0 and W5500 work together and how you can leverage this setup for robust and efficient network communication.

Overview of STM32G0 Microcontroller

The STM32G0 series is part of the STM32 family of 32-bit microcontrollers from STMicroelectronics. Known for their high performance, low power consumption, and rich peripheral set, STM32G0 microcontrollers are ideal for a wide range of applications, including consumer electronics, industrial control, and IoT devices. Key features include:

  • ARM Cortex-M0+ core
  • Up to 64 KB of Flash memory and 8 KB of RAM
  • Flexible GPIOs, timers, and communication interfaces (UART, SPI, I2C)
  • Advanced power management for low-power applications

Introduction to WIZnet W5500 Ethernet Module

The WIZnet W5500 is a hardwired TCP/IP embedded Ethernet controller that provides easier internet connection for embedded systems using SPI (Serial Peripheral Interface). Its features include:

  • Hardwired TCP/IP stack with built-in Ethernet MAC and PHY
  • Support for up to 8 independent sockets simultaneously
  • High-speed SPI interface (up to 80 MHz)
  • Integrated 32 KB internal memory for TX/RX buffers
  • Low power consumption and small form factor

Setting Up STM32G0 with W5500

To connect the STM32G0 microcontroller to a network via the W5500 Ethernet module, follow these steps:

1. Hardware Connections

Ensure the following connections between the STM32G0 microcontroller andW5500 Photo 1 the W5500 module:

  • SPI Interface: Connect the SPI pins (MOSI, MISO, SCK, CS) of the STM32G0 to the corresponding pins on the W5500 module.
  • Power Supply: Provide appropriate power to both the STM32G0 and W5500.
  • Reset Pin: Connect the reset pin of the W5500 to an available GPIO pin on the STM32G0 to control the reset function.

2. Software Environment

Set up the software environment, including the necessary libraries and tools:

  • STM32CubeMX: Use this tool to configure the STM32G0 microcontroller peripherals, particularly the SPI interface for communication with the W5500.
  • STM32CubeIDE: Develop, compile, and debug the firmware using this integrated development environment.

3. Firmware Development

Develop the firmware to initialize and control the W5500 Ethernet module. Key steps include:

  • SPI Initialization: Configure the SPI peripheral on the STM32G0 for communication with the W5500.
  • W5500 Initialization: Initialize the W5500 module, including setting up the SPI interface, configuring network parameters, and resetting the module.
  • DHCP Client Implementation: Implement the DHCP client to obtain an IP address dynamically from the network.

Detailed Implementation

SPI Configuration

Configure the SPI peripheral using STM32CubeMX. Set the SPI parameters such as mode, baud rate, and data frame format to match the W5500 requirements. Here’s an example configuration:

hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
    // Initialization Error
    Error_Handler();
}

 

W5500 Initialization

Initialize the W5500 Ethernet module by setting up the necessary registers and configuring the network parameters. This includes setting the MAC address, IP address, subnet mask, and gateway address. Here's an example of the initialization code:

void W5500_Init(void)
{
    uint8_t mac_addr[] = {0x00, 0x08, 0xDC, 0x00, 0x00, 0x01};
    uint8_t ip_addr[] = {192, 168, 1, 100};
    uint8_t subnet_mask[] = {255, 255, 255, 0};
    uint8_t gateway_addr[] = {192, 168, 1, 1};
    
    // Set MAC address
    W5500_WriteRegister(W5500_MAC_ADDR, mac_addr, 6);
    // Set IP address
    W5500_WriteRegister(W5500_IP_ADDR, ip_addr, 4);
    // Set subnet mask
    W5500_WriteRegister(W5500_SUBNET_MASK, subnet_mask, 4);
    // Set gateway address
    W5500_WriteRegister(W5500_GATEWAY_ADDR, gateway_addr, 4);
}
 

DHCP Client Implementation

Implement the DHCP client to dynamically obtain an IP address. The DHCP client process involves sending a DHCPDISCOVER message, receiving a DHCPOFFER, sending a DHCPREQUEST, and finally receiving a DHCPACK. Here’s a simplified example:

void DHCP_Client(void)
{
    DHCP_init(SOCK_DHCP, DHCP_buffer);
    while (1)
    {
        switch (DHCP_run())
        {
            case DHCP_IP_ASSIGN:
            case DHCP_IP_CHANGED:
                // Get the assigned IP address
                getIPfromDHCP(DHCP_IP);
                break;
            case DHCP_IP_LEASED:
                // Successfully leased an IP address
                break;
            case DHCP_FAILED:
                // DHCP failed, retry
                DHCP_stop();
                DHCP_init(SOCK_DHCP, DHCP_buffer);
                break;
            default:
                break;
        }
    }
}
 

Testing and Debugging

After implementing the firmware, it’s crucial to test and debug the setup. Use tools like Wireshark to monitor the network traffic and ensure the DHCP process is working correctly. Verify that the STM32G0 successfully obtains an IP address and can communicate over the network.

Benefits of Using STM32G0 and W5500

Combining the STM32G0 microcontroller with the W5500 Ethernet module offers several advantages:

  • Cost-Effective: Both the STM32G0 and W5500 are cost-effective solutions, making them ideal for budget-sensitive projects.
  • Simplicity: The hardwired TCP/IP stack of the W5500 simplifies network communication, reducing the software complexity on the microcontroller.
  • Flexibility: The STM32G0’s rich peripheral set and the W5500’s robust Ethernet capabilities provide a flexible platform for various applications.
  • Performance: The high-speed SPI interface and efficient TCP/IP stack ensure reliable and high-performance network communication.
An infographic illustrating the benefits of using STM32G0 microcontroller with W5500 Ethernet module. The infographic should have four sections: Cost-Effective, Simplicity, Flexibility, and Performance. Each section should have a corresponding icon and brief description. The Cost-Effective section might show a dollar sign or a piggy bank, Simplicity could be represented with a plug-and-play icon, Flexibility with a multi-functional device icon, and Performance with a speedometer or rocket. The overall design should be clean and modern, with a professional look suitable for a tech audience.

Conclusion

The STM32G0-W5500 implementation provides a powerful and efficient way to connect embedded systems to a network using the WIZnet W5500 Ethernet module. By leveraging the strengths of both the STM32G0 microcontroller and the W5500, developers can create robust network-enabled applications with ease. The detailed guide and example code provided in the GitHub repository serve as an excellent starting point for integrating Ethernet connectivity into your STM32G0-based projects.

Documents
  • main.c

Comments Write