Template: UART TO ETH for Pico use

From Waveshare Wiki
Jump to: navigation, search

Working with Pico

Hardware connection

You can connect according to the following table.

Correspondence of Pico connection pins
ETH Pico Function
5V VSYS Power input
GND GND Power ground
RXD1 GP0 Serial data input
TXD1 GP1 Serial data output
RXD2 GP4 Serial data input
TXD2 GP5 Serial data output
CFG0 GP14 Network configuration enable pin
RST1 GP17 Reset

Direct connection

2-CH UART TO ETH-Pico.jpg

Program download

Open the Raspberry Pi terminal and execute:

sudo apt-get install p7zip-full
cd ~
sudo wget  https://files.waveshare.com/upload/3/37/2-CH_UART_TO_ETH_CODE.7z
7z x Pico_ETH_CH9121_CODE.7z -o./2-CH UART TO ETH_CODE
cd ~/2-CH UART TO ETH_CODE
cd Pico/c/build/

Demo

C

  • The following tutorial is for the operation on the Raspberry Pi, but due to the multi-platform and portable characteristics of cmake, it can be successfully compiled on the PC, but the operation is slightly different and needs to be judged by the user.

To compile, make sure to be in the c directory:

cd ~/2-CH UART TO ETH_CODE/Pico/C/

2-CH UART TO ETH_CODE/Pico/C/Serial Port Parameter Configuration: Used to configure the mode through the serial port.
2-CH UART TO ETH_CODE/Pico/C/RX_TX: Used to send and receive information, and return what is received.
Enter one of the folders to create and enter the build directory, and add the SDK: Where ../../pico-sdk is the directory of your SDK. There is a build in our sample program, just enter it directly.

cd build
export PICO_SDK_PATH=../../pico-sdk
(Note: Be sure to write the path where your own SDK is located)

Execute cmake to automatically generate Makefile.

cmake ..

Executing make to generate executable files, the first compilation time is relatively long.

make -j9

After the compilation is complete, the uf2 file will be generated. Press and hold the button on the Pico board, connect Pico to the USB port of the Raspberry Pi via the Micro USB cable, and then release the button. After connecting, the Raspberry Pi will automatically recognize a removable disk (RPI-RP2). Copy the main.uf2 file in the build folder to the recognized removable disk (RPI-RP2).

cp main.uf2 /media/pi/RPI-RP2/

Python

Use in PC Environment

  • Press and hold the BOOTSET button on the Pico board, connect Pico to the USB port of the computer through the Micro USB cable, and release the button after the computer recognizes a removable hard disk (RPI-RP2).
  • Copy the rp2-pico-20210418-v1.15.uf2 file in the python directory to the recognized removable disk (RPI-RP2).
  • Open Thonny IDE (note: use the latest version of Thonny, otherwise there is no support package for Pico, the latest version under Windows is v3.3.3).
  • Click Tools -> Settings -> Interpreter, and select Pico and the corresponding port as shown in the figure.

Pico-lcd-0.96-img-config.png
This example provides two demos:
Serial Port Parameter Configuration.py: This demo is used to configure the mode through the serial port.
RX_TX.py: This is used to send and receive information, and return what is received.

  • File -> Open -> RX_TX.py, click to run, as shown in the figure below:

Pico-lcd-0.96-img-run.png

Use in the Raspberry Pi environment

  • The process of flashing the firmware is the same as on Windows. You can choose to copy the rp2-pico-20210418-v1.15.uf2 file into pico on your PC or Raspberry Pi.
  • Open Thonny IDE on the Raspberry Pi Mountain (click the Raspberry logo -> Programming -> Thonny Python IDE), you can check the version information in Help->About Thonny.

To ensure that your version has the Pico support package, you can also click Tools -> Options... -> Interpreter to select MicroPython (Raspberry Pi Pico and ttyACM0 port.
As the picture shows:
Pico-lcd-0.96-img-config2.png
If your current Thonny version does not have a pico support package, enter the following command to update Thonny IDE.

sudo apt upgrade thonny

Click File -> Open...-> python/RX_TX.py and run the script.

Code analysis

C

Configure the parameters through the serial port (modify according to your needs):

  • Data type:
#define UCHAR unsigned char
#define UBYTE uint8_t
#define UWORD uint16_t
#define UDOUBLE uint32_t
  • Module initialization:
void CH9121_init(void);
  • This program only has a simple configuration. If you need to configure other functions, you can refer to the serial port control command and configure it yourself:
UCHAR CH9121_Mode            //Mode selection
UCHAR CH9121_LOCAL_IP[4]     //Local IP
UCHAR CH9121_GATEWAY[4]      //Gateway
UCHAR CH9121_SUBNET_MASK[4]  //Subnet mask
UCHAR CH9121_TARGET_IP[4]    //Target IP
UWORD CH9121_PORT1           //Local port
UWORD CH9121_TARGET_PORT     //Target port
UDOUBLE CH9121_BAUD_RATE     //Serial port baud rate
  • According to the serial port control command, the following functions can be used to configure the parameters:
void CH9121_TX_4_bytes(UCHAR data, int command);  //Used for mode, whether the port is random, whether the port is disconnected from the network, whether to clear the serial port data, whether to open DHCP, //whether to open the serial port 2

void CH9121_TX_5_bytes(UWORD data, int command);  //Used to set the port number of the serial port
void CH9121_TX_7_bytes(UCHAR data[], int command);//Used to set IP, subnet mask, gateway
void CH9121_TX_BAUD(UDOUBLE data, int command);   //Used to set the baud rate of the serial port
void CH9121_Eed();  //Update configuration parameters to EEPROM, execute configuration, reset 9121, leave configuration mode

Python

Users only need to modify the values shown below in Serial Port Parameter Configuration.py to configure the serial port parameters of the module:

MODE = 1  #0:TCP Server 1:TCP Client 2:UDP Server 3:UDP Client
GATEWAY = (169, 254, 88, 1)    # GATEWAY
TARGET_IP = (169, 254, 88, 17) # TARGET_IP
LOCAL_IP = (169,254,88,70)     # LOCAL_IP
SUBNET_MASK = (255,255,255,0)  # SUBNET_MASK
LOCAL_PORT1 = 5000             # LOCAL_PORT1
LOCAL_PORT2 = 4000             # LOCAL_PORT2
TARGET_PORT = 3000             # TARGET_PORT
BAUD_RATE = 115200             # BAUD_RATE