first and working ethernet

This commit is contained in:
2025-07-26 21:48:13 -04:00
commit 473f9013c0
58 changed files with 3405 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.12)
include(pico_sdk_import.cmake)
project(Pico_ePaper_Code)
pico_sdk_init()
#添加编译子目录
add_subdirectory(lib/CH9120)
add_subdirectory(examples)
#添加头文件目录
include_directories(examples)
include_directories(./lib/CH9120)
# 生成可执行文件
add_executable(main
main.c
)
# enable usb output, disable uart output
pico_enable_stdio_usb(main 1)
pico_enable_stdio_uart(main 1)
# create map/bin/hex/uf2 file etc.
pico_add_extra_outputs(main)
target_link_libraries(main examples CH9120 pico_stdlib hardware_spi)

View File

@@ -0,0 +1,45 @@
/*****************************************************************************
* | File : CH9120_Test.h
* | Author : Waveshare team
* | Function : RP2040 ETH test Demo
* | Info :
*----------------
* | This version: V1.0
* | Date : 2023-04-01
* | Info :
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#ifndef _CH9120_TEST_H_
#define _CH9120_TEST_H_
#include "CH9120.h"
extern UCHAR CH9120_LOCAL_IP[4];
extern UCHAR CH9120_GATEWAY[4];
extern UCHAR CH9120_SUBNET_MASK[4];
extern UCHAR CH9120_TARGET_IP[4];
extern UWORD CH9120_PORT1;
extern UWORD CH9120_TARGET_PORT;
extern UDOUBLE CH9120_BAUD_RATE;
int Pico_ETH_CH9120_test(void);
#endif

View File

@@ -0,0 +1,9 @@
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_examples_SRCS 变量
aux_source_directory(. DIR_examples_SRCS)
include_directories(../lib/CH9120)
# 生成链接库
add_library(examples ${DIR_examples_SRCS})
target_link_libraries(examples PUBLIC CH9120)

View File

@@ -0,0 +1,36 @@
/*****************************************************************************
* | File : RP2040_ETH_CH9120_test.c
* | Author : Waveshare team
* | Function : Pico_ETH_CH9120 test demo
* | Info :
*----------------
* | This version: V1.0
* | Date : 2023-04-01
* | Info :
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#include "CH9120_Test.h"
int Pico_ETH_CH9120_test(void)
{
CH9120_init(); //Initialize Configuration CH9120
RX_TX(); //receive and dispatch
}

View File

@@ -0,0 +1,313 @@
#include "CH9120.h"
UCHAR CH9120_Mode = TCP_CLIENT; //Optional:TCP_SERVER、TCP_CLIENT、UDP_SERVER、UDP_CLIENT
UCHAR CH9120_LOCAL_IP[4] = {192, 168, 1, 200}; // LOCAL IP
UCHAR CH9120_GATEWAY[4] = {192, 168, 1, 1}; // GATEWAY
UCHAR CH9120_SUBNET_MASK[4] = {255, 255, 255, 0}; // SUBNET MASK
UCHAR CH9120_TARGET_IP[4] = {192, 168, 1, 10}; // TARGET_IP
UWORD CH9120_PORT1 = 1000; // LOCAL PORT1
UWORD CH9120_TARGET_PORT = 2000; // TARGET PORT
UDOUBLE CH9120_BAUD_RATE = 115200; // BAUD RATE
UCHAR tx[8] = {0x57, 0xAB};
/******************************************************************************
function: Send four bytes
parameter:
data: parameter
command: command code
Info: Set mode, enable port, clear serial port, switch DHCP, switch port 2
******************************************************************************/
void CH9120_TX_4_bytes(UCHAR data, int command)
{
for (int i = 2; i < 4; i++)
{
if (i == 2)
tx[i] = command;
else
tx[i] = data;
}
DEV_Delay_ms(10);
for (int o = 0; o < 4; o++)
uart_putc(UART_ID1, tx[o]);
DEV_Delay_ms(10);
for (int i = 2; i < 4; i++)
tx[i] = 0;
}
/******************************************************************************
function: Send five bytes
parameter:
data: parameter
command: command code
Info: Set the local port and target port
******************************************************************************/
void CH9120_TX_5_bytes(UWORD data, int command)
{
UCHAR Port[2];
Port[0] = data & 0xff;
Port[1] = data >> 8;
for (int i = 2; i < 5; i++)
{
if (i == 2)
tx[i] = command;
else
tx[i] = Port[i - 3];
}
DEV_Delay_ms(10);
for (int o = 0; o < 5; o++)
uart_putc(UART_ID1, tx[o]);
DEV_Delay_ms(10);
for (int i = 2; i < 5; i++)
tx[i] = 0;
}
/******************************************************************************
function: Send seven bytes
parameter:
data: parameter
command: command code
Info: Set the IP address, subnet mask, gateway,
******************************************************************************/
void CH9120_TX_7_bytes(UCHAR data[], int command)
{
for (int i = 2; i < 7; i++)
{
if (i == 2)
tx[i] = command;
else
tx[i] = data[i - 3];
}
DEV_Delay_ms(10);
for (int o = 0; o < 7; o++)
uart_putc(UART_ID1, tx[o]);
DEV_Delay_ms(10);
for (int i = 2; i < 7; i++)
tx[i] = 0;
}
/******************************************************************************
function: CH9120_TX_BAUD
parameter:
data: parameter
command: command code
Info: Set baud rate
******************************************************************************/
void CH9120_TX_BAUD(UDOUBLE data, int command)
{
UCHAR Port[4];
Port[0] = (data & 0xff);
Port[1] = (data >> 8) & 0xff;
Port[2] = (data >> 16) & 0xff;
Port[3] = data >> 24;
for (int i = 2; i < 7; i++)
{
if (i == 2)
tx[i] = command;
else
tx[i] = Port[i - 3];
}
DEV_Delay_ms(10);
for (int o = 0; o < 7; o++)
uart_putc(UART_ID1, tx[o]);
DEV_Delay_ms(10);
for (int i = 2; i < 7; i++)
tx[i] = 0;
}
/******************************************************************************
function: CH9120_Start
parameter:
Info: Start configuration Parameters
******************************************************************************/
void CH9120_Start()
{
gpio_put(RES_PIN, 1);
gpio_put(CFG_PIN, 0);
DEV_Delay_ms(500);
}
/******************************************************************************
function: CH9120_End
parameter:
Info: Updating configuration Parameters
******************************************************************************/
void CH9120_End()
{
tx[2] = 0x0d;
uart_puts(UART_ID1, tx);
DEV_Delay_ms(200);
tx[2] = 0x0e;
uart_puts(UART_ID1, tx);
DEV_Delay_ms(200);
tx[2] = 0x5e;
uart_puts(UART_ID1, tx);
DEV_Delay_ms(200);
gpio_put(CFG_PIN, 1);
}
/******************************************************************************
Function: CH9120_SetMode
Parameters:
Mode: Mode parameter
Info: Configure communication mode
******************************************************************************/
void CH9120_SetMode(UCHAR Mode)
{
CH9120_TX_4_bytes(Mode, Mode1); //Mode
DEV_Delay_ms(100);
}
/******************************************************************************
Function: CH9120_SetLocalIP
Parameters:
CH9120_LOCAL_IP: Local IP parameter
Info: Configure local IP
******************************************************************************/
void CH9120_SetLocalIP(UCHAR CH9120_LOCAL_IP[])
{
CH9120_TX_7_bytes(CH9120_LOCAL_IP, LOCAL_IP); //LOCALIP
DEV_Delay_ms(100);
}
/******************************************************************************
Function: CH9120_SetSubnetMask
Parameters:
CH9120_SUBNET_MASK: SUBNET MASK parameter
Info: Configure subnet mask
******************************************************************************/
void CH9120_SetSubnetMask(UCHAR CH9120_SUBNET_MASK[])
{
CH9120_TX_7_bytes(CH9120_SUBNET_MASK, SUBNET_MASK); //SUBNET MASK
DEV_Delay_ms(100);
}
/******************************************************************************
Function: CH9120_SetGateway
Parameters:
CH9120_GATEWAY: Gateway parameter
Info: Configure gateway
******************************************************************************/
void CH9120_SetGateway(UCHAR CH9120_GATEWAY[])
{
CH9120_TX_7_bytes(CH9120_GATEWAY, GATEWAY); //GATEWAY
DEV_Delay_ms(100);
}
/******************************************************************************
Function: CH9120_SetTargetIP
Parameters:
CH9120_TARGET_IP: Target IP parameter
Info: Configure target IP
******************************************************************************/
void CH9120_SetTargetIP(UCHAR CH9120_TARGET_IP[])
{
CH9120_TX_7_bytes(CH9120_TARGET_IP, TARGET_IP1); //TARGET IP
DEV_Delay_ms(100);
}
/******************************************************************************
Function: CH9120_SetLocalPort
Parameters:
CH9120_PORT: Local Port parameter
Info: Configure local port number
******************************************************************************/
void CH9120_SetLocalPort(UWORD CH9120_PORT)
{
CH9120_TX_5_bytes(CH9120_PORT, LOCAL_PORT1); //Local port
DEV_Delay_ms(100);
}
/******************************************************************************
Function: CH9120_SetTargetPort
Parameters:
CH9120_TARGET_PORT: Target Port parameter
Info: Configure target port number
******************************************************************************/
void CH9120_SetTargetPort(UWORD CH9120_TARGET_PORT)
{
CH9120_TX_5_bytes(CH9120_TARGET_PORT, TARGET_PORT1); //Target port
DEV_Delay_ms(100);
}
/******************************************************************************
Function: CH9120_SetBaudRate
Parameters:
CH9120_BAUD_RATE: Baud Rate parameter
Info: Configure communication baud rate
******************************************************************************/
void CH9120_SetBaudRate(UDOUBLE CH9120_BAUD_RATE)
{
CH9120_TX_BAUD(CH9120_BAUD_RATE, UART1_BAUD1); //Port 1 baud rate
DEV_Delay_ms(100);
}
/**
* delay x ms
**/
void DEV_Delay_ms(UDOUBLE xms)
{
sleep_ms(xms);
}
void DEV_Delay_us(UDOUBLE xus)
{
sleep_us(xus);
}
/******************************************************************************
function: CH9120_init
parameter:
Info: Initialize CH9120
******************************************************************************/
void CH9120_init(void)
{
stdio_init_all();
uart_init(UART_ID1, Inti_BAUD_RATE);
gpio_set_function(UART_TX_PIN1, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN1, GPIO_FUNC_UART);
gpio_init(CFG_PIN);
gpio_init(RES_PIN);
gpio_set_dir(CFG_PIN, GPIO_OUT);
gpio_set_dir(RES_PIN, GPIO_OUT);
CH9120_Start();
CH9120_SetMode(CH9120_Mode); //Mode
CH9120_SetLocalIP(CH9120_LOCAL_IP); //LOCALIP
CH9120_SetSubnetMask(CH9120_SUBNET_MASK); //SUBNET MASK
CH9120_SetGateway(CH9120_GATEWAY); //GATEWAY
CH9120_SetTargetIP(CH9120_TARGET_IP); //TARGET IP
CH9120_SetLocalPort(CH9120_PORT1); //Local port
CH9120_SetTargetPort(CH9120_TARGET_PORT); //Target port
CH9120_SetBaudRate(CH9120_BAUD_RATE); //Port 1 baud rate
CH9120_End();
uart_set_baudrate(UART_ID1, Transport_BAUD_RATE);
while (uart_is_readable(UART_ID1))
{
UBYTE ch1 = uart_getc(UART_ID1);
}
}
/******************************************************************************
function: RX_TX
parameter:
Info: Serial port 1 and serial port 2 receive and dispatch
******************************************************************************/
void RX_TX()
{
while (1)
{
while (uart_is_readable(UART_ID1))
{
UBYTE ch1 = uart_getc(UART_ID1);
if (uart_is_writable(UART_ID1))
{
uart_putc(UART_ID1, ch1);
}
}
}
}

