Implemented Command-Level Interaction with CAN Interface
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "esp_implementation_init.cpp"
|
||||
"esp_implementation_cmd.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES driver)
|
||||
@@ -0,0 +1,201 @@
|
||||
#include "mcp2521_hardware_handle.hpp"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "mcp2521_hardware_esp.hpp"
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::execute(uint8_t cmd) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = 0,
|
||||
.length = 0,
|
||||
.rxlength = 0,
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = NULL
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::execute(uint8_t cmd, uint8_t address) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = address,
|
||||
.length = 0,
|
||||
.rxlength = 0,
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = NULL
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 8,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t *data, size_t length, uint8_t address) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = address,
|
||||
.length = 8*length,
|
||||
.rxlength = 8*length,
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = data
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t *data, size_t length) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = 0,
|
||||
.length = 8*length,
|
||||
.rxlength = 8*length,
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = data
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
uint8_t MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t address) {
|
||||
uint8_t result = 0;
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = address,
|
||||
.length = 8,
|
||||
.rxlength = 8,
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = &result
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 8,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t MCP2521_Hardware_Handle_ESP::read(uint8_t cmd) {
|
||||
uint8_t result;
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = 0,
|
||||
.length = 8,
|
||||
.rxlength = 8,
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = &result
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
return result;
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t *data, size_t length, uint8_t address) {
|
||||
uint8_t result;
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = address,
|
||||
.length = 8*length,
|
||||
.rxlength = 0,
|
||||
.tx_buffer = data,
|
||||
.rx_buffer = NULL
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 8,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t *data, size_t length) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = 0,
|
||||
.length = 8*length,
|
||||
.rxlength = 0,
|
||||
.tx_buffer = data,
|
||||
.rx_buffer = NULL
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t data, uint8_t address) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = address,
|
||||
.length = 8,
|
||||
.rxlength = 0,
|
||||
.tx_buffer = &data,
|
||||
.rx_buffer = NULL
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 8,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t data) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = cmd,
|
||||
.addr = 0,
|
||||
.length = 8,
|
||||
.rxlength = 0,
|
||||
.tx_buffer = &data,
|
||||
.rx_buffer = NULL
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,98 @@
|
||||
#include "mcp2521_hardware_handle.hpp"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "mcp2521_hardware_esp.hpp"
|
||||
#include <string.h>
|
||||
|
||||
MCP2521_Hardware_Handle_ESP::MCP2521_Hardware_Handle_ESP(
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t *bus_config,
|
||||
gpio_num_t mosi,
|
||||
gpio_num_t miso,
|
||||
gpio_num_t sclk,
|
||||
gpio_num_t cs,
|
||||
gpio_num_t int_pin
|
||||
) {
|
||||
initPins(int_pin);
|
||||
initSPIBus(spi_host, mosi, miso, sclk, bus_config);
|
||||
this->spi_bus_config = bus_config;
|
||||
initSPIDevice(spi_host, cs);
|
||||
}
|
||||
|
||||
MCP2521_Hardware_Handle_ESP::MCP2521_Hardware_Handle_ESP(
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t *bus_config,
|
||||
gpio_num_t cs,
|
||||
gpio_num_t int_pin
|
||||
) {
|
||||
initPins(int_pin);
|
||||
this->spi_bus_config = bus_config;
|
||||
initSPIDevice(spi_host, cs);
|
||||
}
|
||||
|
||||
MCP2521_Hardware_Handle_ESP::~MCP2521_Hardware_Handle_ESP() {
|
||||
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::initPins(
|
||||
gpio_num_t int_pin
|
||||
) {
|
||||
gpio_config_t io_conf;
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = 1 << int_pin;
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&io_conf);
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::initSPIBus(
|
||||
spi_host_device_t spi_host,
|
||||
gpio_num_t mosi,
|
||||
gpio_num_t miso,
|
||||
gpio_num_t sclk,
|
||||
spi_bus_config_t *bus_config
|
||||
) {
|
||||
memset(bus_config, 0, sizeof(spi_bus_config_t));
|
||||
bus_config->mosi_io_num = mosi;
|
||||
bus_config->miso_io_num = miso;
|
||||
bus_config->sclk_io_num = sclk;
|
||||
|
||||
bus_config->quadwp_io_num = -1;
|
||||
bus_config->quadhd_io_num = -1;
|
||||
|
||||
bus_config->flags = SPICOMMON_BUSFLAG_MASTER;
|
||||
spi_bus_initialize(spi_host, bus_config, SPI_DMA_CH_AUTO);
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::initSPIDevice(
|
||||
spi_host_device_t spi_host,
|
||||
gpio_num_t cs
|
||||
) {
|
||||
memset(&this->spi_device_config, 0, sizeof(spi_device_interface_config_t));
|
||||
this->spi_device_config = {
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.dummy_bits = 0,
|
||||
|
||||
.mode = 0,
|
||||
.duty_cycle_pos = 128,
|
||||
// cs_ena_pretrans = 0 and cs_ena_posttrans = 0 need to be set to zero, if not its not compatible with full-duplex mode
|
||||
// Learned this the hard way
|
||||
.cs_ena_pretrans = 0,
|
||||
.cs_ena_posttrans = 0,
|
||||
.clock_speed_hz = 10000,
|
||||
|
||||
.spics_io_num = cs,
|
||||
.flags = SPI_DEVICE_NO_DUMMY,
|
||||
.queue_size = 5,
|
||||
};
|
||||
|
||||
spi_bus_add_device(spi_host, &this->spi_device_config, &this->spi_device_handle);
|
||||
}
|
||||
|
||||
spi_bus_config_t * MCP2521_Hardware_Handle_ESP::getSPI_bus_config() {
|
||||
return this->spi_bus_config;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
#include "mcp2521_hardware_handle.hpp"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
|
||||
class MCP2521_Hardware_Handle_ESP : public MCP2521_Hardware_Handle {
|
||||
char spi_tmp_buffer;
|
||||
|
||||
spi_bus_config_t * spi_bus_config;
|
||||
spi_device_interface_config_t spi_device_config;
|
||||
spi_device_handle_t spi_device_handle;
|
||||
|
||||
public:
|
||||
MCP2521_Hardware_Handle_ESP(
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t *bus_config,
|
||||
gpio_num_t mosi,
|
||||
gpio_num_t miso,
|
||||
gpio_num_t sclk,
|
||||
gpio_num_t cs,
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
|
||||
MCP2521_Hardware_Handle_ESP(
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t *bus_config,
|
||||
gpio_num_t cs,
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
|
||||
static void initSPIBus(
|
||||
spi_host_device_t spi_host,
|
||||
gpio_num_t mosi,
|
||||
gpio_num_t miso,
|
||||
gpio_num_t sclk,
|
||||
spi_bus_config_t *bus_config
|
||||
);
|
||||
|
||||
void initSPIDevice(
|
||||
spi_host_device_t spi_host,
|
||||
gpio_num_t cs
|
||||
);
|
||||
|
||||
void initPins(
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
|
||||
~MCP2521_Hardware_Handle_ESP();
|
||||
|
||||
spi_bus_config_t * getSPI_bus_config();
|
||||
|
||||
// Inherited from MCP2521_Hardware_Handle
|
||||
void execute(uint8_t cmd);
|
||||
void execute(uint8_t cmd, uint8_t address);
|
||||
|
||||
void read(uint8_t cmd, uint8_t *data, size_t length, uint8_t address);
|
||||
void read(uint8_t cmd, uint8_t *data, size_t length);
|
||||
uint8_t read(uint8_t cmd, uint8_t address);
|
||||
uint8_t read(uint8_t cmd);
|
||||
|
||||
void write(uint8_t cmd, uint8_t *data, size_t length, uint8_t address);
|
||||
void write(uint8_t cmd, uint8_t *data, size_t length);
|
||||
void write(uint8_t cmd, uint8_t data, uint8_t address);
|
||||
void write(uint8_t cmd, uint8_t data);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <strings.h>
|
||||
|
||||
class MCP2521_Hardware_Handle {
|
||||
public:
|
||||
virtual void execute(uint8_t cmd) = 0;
|
||||
virtual void execute(uint8_t cmd, uint8_t address) = 0;
|
||||
|
||||
virtual void read(uint8_t cmd, uint8_t *data, size_t length, uint8_t address) = 0;
|
||||
virtual void read(uint8_t cmd, uint8_t *data, size_t length) = 0;
|
||||
virtual uint8_t read(uint8_t cmd, uint8_t address) = 0;
|
||||
virtual uint8_t read(uint8_t cmd) = 0;
|
||||
|
||||
virtual void write(uint8_t cmd, uint8_t *data, size_t length, uint8_t address) = 0;
|
||||
virtual void write(uint8_t cmd, uint8_t *data, size_t length) = 0;
|
||||
virtual void write(uint8_t cmd, uint8_t data, uint8_t address) = 0;
|
||||
virtual void write(uint8_t cmd, uint8_t data) = 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user