Use SpiManager for nRF, CMT and W5500
This commit is contained in:
parent
ece4520687
commit
5457db269c
@ -1,6 +1,7 @@
|
||||
#include "cmt_spi3.h"
|
||||
#include <Arduino.h>
|
||||
#include <driver/spi_master.h>
|
||||
#include <SpiManager.h>
|
||||
|
||||
SemaphoreHandle_t paramLock = NULL;
|
||||
#define SPI_PARAM_LOCK() \
|
||||
@ -8,17 +9,16 @@ SemaphoreHandle_t paramLock = NULL;
|
||||
} while (xSemaphoreTake(paramLock, portMAX_DELAY) != pdPASS)
|
||||
#define SPI_PARAM_UNLOCK() xSemaphoreGive(paramLock)
|
||||
|
||||
// for ESP32 this is the so-called HSPI
|
||||
// for ESP32-S2/S3/C3 this nomenclature does not really exist anymore,
|
||||
// it is simply the first externally usable hardware SPI master controller
|
||||
#define SPI_CMT SPI2_HOST
|
||||
|
||||
spi_device_handle_t spi_reg, spi_fifo;
|
||||
|
||||
void cmt_spi3_init(const int8_t pin_sdio, const int8_t pin_clk, const int8_t pin_cs, const int8_t pin_fcs, const int32_t spi_speed)
|
||||
{
|
||||
paramLock = xSemaphoreCreateMutex();
|
||||
|
||||
spi_host_device_t host_device;
|
||||
if (!SpiManagerInst.claim_bus(host_device))
|
||||
ESP_ERROR_CHECK(ESP_FAIL);
|
||||
|
||||
spi_bus_config_t buscfg = {
|
||||
.mosi_io_num = pin_sdio,
|
||||
.miso_io_num = -1, // single wire MOSI/MISO
|
||||
@ -33,7 +33,7 @@ void cmt_spi3_init(const int8_t pin_sdio, const int8_t pin_clk, const int8_t pin
|
||||
.flags = 0,
|
||||
.intr_flags = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(SPI_CMT, &buscfg, SPI_DMA_DISABLED));
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(host_device, &buscfg, SPI_DMA_DISABLED));
|
||||
|
||||
spi_device_interface_config_t devcfg = {
|
||||
.command_bits = 1,
|
||||
@ -51,7 +51,7 @@ void cmt_spi3_init(const int8_t pin_sdio, const int8_t pin_clk, const int8_t pin
|
||||
.pre_cb = nullptr,
|
||||
.post_cb = nullptr,
|
||||
};
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(SPI_CMT, &devcfg, &spi_reg));
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(host_device, &devcfg, &spi_reg));
|
||||
|
||||
// FiFo
|
||||
spi_device_interface_config_t devcfg2 = {
|
||||
@ -70,7 +70,7 @@ void cmt_spi3_init(const int8_t pin_sdio, const int8_t pin_clk, const int8_t pin
|
||||
.pre_cb = nullptr,
|
||||
.post_cb = nullptr,
|
||||
};
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(SPI_CMT, &devcfg2, &spi_fifo));
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(host_device, &devcfg2, &spi_fifo));
|
||||
}
|
||||
|
||||
void cmt_spi3_write(const uint8_t addr, const uint8_t data)
|
||||
|
||||
@ -8,20 +8,7 @@
|
||||
#include "PinMapping.h"
|
||||
#include "SunPosition.h"
|
||||
#include <Hoymiles.h>
|
||||
|
||||
// the NRF shall use the second externally usable HW SPI controller
|
||||
// for ESP32 that is the so-called VSPI, for ESP32-S2/S3 it is now called implicitly
|
||||
// HSPI, as it has shifted places for these chip generations
|
||||
// for all generations, this is equivalent to SPI3_HOST in the lower level driver
|
||||
// For ESP32-C2, the only externally usable HW SPI controller is SPI2, its signal names
|
||||
// being prefixed with FSPI.
|
||||
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
|
||||
#define SPI_NRF HSPI
|
||||
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||
#define SPI_NRF FSPI
|
||||
#else
|
||||
#define SPI_NRF VSPI
|
||||
#endif
|
||||
#include <SpiManager.h>
|
||||
|
||||
InverterSettingsClass InverterSettings;
|
||||
|
||||
@ -44,7 +31,10 @@ void InverterSettingsClass::init(Scheduler& scheduler)
|
||||
|
||||
if (PinMapping.isValidNrf24Config() || PinMapping.isValidCmt2300Config()) {
|
||||
if (PinMapping.isValidNrf24Config()) {
|
||||
SPIClass* spiClass = new SPIClass(SPI_NRF);
|
||||
auto spi_bus = SpiManagerInst.claim_bus_arduino();
|
||||
ESP_ERROR_CHECK(spi_bus ? ESP_OK : ESP_FAIL);
|
||||
|
||||
SPIClass* spiClass = new SPIClass(*spi_bus);
|
||||
spiClass->begin(pin.nrf24_clk, pin.nrf24_miso, pin.nrf24_mosi, pin.nrf24_cs);
|
||||
Hoymiles.initNRF(spiClass, pin.nrf24_en, pin.nrf24_irq);
|
||||
}
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
#include "W5500.h"
|
||||
|
||||
#include <SpiManager.h>
|
||||
|
||||
#include <driver/spi_master.h>
|
||||
|
||||
// Internal Arduino functions from WiFiGeneric
|
||||
@ -10,6 +12,10 @@ W5500::W5500(int8_t pin_mosi, int8_t pin_miso, int8_t pin_sclk, int8_t pin_cs, i
|
||||
eth_handle(nullptr),
|
||||
eth_netif(nullptr)
|
||||
{
|
||||
spi_host_device_t host_device;
|
||||
if (!SpiManagerInst.claim_bus(host_device))
|
||||
ESP_ERROR_CHECK(ESP_FAIL);
|
||||
|
||||
gpio_reset_pin(static_cast<gpio_num_t>(pin_rst));
|
||||
gpio_set_level(static_cast<gpio_num_t>(pin_rst), 0);
|
||||
gpio_set_direction(static_cast<gpio_num_t>(pin_rst), GPIO_MODE_OUTPUT);
|
||||
@ -40,7 +46,7 @@ W5500::W5500(int8_t pin_mosi, int8_t pin_miso, int8_t pin_sclk, int8_t pin_cs, i
|
||||
.intr_flags = 0,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &bus_config, SPI_DMA_CH_AUTO));
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(host_device, &bus_config, SPI_DMA_CH_AUTO));
|
||||
|
||||
spi_device_interface_config_t device_config {
|
||||
.command_bits = 16, // actually address phase
|
||||
@ -60,7 +66,7 @@ W5500::W5500(int8_t pin_mosi, int8_t pin_miso, int8_t pin_sclk, int8_t pin_cs, i
|
||||
};
|
||||
|
||||
spi_device_handle_t spi;
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(SPI3_HOST, &device_config, &spi));
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(host_device, &device_config, &spi));
|
||||
|
||||
// Reset sequence
|
||||
delayMicroseconds(500);
|
||||
|
||||
@ -26,12 +26,21 @@
|
||||
#include <LittleFS.h>
|
||||
#include <TaskScheduler.h>
|
||||
#include <esp_heap_caps.h>
|
||||
#include <SpiManager.h>
|
||||
|
||||
#include <driver/uart.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Move all dynamic allocations >512byte to psram (if available)
|
||||
heap_caps_malloc_extmem_enable(512);
|
||||
|
||||
// Initialize SpiManager
|
||||
SpiManagerInst.register_bus(SPI2_HOST);
|
||||
#if SOC_SPI_PERIPH_NUM > 2
|
||||
SpiManagerInst.register_bus(SPI3_HOST);
|
||||
#endif
|
||||
|
||||
// Initialize serial output
|
||||
Serial.begin(SERIAL_BAUDRATE);
|
||||
#if ARDUINO_USB_CDC_ON_BOOT
|
||||
|
||||
Loading…
Reference in New Issue
Block a user