View File

@@ -0,0 +1,54 @@
#ifndef _CH9120_H_
#define _CH9120_H_
#include <stdlib.h> // malloc() free()
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
/// \tag::uart_advanced[]
#define UART_ID1 uart1
#define Inti_BAUD_RATE 9600
#define Transport_BAUD_RATE 115200
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
// We are using pins 0 and 1, but see the GPIO function select table in the
// datasheet for information on which other pins can be used.
#define UART_TX_PIN1 20
#define UART_RX_PIN1 21
#define CFG_PIN 18
#define RES_PIN 19
#define GPIO_OUT 1
#define GPIO_IN 0
#define UCHAR unsigned char
#define UBYTE uint8_t
#define UWORD uint16_t
#define UDOUBLE uint32_t
#define TCP_SERVER 0
#define TCP_CLIENT 1
#define UDP_SERVER 2
#define UDP_CLIENT 3
#define Mode1 0x10 //Port 1: Setup Mode 0x00:TCP Server 0x01:TCP Client 0x02:UDP Server 0x03:UDP Client
#define LOCAL_IP 0x11 //Local IP
#define SUBNET_MASK 0x12 //Subnet Mask
#define GATEWAY 0x13 //Gateway
#define LOCAL_PORT1 0X14 //Port 1:Local Port
#define TARGET_IP1 0x15 //Port 1:Target IP
#define TARGET_PORT1 0x16 //Port 1:Target Port
#define PORT_RANDOM_ENABLE1 0x17 //Port 1:Port Random Enable
#define UART1_BAUD1 0x21 //Port 1:Baud rate of serial port 1
void CH9120_init(void);
void RX_TX();
void DEV_Delay_ms(UDOUBLE xms);
void DEV_Delay_us(UDOUBLE xus);
#endif

View File

@@ -0,0 +1,9 @@
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_CH9120_SRCS 变量
aux_source_directory(. DIR_CH9120_SRCS)
include_directories(../Config)
# 生成链接库
add_library(CH9120 ${DIR_CH9120_SRCS})
target_link_libraries( CH9120 PUBLIC pico_stdlib hardware_spi)

View File

@@ -0,0 +1,6 @@
#include "CH9120_Test.h"
int main()
{
Pico_ETH_CH9120_test();
}

View File

@@ -0,0 +1,62 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the PICO SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading PICO SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"PICO SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git. \r\nexport PICO_SDK_PATH=../../pico-sdk"
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the PICO SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the PICO SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})