2021-12-15 16:30:29 +01:00
/*
2023-08-22 16:45:34 +02:00
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2021-12-15 16:30:29 +01:00
*
* SPDX-License-Identifier: Apache-2.0
*/
2019-04-10 16:24:50 +08:00
# pragma once
# include <stdbool.h>
2023-08-22 16:45:34 +02:00
# include "soc/soc_caps.h"
2019-11-12 22:10:57 +08:00
# include "esp_eth_com.h"
2019-11-26 17:48:38 +08:00
# include "sdkconfig.h"
2019-06-25 19:36:56 +08:00
2020-09-08 17:39:56 +08:00
# ifdef __cplusplus
extern " C " {
# endif
2019-04-10 16:24:50 +08:00
/**
* @brief Ethernet MAC
*
*/
typedef struct esp_eth_mac_s esp_eth_mac_t ;
/**
* @brief Ethernet MAC
*
*/
struct esp_eth_mac_s {
/**
* @brief Set mediator for Ethernet MAC
*
* @param[in] mac: Ethernet MAC instance
* @param[in] eth: Ethernet mediator
*
* @return
* - ESP_OK: set mediator for Ethernet MAC successfully
* - ESP_ERR_INVALID_ARG: set mediator for Ethernet MAC failed because of invalid argument
*
*/
esp_err_t ( * set_mediator ) ( esp_eth_mac_t * mac , esp_eth_mediator_t * eth ) ;
/**
* @brief Initialize Ethernet MAC
*
* @param[in] mac: Ethernet MAC instance
*
* @return
* - ESP_OK: initialize Ethernet MAC successfully
* - ESP_ERR_TIMEOUT: initialize Ethernet MAC failed because of timeout
* - ESP_FAIL: initialize Ethernet MAC failed because some other error occurred
*
*/
esp_err_t ( * init ) ( esp_eth_mac_t * mac ) ;
/**
* @brief Deinitialize Ethernet MAC
*
* @param[in] mac: Ethernet MAC instance
*
* @return
* - ESP_OK: deinitialize Ethernet MAC successfully
* - ESP_FAIL: deinitialize Ethernet MAC failed because some error occurred
*
*/
esp_err_t ( * deinit ) ( esp_eth_mac_t * mac ) ;
2020-05-08 21:44:30 +08:00
/**
* @brief Start Ethernet MAC
*
* @param[in] mac: Ethernet MAC instance
*
* @return
* - ESP_OK: start Ethernet MAC successfully
* - ESP_FAIL: start Ethernet MAC failed because some other error occurred
*
*/
esp_err_t ( * start ) ( esp_eth_mac_t * mac ) ;
/**
* @brief Stop Ethernet MAC
*
* @param[in] mac: Ethernet MAC instance
*
* @return
* - ESP_OK: stop Ethernet MAC successfully
* - ESP_FAIL: stop Ethernet MAC failed because some error occurred
*
*/
esp_err_t ( * stop ) ( esp_eth_mac_t * mac ) ;
2019-04-10 16:24:50 +08:00
/**
* @brief Transmit packet from Ethernet MAC
*
* @param[in] mac: Ethernet MAC instance
* @param[in] buf: packet buffer to transmit
* @param[in] length: length of packet
*
* @return
* - ESP_OK: transmit packet successfully
2021-12-01 09:53:25 +01:00
* - ESP_ERR_INVALID_SIZE: number of actually sent bytes differs to expected
2019-04-10 16:24:50 +08:00
* - ESP_FAIL: transmit packet failed because some other error occurred
*
2021-12-01 09:53:25 +01:00
* @note Returned error codes may differ for each specific MAC chip.
*
2019-04-10 16:24:50 +08:00
*/
esp_err_t ( * transmit ) ( esp_eth_mac_t * mac , uint8_t * buf , uint32_t length ) ;
2024-06-26 15:40:32 +02:00
/**
* @brief Transmit packet with extended control from Ethernet MAC and constructed with special parameters at Layer2.
*
* @param[in] mac: Ethernet MAC instance
* @param[in] ctrl: optional transmit control structure (chip specific), set to NULL when not required
* @param[in] argc: number variable arguments
* @param[in] args: variable arguments
*
* @note Typical intended use case is to make possible to construct a frame from multiple higher layer
* buffers without a need of buffer reallocations. However, other use cases are not limited.
*
* @return
* - ESP_OK: transmit packet successfully
* - ESP_ERR_INVALID_SIZE: number of actually sent bytes differs to expected
* - ESP_FAIL: transmit packet failed because some other error occurred
*
* @note Returned error codes may differ for each specific MAC chip.
*
*/
esp_err_t ( * transmit_ctrl_vargs ) ( esp_eth_mac_t * mac , void * ctrl , uint32_t argc , va_list args ) ;
2021-12-01 09:53:25 +01:00
/**
* @brief Transmit packet from Ethernet MAC constructed with special parameters at Layer2.
*
2024-06-26 15:40:32 +02:00
* @warning Deprecated, use `transmit_ctrl_vargs()` function instead.
*
2021-12-01 09:53:25 +01:00
* @param[in] mac: Ethernet MAC instance
* @param[in] argc: number variable arguments
* @param[in] args: variable arguments
*
* @note Typical intended use case is to make possible to construct a frame from multiple higher layer
* buffers without a need of buffer reallocations. However, other use cases are not limited.
*
* @return
* - ESP_OK: transmit packet successfully
* - ESP_ERR_INVALID_SIZE: number of actually sent bytes differs to expected
* - ESP_FAIL: transmit packet failed because some other error occurred
*
* @note Returned error codes may differ for each specific MAC chip.
*
*/
2024-06-26 15:40:32 +02:00
esp_err_t ( * transmit_vargs ) ( esp_eth_mac_t * mac , uint32_t argc , va_list args ) __attribute__ ( ( deprecated ( " Use transmit_ctrl_vargs instead " ) ) ) ;
2021-12-01 09:53:25 +01:00
2019-04-10 16:24:50 +08:00
/**
* @brief Receive packet from Ethernet MAC
*
* @param[in] mac: Ethernet MAC instance
* @param[out] buf: packet buffer which will preserve the received frame
* @param[out] length: length of the received packet
*
* @note Memory of buf is allocated in the Layer2, make sure it get free after process.
2019-09-19 11:27:42 +08:00
* @note Before this function got invoked, the value of "length" should set by user, equals the size of buffer.
* After the function returned, the value of "length" means the real length of received data.
2019-04-10 16:24:50 +08:00
*
* @return
* - ESP_OK: receive packet successfully
* - ESP_ERR_INVALID_ARG: receive packet failed because of invalid argument
2019-09-19 11:27:42 +08:00
* - ESP_ERR_INVALID_SIZE: input buffer size is not enough to hold the incoming data.
* in this case, value of returned "length" indicates the real size of incoming data.
2019-04-10 16:24:50 +08:00
* - ESP_FAIL: receive packet failed because some other error occurred
*
*/
esp_err_t ( * receive ) ( esp_eth_mac_t * mac , uint8_t * buf , uint32_t * length ) ;
/**
* @brief Read PHY register
*
* @param[in] mac: Ethernet MAC instance
* @param[in] phy_addr: PHY chip address (0~31)
* @param[in] phy_reg: PHY register index code
* @param[out] reg_value: PHY register value
*
* @return
* - ESP_OK: read PHY register successfully
* - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
* - ESP_ERR_INVALID_STATE: read PHY register failed because of wrong state of MAC
2019-06-25 19:36:56 +08:00
* - ESP_ERR_TIMEOUT: read PHY register failed because of timeout
2019-04-10 16:24:50 +08:00
* - ESP_FAIL: read PHY register failed because some other error occurred
*
*/
esp_err_t ( * read_phy_reg ) ( esp_eth_mac_t * mac , uint32_t phy_addr , uint32_t phy_reg , uint32_t * reg_value ) ;
/**
* @brief Write PHY register
*
* @param[in] mac: Ethernet MAC instance
* @param[in] phy_addr: PHY chip address (0~31)
* @param[in] phy_reg: PHY register index code
* @param[in] reg_value: PHY register value
*
* @return
* - ESP_OK: write PHY register successfully
* - ESP_ERR_INVALID_STATE: write PHY register failed because of wrong state of MAC
2019-06-25 19:36:56 +08:00
* - ESP_ERR_TIMEOUT: write PHY register failed because of timeout
2019-04-10 16:24:50 +08:00
* - ESP_FAIL: write PHY register failed because some other error occurred
*
*/
esp_err_t ( * write_phy_reg ) ( esp_eth_mac_t * mac , uint32_t phy_addr , uint32_t phy_reg , uint32_t reg_value ) ;
/**
* @brief Set MAC address
*
* @param[in] mac: Ethernet MAC instance
* @param[in] addr: MAC address
*
* @return
* - ESP_OK: set MAC address successfully
* - ESP_ERR_INVALID_ARG: set MAC address failed because of invalid argument
* - ESP_FAIL: set MAC address failed because some other error occurred
*
*/
esp_err_t ( * set_addr ) ( esp_eth_mac_t * mac , uint8_t * addr ) ;
/**
* @brief Get MAC address
*
* @param[in] mac: Ethernet MAC instance
* @param[out] addr: MAC address
*
* @return
* - ESP_OK: get MAC address successfully
* - ESP_ERR_INVALID_ARG: get MAC address failed because of invalid argument
* - ESP_FAIL: get MAC address failed because some other error occurred
*
*/
esp_err_t ( * get_addr ) ( esp_eth_mac_t * mac , uint8_t * addr ) ;
/**
* @brief Set speed of MAC
*
* @param[in] ma:c Ethernet MAC instance
* @param[in] speed: MAC speed
*
* @return
* - ESP_OK: set MAC speed successfully
* - ESP_ERR_INVALID_ARG: set MAC speed failed because of invalid argument
* - ESP_FAIL: set MAC speed failed because some other error occurred
*
*/
esp_err_t ( * set_speed ) ( esp_eth_mac_t * mac , eth_speed_t speed ) ;
/**
* @brief Set duplex mode of MAC
*
* @param[in] mac: Ethernet MAC instance
* @param[in] duplex: MAC duplex
*
* @return
* - ESP_OK: set MAC duplex mode successfully
* - ESP_ERR_INVALID_ARG: set MAC duplex failed because of invalid argument
* - ESP_FAIL: set MAC duplex failed because some other error occurred
*
*/
esp_err_t ( * set_duplex ) ( esp_eth_mac_t * mac , eth_duplex_t duplex ) ;
/**
* @brief Set link status of MAC
*
* @param[in] mac: Ethernet MAC instance
* @param[in] link: Link status
*
* @return
* - ESP_OK: set link status successfully
* - ESP_ERR_INVALID_ARG: set link status failed because of invalid argument
* - ESP_FAIL: set link status failed because some other error occurred
*
*/
esp_err_t ( * set_link ) ( esp_eth_mac_t * mac , eth_link_t link ) ;
/**
* @brief Set promiscuous of MAC
*
* @param[in] mac: Ethernet MAC instance
* @param[in] enable: set true to enable promiscuous mode; set false to disable promiscuous mode
*
* @return
* - ESP_OK: set promiscuous mode successfully
* - ESP_FAIL: set promiscuous mode failed because some error occurred
*
*/
esp_err_t ( * set_promiscuous ) ( esp_eth_mac_t * mac , bool enable ) ;
2020-07-20 20:42:52 +08:00
/**
* @brief Enable flow control on MAC layer or not
*
* @param[in] mac: Ethernet MAC instance
* @param[in] enable: set true to enable flow control; set false to disable flow control
*
* @return
* - ESP_OK: set flow control successfully
* - ESP_FAIL: set flow control failed because some error occurred
*
*/
esp_err_t ( * enable_flow_ctrl ) ( esp_eth_mac_t * mac , bool enable ) ;
/**
* @brief Set the PAUSE ability of peer node
*
* @param[in] mac: Ethernet MAC instance
* @param[in] ability: zero indicates that pause function is supported by link partner; non-zero indicates that pause function is not supported by link partner
*
* @return
* - ESP_OK: set peer pause ability successfully
* - ESP_FAIL: set peer pause ability failed because some error occurred
*/
esp_err_t ( * set_peer_pause_ability ) ( esp_eth_mac_t * mac , uint32_t ability ) ;
2021-12-01 09:53:25 +01:00
/**
* @brief Custom IO function of MAC driver. This function is intended to extend common options of esp_eth_ioctl to cover specifics of MAC chip.
*
* @note This function may not be assigned when the MAC chip supports only most common set of configuration options.
*
* @param[in] mac: Ethernet MAC instance
* @param[in] cmd: IO control command
* @param[in, out] data: address of data for `set` command or address where to store the data when used with `get` command
*
* @return
* - ESP_OK: process io command successfully
* - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
* - ESP_FAIL: process io command failed because some other error occurred
* - ESP_ERR_NOT_SUPPORTED: requested feature is not supported
*/
2024-04-26 12:27:54 +02:00
esp_err_t ( * custom_ioctl ) ( esp_eth_mac_t * mac , int cmd , void * data ) ;
2021-12-01 09:53:25 +01:00
2019-04-10 16:24:50 +08:00
/**
* @brief Free memory of Ethernet MAC
*
* @param[in] mac: Ethernet MAC instance
*
* @return
* - ESP_OK: free Ethernet MAC instance successfully
* - ESP_FAIL: free Ethernet MAC instance failed because some error occurred
*
*/
esp_err_t ( * del ) ( esp_eth_mac_t * mac ) ;
} ;
2024-06-26 15:40:32 +02:00
/**
* @brief Ethernet MAC Time Stamp
*
*/
typedef struct {
uint32_t seconds ; /*!< Seconds */
uint32_t nanoseconds ; /*!< Nanoseconds */
} eth_mac_time_t ;
2019-04-10 16:24:50 +08:00
/**
* @brief Configuration of Ethernet MAC object
*
*/
typedef struct {
2021-06-23 16:23:58 +02:00
uint32_t sw_reset_timeout_ms ; /*!< Software reset timeout value (Unit: ms) */
uint32_t rx_task_stack_size ; /*!< Stack size of the receive task */
uint32_t rx_task_prio ; /*!< Priority of the receive task */
uint32_t flags ; /*!< Flags that specify extra capability for mac driver */
2019-04-10 16:24:50 +08:00
} eth_mac_config_t ;
2020-01-13 21:34:23 +08:00
# define ETH_MAC_FLAG_WORK_WITH_CACHE_DISABLE (1 << 0) /*!< MAC driver can work when cache is disabled */
2020-05-27 18:55:38 +08:00
# define ETH_MAC_FLAG_PIN_TO_CORE (1 << 1) /*!< Pin MAC task to the CPU core where driver installation happened */
2020-01-13 21:34:23 +08:00
2019-04-10 16:24:50 +08:00
/**
* @brief Default configuration for Ethernet MAC object
*
*/
2021-06-23 16:23:58 +02:00
# define ETH_MAC_DEFAULT_CONFIG() \
{ \
.sw_reset_timeout_ms = 100, \
2024-04-02 16:17:10 +08:00
.rx_task_stack_size = 4096, \
2021-06-23 16:23:58 +02:00
.rx_task_prio = 15, \
.flags = 0, \
2019-04-10 16:24:50 +08:00
}
# ifdef __cplusplus
}
# endif