I didn`t find where to use this function in my code (spi_device_polling_transmit).
I fixed the stack overflow by setting the stack size to LWIP -> tcp/ip -> 8190.
But still new problem:
Code: Select all
E (1266562) w5500.mac: w5500_write(75): spi transmit failed
E (1266562) w5500.mac: w5500_write_buffer(174): write TX buffer failed
E (1266562) w5500.mac: emac_w5500_transmit(478): write frame failed
E (1266722) w5500.mac: emac_w5500_read_phy_reg(335): read PHY register failed
E (1266722) w5500.phy: w5500_update_link_duplex_speed(69): read PHYCFG failed
E (1266722) w5500.phy: w5500_get_link(112): update link duplex speed failed
and
Code: Select all
E (1410042) w5500.mac: w5500_write(75): spi transmit failed
E (1410042) w5500.mac: w5500_write_buffer(174): write TX buffer failed
E (1410042) w5500.mac: emac_w5500_transmit(478): write frame failed
E (1410182) w5500.mac: w5500_get_tx_free_size(137): read TX FSR failed
E (1410182) w5500.mac: emac_w5500_transmit(472): get free size failed
E (1410232) w5500.mac: w5500_get_tx_free_size(137): read TX FSR failed
E (1410232) w5500.mac: emac_w5500_transmit(472): get free size failed
I found out what the problem was. It was the sd-card spi.
If I comment out the sd_card_mount() function, the problem disappears.
Why did the "spi transmit failed"?
I did not use the spi functions directly. I create a pointer to the spi bus and then pass it to the following components (w5500, sd card file system)
Is it possible that I have the spi peripheral configured incorrectly?
Or maybe I forgot to execute free (any malloc) on the http server somewhere?
- W5500 - SPI2_HOST and SPI_DMA_CH_AUTO
- SD-CARD - SPI3_HOST and SPI_DMA_CH_AUTO
Next, I try to make the functions clear:
Code snippet of main.c
Code: Select all
void app_main(void)
{
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
if (spiffs_mount() != ESP_OK)
{
ESP_LOGE(TAG, "spiffs_mount() error!!!");
return;
}
// Initialize sqlite
sqlite3_initialize();
sd_card_mount();
vTaskDelay(pdMS_TO_TICKS(200));
ESP_ERROR_CHECK(start_rest_server(WEB_PATH));
myW5500_connect();
}
Code snippet of w5500 mount
Code: Select all
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_netif.h"
#include "lwip/ip_addr.h"
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "driver/spi_master.h"
#include "myW5500.h"
#define ETH_SPI_MISO_GPIO 40
#define ETH_SPI_MOSI_GPIO 1
#define ETH_SPI_SCLK_GPIO 39
#define ETH_SPI_CS_GPIO 38
#define ETH_SPI_INTn_GPIO 2
#define ETH_SPI_PHY_RST_GPIO 42
#define ETH_SPI_PHY_ADDR 1
#define ETH_SPI_HOST SPI2_HOST
#define ETH_SPI_CLOCK_MHZ 35
static const char *TAG = "W5500_LOG --- ";
esp_netif_t *eth_netif_spi = NULL;
/** Event handler for Ethernet events */
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
uint8_t mac_addr[6] = {0};
/* we can get the ethernet driver handle from event data */
esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
switch (event_id)
{
case ETHERNET_EVENT_CONNECTED:
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
ESP_LOGI(TAG, "Ethernet Link Up");
ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
break;
case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet Link Down");
break;
case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started");
break;
case ETHERNET_EVENT_STOP:
ESP_LOGI(TAG, "Ethernet Stopped");
break;
default:
break;
}
}
/** Event handler for IP_EVENT_ETH_GOT_IP */
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
const esp_netif_ip_info_t *ip_info = &event->ip_info;
ESP_LOGI(TAG, "Ethernet Got IP Address");
ESP_LOGI(TAG, "~~~~~~~~~~~");
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");
}
void myW5500_connect()
{
/* ============================================================ */
// Create instance(s) of esp-netif for SPI Ethernet(s)
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
esp_netif_config.if_key = "[ETH_SPI_W5500]";
esp_netif_config.if_desc = "[eth]";
esp_netif_config.route_prio = 30;
esp_netif_config_t cfg_spi = {
.base = &esp_netif_config,
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH};
/* ============================================================ */
eth_netif_spi = esp_netif_new(&cfg_spi);
/* ============================================================ */
// Init MAC and PHY configs to default
eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
/* ============================================================ */
// Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
gpio_install_isr_service(0);
/* ============================================================ */
// Init SPI bus
spi_bus_config_t buscfg = {
.miso_io_num = ETH_SPI_MISO_GPIO,
.mosi_io_num = ETH_SPI_MOSI_GPIO,
.sclk_io_num = ETH_SPI_SCLK_GPIO,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
ESP_ERROR_CHECK(spi_bus_initialize(ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
/* ============================================================ */
// Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
spi_eth_module_config_t spi_eth_module_config;
spi_eth_module_config.spi_cs_gpio = ETH_SPI_CS_GPIO;
spi_eth_module_config.int_gpio = ETH_SPI_INTn_GPIO;
spi_eth_module_config.phy_reset_gpio = ETH_SPI_PHY_RST_GPIO;
spi_eth_module_config.phy_addr = ETH_SPI_PHY_ADDR;
// Configure SPI interface and Ethernet driver for specific SPI module
esp_eth_mac_t *mac_spi;
esp_eth_phy_t *phy_spi;
esp_eth_handle_t eth_handle_spi = NULL;
spi_device_interface_config_t spi_devcfg = {
.mode = 0,
.clock_speed_hz = ETH_SPI_CLOCK_MHZ * 1000 * 1000,
.queue_size = 20};
// Set SPI module Chip Select GPIO
spi_devcfg.spics_io_num = spi_eth_module_config.spi_cs_gpio;
// Set remaining GPIO numbers and configuration used by the SPI module
phy_config_spi.phy_addr = spi_eth_module_config.phy_addr;
phy_config_spi.reset_gpio_num = spi_eth_module_config.phy_reset_gpio;
/* ============================================================ */
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(SPI2_HOST, &spi_devcfg);
w5500_config.int_gpio_num = spi_eth_module_config.int_gpio;
mac_spi = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
phy_spi = esp_eth_phy_new_w5500(&phy_config_spi);
/* ============================================================ */
esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi, phy_spi);
ESP_ERROR_CHECK(esp_eth_driver_install(ð_config_spi, ð_handle_spi));
/* The SPI Ethernet MAC address */
ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi,
ETH_CMD_S_MAC_ADDR,
(uint8_t[]){0xAA, 0x77, 0x12, 0x33, 0x44, 0x56}));
// attach Ethernet driver to TCP/IP stack
ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi, esp_eth_new_netif_glue(eth_handle_spi)));
// Register user defined event handers
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
/* start Ethernet driver state machine */
ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi));
}
Code snippet of sd-card mount
Code: Select all
#include <stdio.h>
#include "sdmmc_cmd.h"
#include "esp_vfs_fat.h"
#include "sd_card_module.h"
static const char *TAG = "SD_CARD_MODULE >>> ";
sd_card_obj_t sd_card_obj;
esp_err_t sd_card_mount(void)
{
sd_card_obj.isMounted = 0;
esp_err_t ret;
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 1,
.allocation_unit_size = 16 * 1024,
.disk_status_check_enable = false};
sdmmc_card_t *card;
ESP_LOGI(TAG, "Initializing SD card");
ESP_LOGI(TAG, "Using SPI peripheral");
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = 5000;
host.slot = SPI3_HOST;
spi_bus_config_t bus_cfg = {
.mosi_io_num = PIN_NUM_MOSI,
.miso_io_num = PIN_NUM_MISO,
.sclk_io_num = PIN_NUM_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to initialize bus.");
sd_card_obj.isMounted = 0;
return ESP_FAIL;
}
// This initializes the slot without card detect (CD) and write protect (WP) signals.
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = PIN_NUM_CS;
slot_config.host_id = host.slot;
ret = esp_vfs_fat_sdspi_mount(SDCARD_MOUNT_POINT, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK)
{
sd_card_obj.isMounted = 0;
return ESP_FAIL;
}
ESP_LOGI(TAG, "\n\n --- SD-Card Successfully installed!\n\n");
sdmmc_card_print_info(stdout, card);
sd_card_obj.isMounted = 1;
return ESP_OK;
}