ESP32 SPI To Wifi.client stops sending

Hello community,

i want to use a esp32 as wifi co processor for my stm32. the mcu are connected by spi and i try to send frames from the stm over the esp to a wifi client (pc with a java terminal) in th beginning its working fine and i get the frames correct but after a short time its stop sending.

the complete code is in the attachement

ty for your help

ESP32DMASPISlave.cpp (5.86 KB)

ESP32DMASPISlave.h (2.65 KB)

WiFiTelnetToSerial.ino (32.2 KB)

I am finding that the ESP32 can stop working with WiFi if there are any brownout conditions. Make sure that you have plenty of current in your power to the ESP32.

Before WiFI.connect(), issue a WiFi.disconnect()

You'll get 2 things. One thing is if connected, disconnect. Eh. The other thing is that a WiDi.disconnect() resets the WiFi stack to default.

The next thing, with the ESP32 are you using the ESP32 SPI API?

ESP32 SPI API example use .h

#include <driver/spi_master.h>
#include "sdkconfig.h"
#include "esp_system.h" //This inclusion configures the peripherals in the ESP system.
////////////////////////////////////
//
//#define MAGTYPE  true
//#define XGTYPE   false
//////////////////////////
///////////////////////////
//
////////////////////////////
 uint8_t GetLowBits();
 uint16_t GetHighBits();
 void fReadSPIdata16bits( spi_device_handle_t &h, int address );
 int fWriteSPIdata8bits( spi_device_handle_t &h, int address, int sendData );
 int fInitializeSPI_Devices( spi_device_handle_t &h, int csPin);
// spi_device_handle_t fInitializeSPI_Devices( int csPin);
int fInitializeSPI_Channel( int spiCLK, int spiMOSI, int spiMISO, spi_host_device_t SPI_Host, bool EnableDMA);
int fWriteSPIdata32bits( spi_device_handle_t &h, int _sendData0, int _sendData1, int _sendData2, int _sendData3 );
int fReadSPIdataXbits( spi_device_handle_t &h, int _readaddress, int *rxbuf, int rxlen );
int fWriteSPIdata8bits2( spi_device_handle_t &h, int _sendData );

.cpp file

