ZinggJM
February 17, 2019, 6:01pm
1
pins_arduino.h and inking on the board pins differs from (all?) other ESP32 boards.
And users of this board have problems using it with GxEPD or GxEPD2, that use HW SPI.
It is unclear to me, if the ESP32 SPIClass uses SW SPI in this case, or how this difference can be explained.
Any help is appreciated!
See also: Waveshare e-paper displays with SPI #1137
ZinggJM
February 18, 2019, 4:10pm
2
Ok, the ESP32 is more flexible than I thought. In document esp32_datasheet_en.pdf on page 42:
Table GPIO Matrix is for the GPIO-Matrix. The signals of the on-chip functional modules can
be mapped onto any GPIO pin. Some signals can be mapped onto a pin by both IO-MUX
and GPIO-Matrix, as shown in the column tagged as “Same input signal from IO_MUX core”
in Table GPIO Matrix.
The SPIClass for ESP32 supports this flexibility.
Dear I want to use SPI with my ESP 32
Can you please suggest how use and initialize SPI in ESP 32
To use the SPI API with the ESP32 seehttps://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/spi_master.html , SPI Slave Driver - ESP32 - — ESP-IDF Programming Guide latest documentation , and SPI Master driver — ESP-IDF Programming Guide 文档 . Whiles there are 2 SPI Master driver links they contain different information.
If you can get Kolbans notes Kolban's book on ESP32 by Neil Kolban [Leanpub PDF/iPad/Kindle] , though the notes are for an older version of the SPI API the example is quite useful.
Here is my cpp file for the ESP32 SPI api:
#include "myESP32_SPI.h"
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
ESP32_SPI_API::ESP32_SPI_API() {}
ESP32_SPI_API::ESP32_SPI_API( int q_size )
{
qSize = q_size;
}
ESP32_SPI_API::ESP32_SPI_API( int q_size, int _clk_speed, int cs_gpio_pin, int mosi_gpio_pin, int miso_gpio_pin, int clk_gpio_pin, bool dma_enable, bool hspi )
{
qSize = q_size;
clock_speed = _clk_speed;
spi_csPin = cs_gpio_pin;
spi_MOSI_pin = mosi_gpio_pin;
spi_MISO_pin = miso_gpio_pin;
spi_CLK_pin = clk_gpio_pin;
enableDMA = dma_enable;
HSPI_host = hspi;
}
//////////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setClockSpeed( int _clk_speed )
{
clock_speed = _clk_speed;
}
////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setCS_pin( int _gpio_pin )
{
spi_csPin = _gpio_pin;
}
////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setMOSI_pin( int _gpio_pin )
{
spi_MOSI_pin = _gpio_pin;
}
////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setMISO_pin( int _gpio_pin )
{
spi_MISO_pin = _gpio_pin;
}
////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setEnable_DMA( bool _enable )
{
enableDMA = _enable;
}
////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setSPIclkPIN( int _gpio_pin )
{
spi_CLK_pin = _gpio_pin;
}
//////////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setHost_VSPI( )
{
HSPI_host = false;
}
//////////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setHost_HSPI( )
{
HSPI_host = true;
}
//////////////////////////////////////////////////////////////////////////////////////////
void ESP32_SPI_API::setQSize( int _size )
{
qSize = _size;
}
////////////////////////////////////////////////////////////////////////////////////////
int ESP32_SPI_API::fReadSPI( int byteReadSize, int addressToRead )
{
spi_transaction_t trans_desc;
trans_desc = { };
trans_desc.addr = 0;
trans_desc.cmd = 0;
trans_desc.flags = 0;
trans_desc.length = (8 * 2) + (8 * byteReadSize) ; // total data bits
trans_desc.tx_buffer = txData;
trans_desc.rxlength = byteReadSize * 8 ; // Number of bits NOT number of bytes
trans_desc.rx_buffer = rxData;
txData[0] = (uint8_t)addressToRead;
txData[1] = 0x0;
return spi_device_transmit( hSPI, &trans_desc);
} // int fReadSPI( int byteReadSize, int addressToRead )
////////////////////////////////////////////////////////////////////////////////////////
int ESP32_SPI_API::get_rxData8bit( int _bit )
{
int8_t temp;
temp = rxData[_bit];
return temp;
}
//////////////////////////////////////////////////////////////////////////////////
//// for _bit use low bit
int ESP32_SPI_API::get_rxData16bit( int _bit )
{
int16_t temp;
temp = rxData[ _bit] << 8;
temp |= rxData[ _bit + 1];
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////
int ESP32_SPI_API::fWriteSPIdata8bits( int _address, int _DataToSend)
{
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;
txData[0] = (uint8_t)_address;
txData[1] = (uint8_t)_DataToSend;
return spi_device_transmit( hSPI, &trans_desc);
} // void fSendSPI( uint8_t count, uint8_t address, uint8_t DataToSend)
/////////////////////////////////////////////////////////////////////////////////////////////////////
int ESP32_SPI_API::fSPIdeviceConfig( )
{
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 = clock_speed;
dev_config.spics_io_num = spi_csPin;
dev_config.flags = 0;
dev_config.queue_size = qSize;
dev_config.pre_cb = NULL;
dev_config.post_cb = NULL;
if ( HSPI_host )
{
return spi_bus_add_device( HSPI_HOST, &dev_config, &hSPI );
}
else
{
return spi_bus_add_device( VSPI_HOST, &dev_config, &hSPI );
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
int ESP32_SPI_API::fSPIbusConfig( )
{
spi_bus_config_t bus_config = { };
bus_config.sclk_io_num = spi_CLK_pin; // CLK
bus_config.mosi_io_num = spi_MOSI_pin; // MOSI
bus_config.miso_io_num = spi_MISO_pin; // MISO
bus_config.quadwp_io_num = -1; // Not used
bus_config.quadhd_io_num = -1; // Not used
if ( HSPI_host )
{
return spi_bus_initialize( HSPI_HOST, &bus_config, enableDMA );
}
else
{
return spi_bus_initialize( VSPI_HOST, &bus_config, enableDMA );
}
} //int fSPIinit ( )
///////////////////////////////////////////////////////////////////////////////////////////////////
int ESP32_SPI_API::fSPIqueueupTransactions_read( int byteReadSize, int addressToRead )
{
spi_transaction_t trans_desc;
trans_desc = { };
trans_desc.addr = 0;
trans_desc.cmd = 0;
trans_desc.flags = 0;
trans_desc.length = (8 * 2) + (8 * byteReadSize) ; // total data bits
trans_desc.tx_buffer = txData;
trans_desc.rxlength = byteReadSize * 8 ; // Number of bits NOT number of bytes
trans_desc.rx_buffer = rxData;
txData[0] = (uint8_t)addressToRead;
txData[1] = 0x0;
return spi_device_queue_trans( hSPI, &trans_desc, 3 );
}
///////////////////////////////////////////////////////////////////////////////////////////////////
int ESP32_SPI_API::fSPIreadQueuedTransaction( int NumofTransactions )
{
// spi_transaction_t *trans_desc;
// trans_desc = { };
////Wait for all transactions to be done and get back the results.
// for (int x=0; x < NumofTransactions; x++) {
// spi_device_get_trans_result( hSPI, &trans_desc, 3 );
// //We could inspect rtrans now if we received any info back. The LCD is treated as write-only, though.
// }
//
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//int ESP32_SPI_API::fSPIqueueupTransactions_write8bits( int _address, int _DataToSend, int numOfTransactions)
//{
// 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;
// txData[0] = (uint8_t)_address;
// txData[1] = (uint8_t)_DataToSend;
//
//
// return spi_device_queue_trans( hSPI, &trans_desc, 3 );
//
//}
Here is my h file for using the SPI API:
#ifndef myESP32SPI
#define myESP32SPI
////
#include <driver/spi_master.h>
#include "sdkconfig.h"
// #include <driver/gpio.h>
////
////
class ESP32_SPI_API
{
public:
ESP32_SPI_API();
ESP32_SPI_API( int q_size );
ESP32_SPI_API( int q_size, int _clk_speed, int cs_gpio_pin, int mosi_gpio_pin, int miso_gpio_pin, int clk_gpio_pin, bool dma_enable, bool hspi );
void setHost_HSPI( );
void setHost_VSPI( );
int get_rxData8bit( int _bit );
int get_rxData16bit( int _bit );
int fSPIdeviceConfig( );
void setClockSpeed( int _clk_speed );
void setQSize( int _size );
void setCS_pin( int _gpio_pin );
void setMOSI_pin( int _gpio_pin );
void setMISO_pin( int _gpio_pin );
void setEnable_DMA( bool _enable );
void setSPIclkPIN( int _gpio_pin );
int fSPIbusConfig( );
// int fwriteSPIregister( int addr, int sendData );
int fReadSPI( int byteReadSize, int addressToRead );
int fWriteSPIdata8bits( int _address, int _DataToSend);
int fSPIqueueupTransactions_read( int byteReadSize, int addressToRead );
int fSPIreadQueuedTransaction( int NumofTransactions );
private:
////
spi_device_handle_t hSPI;
bool HSPI_host = true; // HSPI_HOST = true, VSPI_HOST = false
int spi_csPin = 15;
int spi_MOSI_pin = 13;
int spi_MISO_pin = 12;
int spi_CLK_pin = 14;
bool enableDMA = true;
int qSize = 1;
int clock_speed = 1000000;
uint8_t txData[2] = { };
int8_t rxData[24] = { };
protected:
////
};
#endif
here is a code snippet to initialize the SPI API and set the device:
int myMPU9250::fInit_MPU9250( bool resetMPU9250, bool resetAK8962 )
{
SPI_Err = _spi.fSPIbusConfig( );
// if ( SPI_Err == 0 )
// {
SPI_Err = _spi.fSPIdeviceConfig( );