esp_eth: new test_app: emac layer tests

This commit is contained in:
SalimTerryLi 2021-07-14 15:37:47 +08:00
parent 4c7b31f6ff
commit 14a6ab4a8e
No known key found for this signature in database
GPG Key ID: F05CCEF2191AF770
10 changed files with 430 additions and 0 deletions

View File

@ -702,6 +702,18 @@ UT_S3_FLASH:
- ESP32S3_IDF
- UT_T1_ESP_FLASH
component_ut_test_ip101:
extends: .component_ut_esp32_template
tags:
- ESP32
- COMPONENT_UT_IP101
component_ut_test_lan8720:
extends: .component_ut_esp32_template
tags:
- ESP32
- COMPONENT_UT_LAN8720
.integration_test_template:
extends:
- .target_test_job_template

View File

@ -0,0 +1,2 @@
# JUnit report
*XUNIT_RESULT.xml

View File

@ -0,0 +1,8 @@
# This is the project CMakeLists.txt file for the test subproject
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp_eth_test)
idf_component_get_property(lib esp_eth COMPONENT_LIB)
target_compile_options(${lib} PRIVATE "-fsanitize=undefined" "-fno-sanitize=shift-base")

View File

@ -0,0 +1,7 @@
| Supported Targets | ESP32 |
| ----------------- | ----- |
This test app is used to test MAC layer behavior with different PHY chips:
- ip101
- lan8720

View File

@ -0,0 +1,102 @@
import os
import re
import socket
import tiny_test_fw
import ttfw_idf
from ttfw_idf import TestFormat
try:
import typing # noqa: F401 # pylint: disable=unused-import
except ImportError:
pass
def configure_eth_if(func): # type: (typing.Any) -> typing.Any
def inner(*args, **kwargs): # type: (typing.Any, typing.Any) -> typing.Any
# try to determine which interface to use
netifs = os.listdir('/sys/class/net/')
target_if = ''
print('detected interfaces: ' + str(netifs))
for netif in netifs:
if netif.find('eth') == 0 or netif.find('enp') == 0 or netif.find('eno') == 0:
target_if = netif
break
if target_if == '':
raise Exception('no network interface found')
print('Use ' + target_if + ' for testing')
so = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0x2222)
so.bind((target_if, 0))
func(so, *args, **kwargs)
so.close()
return inner
@configure_eth_if
def check_eth_recv_packet(so): # type: (socket.socket) -> None
so.settimeout(10)
try:
pkt = so.recv(1024)
for i in range(128, 1024):
if pkt[i] != i & 0xff:
raise Exception('Packet content mismatch')
except Exception as e:
raise e
@configure_eth_if
def send_eth_packet(so, mac): # type: (socket.socket, bytes) -> None
so.settimeout(10)
pkt = bytearray()
pkt += mac # dest
pkt += so.getsockname()[4] # src
pkt += bytes.fromhex('2222') # proto
pkt += bytes(1010) # padding to 1024
for i in range(128, 1024):
pkt[i] = i & 0xff
try:
so.send(pkt)
except Exception as e:
raise e
def test_component_ut_esp_eth(env, appname): # type: (tiny_test_fw.Env, str) -> None
dut = env.get_dut('esp_eth', 'components/esp_eth/test_apps', app_config_name=appname)
dut.start_app()
stdout = dut.expect('Press ENTER to see the list of tests', full_stdout=True)
dut.write('"start_and_stop"')
stdout += dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
dut.write('"get_set_mac"')
stdout = dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
dut.write('"ethernet_broadcast_transmit"')
check_eth_recv_packet()
stdout = dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
dut.write('"recv_pkt"')
expect_result = dut.expect(re.compile(r'([\s\S]*)DUT MAC: ([0-9a-zA-Z:]*)'), timeout=10)
stdout = expect_result[0]
send_eth_packet(bytes.fromhex('ffffffffffff')) # broadcast frame
send_eth_packet(bytes.fromhex('010000000000')) # multicast frame
send_eth_packet(bytes.fromhex(expect_result[1].replace(':', ''))) # unicast frame
stdout += dut.expect("Enter next test, or 'enter' to see menu", full_stdout=True)
ttfw_idf.ComponentUTResult.parse_result(stdout, test_format=TestFormat.UNITY_BASIC)
@ttfw_idf.idf_component_unit_test(env_tag='COMPONENT_UT_IP101', target=['esp32'])
def test_component_ut_esp_eth_ip101(env, _): # type: (tiny_test_fw.Env, typing.Any) -> None
test_component_ut_esp_eth(env, 'ip101')
@ttfw_idf.idf_component_unit_test(env_tag='COMPONENT_UT_LAN8720', target=['esp32'])
def test_component_ut_esp_eth_lan8720(env, _): # type: (tiny_test_fw.Env, typing.Any) -> None
test_component_ut_esp_eth(env, 'lan8720')
if __name__ == '__main__':
test_component_ut_esp_eth_ip101()
test_component_ut_esp_eth_lan8720()

