1. MicroPython development for W5100S/W5500+RP2040<Static network example>
1. MicroPython development for W5100S/W5500+RP2040<Static network example>
Software Apps and online services
1 Introduction
In this era of smart hardware and the Internet of Things, MicroPython and Raspberry Pi PICO are leading the new trend of embedded development with their unique advantages. MicroPython, as a streamlined and optimized Python 3 language, provides efficient development and easy debugging for microcontrollers and embedded devices.
When we combine it with the WIZnet W5100S/W5500 network module, the development potential of MicroPython and Raspberry Pi PICO is further amplified. Both modules have built-in TCP/IP protocol stacks, making it easier to implement network connections on embedded devices. Whether it is data transmission, remote control, or building IoT applications, they provide powerful support.
In this chapter, we will take WIZnet W5100S as an example to set up static network address information using the MicroPython development method.
2. MicroPython Introduction
2.1 Brief introduction
Micro Python is a compact and efficient implementation of Python that runs on microcontrollers and constrained environments. It includes a small portion of the standard library of the Python 3 programming language and is optimized for microcontrollers and constrained environments. The MicroPython pyboard is a compact electronic circuit board that runs MicroPython on bare metal, giving you a low-level Python operating system you can use to control a variety of electronic projects. MicroPython is packed with advanced features such as interactive prompts, arbitrary-precision integers, closures, list comprehensions, generators, exception handling, and more. But it's compact enough to fit into just 256k of code space and 16k of RAM. MicroPython is designed to be as compatible as possible with regular Python so that you can easily transfer code from your desktop to a microcontroller or embedded system.
Micro Python is a complete Python compiler and runtime that runs on bare metal. You get an interactive prompt (REPL) to execute commands immediately, as well as the ability to run and import scripts from the built-in file system. REPL has history, tab completion, auto-indent and paste mode for a great user experience. MicroPython strives to be as compatible as possible with regular Python (called CPython) so that if you know Python, you already know MicroPython. On the other hand, the more you know MicroPython, the better you will master Python. In addition to implementing some core Python libraries, MicroPython also includes modules such as "machine" for accessing low-level hardware
2.2 Advantages
Features of Python Programming Language: Python programming language has a very short learning curve, which makes it very easy for developers to start using it. Python provides developers with a high-level programming language that can be used to build simple scripts or develop complex object-oriented architectures that use all the best practices of modern software projects. Compared to C, Python also provides built-in mechanisms for creating threads, handling errors, and easy integration into testing tools.
Library support: MicroPython provides a series of libraries that provide control of low-level microcontroller functions that abstract away the complexity. For example, a hardware engineer can design a circuit board with little knowledge of how a microcontroller (or C) works, develop high-level scripts to test the board by controlling GPIOs, and even communicate with I2C devices.
Ease of use: MicroPython has a short learning curve, which makes it very easy for developers to start using it. MicroPython provides a high-level programming language that can be used to build simple scripts or to develop complex object-oriented architectures that use all the best practices of modern software projects.
Portability: MicroPython can run on a variety of platforms, including Windows, Linux, Mac OS X, Raspberry Pi, and more.
Open Source: MicroPython is open source, which means developers are free to use, modify, and distribute it.
2.3 Application
IoT devices: MicroPython can be used for the development of IoT devices, such as sensors, smart home devices, smart city devices, etc.
Robots: MicroPython can be used for robot development, such as robot control, sensor data processing, etc.
Automation: MicroPython can be used for the development of automation systems, such as automated homes, automated factories, etc.
Education: MicroPython can be used in the education field, such as teaching programming, robotics, Internet of Things, etc.
Scientific research: MicroPython can be used for scientific research, such as data collection, data analysis, etc.
3. WIZnet Ethernet chip
WIZnet mainstream hardware protocol stack Ethernet chip parameter comparison
Model | Embedded Core | Host I/F | TX/RX Buffer | HW Socket | Network Performance |
---|---|---|---|---|---|
W5100S | TCP/IPv4, MAC & PHY | 8bit BUS, SPI | 16KB | 4 | Max 25Mbps |
W6100 | TCP/IPv4/IPv6, MAC & PHY | 8bit BUS, Fast SPI | 32KB | 8 | Max 25Mbps |
W5500 | TCP/IPv4, MAC & PHY | Fast SPI | 32KB | 8 | Max 15Mbps |
W5100S/W6100 supports 8-bit data bus interface, and the network transmission speed will be better than W5500.
W6100 supports IPV6 and is compatible with W5100S hardware. If users who already use W5100S need to support IPv6, they can be Pin to Pin compatible.
W5500 has more Sockets and send and receive buffers than W5100S
Compared with the software protocol stack, WIZnet's hardware protocol stack Ethernet chip has the following advantages:
Hardware TCP/IP protocol stack: WIZnet's hardware protocol stack chip provides a hardware-implemented TCP/IP protocol stack. This hardware-implemented protocol stack has better performance and stability than software-implemented protocol stacks.
No additional embedded system software stack and memory resources required: Since all Ethernet transmit and receive operations are handled by the independent Ethernet controller, no additional embedded system software stack and memory resources are required.
Resistant to network environment changes and DDoS attacks: Compared with software TCP/IP protocol stacks that are susceptible to network environment changes and DDoS attacks, hardware protocol stack chips can provide more stable Ethernet performance.
Suitable for low-specification embedded systems: Even in low-specification embedded systems, hardware protocol stack chips using WIZnet can show more efficient Internet application operating performance than high-specification systems using software TCP/IP protocol stacks.
4. Static IP network setting example explanation and usage
This routine includes SPI communication between RP2040 and WIZnet chip, by configuring a static IP address for the WIZnet chip and pinging through the computer.
4.1 Program flow chart
4.2 Test preparation
Software:
Thonny
Hardware:
W5100S IO module + RP2040 Raspberry Pi Pico development board or WIZnet W5100S-EVB-Pico development board
Micro USB interface data cable
cable
4.3 Connection method
Connect to PC USB port via data cable
When using W5100S/W5500 IO module to connect to RP2040
RP2040 GPIO 16 <----> W5100S/W5500 MISO
RP2040 GPIO 17 <----> W5100S/W5500 CS
RP2040 GPIO 18 <----> W5100S/W5500 SCK
RP2040 GPIO 19 <----> W5100S/W5500 MOSI
RP2040 GPIO 20 <----> W5100S/W5500 RST
Directly connect to the PC network port through a network cable (or: both the PC and the device are connected to the switch or router LAN port through a network cable)
4.4 Related code
We directly open the network_install.py file and we can see that SPI is initialized in the w5x00_init() function. And register the spi-related pins and reset pins into the library, followed by activating the network and configuring static network address information. When the configuration is not successful, the information about the network address-related registers will be printed out, which can help us better troubleshoot the problem. Finally, print out the configured network address information.
#import library
from usocket import socket
from machine import Pin,SPI
import network
import time
#LED define
led = Pin(25, Pin.OUT)
"""
W5x00 chip initialization.
param: None
returns: None
"""
def w5x00_init():
#spi init
spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
nic = network.WIZNET5K(spi,Pin(17),Pin(20)) #spi,cs,reset pin
nic.active(True)#network active
nic.ifconfig(('192.168.1.20','255.255.255.0','192.168.1.1','8.8.8.8'))#Set static network address information
while not nic.isconnected():
time.sleep(1)
print(nic.regs())#Print register information
#Print network address information
print("IP Address:",nic.ifconfig()[0])
print("Subnet Mask:",nic.ifconfig()[1])
print("Gateway:",nic.ifconfig()[2])
print("DNS:",nic.ifconfig()[3])
return nic
def main():
print("WIZnet chip network install example");
nic = w5x00_init()
while True:
led.value(1)
time.sleep(1)
led.value(0)
time.sleep(1)
print("try ping",nic.ifconfig()[0])
if __name__ == "__main__":
main()
4.5 Burning Verification
To test the Ethernet examples, the development environment must be configured to use a Raspberry Pi Pico.
Required development environment
If you must compile MicroPython, you must use a Linux or Unix environment.
Step 1: Deploy firmware to device
The corresponding firmware for W5100S and W5500 is as follows:
rp2_w5100s_20221111_v2.0.0.uf2
The firmware must be burned into the RP2040 Raspberry Pi Pico to run the Python script file we wrote.
For the burning method, please refer to the figure below:
Step 2: Run the network_install.py program
Open the network_install.py program in Thonny, select the development board, and click Run.
Step 3: Ping Test Verification
5. Precautions
Static configured IP settings should avoid IP duplication causing IP conflicts.
If you use WIZnet's W5500 to implement the examples in this chapter, you only need to burn the firmware of the W5500 and run the example program.