ethernet_example_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Ethernet Basic Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_netif.h"
  12. #include "esp_eth.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "driver/gpio.h"
  16. #include "sdkconfig.h"
  17. #if CONFIG_ETH_USE_SPI_ETHERNET
  18. #include "driver/spi_master.h"
  19. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  20. static const char *TAG = "eth_example";
  21. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  22. #define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num) \
  23. do { \
  24. eth_module_config[num].spi_cs_gpio = CONFIG_EXAMPLE_ETH_SPI_CS ##num## _GPIO; \
  25. eth_module_config[num].int_gpio = CONFIG_EXAMPLE_ETH_SPI_INT ##num## _GPIO; \
  26. eth_module_config[num].phy_reset_gpio = CONFIG_EXAMPLE_ETH_SPI_PHY_RST ##num## _GPIO; \
  27. eth_module_config[num].phy_addr = CONFIG_EXAMPLE_ETH_SPI_PHY_ADDR ##num; \
  28. } while(0)
  29. typedef struct {
  30. uint8_t spi_cs_gpio;
  31. uint8_t int_gpio;
  32. int8_t phy_reset_gpio;
  33. uint8_t phy_addr;
  34. }spi_eth_module_config_t;
  35. #endif
  36. /** Event handler for Ethernet events */
  37. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  38. int32_t event_id, void *event_data)
  39. {
  40. uint8_t mac_addr[6] = {0};
  41. /* we can get the ethernet driver handle from event data */
  42. esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  43. switch (event_id) {
  44. case ETHERNET_EVENT_CONNECTED:
  45. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  46. ESP_LOGI(TAG, "Ethernet Link Up");
  47. ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  48. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  49. break;
  50. case ETHERNET_EVENT_DISCONNECTED:
  51. ESP_LOGI(TAG, "Ethernet Link Down");
  52. break;
  53. case ETHERNET_EVENT_START:
  54. ESP_LOGI(TAG, "Ethernet Started");
  55. break;
  56. case ETHERNET_EVENT_STOP:
  57. ESP_LOGI(TAG, "Ethernet Stopped");
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. /** Event handler for IP_EVENT_ETH_GOT_IP */
  64. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  65. int32_t event_id, void *event_data)
  66. {
  67. ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  68. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  69. ESP_LOGI(TAG, "Ethernet Got IP Address");
  70. ESP_LOGI(TAG, "~~~~~~~~~~~");
  71. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  72. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  73. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  74. ESP_LOGI(TAG, "~~~~~~~~~~~");
  75. }
  76. void app_main(void)
  77. {
  78. // Initialize TCP/IP network interface (should be called only once in application)
  79. ESP_ERROR_CHECK(esp_netif_init());
  80. // Create default event loop that running in background
  81. ESP_ERROR_CHECK(esp_event_loop_create_default());
  82. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  83. // Create new default instance of esp-netif for Ethernet
  84. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
  85. esp_netif_t *eth_netif = esp_netif_new(&cfg);
  86. // Init MAC and PHY configs to default
  87. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  88. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  89. phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
  90. phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
  91. mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
  92. mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
  93. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  94. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  95. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  96. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  97. esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
  98. #elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
  99. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  100. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  101. esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
  102. #elif CONFIG_EXAMPLE_ETH_PHY_KSZ8041
  103. esp_eth_phy_t *phy = esp_eth_phy_new_ksz8041(&phy_config);
  104. #elif CONFIG_EXAMPLE_ETH_PHY_KSZ8081
  105. esp_eth_phy_t *phy = esp_eth_phy_new_ksz8081(&phy_config);
  106. #endif
  107. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  108. esp_eth_handle_t eth_handle = NULL;
  109. ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
  110. /* attach Ethernet driver to TCP/IP stack */
  111. ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
  112. #endif //CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  113. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  114. // Create instance(s) of esp-netif for SPI Ethernet(s)
  115. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  116. esp_netif_config_t cfg_spi = {
  117. .base = &esp_netif_config,
  118. .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
  119. };
  120. esp_netif_t *eth_netif_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL };
  121. char if_key_str[10];
  122. char if_desc_str[10];
  123. char num_str[3];
  124. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  125. itoa(i, num_str, 10);
  126. strcat(strcpy(if_key_str, "ETH_SPI_"), num_str);
  127. strcat(strcpy(if_desc_str, "eth"), num_str);
  128. esp_netif_config.if_key = if_key_str;
  129. esp_netif_config.if_desc = if_desc_str;
  130. esp_netif_config.route_prio = 30 - i;
  131. eth_netif_spi[i] = esp_netif_new(&cfg_spi);
  132. }
  133. // Init MAC and PHY configs to default
  134. eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
  135. eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
  136. // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
  137. gpio_install_isr_service(0);
  138. // Init SPI bus
  139. spi_device_handle_t spi_handle[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL };
  140. spi_bus_config_t buscfg = {
  141. .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
  142. .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
  143. .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
  144. .quadwp_io_num = -1,
  145. .quadhd_io_num = -1,
  146. };
  147. ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
  148. // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
  149. spi_eth_module_config_t spi_eth_module_config[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
  150. INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
  151. #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM > 1
  152. INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 1);
  153. #endif
  154. // Configure SPI interface and Ethernet driver for specific SPI module
  155. esp_eth_mac_t *mac_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
  156. esp_eth_phy_t *phy_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
  157. esp_eth_handle_t eth_handle_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL };
  158. #if CONFIG_EXAMPLE_USE_KSZ8851SNL
  159. spi_device_interface_config_t devcfg = {
  160. .mode = 0,
  161. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  162. .queue_size = 20
  163. };
  164. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  165. // Set SPI module Chip Select GPIO
  166. devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio;
  167. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i]));
  168. // KSZ8851SNL ethernet driver is based on spi driver
  169. eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle[i]);
  170. // Set remaining GPIO numbers and configuration used by the SPI module
  171. ksz8851snl_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
  172. phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr;
  173. phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio;
  174. mac_spi[i] = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config_spi);
  175. phy_spi[i] = esp_eth_phy_new_ksz8851snl(&phy_config_spi);
  176. }
  177. #elif CONFIG_EXAMPLE_USE_DM9051
  178. spi_device_interface_config_t devcfg = {
  179. .command_bits = 1,
  180. .address_bits = 7,
  181. .mode = 0,
  182. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  183. .queue_size = 20
  184. };
  185. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  186. // Set SPI module Chip Select GPIO
  187. devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio;
  188. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i]));
  189. // dm9051 ethernet driver is based on spi driver
  190. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle[i]);
  191. // Set remaining GPIO numbers and configuration used by the SPI module
  192. dm9051_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
  193. phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr;
  194. phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio;
  195. mac_spi[i] = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config_spi);
  196. phy_spi[i] = esp_eth_phy_new_dm9051(&phy_config_spi);
  197. }
  198. #elif CONFIG_EXAMPLE_USE_W5500
  199. spi_device_interface_config_t devcfg = {
  200. .command_bits = 16, // Actually it's the address phase in W5500 SPI frame
  201. .address_bits = 8, // Actually it's the control phase in W5500 SPI frame
  202. .mode = 0,
  203. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  204. .queue_size = 20
  205. };
  206. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  207. // Set SPI module Chip Select GPIO
  208. devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio;
  209. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i]));
  210. // w5500 ethernet driver is based on spi driver
  211. eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle[i]);
  212. // Set remaining GPIO numbers and configuration used by the SPI module
  213. w5500_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
  214. phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr;
  215. phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio;
  216. mac_spi[i] = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
  217. phy_spi[i] = esp_eth_phy_new_w5500(&phy_config_spi);
  218. }
  219. #endif //CONFIG_EXAMPLE_USE_W5500
  220. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  221. esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi[i], phy_spi[i]);
  222. ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config_spi, &eth_handle_spi[i]));
  223. /* The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually.
  224. 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
  225. */
  226. ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi[i], ETH_CMD_S_MAC_ADDR, (uint8_t[]) {
  227. 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 + i
  228. }));
  229. // attach Ethernet driver to TCP/IP stack
  230. ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi[i], esp_eth_new_netif_glue(eth_handle_spi[i])));
  231. }
  232. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  233. // Register user defined event handers
  234. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  235. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  236. /* start Ethernet driver state machine */
  237. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  238. ESP_ERROR_CHECK(esp_eth_start(eth_handle));
  239. #endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  240. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  241. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  242. ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi[i]));
  243. }
  244. #endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET
  245. }