mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-12 21:40:02 +01:00
feat(esp_eth): added SPI Ethernet module polling mode
Closes https://github.com/espressif/esp-idf/issues/12682
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "esp_rom_gpio.h"
|
||||
#include "esp_rom_sys.h"
|
||||
#include "esp_cpu.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
static const char *TAG = "dm9051.mac";
|
||||
|
||||
@@ -67,6 +68,8 @@ typedef struct {
|
||||
TaskHandle_t rx_task_hdl;
|
||||
uint32_t sw_reset_timeout_ms;
|
||||
int int_gpio_num;
|
||||
esp_timer_handle_t poll_timer;
|
||||
uint32_t poll_period_ms;
|
||||
uint8_t addr[6];
|
||||
bool packets_remain;
|
||||
bool flow_ctrl_enabled;
|
||||
@@ -421,6 +424,12 @@ IRAM_ATTR static void dm9051_isr_handler(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
static void dm9051_poll_timer(void *arg)
|
||||
{
|
||||
emac_dm9051_t *emac = (emac_dm9051_t *)arg;
|
||||
xTaskNotifyGive(emac->rx_task_hdl);
|
||||
}
|
||||
|
||||
static esp_err_t emac_dm9051_set_mediator(esp_eth_mac_t *mac, esp_eth_mediator_t *eth)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
@@ -514,12 +523,21 @@ err:
|
||||
static esp_err_t emac_dm9051_set_link(esp_eth_mac_t *mac, eth_link_t link)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
emac_dm9051_t *emac = __containerof(mac, emac_dm9051_t, parent);
|
||||
switch (link) {
|
||||
case ETH_LINK_UP:
|
||||
ESP_GOTO_ON_ERROR(mac->start(mac), err, TAG, "dm9051 start failed");
|
||||
if (emac->poll_timer) {
|
||||
ESP_GOTO_ON_ERROR(esp_timer_start_periodic(emac->poll_timer, emac->poll_period_ms * 1000),
|
||||
err, TAG, "start poll timer failed");
|
||||
}
|
||||
break;
|
||||
case ETH_LINK_DOWN:
|
||||
ESP_GOTO_ON_ERROR(mac->stop(mac), err, TAG, "dm9051 stop failed");
|
||||
if (emac->poll_timer) {
|
||||
ESP_GOTO_ON_ERROR(esp_timer_stop(emac->poll_timer),
|
||||
err, TAG, "stop poll timer failed");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "unknown link status");
|
||||
@@ -777,12 +795,14 @@ static esp_err_t emac_dm9051_init(esp_eth_mac_t *mac)
|
||||
esp_err_t ret = ESP_OK;
|
||||
emac_dm9051_t *emac = __containerof(mac, emac_dm9051_t, parent);
|
||||
esp_eth_mediator_t *eth = emac->eth;
|
||||
esp_rom_gpio_pad_select_gpio(emac->int_gpio_num);
|
||||
gpio_set_direction(emac->int_gpio_num, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(emac->int_gpio_num, GPIO_PULLDOWN_ONLY);
|
||||
gpio_set_intr_type(emac->int_gpio_num, GPIO_INTR_POSEDGE);
|
||||
gpio_intr_enable(emac->int_gpio_num);
|
||||
gpio_isr_handler_add(emac->int_gpio_num, dm9051_isr_handler, emac);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
esp_rom_gpio_pad_select_gpio(emac->int_gpio_num);
|
||||
gpio_set_direction(emac->int_gpio_num, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(emac->int_gpio_num, GPIO_PULLDOWN_ONLY);
|
||||
gpio_set_intr_type(emac->int_gpio_num, GPIO_INTR_POSEDGE);
|
||||
gpio_intr_enable(emac->int_gpio_num);
|
||||
gpio_isr_handler_add(emac->int_gpio_num, dm9051_isr_handler, emac);
|
||||
}
|
||||
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LLINIT, NULL), err, TAG, "lowlevel init failed");
|
||||
/* reset dm9051 */
|
||||
ESP_GOTO_ON_ERROR(dm9051_reset(emac), err, TAG, "reset dm9051 failed");
|
||||
@@ -796,8 +816,10 @@ static esp_err_t emac_dm9051_init(esp_eth_mac_t *mac)
|
||||
ESP_GOTO_ON_ERROR(dm9051_get_mac_addr(emac), err, TAG, "fetch ethernet mac address failed");
|
||||
return ESP_OK;
|
||||
err:
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
}
|
||||
eth->on_state_changed(eth, ETH_STATE_DEINIT, NULL);
|
||||
return ret;
|
||||
}
|
||||
@@ -807,8 +829,13 @@ static esp_err_t emac_dm9051_deinit(esp_eth_mac_t *mac)
|
||||
emac_dm9051_t *emac = __containerof(mac, emac_dm9051_t, parent);
|
||||
esp_eth_mediator_t *eth = emac->eth;
|
||||
mac->stop(mac);
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
}
|
||||
if (emac->poll_timer && esp_timer_is_active(emac->poll_timer)) {
|
||||
esp_timer_stop(emac->poll_timer);
|
||||
}
|
||||
eth->on_state_changed(eth, ETH_STATE_DEINIT, NULL);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -819,9 +846,13 @@ static void emac_dm9051_task(void *arg)
|
||||
uint8_t status = 0;
|
||||
while (1) {
|
||||
// check if the task receives any notification
|
||||
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ...
|
||||
gpio_get_level(emac->int_gpio_num) == 0) { // ...and no interrupt asserted
|
||||
continue; // -> just continue to check again
|
||||
if (emac->int_gpio_num >= 0) { // if in interrupt mode
|
||||
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ...
|
||||
gpio_get_level(emac->int_gpio_num) == 0) { // ...and no interrupt asserted
|
||||
continue; // -> just continue to check again
|
||||
}
|
||||
} else {
|
||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||
}
|
||||
/* clear interrupt status */
|
||||
dm9051_register_read(emac, DM9051_ISR, &status);
|
||||
@@ -867,6 +898,9 @@ static void emac_dm9051_task(void *arg)
|
||||
static esp_err_t emac_dm9051_del(esp_eth_mac_t *mac)
|
||||
{
|
||||
emac_dm9051_t *emac = __containerof(mac, emac_dm9051_t, parent);
|
||||
if (emac->poll_timer) {
|
||||
esp_timer_delete(emac->poll_timer);
|
||||
}
|
||||
vTaskDelete(emac->rx_task_hdl);
|
||||
emac->spi.deinit(emac->spi.ctx);
|
||||
heap_caps_free(emac->rx_buffer);
|
||||
@@ -880,13 +914,13 @@ esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config,
|
||||
emac_dm9051_t *emac = NULL;
|
||||
ESP_GOTO_ON_FALSE(dm9051_config, NULL, err, TAG, "can't set dm9051 specific config to null");
|
||||
ESP_GOTO_ON_FALSE(mac_config, NULL, err, TAG, "can't set mac config to null");
|
||||
ESP_GOTO_ON_FALSE((dm9051_config->int_gpio_num >= 0) != (dm9051_config->poll_period_ms > 0), NULL, err, TAG, "invalid configuration argument combination");
|
||||
emac = calloc(1, sizeof(emac_dm9051_t));
|
||||
ESP_GOTO_ON_FALSE(emac, NULL, err, TAG, "calloc emac failed");
|
||||
/* dm9051 receive is driven by interrupt only for now*/
|
||||
ESP_GOTO_ON_FALSE(dm9051_config->int_gpio_num >= 0, NULL, err, TAG, "error interrupt gpio number");
|
||||
/* bind methods and attributes */
|
||||
emac->sw_reset_timeout_ms = mac_config->sw_reset_timeout_ms;
|
||||
emac->int_gpio_num = dm9051_config->int_gpio_num;
|
||||
emac->poll_period_ms = dm9051_config->poll_period_ms;
|
||||
emac->parent.set_mediator = emac_dm9051_set_mediator;
|
||||
emac->parent.init = emac_dm9051_init;
|
||||
emac->parent.deinit = emac_dm9051_deinit;
|
||||
@@ -937,10 +971,23 @@ esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config,
|
||||
emac->rx_buffer = heap_caps_malloc(ETH_MAX_PACKET_SIZE + DM9051_RX_HDR_SIZE, MALLOC_CAP_DMA);
|
||||
ESP_GOTO_ON_FALSE(emac->rx_buffer, NULL, err, TAG, "RX buffer allocation failed");
|
||||
|
||||
if (emac->int_gpio_num < 0) {
|
||||
const esp_timer_create_args_t poll_timer_args = {
|
||||
.callback = dm9051_poll_timer,
|
||||
.name = "emac_spi_poll_timer",
|
||||
.arg = emac,
|
||||
.skip_unhandled_events = true
|
||||
};
|
||||
ESP_GOTO_ON_FALSE(esp_timer_create(&poll_timer_args, &emac->poll_timer) == ESP_OK, NULL, err, TAG, "create poll timer failed");
|
||||
}
|
||||
|
||||
return &(emac->parent);
|
||||
|
||||
err:
|
||||
if (emac) {
|
||||
if (emac->poll_timer) {
|
||||
esp_timer_delete(emac->poll_timer);
|
||||
}
|
||||
if (emac->rx_task_hdl) {
|
||||
vTaskDelete(emac->rx_task_hdl);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
* SPDX-FileContributor: 2021-2023 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileContributor: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_eth_driver.h"
|
||||
#include "ksz8851.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
|
||||
#define KSZ8851_ETH_MAC_RX_BUF_SIZE_AUTO (0)
|
||||
@@ -42,6 +43,8 @@ typedef struct {
|
||||
TaskHandle_t rx_task_hdl;
|
||||
uint32_t sw_reset_timeout_ms;
|
||||
int int_gpio_num;
|
||||
esp_timer_handle_t poll_timer;
|
||||
uint32_t poll_period_ms;
|
||||
uint8_t *rx_buffer;
|
||||
uint8_t *tx_buffer;
|
||||
} emac_ksz8851snl_t;
|
||||
@@ -85,6 +88,12 @@ IRAM_ATTR static void ksz8851_isr_handler(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
static void ksz8851_poll_timer(void *arg)
|
||||
{
|
||||
emac_ksz8851snl_t *emac = (emac_ksz8851snl_t *)arg;
|
||||
xTaskNotifyGive(emac->rx_task_hdl);
|
||||
}
|
||||
|
||||
static void *ksz8851_spi_init(const void *spi_config)
|
||||
{
|
||||
void *ret = NULL;
|
||||
@@ -317,12 +326,14 @@ static esp_err_t emac_ksz8851_init(esp_eth_mac_t *mac)
|
||||
esp_err_t ret = ESP_OK;
|
||||
emac_ksz8851snl_t *emac = __containerof(mac, emac_ksz8851snl_t, parent);
|
||||
esp_eth_mediator_t *eth = emac->eth;
|
||||
esp_rom_gpio_pad_select_gpio(emac->int_gpio_num);
|
||||
gpio_set_direction(emac->int_gpio_num, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(emac->int_gpio_num, GPIO_PULLUP_ONLY);
|
||||
gpio_set_intr_type(emac->int_gpio_num, GPIO_INTR_NEGEDGE); // NOTE(v.chistyakov): active low
|
||||
gpio_intr_enable(emac->int_gpio_num);
|
||||
gpio_isr_handler_add(emac->int_gpio_num, ksz8851_isr_handler, emac);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
esp_rom_gpio_pad_select_gpio(emac->int_gpio_num);
|
||||
gpio_set_direction(emac->int_gpio_num, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(emac->int_gpio_num, GPIO_PULLUP_ONLY);
|
||||
gpio_set_intr_type(emac->int_gpio_num, GPIO_INTR_NEGEDGE); // NOTE(v.chistyakov): active low
|
||||
gpio_intr_enable(emac->int_gpio_num);
|
||||
gpio_isr_handler_add(emac->int_gpio_num, ksz8851_isr_handler, emac);
|
||||
}
|
||||
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LLINIT, NULL), err, TAG, "lowlevel init failed");
|
||||
|
||||
// NOTE(v.chistyakov): soft reset
|
||||
@@ -338,8 +349,10 @@ static esp_err_t emac_ksz8851_init(esp_eth_mac_t *mac)
|
||||
return ESP_OK;
|
||||
err:
|
||||
ESP_LOGD(TAG, "MAC initialization failed");
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
}
|
||||
eth->on_state_changed(eth, ETH_STATE_DEINIT, NULL);
|
||||
return ret;
|
||||
}
|
||||
@@ -349,8 +362,13 @@ static esp_err_t emac_ksz8851_deinit(esp_eth_mac_t *mac)
|
||||
emac_ksz8851snl_t *emac = __containerof(mac, emac_ksz8851snl_t, parent);
|
||||
esp_eth_mediator_t *eth = emac->eth;
|
||||
mac->stop(mac);
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
}
|
||||
if (emac->poll_timer && esp_timer_is_active(emac->poll_timer)) {
|
||||
esp_timer_stop(emac->poll_timer);
|
||||
}
|
||||
eth->on_state_changed(eth, ETH_STATE_DEINIT, NULL);
|
||||
ESP_LOGD(TAG, "MAC deinitialized");
|
||||
return ESP_OK;
|
||||
@@ -626,13 +644,22 @@ err:
|
||||
static esp_err_t emac_ksz8851_set_link(esp_eth_mac_t *mac, eth_link_t link)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
emac_ksz8851snl_t *emac = __containerof(mac, emac_ksz8851snl_t, parent);
|
||||
switch (link) {
|
||||
case ETH_LINK_UP:
|
||||
ESP_GOTO_ON_ERROR(mac->start(mac), err, TAG, "ksz8851 start failed");
|
||||
if (emac->poll_timer) {
|
||||
ESP_GOTO_ON_ERROR(esp_timer_start_periodic(emac->poll_timer, emac->poll_period_ms * 1000),
|
||||
err, TAG, "start poll timer failed");
|
||||
}
|
||||
ESP_LOGD(TAG, "link is up");
|
||||
break;
|
||||
case ETH_LINK_DOWN:
|
||||
ESP_GOTO_ON_ERROR(mac->stop(mac), err, TAG, "ksz8851 stop failed");
|
||||
if (emac->poll_timer) {
|
||||
ESP_GOTO_ON_ERROR(esp_timer_stop(emac->poll_timer),
|
||||
err, TAG, "stop poll timer failed");
|
||||
}
|
||||
ESP_LOGD(TAG, "link is down");
|
||||
break;
|
||||
default: ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "unknown link status"); break;
|
||||
@@ -690,7 +717,14 @@ static void emac_ksz8851snl_task(void *arg)
|
||||
{
|
||||
emac_ksz8851snl_t *emac = (emac_ksz8851snl_t *)arg;
|
||||
while (1) {
|
||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||
if (emac->int_gpio_num >= 0) { // if in interrupt mode
|
||||
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ...
|
||||
gpio_get_level(emac->int_gpio_num) != 0) { // ...and no interrupt asserted
|
||||
continue; // -> just continue to check again
|
||||
}
|
||||
} else {
|
||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||
}
|
||||
|
||||
uint16_t interrupt_status;
|
||||
ksz8851_read_reg(emac, KSZ8851_ISR, &interrupt_status);
|
||||
@@ -778,6 +812,9 @@ static void emac_ksz8851snl_task(void *arg)
|
||||
static esp_err_t emac_ksz8851_del(esp_eth_mac_t *mac)
|
||||
{
|
||||
emac_ksz8851snl_t *emac = __containerof(mac, emac_ksz8851snl_t, parent);
|
||||
if (emac->poll_timer) {
|
||||
esp_timer_delete(emac->poll_timer);
|
||||
}
|
||||
vTaskDelete(emac->rx_task_hdl);
|
||||
emac->spi.deinit(emac->spi.ctx);
|
||||
vSemaphoreDelete(emac->spi_lock);
|
||||
@@ -794,13 +831,14 @@ esp_eth_mac_t *esp_eth_mac_new_ksz8851snl(const eth_ksz8851snl_config_t *ksz8851
|
||||
emac_ksz8851snl_t *emac = NULL;
|
||||
|
||||
ESP_GOTO_ON_FALSE(ksz8851snl_config && mac_config, NULL, err, TAG, "arguments can not be null");
|
||||
ESP_GOTO_ON_FALSE(ksz8851snl_config->int_gpio_num >= 0, NULL, err, TAG, "invalid interrupt gpio number");
|
||||
ESP_GOTO_ON_FALSE((ksz8851snl_config->int_gpio_num >= 0) != (ksz8851snl_config->poll_period_ms > 0), NULL, err, TAG, "invalid configuration argument combination");
|
||||
|
||||
emac = calloc(1, sizeof(emac_ksz8851snl_t));
|
||||
ESP_GOTO_ON_FALSE(emac, NULL, err, TAG, "no mem for MAC instance");
|
||||
|
||||
emac->sw_reset_timeout_ms = mac_config->sw_reset_timeout_ms;
|
||||
emac->int_gpio_num = ksz8851snl_config->int_gpio_num;
|
||||
emac->poll_period_ms = ksz8851snl_config->poll_period_ms;
|
||||
emac->parent.set_mediator = emac_ksz8851_set_mediator;
|
||||
emac->parent.init = emac_ksz8851_init;
|
||||
emac->parent.deinit = emac_ksz8851_deinit;
|
||||
@@ -856,10 +894,24 @@ esp_eth_mac_t *esp_eth_mac_new_ksz8851snl(const eth_ksz8851snl_config_t *ksz8851
|
||||
BaseType_t xReturned = xTaskCreatePinnedToCore(emac_ksz8851snl_task, "ksz8851snl_tsk", mac_config->rx_task_stack_size,
|
||||
emac, mac_config->rx_task_prio, &emac->rx_task_hdl, core_num);
|
||||
ESP_GOTO_ON_FALSE(xReturned == pdPASS, NULL, err, TAG, "create ksz8851 task failed");
|
||||
|
||||
if (emac->int_gpio_num < 0) {
|
||||
const esp_timer_create_args_t poll_timer_args = {
|
||||
.callback = ksz8851_poll_timer,
|
||||
.name = "emac_spi_poll_timer",
|
||||
.arg = emac,
|
||||
.skip_unhandled_events = true
|
||||
};
|
||||
ESP_GOTO_ON_FALSE(esp_timer_create(&poll_timer_args, &emac->poll_timer) == ESP_OK, NULL, err, TAG, "create poll timer failed");
|
||||
}
|
||||
|
||||
return &(emac->parent);
|
||||
|
||||
err:
|
||||
if (emac) {
|
||||
if (emac->poll_timer) {
|
||||
esp_timer_delete(emac->poll_timer);
|
||||
}
|
||||
if (emac->rx_task_hdl) {
|
||||
vTaskDelete(emac->rx_task_hdl);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_rom_gpio.h"
|
||||
#include "esp_cpu.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
@@ -57,6 +58,8 @@ typedef struct {
|
||||
TaskHandle_t rx_task_hdl;
|
||||
uint32_t sw_reset_timeout_ms;
|
||||
int int_gpio_num;
|
||||
esp_timer_handle_t poll_timer;
|
||||
uint32_t poll_period_ms;
|
||||
uint8_t addr[6];
|
||||
bool packets_remain;
|
||||
uint8_t *rx_buffer;
|
||||
@@ -456,14 +459,23 @@ err:
|
||||
static esp_err_t emac_w5500_set_link(esp_eth_mac_t *mac, eth_link_t link)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
|
||||
switch (link) {
|
||||
case ETH_LINK_UP:
|
||||
ESP_LOGD(TAG, "link is up");
|
||||
ESP_GOTO_ON_ERROR(mac->start(mac), err, TAG, "w5500 start failed");
|
||||
if (emac->poll_timer) {
|
||||
ESP_GOTO_ON_ERROR(esp_timer_start_periodic(emac->poll_timer, emac->poll_period_ms * 1000),
|
||||
err, TAG, "start poll timer failed");
|
||||
}
|
||||
break;
|
||||
case ETH_LINK_DOWN:
|
||||
ESP_LOGD(TAG, "link is down");
|
||||
ESP_GOTO_ON_ERROR(mac->stop(mac), err, TAG, "w5500 stop failed");
|
||||
if (emac->poll_timer) {
|
||||
ESP_GOTO_ON_ERROR(esp_timer_stop(emac->poll_timer),
|
||||
err, TAG, "stop poll timer failed");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "unknown link status");
|
||||
@@ -724,6 +736,12 @@ IRAM_ATTR static void w5500_isr_handler(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
static void w5500_poll_timer(void *arg)
|
||||
{
|
||||
emac_w5500_t *emac = (emac_w5500_t *)arg;
|
||||
xTaskNotifyGive(emac->rx_task_hdl);
|
||||
}
|
||||
|
||||
static void emac_w5500_task(void *arg)
|
||||
{
|
||||
emac_w5500_t *emac = (emac_w5500_t *)arg;
|
||||
@@ -733,9 +751,13 @@ static void emac_w5500_task(void *arg)
|
||||
uint32_t buf_len = 0;
|
||||
while (1) {
|
||||
/* check if the task receives any notification */
|
||||
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ...
|
||||
gpio_get_level(emac->int_gpio_num) != 0) { // ...and no interrupt asserted
|
||||
continue; // -> just continue to check again
|
||||
if (emac->int_gpio_num >= 0) { // if in interrupt mode
|
||||
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ...
|
||||
gpio_get_level(emac->int_gpio_num) != 0) { // ...and no interrupt asserted
|
||||
continue; // -> just continue to check again
|
||||
}
|
||||
} else {
|
||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||
}
|
||||
/* read interrupt status */
|
||||
w5500_read(emac, W5500_REG_SOCK_IR(0), &status, sizeof(status));
|
||||
@@ -782,12 +804,14 @@ static esp_err_t emac_w5500_init(esp_eth_mac_t *mac)
|
||||
esp_err_t ret = ESP_OK;
|
||||
emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
|
||||
esp_eth_mediator_t *eth = emac->eth;
|
||||
esp_rom_gpio_pad_select_gpio(emac->int_gpio_num);
|
||||
gpio_set_direction(emac->int_gpio_num, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(emac->int_gpio_num, GPIO_PULLUP_ONLY);
|
||||
gpio_set_intr_type(emac->int_gpio_num, GPIO_INTR_NEGEDGE); // active low
|
||||
gpio_intr_enable(emac->int_gpio_num);
|
||||
gpio_isr_handler_add(emac->int_gpio_num, w5500_isr_handler, emac);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
esp_rom_gpio_pad_select_gpio(emac->int_gpio_num);
|
||||
gpio_set_direction(emac->int_gpio_num, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(emac->int_gpio_num, GPIO_PULLUP_ONLY);
|
||||
gpio_set_intr_type(emac->int_gpio_num, GPIO_INTR_NEGEDGE); // active low
|
||||
gpio_intr_enable(emac->int_gpio_num);
|
||||
gpio_isr_handler_add(emac->int_gpio_num, w5500_isr_handler, emac);
|
||||
}
|
||||
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LLINIT, NULL), err, TAG, "lowlevel init failed");
|
||||
/* reset w5500 */
|
||||
ESP_GOTO_ON_ERROR(w5500_reset(emac), err, TAG, "reset w5500 failed");
|
||||
@@ -797,8 +821,10 @@ static esp_err_t emac_w5500_init(esp_eth_mac_t *mac)
|
||||
ESP_GOTO_ON_ERROR(w5500_setup_default(emac), err, TAG, "w5500 default setup failed");
|
||||
return ESP_OK;
|
||||
err:
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
}
|
||||
eth->on_state_changed(eth, ETH_STATE_DEINIT, NULL);
|
||||
return ret;
|
||||
}
|
||||
@@ -808,8 +834,13 @@ static esp_err_t emac_w5500_deinit(esp_eth_mac_t *mac)
|
||||
emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
|
||||
esp_eth_mediator_t *eth = emac->eth;
|
||||
mac->stop(mac);
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
if (emac->int_gpio_num >= 0) {
|
||||
gpio_isr_handler_remove(emac->int_gpio_num);
|
||||
gpio_reset_pin(emac->int_gpio_num);
|
||||
}
|
||||
if (emac->poll_timer && esp_timer_is_active(emac->poll_timer)) {
|
||||
esp_timer_stop(emac->poll_timer);
|
||||
}
|
||||
eth->on_state_changed(eth, ETH_STATE_DEINIT, NULL);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -817,6 +848,9 @@ static esp_err_t emac_w5500_deinit(esp_eth_mac_t *mac)
|
||||
static esp_err_t emac_w5500_del(esp_eth_mac_t *mac)
|
||||
{
|
||||
emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent);
|
||||
if (emac->poll_timer) {
|
||||
esp_timer_delete(emac->poll_timer);
|
||||
}
|
||||
vTaskDelete(emac->rx_task_hdl);
|
||||
emac->spi.deinit(emac->spi.ctx);
|
||||
heap_caps_free(emac->rx_buffer);
|
||||
@@ -829,13 +863,13 @@ esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config, con
|
||||
esp_eth_mac_t *ret = NULL;
|
||||
emac_w5500_t *emac = NULL;
|
||||
ESP_GOTO_ON_FALSE(w5500_config && mac_config, NULL, err, TAG, "invalid argument");
|
||||
ESP_GOTO_ON_FALSE((w5500_config->int_gpio_num >= 0) != (w5500_config->poll_period_ms > 0), NULL, err, TAG, "invalid configuration argument combination");
|
||||
emac = calloc(1, sizeof(emac_w5500_t));
|
||||
ESP_GOTO_ON_FALSE(emac, NULL, err, TAG, "no mem for MAC instance");
|
||||
/* w5500 driver is interrupt driven */
|
||||
ESP_GOTO_ON_FALSE(w5500_config->int_gpio_num >= 0, NULL, err, TAG, "invalid interrupt gpio number");
|
||||
/* bind methods and attributes */
|
||||
emac->sw_reset_timeout_ms = mac_config->sw_reset_timeout_ms;
|
||||
emac->int_gpio_num = w5500_config->int_gpio_num;
|
||||
emac->poll_period_ms = w5500_config->poll_period_ms;
|
||||
emac->parent.set_mediator = emac_w5500_set_mediator;
|
||||
emac->parent.init = emac_w5500_init;
|
||||
emac->parent.deinit = emac_w5500_deinit;
|
||||
@@ -886,10 +920,23 @@ esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config, con
|
||||
emac->rx_buffer = heap_caps_malloc(ETH_MAX_PACKET_SIZE, MALLOC_CAP_DMA);
|
||||
ESP_GOTO_ON_FALSE(emac->rx_buffer, NULL, err, TAG, "RX buffer allocation failed");
|
||||
|
||||
if (emac->int_gpio_num < 0) {
|
||||
const esp_timer_create_args_t poll_timer_args = {
|
||||
.callback = w5500_poll_timer,
|
||||
.name = "emac_spi_poll_timer",
|
||||
.arg = emac,
|
||||
.skip_unhandled_events = true
|
||||
};
|
||||
ESP_GOTO_ON_FALSE(esp_timer_create(&poll_timer_args, &emac->poll_timer) == ESP_OK, NULL, err, TAG, "create poll timer failed");
|
||||
}
|
||||
|
||||
return &(emac->parent);
|
||||
|
||||
err:
|
||||
if (emac) {
|
||||
if (emac->poll_timer) {
|
||||
esp_timer_delete(emac->poll_timer);
|
||||
}
|
||||
if (emac->rx_task_hdl) {
|
||||
vTaskDelete(emac->rx_task_hdl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user