1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #pragma once 15 16 #include "esp_err.h" 17 #include "esp_event_base.h" 18 #include "hal/eth_types.h" 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 /** 25 * @brief Maximum Ethernet payload size 26 * 27 */ 28 #define ETH_MAX_PAYLOAD_LEN (1500) 29 30 /** 31 * @brief Minimum Ethernet payload size 32 * 33 */ 34 #define ETH_MIN_PAYLOAD_LEN (46) 35 36 /** 37 * @brief Ethernet frame header size: Dest addr(6 Bytes) + Src addr(6 Bytes) + length/type(2 Bytes) 38 * 39 */ 40 #define ETH_HEADER_LEN (14) 41 42 /** 43 * @brief Optional 802.1q VLAN Tag length 44 * 45 */ 46 #define ETH_VLAN_TAG_LEN (4) 47 48 /** 49 * @brief Jumbo frame payload size 50 * 51 */ 52 #define ETH_JUMBO_FRAME_PAYLOAD_LEN (9000) 53 54 /** 55 * @brief Maximum frame size (1522 Bytes) 56 * 57 */ 58 #define ETH_MAX_PACKET_SIZE (ETH_HEADER_LEN + ETH_VLAN_TAG_LEN + ETH_MAX_PAYLOAD_LEN + ETH_CRC_LEN) 59 60 /** 61 * @brief Minimum frame size (64 Bytes) 62 * 63 */ 64 #define ETH_MIN_PACKET_SIZE (ETH_HEADER_LEN + ETH_MIN_PAYLOAD_LEN + ETH_CRC_LEN) 65 66 /** 67 * @brief Ethernet driver state 68 * 69 */ 70 typedef enum { 71 ETH_STATE_LLINIT, /*!< Lowlevel init done */ 72 ETH_STATE_DEINIT, /*!< Deinit done */ 73 ETH_STATE_LINK, /*!< Link status changed */ 74 ETH_STATE_SPEED, /*!< Speed updated */ 75 ETH_STATE_DUPLEX, /*!< Duplex updated */ 76 ETH_STATE_PAUSE, /*!< Pause ability updated */ 77 } esp_eth_state_t; 78 79 /** 80 * @brief Command list for ioctl API 81 * 82 */ 83 typedef enum { 84 ETH_CMD_G_MAC_ADDR, /*!< Get MAC address */ 85 ETH_CMD_S_MAC_ADDR, /*!< Set MAC address */ 86 ETH_CMD_G_PHY_ADDR, /*!< Get PHY address */ 87 ETH_CMD_S_PHY_ADDR, /*!< Set PHY address */ 88 ETH_CMD_G_SPEED, /*!< Get Speed */ 89 ETH_CMD_S_PROMISCUOUS, /*!< Set promiscuous mode */ 90 ETH_CMD_S_FLOW_CTRL, /*!< Set flow control */ 91 ETH_CMD_G_DUPLEX_MODE, /*!< Get Duplex mode */ 92 ETH_CMD_S_PHY_LOOPBACK,/*!< Set PHY loopback */ 93 } esp_eth_io_cmd_t; 94 95 /** 96 * @brief Ethernet mediator 97 * 98 */ 99 typedef struct esp_eth_mediator_s esp_eth_mediator_t; 100 101 /** 102 * @brief Ethernet mediator 103 * 104 */ 105 struct esp_eth_mediator_s { 106 /** 107 * @brief Read PHY register 108 * 109 * @param[in] eth: mediator of Ethernet driver 110 * @param[in] phy_addr: PHY Chip address (0~31) 111 * @param[in] phy_reg: PHY register index code 112 * @param[out] reg_value: PHY register value 113 * 114 * @return 115 * - ESP_OK: read PHY register successfully 116 * - ESP_FAIL: read PHY register failed because some error occurred 117 * 118 */ 119 esp_err_t (*phy_reg_read)(esp_eth_mediator_t *eth, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value); 120 121 /** 122 * @brief Write PHY register 123 * 124 * @param[in] eth: mediator of Ethernet driver 125 * @param[in] phy_addr: PHY Chip address (0~31) 126 * @param[in] phy_reg: PHY register index code 127 * @param[in] reg_value: PHY register value 128 * 129 * @return 130 * - ESP_OK: write PHY register successfully 131 * - ESP_FAIL: write PHY register failed because some error occurred 132 */ 133 esp_err_t (*phy_reg_write)(esp_eth_mediator_t *eth, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value); 134 135 /** 136 * @brief Deliver packet to upper stack 137 * 138 * @param[in] eth: mediator of Ethernet driver 139 * @param[in] buffer: packet buffer 140 * @param[in] length: length of the packet 141 * 142 * @return 143 * - ESP_OK: deliver packet to upper stack successfully 144 * - ESP_FAIL: deliver packet failed because some error occurred 145 * 146 */ 147 esp_err_t (*stack_input)(esp_eth_mediator_t *eth, uint8_t *buffer, uint32_t length); 148 149 /** 150 * @brief Callback on Ethernet state changed 151 * 152 * @param[in] eth: mediator of Ethernet driver 153 * @param[in] state: new state 154 * @param[in] args: optional argument for the new state 155 * 156 * @return 157 * - ESP_OK: process the new state successfully 158 * - ESP_FAIL: process the new state failed because some error occurred 159 * 160 */ 161 esp_err_t (*on_state_changed)(esp_eth_mediator_t *eth, esp_eth_state_t state, void *args); 162 }; 163 164 /** 165 * @brief Ethernet event declarations 166 * 167 */ 168 typedef enum { 169 ETHERNET_EVENT_START, /*!< Ethernet driver start */ 170 ETHERNET_EVENT_STOP, /*!< Ethernet driver stop */ 171 ETHERNET_EVENT_CONNECTED, /*!< Ethernet got a valid link */ 172 ETHERNET_EVENT_DISCONNECTED, /*!< Ethernet lost a valid link */ 173 } eth_event_t; 174 175 /** 176 * @brief Ethernet event base declaration 177 * 178 */ 179 ESP_EVENT_DECLARE_BASE(ETH_EVENT); 180 181 /** 182 * @brief Detect PHY address 183 * 184 * @param[in] eth: mediator of Ethernet driver 185 * @param[out] detected_addr: a valid address after detection 186 * @return 187 * - ESP_OK: detect phy address successfully 188 * - ESP_ERR_INVALID_ARG: invalid parameter 189 * - ESP_ERR_NOT_FOUND: can't detect any PHY device 190 * - ESP_FAIL: detect phy address failed because some error occurred 191 */ 192 esp_err_t esp_eth_detect_phy_addr(esp_eth_mediator_t *eth, int *detected_addr); 193 194 #ifdef __cplusplus 195 } 196 #endif 197