examples/ethernet: use esp_event library to handle events

This commit is contained in:
Ivan Grokhotkov 2018-11-20 18:24:49 +08:00
parent 25784ba87a
commit 2a48db0f48
2 changed files with 39 additions and 42 deletions

View file

@ -12,6 +12,7 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_event_loop.h" #include "esp_event_loop.h"
#include "esp_err.h"
#include "esp_event.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_eth.h" #include "esp_eth.h"
@ -92,51 +93,48 @@ static void eth_gpio_config_rmii(void)
phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO); phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
} }
/** /** Event handler for Ethernet events */
* @brief event handler for ethernet static void eth_event_handler(void* arg, esp_event_base_t event_base,
* int32_t event_id, void* event_data)
* @param ctx
* @param event
* @return esp_err_t
*/
static esp_err_t eth_event_handler(void *ctx, system_event_t *event)
{ {
tcpip_adapter_ip_info_t ip; switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
switch (event->event_id) {
case SYSTEM_EVENT_ETH_CONNECTED:
ESP_LOGI(TAG, "Ethernet Link Up"); ESP_LOGI(TAG, "Ethernet Link Up");
break; break;
case SYSTEM_EVENT_ETH_DISCONNECTED: case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet Link Down"); ESP_LOGI(TAG, "Ethernet Link Down");
break; break;
case SYSTEM_EVENT_ETH_START: case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started"); ESP_LOGI(TAG, "Ethernet Started");
break; break;
case SYSTEM_EVENT_ETH_GOT_IP: case ETHERNET_EVENT_STOP:
memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip));
ESP_LOGI(TAG, "Ethernet Got IP Addr");
ESP_LOGI(TAG, "~~~~~~~~~~~");
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip.ip));
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip.netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip.gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");
break;
case SYSTEM_EVENT_ETH_STOP:
ESP_LOGI(TAG, "Ethernet Stopped"); ESP_LOGI(TAG, "Ethernet Stopped");
break; break;
default: default:
break; break;
} }
return ESP_OK; }
/** Event handler for IP_EVENT_ETH_GOT_IP */
static void got_ip_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
const tcpip_adapter_ip_info_t* ip_info = &event->ip_info;
ESP_LOGI(TAG, "Ethernet Got IP Address");
ESP_LOGI(TAG, "~~~~~~~~~~~");
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");
} }
void app_main() void app_main()
{ {
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
config.phy_addr = CONFIG_PHY_ADDRESS; config.phy_addr = CONFIG_PHY_ADDRESS;
@ -150,5 +148,7 @@ void app_main()
#endif #endif
ESP_ERROR_CHECK(esp_eth_init(&config)); ESP_ERROR_CHECK(esp_eth_init(&config));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
ESP_ERROR_CHECK(esp_eth_enable()) ; ESP_ERROR_CHECK(esp_eth_enable()) ;
} }

View file

@ -13,7 +13,7 @@
#include "tcpip_adapter.h" #include "tcpip_adapter.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_console.h" #include "esp_console.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_eth.h" #include "esp_eth.h"
#include "argtable3/argtable3.h" #include "argtable3/argtable3.h"
#include "iperf.h" #include "iperf.h"
@ -216,31 +216,26 @@ static int eth_cmd_iperf(int argc, char **argv)
return 0; return 0;
} }
static esp_err_t eth_event_handler(void *ctx, system_event_t *event) static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
switch (event->event_id) { if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_START) {
case SYSTEM_EVENT_ETH_START:
started = true; started = true;
break; } else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_STOP) {
case SYSTEM_EVENT_ETH_GOT_IP:
memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip));
xEventGroupSetBits(eth_event_group, GOTIP_BIT);
break;
case SYSTEM_EVENT_ETH_STOP:
xEventGroupClearBits(eth_event_group, GOTIP_BIT); xEventGroupClearBits(eth_event_group, GOTIP_BIT);
started = false; started = false;
default: } else if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) {
break; ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
memcpy(&ip, &event->ip_info, sizeof(ip));
xEventGroupSetBits(eth_event_group, GOTIP_BIT);
} }
return ESP_OK;
} }
void register_ethernet() void register_ethernet()
{ {
eth_event_group = xEventGroupCreate(); eth_event_group = xEventGroupCreate();
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
config.phy_addr = CONFIG_PHY_ADDRESS; config.phy_addr = CONFIG_PHY_ADDRESS;
@ -254,6 +249,8 @@ void register_ethernet()
#endif #endif
ESP_ERROR_CHECK(esp_eth_init(&config)); ESP_ERROR_CHECK(esp_eth_init(&config));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL));
eth_control_args.control = arg_str1(NULL, NULL, "<start|stop|info>", "Start/Stop Ethernet or Get info of Ethernet"); eth_control_args.control = arg_str1(NULL, NULL, "<start|stop|info>", "Start/Stop Ethernet or Get info of Ethernet");
eth_control_args.end = arg_end(1); eth_control_args.end = arg_end(1);