Files
OpenDTU/lib/CMT2300a/cmt_spi3.cpp

149 lines
3.9 KiB
C++
Raw Normal View History

2024-01-14 16:36:35 +01:00
#include "cmt_spi3.h"
#include <Arduino.h>
#include <driver/spi_master.h>
2024-09-21 21:45:50 +02:00
#include <SpiManager.h>
2024-01-14 16:36:35 +01:00
SemaphoreHandle_t paramLock = NULL;
#define SPI_PARAM_LOCK() \
do { \
} while (xSemaphoreTake(paramLock, portMAX_DELAY) != pdPASS)
#define SPI_PARAM_UNLOCK() xSemaphoreGive(paramLock)
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)
2024-01-14 16:36:35 +01:00
{
paramLock = xSemaphoreCreateMutex();
2024-09-21 22:06:10 +02:00
auto bus_config = std::make_shared<SpiBusConfig>(
static_cast<gpio_num_t>(pin_sdio),
GPIO_NUM_NC,
static_cast<gpio_num_t>(pin_clk)
);
2024-01-14 16:36:35 +01:00
spi_device_interface_config_t devcfg = {
.command_bits = 1,
.address_bits = 7,
.dummy_bits = 0,
.mode = 0, // SPI mode 0
.duty_cycle_pos = 0,
2024-01-14 16:36:35 +01:00
.cs_ena_pretrans = 1,
.cs_ena_posttrans = 1,
.clock_speed_hz = spi_speed,
.input_delay_ns = 0,
2024-01-14 16:36:35 +01:00
.spics_io_num = pin_cs,
.flags = SPI_DEVICE_HALFDUPLEX | SPI_DEVICE_3WIRE,
.queue_size = 1,
.pre_cb = nullptr,
.post_cb = nullptr,
2024-01-14 16:36:35 +01:00
};
2024-09-21 22:06:10 +02:00
spi_reg = SpiManagerInst.alloc_device("", bus_config, devcfg);
if (!spi_reg)
ESP_ERROR_CHECK(ESP_FAIL);
2024-01-14 16:36:35 +01:00
// FiFo
spi_device_interface_config_t devcfg2 = {
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0,
.mode = 0, // SPI mode 0
.duty_cycle_pos = 0,
2024-01-14 16:36:35 +01:00
.cs_ena_pretrans = 2,
.cs_ena_posttrans = static_cast<uint8_t>(2 * spi_speed / 1000000), // >2 us
2024-01-14 16:36:35 +01:00
.clock_speed_hz = spi_speed,
.input_delay_ns = 0,
2024-01-14 16:36:35 +01:00
.spics_io_num = pin_fcs,
.flags = SPI_DEVICE_HALFDUPLEX | SPI_DEVICE_3WIRE,
.queue_size = 1,
.pre_cb = nullptr,
.post_cb = nullptr,
2024-01-14 16:36:35 +01:00
};
2024-09-21 22:06:10 +02:00
spi_fifo = SpiManagerInst.alloc_device("", bus_config, devcfg2);
if (!spi_fifo)
ESP_ERROR_CHECK(ESP_ERR_NOT_SUPPORTED);
2024-01-14 16:36:35 +01:00
}
2023-07-03 23:47:37 +02:00
void cmt_spi3_write(const uint8_t addr, const uint8_t data)
2024-01-14 16:36:35 +01:00
{
spi_transaction_t t = {
.flags = 0,
2023-07-03 23:47:37 +02:00
.cmd = 0,
.addr = addr,
2024-01-14 16:36:35 +01:00
.length = 8,
.rxlength = 0,
.user = nullptr,
2023-07-03 23:47:37 +02:00
.tx_buffer = &data,
.rx_buffer = nullptr,
2024-01-14 16:36:35 +01:00
};
SPI_PARAM_LOCK();
ESP_ERROR_CHECK(spi_device_polling_transmit(spi_reg, &t));
SPI_PARAM_UNLOCK();
}
uint8_t cmt_spi3_read(const uint8_t addr)
{
2023-07-03 23:47:37 +02:00
uint8_t data;
2024-01-14 16:36:35 +01:00
spi_transaction_t t = {
.flags = 0,
2023-07-03 23:47:37 +02:00
.cmd = 1,
.addr = addr,
.length = 0,
2024-01-14 16:36:35 +01:00
.rxlength = 8,
.user = nullptr,
.tx_buffer = nullptr,
.rx_buffer = &data,
2024-01-14 16:36:35 +01:00
};
SPI_PARAM_LOCK();
ESP_ERROR_CHECK(spi_device_polling_transmit(spi_reg, &t));
SPI_PARAM_UNLOCK();
2023-07-03 23:47:37 +02:00
return data;
2024-01-14 16:36:35 +01:00
}
void cmt_spi3_write_fifo(const uint8_t* buf, const uint16_t len)
{
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = 0,
2024-01-14 16:36:35 +01:00
.length = 8,
.rxlength = 0,
.user = nullptr,
.tx_buffer = nullptr,
.rx_buffer = nullptr,
2024-01-14 16:36:35 +01:00
};
SPI_PARAM_LOCK();
2024-09-21 18:46:29 +02:00
spi_device_acquire_bus(spi_fifo, portMAX_DELAY);
2024-01-14 16:36:35 +01:00
for (uint8_t i = 0; i < len; i++) {
2023-07-03 23:47:37 +02:00
t.tx_buffer = buf + i;
2024-01-14 16:36:35 +01:00
ESP_ERROR_CHECK(spi_device_polling_transmit(spi_fifo, &t));
}
2024-09-21 18:46:29 +02:00
spi_device_release_bus(spi_fifo);
2024-01-14 16:36:35 +01:00
SPI_PARAM_UNLOCK();
}
void cmt_spi3_read_fifo(uint8_t* buf, const uint16_t len)
{
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = 0,
.length = 0,
2024-01-14 16:36:35 +01:00
.rxlength = 8,
.user = nullptr,
.tx_buffer = nullptr,
.rx_buffer = nullptr,
2024-01-14 16:36:35 +01:00
};
SPI_PARAM_LOCK();
2024-09-21 18:46:29 +02:00
spi_device_acquire_bus(spi_fifo, portMAX_DELAY);
2024-01-14 16:36:35 +01:00
for (uint8_t i = 0; i < len; i++) {
2023-07-03 23:47:37 +02:00
t.rx_buffer = buf + i;
2024-01-14 16:36:35 +01:00
ESP_ERROR_CHECK(spi_device_polling_transmit(spi_fifo, &t));
}
2024-09-21 18:46:29 +02:00
spi_device_release_bus(spi_fifo);
2024-01-14 16:36:35 +01:00
SPI_PARAM_UNLOCK();
}