ESP-IDF 提供一系列灵活度高且兼具一致性的 API,为内部以太网 MAC (EMAC) 控制器和外部 SPI-Ethernet 模块提供支持。
因为ESP32-S3芯片内部不具有EMAC,本章采用外置的W5500嵌入式以太网控制器,SPI通信接口驱动。
函数调用关系:
仅需配置几个关键宏
idf example/ethernet/basic/ethernet_example_main.c例程移植
1. 初始化以太网入口
/**
* @brief 网口状态管理机制
* @date 2023-02-22
*/
typedef struct _anker_eth_register_status_callback
{
void (*eth_get_ip_address)(void); ///< 获取到IP地址
void (*eth_connect_success)(void); ///< 已插上网口
void (*eth_connect_loss)(void); ///< 拔掉网口
void (*eth_notify_net_ok)(void); ///< 连接外网成功
} anker_eth_register_status_callback_t;
sdk_error_code_t aws_register_eth_status_change_callback(anker_eth_register_status_callback_t *callback);
/*初始化以太网入口*/
void aws_eth_status_init(void)
{
anker_eth_register_status_callback_t cb =
{
.eth_connect_success = eth_connect_success, //以太网连接成功通知
.eth_get_ip_address = eth_get_ip_addr_info, //获取ip地址信息
.eth_connect_loss = eth_connect_lost, //以太网断联成功通知
.eth_notify_net_ok = aws_eth_network_ok, //
};
aws_register_eth_status_change_callback(&cb);
}
2. 注册以太网状态接口
void eth_connect_lost(void)
{
extern void aws_wifi_connect_ap_not_ssid(void);
extern bool aws_get_is_ap_connect(void);
if (aws_get_is_ap_connect() == false)
{
aws_wifi_connect_ap_not_ssid();
}
//判断eth_disconnect是否为NULL
if (_notify.eth_disconnect != NULL)
{
_notify.eth_disconnect(E_ETH_DISCONNECT_REASON_NO_CABLE);
}
}
void eth_connect_success(void)
{
}
void eth_get_ip_addr_info(void)
{
#if ANKER_WIFI_SDK_NET_COM_ENABLE
aws_p2p_need_tcp_connect();
#endif
#if ANKER_WIFI_SDK_MQTT_ENABLE
extern void aws_iot_mqtt_connect_status_get(unsigned int *connect_status);
unsigned int state = 0;
aws_iot_mqtt_connect_status_get(&state);
if (aws_get_is_use_mqtt() && (state == false))
{
// aws_notify_mqtt_client_net_disconnect();
aws_mqtt_need_resubscribed();
aws_notify_mqtt_client_net_connect();
}
#endif
#if ANKER_WIFI_SDK_HTTP_ENABLE
extern void aws_cloud_notice_ap_connect(void);
aws_cloud_notice_ap_connect();
#endif
#if ANKER_WIFI_SDK_WEBRTC_TRANSMIT_ENABLE
if (aws_get_is_use_webrtc() && aws_get_real_ka_type() == AWS_DEVICE_CONNECT_ROUTE)
{
aws_webrtc_notify_ap_connect();
}
#endif
//判断eth_connect是否为NULL
if (_notify.eth_connect != NULL)
{
_notify.eth_connect();
}
}
void eth_connect_lost(void)
{
extern void aws_wifi_connect_ap_not_ssid(void);
extern bool aws_get_is_ap_connect(void);
if (aws_get_is_ap_connect() == false)
{
aws_wifi_connect_ap_not_ssid();
}
//判断eth_disconnect是否为