Skip to content

Commit

Permalink
Change cmt_spi3 implementation from C to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
LennartF22 committed Sep 24, 2024
1 parent 4364daf commit 1a583e7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
54 changes: 41 additions & 13 deletions lib/CMT2300a/cmt_spi3.c → lib/CMT2300a/cmt_spi3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SemaphoreHandle_t paramLock = NULL;

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 uint32_t spi_speed)
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();

Expand All @@ -25,24 +25,32 @@ void cmt_spi3_init(const int8_t pin_sdio, const int8_t pin_clk, const int8_t pin
.sclk_io_num = pin_clk,
.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 = 32,
.flags = 0,
.intr_flags = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI_CMT, &buscfg, SPI_DMA_DISABLED));

spi_device_interface_config_t devcfg = {
.command_bits = 1,
.address_bits = 7,
.dummy_bits = 0,
.mode = 0, // SPI mode 0
.duty_cycle_pos = 0,
.cs_ena_pretrans = 1,
.cs_ena_posttrans = 1,
.clock_speed_hz = spi_speed,
.input_delay_ns = 0,
.spics_io_num = pin_cs,
.flags = SPI_DEVICE_HALFDUPLEX | SPI_DEVICE_3WIRE,
.queue_size = 1,
.pre_cb = NULL,
.post_cb = NULL,
.pre_cb = nullptr,
.post_cb = nullptr,
};

ESP_ERROR_CHECK(spi_bus_initialize(SPI_CMT, &buscfg, SPI_DMA_DISABLED));
ESP_ERROR_CHECK(spi_bus_add_device(SPI_CMT, &devcfg, &spi_reg));

// FiFo
Expand All @@ -51,26 +59,31 @@ void cmt_spi3_init(const int8_t pin_sdio, const int8_t pin_clk, const int8_t pin
.address_bits = 0,
.dummy_bits = 0,
.mode = 0, // SPI mode 0
.duty_cycle_pos = 0,
.cs_ena_pretrans = 2,
.cs_ena_posttrans = (uint8_t)(2 * spi_speed / 1000000), // >2 us
.cs_ena_posttrans = static_cast<uint8_t>(2 * spi_speed / 1000000), // >2 us
.clock_speed_hz = spi_speed,
.input_delay_ns = 0,
.spics_io_num = pin_fcs,
.flags = SPI_DEVICE_HALFDUPLEX | SPI_DEVICE_3WIRE,
.queue_size = 1,
.pre_cb = NULL,
.post_cb = NULL,
.pre_cb = nullptr,
.post_cb = nullptr,
};
ESP_ERROR_CHECK(spi_bus_add_device(SPI_CMT, &devcfg2, &spi_fifo));
}

void cmt_spi3_write(const uint8_t addr, const uint8_t data)
{
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = addr,
.length = 8,
.rxlength = 0,
.user = nullptr,
.tx_buffer = &data,
.rx_buffer = NULL
.rx_buffer = nullptr,
};
SPI_PARAM_LOCK();
ESP_ERROR_CHECK(spi_device_polling_transmit(spi_reg, &t));
Expand All @@ -81,11 +94,14 @@ uint8_t cmt_spi3_read(const uint8_t addr)
{
uint8_t data;
spi_transaction_t t = {
.flags = 0,
.cmd = 1,
.addr = addr,
.length = 0,
.rxlength = 8,
.tx_buffer = NULL,
.rx_buffer = &data
.user = nullptr,
.tx_buffer = nullptr,
.rx_buffer = &data,
};
SPI_PARAM_LOCK();
ESP_ERROR_CHECK(spi_device_polling_transmit(spi_reg, &t));
Expand All @@ -96,8 +112,14 @@ uint8_t cmt_spi3_read(const uint8_t addr)
void cmt_spi3_write_fifo(const uint8_t* buf, const uint16_t len)
{
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = 0,
.length = 8,
.rx_buffer = NULL
.rxlength = 0,
.user = nullptr,
.tx_buffer = nullptr,
.rx_buffer = nullptr,
};

SPI_PARAM_LOCK();
Expand All @@ -113,8 +135,14 @@ void cmt_spi3_write_fifo(const uint8_t* buf, const uint16_t len)
void cmt_spi3_read_fifo(uint8_t* buf, const uint16_t len)
{
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = 0,
.length = 0,
.rxlength = 8,
.tx_buffer = NULL
.user = nullptr,
.tx_buffer = nullptr,
.rx_buffer = nullptr,
};

SPI_PARAM_LOCK();
Expand Down
10 changes: 9 additions & 1 deletion lib/CMT2300a/cmt_spi3.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@

#include <stdint.h>

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 uint32_t spi_speed);
#ifdef __cplusplus
extern "C" {
#endif

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);

void cmt_spi3_write(const uint8_t addr, const uint8_t dat);
uint8_t cmt_spi3_read(const uint8_t addr);

void cmt_spi3_write_fifo(const uint8_t* p_buf, const uint16_t len);
void cmt_spi3_read_fifo(uint8_t* p_buf, const uint16_t len);

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit 1a583e7

Please sign in to comment.