View File

@ -0,0 +1,4 @@
idf_component_register(SRCS "esp_eth_test.c"
INCLUDE_DIRS "."
PRIV_INCLUDE_DIRS "."
PRIV_REQUIRES unity esp_eth)

View File

@ -0,0 +1,14 @@
menu "esp_eth TEST_APPS Configuration"
choice TARGET_ETH_PHY_DEVICE
prompt "Ethernet peripheral device"
default TARGET_ETH_PHY_DEVICE_IP101
help
Select one of the devices listed here
config TARGET_ETH_PHY_DEVICE_IP101
bool "IP101"
config TARGET_ETH_PHY_DEVICE_LAN8720
bool "LAN8720"
endchoice
endmenu

View File

@ -0,0 +1,265 @@
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_event.h"
#include "unity.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "sdkconfig.h"
#include "lwip/sockets.h"
#define ETH_START_BIT BIT(0)
#define ETH_STOP_BIT BIT(1)
#define ETH_CONNECT_BIT BIT(2)
#define ETH_BROADCAST_RECV_BIT BIT(0)
#define ETH_MULTICAST_RECV_BIT BIT(1)
#define ETH_UNICAST_RECV_BIT BIT(2)
typedef struct {
uint8_t dest[6];
uint8_t src[6];
uint16_t proto;
uint8_t data[];
} __attribute__((__packed__)) emac_frame_t;
TEST_CASE("start_and_stop", "[esp_eth]")
{
void eth_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data){
EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT);
break;
case ETHERNET_EVENT_START:
xEventGroupSetBits(eth_event_group, ETH_START_BIT);
break;
case ETHERNET_EVENT_STOP:
xEventGroupSetBits(eth_event_group, ETH_STOP_BIT);
break;
default:
break;
}
}
EventGroupHandle_t eth_event_group = xEventGroupCreate();
TEST_ASSERT(eth_event_group != NULL);
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); // create MAC instance
TEST_ASSERT_NOT_NULL(mac);
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
#if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
#elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720)
esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
#endif
TEST_ASSERT_NOT_NULL(phy);
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
TEST_ASSERT_NOT_NULL(eth_handle);
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
EventBits_t bits = 0;
bits = xEventGroupWaitBits(eth_event_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(3000));
TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT);
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000));
TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
phy->del(phy);
mac->del(mac);
vEventGroupDelete(eth_event_group);
}
TEST_CASE("get_set_mac", "[esp_eth]")
{
void eth_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data){
SemaphoreHandle_t mutex = (SemaphoreHandle_t)arg;
switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
xSemaphoreGive(mutex);
break;
default:
break;
}
}
SemaphoreHandle_t mutex = xSemaphoreCreateBinary();
TEST_ASSERT_NOT_NULL(mutex);
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); // create MAC instance
TEST_ASSERT_NOT_NULL(mac);
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
#if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
#elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720)
esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
#endif
TEST_ASSERT_NOT_NULL(phy);
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
TEST_ASSERT_NOT_NULL(eth_handle);
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, mutex));
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
TEST_ASSERT(xSemaphoreTake(mutex, pdMS_TO_TICKS(3000)));
uint8_t mac_addr[6] = {};
TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, mac_addr));
TEST_ASSERT_BITS(0b00000011, 0b00, mac_addr[0]); // Check UL&IG, should be UI
mac_addr[5] ^= mac_addr[4];
TEST_ASSERT_EQUAL(ESP_OK, mac->set_addr(mac, mac_addr));
uint8_t new_mac_addr[6] = {};
TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, new_mac_addr));
TEST_ASSERT_EQUAL(0, memcmp(mac_addr, new_mac_addr, 6));
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
phy->del(phy);
mac->del(mac);
vSemaphoreDelete(mutex);
}
TEST_CASE("ethernet_broadcast_transmit", "[esp_eth]")
{
void eth_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data){
SemaphoreHandle_t mutex = (SemaphoreHandle_t)arg;
switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
xSemaphoreGive(mutex);
break;
default:
break;
}
}
SemaphoreHandle_t mutex = xSemaphoreCreateBinary();
TEST_ASSERT_NOT_NULL(mutex);
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); // create MAC instance
TEST_ASSERT_NOT_NULL(mac);
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
#if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
#elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720)
esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
#endif
TEST_ASSERT_NOT_NULL(phy);
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
TEST_ASSERT_NOT_NULL(eth_handle);
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, mutex));
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
TEST_ASSERT(xSemaphoreTake(mutex, pdMS_TO_TICKS(3000)));
emac_frame_t *pkt = malloc(1024);
pkt->proto = 0x2222;
memset(pkt->dest, 0xff, 6); // broadcast addr
for (int i = 128; i < 1024; ++i){
((uint8_t*)pkt)[i] = i & 0xff;
}
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_transmit(eth_handle, pkt, 1024));
vTaskDelay(pdMS_TO_TICKS(100));
free(pkt);
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
phy->del(phy);
mac->del(mac);
vSemaphoreDelete(mutex);
}
static uint8_t local_mac_addr[6] = {};
esp_err_t l2_packet_txrx_test_cb(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv) {
EventGroupHandle_t eth_event_group = (EventGroupHandle_t)priv;
emac_frame_t *pkt = (emac_frame_t *) buffer;
// check header
if (pkt->proto == 0x2222 && length == 1024) {
// check content
for (int i = 128; i < 1024; ++i) {
if (buffer[i] != (i & 0xff)) {
return ESP_OK;
}
}
if (memcmp(pkt->dest, "\xff\xff\xff\xff\xff\xff", 6) == 0) {
xEventGroupSetBits(eth_event_group, ETH_BROADCAST_RECV_BIT);
}
if (pkt->dest[0] & 0x1) {
xEventGroupSetBits(eth_event_group, ETH_MULTICAST_RECV_BIT);
}
if (memcmp(pkt->dest, local_mac_addr, 6) == 0) {
xEventGroupSetBits(eth_event_group, ETH_UNICAST_RECV_BIT);
}
}
return ESP_OK;
};
TEST_CASE("recv_pkt", "[esp_eth]")
{
EventGroupHandle_t eth_event_group = xEventGroupCreate();
TEST_ASSERT(eth_event_group != NULL);
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); // create MAC instance
TEST_ASSERT_NOT_NULL(mac);
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
#if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
#elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720)
esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
#endif
TEST_ASSERT_NOT_NULL(phy);
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
TEST_ASSERT_NOT_NULL(eth_handle);
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, local_mac_addr));
// test app will parse the DUT MAC from this line of log output
printf("DUT MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", local_mac_addr[0], local_mac_addr[1], local_mac_addr[2],
local_mac_addr[3], local_mac_addr[4], local_mac_addr[5]);
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, eth_event_group));
EventBits_t bits = 0;
bits = xEventGroupWaitBits(eth_event_group, ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT,
true, true, pdMS_TO_TICKS(3000));
TEST_ASSERT((bits & (ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT)) ==
(ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT));
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
phy->del(phy);
mac->del(mac);
vEventGroupDelete(eth_event_group);
}
void app_main(void)
{
unity_run_menu();
}

View File

@ -0,0 +1,7 @@
CONFIG_IDF_TARGET="esp32"
CONFIG_UNITY_ENABLE_FIXTURE=y
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y
CONFIG_ETH_USE_ESP32_EMAC=y
CONFIG_ESP_TASK_WDT=n
CONFIG_TARGET_ETH_PHY_DEVICE_IP101=y

View File

@ -0,0 +1,9 @@
CONFIG_IDF_TARGET="esp32"
CONFIG_UNITY_ENABLE_FIXTURE=y
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y
CONFIG_ETH_USE_ESP32_EMAC=y
CONFIG_ESP_TASK_WDT=n
CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720=y
CONFIG_ETH_RMII_CLK_OUTPUT=y
CONFIG_ETH_RMII_CLK_OUT_GPIO=17