#include "ESP32_SPI_API.h"
/////////////////////////////
///////////////////////////
uint8_t txData[2] = { };
int8_t rxData[25] = { };
uint8_t low;
uint8_t high;
//////
//////////////////////////////////
uint8_t GetLowBits()
{
  return low;
}
uint16_t GetHighBits()
{
  return high;
}
////////////////////////////////////////
int fInitializeSPI_Channel( int spiCLK, int spiMOSI, int spiMISO, spi_host_device_t SPI_Host, bool EnableDMA)
{
  esp_err_t intError;
  spi_bus_config_t bus_config = { };
  bus_config.sclk_io_num = spiCLK; // CLK
  bus_config.mosi_io_num = spiMOSI; // MOSI
  bus_config.miso_io_num = spiMISO; // MISO
  bus_config.quadwp_io_num = -1; // Not used
  bus_config.quadhd_io_num = -1; // Not used
  intError = spi_bus_initialize( HSPI_HOST, &bus_config, EnableDMA) ;
  return intError;
}
//////
int fInitializeSPI_Devices( spi_device_handle_t &h, int csPin)
{
  esp_err_t intError;
  spi_device_interface_config_t dev_config = { };  // initializes all field to 0
  dev_config.address_bits     = 0;
  dev_config.command_bits     = 0;
  dev_config.dummy_bits       = 0;
  dev_config.mode             = 3 ;
  dev_config.duty_cycle_pos   = 0;
  dev_config.cs_ena_posttrans = 0;
  dev_config.cs_ena_pretrans  = 0;
  dev_config.clock_speed_hz   = 5000000;
  dev_config.spics_io_num     = csPin;
  dev_config.flags            = 0;
  dev_config.queue_size       = 1;
  dev_config.pre_cb           = NULL;
  dev_config.post_cb          = NULL;
  spi_bus_add_device(HSPI_HOST, &dev_config, &h);
  return intError;
  // return h;
} // void fInitializeSPI_Devices()
///////////////////////////////////////////////////////////////
void fReadSPIdata16bits( spi_device_handle_t &h, int _address )
{
  uint8_t address = _address;
  esp_err_t intError;
  spi_transaction_t trans_desc;
  trans_desc = { };
  trans_desc.addr =  0;
  trans_desc.cmd = 0;
  trans_desc.flags = 0;
  trans_desc.length = (8 * 3); // total data bits
  trans_desc.tx_buffer = txData;
  trans_desc.rxlength = 8 * 2 ; // Number of bits NOT number of bytes
  trans_desc.rx_buffer = rxData;
  txData[0] = address | 0x80;
  intError = spi_device_transmit( h, &trans_desc);
  low = rxData[0]; high = rxData[1];
  //  if ( intError != 0 )
  //  {
  //    Serial.print( " WHO I am LSM9DS1. Transmitting error = ");
  //    Serial.println ( esp_err_to_name(intError) );
  //  }
} // void fSendSPI( uint8_t count, uint8_t address, uint8_t DataToSend)
////
int fWriteSPIdata8bits( spi_device_handle_t &h, int _address, int _sendData )
{
  uint8_t address =  _address;
  uint8_t sendData = _sendData;
  esp_err_t intError;
  spi_transaction_t trans_desc;
  trans_desc = { };
  trans_desc.addr =  0;
  trans_desc.cmd = 0;
  trans_desc.flags = 0;
  trans_desc.length = (8 * 2); // total data bits
  trans_desc.tx_buffer = txData;
  trans_desc.rxlength = 0 ; // Number of bits NOT number of bytes
  trans_desc.rx_buffer = NULL;
  txData[0] = address  & 0x7F;
  txData[1] = sendData;
  intError = spi_device_transmit( h, &trans_desc);
  //  //  if ( intError != 0 )
  //  //  {
  //  //    Serial.print( " LSM9DS1_REGISTER_CTRL_REG6_XL. Transmitting error = ");
  //  //    Serial.println ( esp_err_to_name(intError) );
  //  //  }
  return intError;
} // void fWriteSPIdata8bits(  spi_device_handle_t &h, uint8_t address, uint8_t sendData )
//
int fWriteSPIdata8bits2( spi_device_handle_t &h, int _sendData )
{
  rxData[0] = 0x00;
  // uint8_t address =  _address;
  uint8_t sendData = _sendData;
  esp_err_t intError;
  spi_transaction_t trans_desc;
  trans_desc = { };
  trans_desc.addr =  0;
  trans_desc.cmd = 0;
  trans_desc.flags = 0;
  trans_desc.length = (8 * 2); // total data bits
  trans_desc.tx_buffer = txData;
  trans_desc.rxlength = 8 ; // Number of bits NOT number of bytes
  trans_desc.rx_buffer = rxData;
  txData[0] = sendData;
  // txData[0] = address;
  // txData[1] = sendData;
  intError = spi_device_transmit( h, &trans_desc);
  if ( intError == 0 )
  {
    return rxData[0];
  }
  else
  {
  return intError;  
  }
} // void fWriteSPIdata8bits(  spi_device_handle_t &h, uint8_t address, uint8_t sendData )
////
int fWriteSPIdata32bits( spi_device_handle_t &h, int _sendData0, int _sendData1, int _sendData2, int _sendData3 )
{
  // uint8_t address =  _address;
  // uint8_t sendData = _sendData;
  esp_err_t intError;
  spi_transaction_t trans_desc;
  trans_desc = { };
  trans_desc.addr =  0;
  trans_desc.cmd = 0;
  trans_desc.flags = 0;
  trans_desc.length = (8 * 4); // total data bits
  trans_desc.tx_buffer = txData;
  trans_desc.rxlength = 0 ; // Number of bits NOT number of bytes
  trans_desc.rx_buffer = NULL;
  txData[0] = (uint8_t)_sendData0; // command bits
  txData[1] = (uint8_t)_sendData1; // lower bits
  txData[2] = (uint8_t)_sendData2; // higher bits
  txData[3] = (uint8_t)_sendData3; // address
  intError = spi_device_transmit( h, &trans_desc);
  return intError;
} // void fWriteSPIdata8bits(  spi_device_handle_t &h, uint8_t address, uint8_t sendData )
////
int fReadSPIdataXbits( spi_device_handle_t &h, int _readaddress, int *rxbuf, int rxlen )
{
  uint8_t address = _readaddress;
  int8_t rxBuf[rxlen] = {0};
  esp_err_t intError;
  spi_transaction_t trans_desc;
  trans_desc = { };
  trans_desc.addr =  0;
  trans_desc.cmd = 0;
  trans_desc.flags = 0;
  trans_desc.length = ( (8 * 1) + (8 * rxlen)); // total data bits
  trans_desc.tx_buffer = txData ;
  trans_desc.rxlength = 8 * rxlen ; // Number of bits NOT number of bytes
  trans_desc.rx_buffer = rxBuf;
  txData[0] = address;
  // txData[1] = 0x00;
  intError = spi_device_transmit( h, &trans_desc);
  for (int i = 0; i < rxlen; i++)
  {
    rxbuf[i] = rxBuf[i];
  }
 return intError;
} // int fReadSPIdataXbits( spi_device_handle_t &h, int _readaddress, int *rxbuf, int rxlen )

ESP32 SPI API

hi thank you for the answers

Chevelle:
I am finding that the ESP32 can stop working with WiFi if there are any brownout conditions. Make sure that you have plenty of current in your power to the ESP32.

its no disconnection problem the esp stays connected it just stop sending data if i disconnect the socket client and connect it again it starts sending some frames and stops again but the connection to the wifi router is still there

Idahowalker:
Before WiFI.connect(), issue a WiFi.disconnect()

You'll get 2 things. One thing is if connected, disconnect. Eh. The other thing is that a WiDi.disconnect() resets the WiFi stack to default.

The next thing, with the ESP32 are you using the ESP32 SPI API?

no im not using direct the esp spi api im using a own spi class which is using the arduino api(check the attached files) and i cant see any problems with the spi this is working well.

ok sorry @Idahowalker u r right its losing the wifi connection for a very short time but dont now why and how i can prevent it and if i reconnect the client(pc side) its losing than very fast the connection again someone got an idea whats happening there

tryed ur hint with the disconnect before conneect didnt helped still short connection lost

[D][WiFiGeneric.cpp:337] _eventCallback(): Event: 5 - STA_DISCONNECTED
11:20:25.629 -> [W][WiFiGeneric.cpp:353] _eventCallback(): Reason: 200 - BEACON_TIMEOUT
11:20:25.629 -> [E][WiFiClient.cpp:392] write(): fail on fd 56, errno: 113, "Software caused connection abort"
i getting this error and debug msg maybe someone can help me to prevent it

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.