Browse Source

fix SPI topics, not completely tested

pull/1626/head
lumapu 9 months ago
parent
commit
0afe0d7fc9
  1. 1
      .gitattributes
  2. 9
      src/hm/nrfHal.h
  3. 9
      src/hms/cmtHal.h
  4. 27
      src/network/AhoyEthernetSpi.h
  5. 9
      src/plugins/Display/epdHal.h
  6. 2
      src/utils/dbg.h
  7. 3
      src/utils/spiPatcher.cpp
  8. 36
      src/utils/spiPatcher.h

1
.gitattributes

@ -0,0 +1 @@
patches/GxEPD2_HAL.patch eol=crlf

9
src/hm/nrfHal.h

@ -17,9 +17,7 @@
class nrfHal: public RF24_hal, public SpiPatcherHandle { class nrfHal: public RF24_hal, public SpiPatcherHandle {
public: public:
nrfHal() { nrfHal() {}
mSpiPatcher = SpiPatcher::getInstance(SPI2_HOST);
}
void patch() override { void patch() override {
esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false); esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false);
@ -41,7 +39,8 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle {
mPinEn = static_cast<gpio_num_t>(en); mPinEn = static_cast<gpio_num_t>(en);
mSpiSpeed = speed; mSpiSpeed = speed;
mHostDevice = mSpiPatcher->getDevice(); mHostDevice = (14 == sclk) ? SPI2_HOST : SPI3_HOST;
mSpiPatcher = SpiPatcher::getInstance(mHostDevice);
gpio_reset_pin(mPinMosi); gpio_reset_pin(mPinMosi);
gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT); gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT);
@ -72,7 +71,7 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle {
.pre_cb = nullptr, .pre_cb = nullptr,
.post_cb = nullptr .post_cb = nullptr
}; };
ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg, &spi)); mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi);
release_spi(); release_spi();
gpio_reset_pin(mPinEn); gpio_reset_pin(mPinEn);

9
src/hms/cmtHal.h

