bridge_example_main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_netif.h"
  11. #include "esp_netif_br_glue.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_bridge_example";
  21. #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM
  22. #define SPI_ETHERNETS_NUM CONFIG_EXAMPLE_SPI_ETHERNETS_NUM
  23. #else
  24. #define SPI_ETHERNETS_NUM 0
  25. #endif
  26. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  27. #define INTERNAL_ETHERNETS_NUM 1
  28. #else
  29. #define INTERNAL_ETHERNETS_NUM 0
  30. #endif
  31. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  32. #define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num) \
  33. do { \
  34. eth_module_config[num].spi_cs_gpio = CONFIG_EXAMPLE_ETH_SPI_CS ##num## _GPIO; \
  35. eth_module_config[num].int_gpio = CONFIG_EXAMPLE_ETH_SPI_INT ##num## _GPIO; \
  36. eth_module_config[num].phy_reset_gpio = CONFIG_EXAMPLE_ETH_SPI_PHY_RST ##num## _GPIO; \
  37. eth_module_config[num].phy_addr = CONFIG_EXAMPLE_ETH_SPI_PHY_ADDR ##num; \
  38. } while(0)
  39. typedef struct {
  40. uint8_t spi_cs_gpio;
  41. uint8_t int_gpio;
  42. int8_t phy_reset_gpio;
  43. uint8_t phy_addr;
  44. }spi_eth_module_config_t;
  45. #endif
  46. /** Event handler for Ethernet events */
  47. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  48. int32_t event_id, void *event_data)
  49. {
  50. uint8_t mac_addr[6] = {0};
  51. /* we can get the ethernet driver handle from event data */
  52. esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  53. switch (event_id) {
  54. case ETHERNET_EVENT_CONNECTED:
  55. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  56. ESP_LOGI(TAG, "Ethernet (%p) Link Up", eth_handle);
  57. ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  58. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  59. break;
  60. case ETHERNET_EVENT_DISCONNECTED:
  61. ESP_LOGI(TAG, "Ethernet (%p) Link Down", eth_handle);
  62. break;
  63. case ETHERNET_EVENT_START:
  64. ESP_LOGI(TAG, "Ethernet (%p) Started", eth_handle);
  65. break;
  66. case ETHERNET_EVENT_STOP:
  67. ESP_LOGI(TAG, "Ethernet (%p) Stopped", eth_handle);
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. /** Event handler for IP_EVENT_ETH_GOT_IP */
  74. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  75. int32_t event_id, void *event_data)
  76. {
  77. ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  78. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  79. ESP_LOGI(TAG, "Ethernet Got IP Address");
  80. ESP_LOGI(TAG, "~~~~~~~~~~~");
  81. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  82. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  83. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  84. ESP_LOGI(TAG, "~~~~~~~~~~~");
  85. }
  86. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  87. /** Internal EMAC initialization */
  88. esp_eth_handle_t eth_init_internal(void)
  89. {
  90. esp_eth_handle_t eth_handle;
  91. // Init MAC and PHY configs to default
  92. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  93. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  94. phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
  95. phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
  96. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  97. esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
  98. esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
  99. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  100. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  101. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  102. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  103. esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
  104. #elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
  105. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  106. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  107. esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
  108. #elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX
  109. esp_eth_phy_t *phy = esp_eth_phy_new_ksz80xx(&phy_config);
  110. #endif
  111. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  112. ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
  113. return eth_handle;
  114. }
  115. #endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  116. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  117. /** Ethernet SPI modules initialization */
  118. esp_eth_handle_t eth_init_spi(spi_eth_module_config_t *spi_eth_module_config, uint8_t *mac_addr)
  119. {
  120. esp_eth_handle_t eth_handle;
  121. // Init MAC and PHY configs to default
  122. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  123. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  124. // Configure SPI interface and Ethernet driver for specific SPI module
  125. esp_eth_mac_t *mac;
  126. esp_eth_phy_t *phy;
  127. spi_device_handle_t spi_handle;
  128. #if CONFIG_EXAMPLE_USE_KSZ8851SNL
  129. spi_device_interface_config_t devcfg = {
  130. .mode = 0,
  131. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  132. .queue_size = 20
  133. };
  134. // Set SPI module Chip Select GPIO
  135. devcfg.spics_io_num = spi_eth_module_config->spi_cs_gpio;
  136. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
  137. // KSZ8851SNL ethernet driver is based on spi driver
  138. eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle);
  139. // Set remaining GPIO numbers and configuration used by the SPI module
  140. ksz8851snl_config.int_gpio_num = spi_eth_module_config->int_gpio;
  141. phy_config.phy_addr = spi_eth_module_config->phy_addr;
  142. phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio;
  143. mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
  144. phy = esp_eth_phy_new_ksz8851snl(&phy_config);
  145. #elif CONFIG_EXAMPLE_USE_DM9051
  146. spi_device_interface_config_t devcfg = {
  147. .command_bits = 1,
  148. .address_bits = 7,
  149. .mode = 0,
  150. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  151. .queue_size = 20
  152. };
  153. // Set SPI module Chip Select GPIO
  154. devcfg.spics_io_num = spi_eth_module_config->spi_cs_gpio;
  155. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
  156. // dm9051 ethernet driver is based on spi driver
  157. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
  158. // Set remaining GPIO numbers and configuration used by the SPI module
  159. dm9051_config.int_gpio_num = spi_eth_module_config->int_gpio;
  160. phy_config.phy_addr = spi_eth_module_config->phy_addr;
  161. phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio;
  162. mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
  163. phy = esp_eth_phy_new_dm9051(&phy_config);
  164. #elif CONFIG_EXAMPLE_USE_W5500
  165. spi_device_interface_config_t devcfg = {
  166. .command_bits = 16, // Actually it's the address phase in W5500 SPI frame
  167. .address_bits = 8, // Actually it's the control phase in W5500 SPI frame
  168. .mode = 0,
  169. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  170. .queue_size = 20
  171. };
  172. // Set SPI module Chip Select GPIO
  173. devcfg.spics_io_num = spi_eth_module_config->spi_cs_gpio;
  174. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
  175. // w5500 ethernet driver is based on spi driver
  176. eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
  177. // Set remaining GPIO numbers and configuration used by the SPI module
  178. w5500_config.int_gpio_num = spi_eth_module_config->int_gpio;
  179. phy_config.phy_addr = spi_eth_module_config->phy_addr;
  180. phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio;
  181. mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
  182. phy = esp_eth_phy_new_w5500(&phy_config);
  183. #endif //CONFIG_EXAMPLE_USE_W5500
  184. esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac, phy);
  185. ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config_spi, &eth_handle));
  186. // The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually.
  187. ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, mac_addr));
  188. return eth_handle;
  189. }
  190. #endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET
  191. void app_main(void)
  192. {
  193. // number of Ethernet ports to be used in the bridge
  194. uint8_t port_cnt = 0;
  195. // the same MAC address will be used for all Ethernet ports since the bridge acts as one device
  196. uint8_t common_mac_addr[ETH_ADDR_LEN];
  197. esp_eth_handle_t eth_handles[SPI_ETHERNETS_NUM + INTERNAL_ETHERNETS_NUM] = { NULL };
  198. esp_netif_t *eth_netifs[SPI_ETHERNETS_NUM + INTERNAL_ETHERNETS_NUM] = { NULL };
  199. // Initialize TCP/IP network interface (should be called only once in application)
  200. ESP_ERROR_CHECK(esp_netif_init());
  201. // Create default event loop that running in background
  202. ESP_ERROR_CHECK(esp_event_loop_create_default());
  203. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  204. eth_handles[port_cnt++] = eth_init_internal();
  205. // use burned ESP32 MAC address as commom address for all Ethernet interfaces
  206. ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[0], ETH_CMD_G_MAC_ADDR, common_mac_addr));
  207. #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET
  208. // if ESP32 internal Ethernet is not used, use manually configured MAC address
  209. // 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
  210. memcpy(common_mac_addr, (uint8_t[]) {0x02, 0x00, 0x00, 0x12, 0x34, 0x56}, ETH_ADDR_LEN);
  211. #endif //CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  212. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  213. // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
  214. gpio_install_isr_service(0);
  215. // Init SPI bus
  216. spi_bus_config_t buscfg = {
  217. .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
  218. .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
  219. .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
  220. .quadwp_io_num = -1,
  221. .quadhd_io_num = -1,
  222. };
  223. ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
  224. // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
  225. spi_eth_module_config_t spi_eth_module_config[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
  226. INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
  227. #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM > 1
  228. INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 1);
  229. #endif
  230. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  231. eth_handles[port_cnt++] = eth_init_spi(&spi_eth_module_config[i], common_mac_addr);
  232. }
  233. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  234. // Create instances of esp-netif for Ethernet ports
  235. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  236. esp_netif_config_t netif_cfg = {
  237. .base = &esp_netif_config,
  238. .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
  239. };
  240. char if_key_str[10];
  241. char if_desc_str[10];
  242. char num_str[3];
  243. for (int i = 0; i < port_cnt; i++) {
  244. itoa(i, num_str, 10);
  245. strcat(strcpy(if_key_str, "ETH_"), num_str);
  246. strcat(strcpy(if_desc_str, "eth"), num_str);
  247. esp_netif_config.if_key = if_key_str;
  248. esp_netif_config.if_desc = if_desc_str;
  249. esp_netif_config.route_prio = 50 - i;
  250. esp_netif_config.flags = 0; // ESP-NETIF flags need to be zero when port's to be bridged
  251. eth_netifs[i] = esp_netif_new(&netif_cfg);
  252. // attach Ethernet driver to TCP/IP stack
  253. ESP_ERROR_CHECK(esp_netif_attach(eth_netifs[i], esp_eth_new_netif_glue(eth_handles[i])));
  254. }
  255. // Create instance of esp-netif for bridge interface
  256. esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR();
  257. esp_netif_config_t netif_br_cfg = {
  258. .base = &esp_netif_br_config,
  259. .stack = ESP_NETIF_NETSTACK_DEFAULT_BR,
  260. };
  261. // Bridge configuration
  262. bridgeif_config_t bridgeif_config = {
  263. .max_fdb_dyn_entries = 10, // maximum number of address entries in dynamic forwarding database
  264. .max_fdb_sta_entries = 2, // maximum number of address entries in static forwarding database
  265. .max_ports = port_cnt // maximum number of ports the bridge can consist of
  266. };
  267. esp_netif_br_config.bridge_info = &bridgeif_config;
  268. // Set MAC address of bridge interface the same as the Ethernet interface
  269. memcpy(esp_netif_br_config.mac, common_mac_addr, ETH_ADDR_LEN);
  270. esp_netif_t *br_netif = esp_netif_new(&netif_br_cfg);
  271. // Create new esp netif bridge glue instance
  272. esp_netif_br_glue_handle_t netif_br_glue = esp_netif_br_glue_new();
  273. // Add Ethernet port interfaces to that esp netif bridge glue instance
  274. for (int i = 0; i < port_cnt; i++) {
  275. ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, eth_netifs[i]));
  276. }
  277. // Attach esp netif bridge glue instance with added ports to bridge netif
  278. ESP_ERROR_CHECK(esp_netif_attach(br_netif, netif_br_glue));
  279. // Register user defined event handers
  280. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  281. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  282. for (int i = 0; i < port_cnt; i++) {
  283. // Since the MAC forwarding is performed in lwIP bridge, we need to pass all addresses through the Ethernet MACs
  284. bool promiscuous = true;
  285. esp_eth_ioctl(eth_handles[i], ETH_CMD_S_PROMISCUOUS, &promiscuous);
  286. // Start Ethernet driver state machine
  287. ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
  288. }
  289. }