gfvalvo:
It's all part of the SFM (Sheer Fu**ing Magic) that is Arduino. I wouldn't spend too much time fretting about it. The only thing you really need to know is that if you start writing .cpp files then you must #include <Arduino.h> explicitly (either in the .cpp for via a .h header file).
I do not include the Arduino.h. I try my best to avoid using Arduino.h, when coding for the ESP32. It is not necessary to do and can result in operational slow downs and introduces errors.
For instance, using Analogread with an ESP32 is quite inaccurate over using the ESP32 AD API and printing with log_x is faster then printing with Serial.print.
An .h file without Arduino.h and wrote in the Arduino IDE.
#include <driver/spi_master.h>
#include "sdkconfig.h"
#include "esp_system.h"
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);
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 );
The cpp without Arduino.h
#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];
} // 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);
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 )
Its been working for over a year without Arduino.h.