@ -16,9 +16,7 @@
class cmtHal : public SpiPatcherHandle { class cmtHal : public SpiPatcherHandle {
public: public:
cmtHal() { cmtHal() {}
mSpiPatcher = SpiPatcher::getInstance(DEF_CMT_SPI_HOST);
}
void patch() override { void patch() override {
esp_rom_gpio_connect_out_signal(mPinSdio, spi_periph_signal[mHostDevice].spid_out, false, false); esp_rom_gpio_connect_out_signal(mPinSdio, spi_periph_signal[mHostDevice].spid_out, false, false);
@ -39,7 +37,8 @@ class cmtHal : public SpiPatcherHandle {
mPinFcs = static_cast<gpio_num_t>(fcs); mPinFcs = static_cast<gpio_num_t>(fcs);
mSpiSpeed = speed; mSpiSpeed = speed;
mHostDevice = mSpiPatcher->getDevice(); mHostDevice = (14 == clk) ? SPI2_HOST : SPI3_HOST;
mSpiPatcher = SpiPatcher::getInstance(mHostDevice);
gpio_reset_pin(mPinSdio); gpio_reset_pin(mPinSdio);
gpio_set_direction(mPinSdio, GPIO_MODE_INPUT_OUTPUT); gpio_set_direction(mPinSdio, GPIO_MODE_INPUT_OUTPUT);
@ -67,7 +66,7 @@ class cmtHal : public SpiPatcherHandle {
.pre_cb = nullptr, .pre_cb = nullptr,
.post_cb = nullptr .post_cb = nullptr
}; };
ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg_reg, &spi_reg)); mSpiPatcher->addDevice(mHostDevice, &devcfg_reg, &spi_reg);
release_spi(); release_spi();
gpio_reset_pin(mPinFcs); gpio_reset_pin(mPinFcs);

27
src/network/AhoyEthernetSpi.h

@ -12,7 +12,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <esp_netif.h> #include <esp_netif.h>
#include <WiFiGeneric.h> #include <WiFiGeneric.h>
#include <driver/spi_master.h> #include "../utils/spiPatcher.h"
// Functions from WiFiGeneric // Functions from WiFiGeneric
void tcpipInit(); void tcpipInit();
@ -44,23 +44,8 @@ class AhoyEthernetSpi {
gpio_reset_pin(static_cast<gpio_num_t>(pin_int)); gpio_reset_pin(static_cast<gpio_num_t>(pin_int));
gpio_set_pull_mode(static_cast<gpio_num_t>(pin_int), GPIO_PULLUP_ONLY); gpio_set_pull_mode(static_cast<gpio_num_t>(pin_int), GPIO_PULLUP_ONLY);
mHostDevice = (14 == pin_sclk) ? SPI2_HOST : SPI3_HOST;
spi_bus_config_t buscfg = { mSpiPatcher = SpiPatcher::getInstance(mHostDevice);
.mosi_io_num = pin_mosi,
.miso_io_num = pin_miso,
.sclk_io_num = pin_sclk,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.data4_io_num = -1,
.data5_io_num = -1,
.data6_io_num = -1,
.data7_io_num = -1,
.max_transfer_sz = 0, // uses default value internally
.flags = 0,
.intr_flags = 0
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
spi_device_interface_config_t devcfg = { spi_device_interface_config_t devcfg = {
.command_bits = 16, // actually address phase .command_bits = 16, // actually address phase
@ -79,8 +64,7 @@ class AhoyEthernetSpi {
.post_cb = nullptr .post_cb = nullptr
}; };
spi_device_handle_t spi; mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi);
ESP_ERROR_CHECK(spi_bus_add_device(SPI3_HOST, &devcfg, &spi));
// Reset sequence // Reset sequence
if(-1 != pin_rst) { if(-1 != pin_rst) {
@ -137,6 +121,9 @@ class AhoyEthernetSpi {
private: private:
esp_eth_handle_t eth_handle; esp_eth_handle_t eth_handle;
esp_netif_t *eth_netif; esp_netif_t *eth_netif;
spi_host_device_t mHostDevice;
spi_device_handle_t spi;
SpiPatcher *mSpiPatcher;
}; };
#endif /*__ETH_SPI_H__*/ #endif /*__ETH_SPI_H__*/

9
src/plugins/Display/epdHal.h

@ -15,9 +15,7 @@
class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle { class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle {
public: public:
epdHal() { epdHal() {}
mSpiPatcher = SpiPatcher::getInstance(SPI3_HOST);
}
void patch() override { void patch() override {
esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false); esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false);
@ -40,7 +38,8 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle {
mPinBusy = static_cast<gpio_num_t>(busy); mPinBusy = static_cast<gpio_num_t>(busy);
mSpiSpeed = speed; mSpiSpeed = speed;
mHostDevice = mSpiPatcher->getDevice(); mHostDevice = (14 == sclk) ? SPI2_HOST : SPI3_HOST;
mSpiPatcher = SpiPatcher::getInstance(mHostDevice);
gpio_reset_pin(mPinMosi); gpio_reset_pin(mPinMosi);
gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT); gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT);
@ -68,7 +67,7 @@ class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle {
.pre_cb = nullptr, .pre_cb = nullptr,
.post_cb = nullptr .post_cb = nullptr
}; };
ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg, &spi)); mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi);
release_spi(); release_spi();
if(GPIO_NUM_NC != mPinRst) { if(GPIO_NUM_NC != mPinRst) {

2
src/utils/dbg.h

@ -110,7 +110,7 @@
#if DEBUG_LEVEL >= DBG_ERROR #if DEBUG_LEVEL >= DBG_ERROR
#define PERR(str) DBGPRINT(F("E: ")); DBGPRINT(str); #define PERR(str) DBGPRINT(F("E: ")); DBGPRINT(str);
#define PERRLN(str) DBGPRINT(F("E: ")); DBGPRINTLN(str); #define PERRLN(str) DBGPRINT(F("E: ")); DBGPRINTLN(str); DSERIAL.flush();
#else #else
#define PERR(str) #define PERR(str)
#define PERRLN(str) #define PERRLN(str)

3
src/utils/spiPatcher.cpp

@ -5,5 +5,6 @@
#if defined(ESP32) #if defined(ESP32)
#include "spiPatcher.h" #include "spiPatcher.h"
SpiPatcher *SpiPatcher::mInstance = nullptr; SpiPatcher *SpiPatcher::InstanceHost2 = nullptr;
SpiPatcher *SpiPatcher::InstanceHost3 = nullptr;
#endif #endif

36
src/utils/spiPatcher.h

@ -9,6 +9,7 @@
#if defined(ESP32) #if defined(ESP32)
#include "dbg.h"
#include "spiPatcherHandle.h" #include "spiPatcherHandle.h"
#include <driver/spi_master.h> #include <driver/spi_master.h>
@ -17,7 +18,7 @@
class SpiPatcher { class SpiPatcher {
protected: protected:
explicit SpiPatcher(spi_host_device_t dev) : explicit SpiPatcher(spi_host_device_t dev) :
mHostDevice(dev), mCurHandle(nullptr) { mCurHandle(nullptr) {
// Use binary semaphore instead of mutex for performance reasons // Use binary semaphore instead of mutex for performance reasons
mutex = xSemaphoreCreateBinaryStatic(&mutex_buffer); mutex = xSemaphoreCreateBinaryStatic(&mutex_buffer);
xSemaphoreGive(mutex); xSemaphoreGive(mutex);
@ -36,23 +37,37 @@ class SpiPatcher {
.flags = 0, .flags = 0,
.intr_flags = 0 .intr_flags = 0
}; };
ESP_ERROR_CHECK(spi_bus_initialize(mHostDevice, &buscfg, SPI_DMA_DISABLED)); ESP_ERROR_CHECK(spi_bus_initialize(dev, &buscfg, SPI_DMA_DISABLED));
} }
public: public:
SpiPatcher(SpiPatcher &other) = delete; SpiPatcher(const SpiPatcher &other) = delete;
void operator=(const SpiPatcher &) = delete; void operator=(const SpiPatcher &) = delete;
static SpiPatcher* getInstance(spi_host_device_t dev) { static SpiPatcher* getInstance(spi_host_device_t dev) {
if(nullptr == mInstance) if(SPI2_HOST == dev) {
mInstance = new SpiPatcher(dev); if(nullptr == InstanceHost2)
return mInstance; InstanceHost2 = new SpiPatcher(dev);
return InstanceHost2;
} else { // SPI3_HOST
if(nullptr == InstanceHost3)
InstanceHost3 = new SpiPatcher(dev);
return InstanceHost3;
}
} }
~SpiPatcher() { vSemaphoreDelete(mutex); } ~SpiPatcher() { vSemaphoreDelete(mutex); }
spi_host_device_t getDevice() { inline void addDevice(spi_host_device_t host_id, const spi_device_interface_config_t *dev_config, spi_device_handle_t *handle) {
return mHostDevice; if(SPI2_HOST == host_id)
mHost2Cnt++;
if(SPI3_HOST == host_id)
mHost3Cnt++;
if((mHost2Cnt > 3) || (mHost3Cnt > 3))
DPRINTLN(DBG_ERROR, F("maximum number of SPI devices reached (3)"));
ESP_ERROR_CHECK(spi_bus_add_device(host_id, dev_config, handle));
} }
inline void request(SpiPatcherHandle* handle) { inline void request(SpiPatcherHandle* handle) {
@ -74,13 +89,14 @@ class SpiPatcher {
} }
protected: protected:
static SpiPatcher *mInstance; static SpiPatcher *InstanceHost2;
static SpiPatcher *InstanceHost3;
private: private:
const spi_host_device_t mHostDevice;
SpiPatcherHandle* mCurHandle; SpiPatcherHandle* mCurHandle;
SemaphoreHandle_t mutex; SemaphoreHandle_t mutex;
StaticSemaphore_t mutex_buffer; StaticSemaphore_t mutex_buffer;
uint8_t mHost2Cnt = 0, mHost3Cnt = 0;
}; };
#endif /*ESP32*/ #endif /*ESP32*/

Loading…
Cancel
Save