Implemented Command-Level Interaction with CAN Interface
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
idf_component_register(SRCS "operations.cpp"
|
||||
"spi_interface_init.cpp"
|
||||
"spi_interface_commands.cpp"
|
||||
"register_interface.cpp"
|
||||
idf_component_register(SRCS
|
||||
"interface_commands.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES driver)
|
||||
REQUIRES driver
|
||||
REQUIRES mcp2521_hardware_interface
|
||||
)
|
||||
@@ -1,98 +1,5 @@
|
||||
#pragma once
|
||||
#include "reg.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
|
||||
bool dummy_function(bool flag);
|
||||
|
||||
|
||||
enum MCP2521_RX_BUFFER {
|
||||
RXB0 = 0,
|
||||
RXB1 = 1
|
||||
};
|
||||
|
||||
enum MCP2521_TX_BUFFER {
|
||||
TXB0 = 0,
|
||||
TXB1 = 1,
|
||||
TXB2 = 2
|
||||
};
|
||||
|
||||
enum MCP2521_BUFFER_TYPE {
|
||||
ID = 0,
|
||||
DATA = 1
|
||||
};
|
||||
|
||||
class MCP2521_SPI_Interface {
|
||||
private:
|
||||
char spi_rx_buffer[32];
|
||||
char spi_tx_buffer[32];
|
||||
|
||||
spi_bus_config_t * spi_bus_config;
|
||||
spi_device_interface_config_t spi_device_config;
|
||||
spi_device_handle_t spi_device_handle;
|
||||
|
||||
public:
|
||||
spi_bus_config_t * getSPI_bus_config();
|
||||
|
||||
MCP2521_SPI_Interface(
|
||||
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_SPI_Interface(
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t *bus_config,
|
||||
gpio_num_t cs,
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
|
||||
~MCP2521_SPI_Interface();
|
||||
|
||||
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 deinitSPI();
|
||||
|
||||
void initPins(
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
void deinitPins();
|
||||
|
||||
void reset();
|
||||
|
||||
void read_reg(uint8_t address, uint8_t *data, size_t length);
|
||||
uint8_t read_reg(uint8_t address);
|
||||
|
||||
void read_rx_buf(MCP2521_RX_BUFFER buffer, MCP2521_BUFFER_TYPE type, uint8_t *data, size_t length);
|
||||
|
||||
void write_reg(uint8_t address, uint8_t *data, size_t length);
|
||||
void write_reg(uint8_t address, uint8_t data);
|
||||
|
||||
void write_tx_buf(MCP2521_TX_BUFFER buffer, MCP2521_BUFFER_TYPE type, uint8_t *data, size_t length);
|
||||
|
||||
void request_to_send(bool txb2, bool txb1, bool txb0);
|
||||
void request_to_send(MCP2521_TX_BUFFER buffer);
|
||||
|
||||
uint8_t read_status();
|
||||
uint8_t read_rx_status();
|
||||
|
||||
void bit_modify(uint8_t address, uint8_t mask, uint8_t data);
|
||||
};
|
||||
|
||||
#include "mcp2521_command.hpp"
|
||||
#include "mcp2521_hardware_esp.hpp"
|
||||
#include "mcp2521_addresses.hpp"
|
||||
@@ -20,9 +20,16 @@
|
||||
#define MCP2521_CANSTAT_OPMOD2 (1 << 7)
|
||||
#define MCP2521_CANSTAT_OPMOD1 (1 << 6)
|
||||
#define MCP2521_CANSTAT_OPMOD0 (1 << 5)
|
||||
#define MCP2521_CANSTAT_ICOD2 (1 << 3)
|
||||
#define MCP2521_CANSTAT_ICOD1 (1 << 2)
|
||||
#define MCP2521_CANSTAT_ICOD0 (1 << 1)
|
||||
#define MCP2521_CANSTAT_ICOD2 (1 << 2)
|
||||
#define MCP2521_CANSTAT_ICOD1 (1 << 1)
|
||||
#define MCP2521_CANSTAT_ICOD0 (1 << 0)
|
||||
|
||||
struct CANSTAT_Register {
|
||||
uint8_t ICOD : 3;
|
||||
uint8_t : 1; // Unused bit
|
||||
uint8_t OPMOD : 3;
|
||||
uint8_t : 1; // Unused bit
|
||||
};
|
||||
|
||||
#define MCP2521_CANCTRL 0x0F
|
||||
#define MCP2521_CANCTRL_REQOP2 (1 << 7)
|
||||
@@ -119,6 +126,17 @@
|
||||
#define MCP2521_TXB2CTRL_TXP1 (1 << 1)
|
||||
#define MCP2521_TXB2CTRL_TXP0 (1 << 0)
|
||||
|
||||
struct TXBnCTRL_Register {
|
||||
uint8_t TXP : 2;
|
||||
uint8_t : 1; // Unused bit
|
||||
uint8_t TXREQ : 1;
|
||||
uint8_t TXERR : 1;
|
||||
uint8_t MLOA : 1;
|
||||
uint8_t ABTF : 1;
|
||||
uint8_t : 1; // Unused bit
|
||||
};
|
||||
|
||||
|
||||
#define MCP2521_RXB0CTRL 0x60
|
||||
#define MCP2521_RXB0CTRL_RXM1 (1 << 6)
|
||||
#define MCP2521_RXB0CTRL_RXM0 (1 << 5)
|
||||
53
can-interface/components/mcp2521/include/mcp2521_command.hpp
Normal file
53
can-interface/components/mcp2521/include/mcp2521_command.hpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
#include "mcp2521_addresses.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "mcp2521_hardware_handle.hpp"
|
||||
|
||||
enum MCP2521_RX_BUFFER {
|
||||
RXB0 = 0,
|
||||
RXB1 = 1
|
||||
};
|
||||
|
||||
enum MCP2521_TX_BUFFER {
|
||||
TXB0 = 0,
|
||||
TXB1 = 1,
|
||||
TXB2 = 2
|
||||
};
|
||||
|
||||
enum MCP2521_BUFFER_TYPE {
|
||||
ID = 0,
|
||||
DATA = 1
|
||||
};
|
||||
|
||||
class MCP2521_Command_Interface {
|
||||
private:
|
||||
MCP2521_Hardware_Handle * hardware_handle;
|
||||
public:
|
||||
|
||||
MCP2521_Command_Interface(
|
||||
MCP2521_Hardware_Handle * hardware_handle
|
||||
);
|
||||
|
||||
void reset();
|
||||
|
||||
void read_reg(uint8_t address, uint8_t *data, size_t length);
|
||||
uint8_t read_reg(uint8_t address);
|
||||
|
||||
void read_rx_buf(MCP2521_RX_BUFFER buffer, MCP2521_BUFFER_TYPE type, uint8_t *data, size_t length);
|
||||
|
||||
void write_reg(uint8_t address, uint8_t *data, size_t length);
|
||||
void write_reg(uint8_t address, uint8_t data);
|
||||
|
||||
void write_tx_buf(MCP2521_TX_BUFFER buffer, MCP2521_BUFFER_TYPE type, uint8_t *data, size_t length);
|
||||
|
||||
void request_to_send(bool txb2, bool txb1, bool txb0);
|
||||
void request_to_send(MCP2521_TX_BUFFER buffer);
|
||||
|
||||
uint8_t read_status();
|
||||
uint8_t read_rx_status();
|
||||
|
||||
void bit_modify(uint8_t address, uint8_t mask, uint8_t data);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#include "mcp2521.hpp"
|
||||
|
||||
|
||||
|
||||
class MCP2515 {
|
||||
private:
|
||||
|
||||
|
||||
|
||||
|
||||
public:
|
||||
MCP2515();
|
||||
~MCP2515();
|
||||
|
||||
void reset();
|
||||
|
||||
// Interrupts
|
||||
|
||||
void register_rx_callback(void (*callback)(uint32_t id, uint8_t *data, size_t length));
|
||||
void register_tx_callback(void (*callback)(MCP2521_TX_BUFFER buffer));
|
||||
void register_error_callback(void (*callback)(uint8_t error_flags));
|
||||
void register_message_error_callback(void (*callback)(uint8_t error_flags));
|
||||
|
||||
// Transmittion
|
||||
void set_tx_id(MCP2521_TX_BUFFER buffer, uint32_t id);
|
||||
void set_tx_data(MCP2521_TX_BUFFER buffer, uint8_t *data, size_t length);
|
||||
void set_tx_priority(MCP2521_TX_BUFFER buffer, uint8_t priority);
|
||||
void set_one_shot(MCP2521_TX_BUFFER buffer, bool one_shot);
|
||||
|
||||
void request_to_send(MCP2521_TX_BUFFER buffer);
|
||||
void request_to_send(bool txb2, bool txb1, bool txb0);
|
||||
|
||||
void abort_tx(MCP2521_TX_BUFFER buffer);
|
||||
void abort_all_tx();
|
||||
|
||||
TXBnCTRL_Register get_TXBnCTRL(MCP2521_TX_BUFFER buffer);
|
||||
|
||||
void transmit(MCP2521_TX_BUFFER buffer, uint32_t id, uint8_t *data, size_t length, uint8_t priority, bool one_shot);
|
||||
|
||||
// Reception
|
||||
|
||||
|
||||
|
||||
uint8_t get_TransmitErrorCounter();
|
||||
uint8_t get_ReceiveErrorCounter();
|
||||
|
||||
|
||||
|
||||
};
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
class MCP2515 {
|
||||
private:
|
||||
|
||||
public:
|
||||
uint8_t get_TransmitErrorCounter();
|
||||
uint8_t get_ReceiveErrorCounter();
|
||||
};
|
||||
76
can-interface/components/mcp2521/interface_commands.cpp
Normal file
76
can-interface/components/mcp2521/interface_commands.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <cstring>
|
||||
|
||||
#include "mcp2521.hpp"
|
||||
#include "mcp2521_addresses.hpp"
|
||||
|
||||
MCP2521_Command_Interface::MCP2521_Command_Interface(
|
||||
MCP2521_Hardware_Handle * hardware_handle
|
||||
) {
|
||||
this->hardware_handle = hardware_handle;
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::reset() {
|
||||
hardware_handle->execute(MCP2521_OP_RESET);
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::read_reg(uint8_t address, uint8_t *data, size_t length) {
|
||||
hardware_handle->read(MCP2521_OP_READ, data, length, address);
|
||||
}
|
||||
|
||||
uint8_t MCP2521_Command_Interface::read_reg(uint8_t address) {
|
||||
return hardware_handle->read(MCP2521_OP_READ, address);
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::read_rx_buf(MCP2521_RX_BUFFER buffer, MCP2521_BUFFER_TYPE type, uint8_t *data, size_t length) {
|
||||
uint8_t address = (buffer << 1) | (type << 2);
|
||||
hardware_handle->read(MCP2521_OP_READ_RX_BUFFER | address, data, length);
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::write_reg(uint8_t address, uint8_t *data, size_t length) {
|
||||
hardware_handle->write(MCP2521_OP_WRITE, data, length, address);
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::write_reg(uint8_t address, uint8_t data) {
|
||||
hardware_handle->write(MCP2521_OP_WRITE, data, address);
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::write_tx_buf(MCP2521_TX_BUFFER buffer, MCP2521_BUFFER_TYPE type, uint8_t *data, size_t length) {
|
||||
uint8_t address = buffer | (type << 2);
|
||||
hardware_handle->write(MCP2521_OP_LOAD_TX_BUFFER | address, data, length);
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::request_to_send(bool txb2, bool txb1, bool txb0) {
|
||||
uint8_t data = (txb2 << 2) | (txb1 << 1) | txb0;
|
||||
hardware_handle->execute(MCP2521_OP_RTS | data);
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::request_to_send(MCP2521_TX_BUFFER buffer) {
|
||||
uint8_t mask = 0;
|
||||
|
||||
switch (buffer) {
|
||||
case MCP2521_TX_BUFFER::TXB0:
|
||||
mask = 0b001;
|
||||
break;
|
||||
case MCP2521_TX_BUFFER::TXB1:
|
||||
mask = 0b010;
|
||||
break;
|
||||
case MCP2521_TX_BUFFER::TXB2:
|
||||
mask = 0b100;
|
||||
break;
|
||||
}
|
||||
|
||||
hardware_handle->execute(MCP2521_OP_RTS | mask);
|
||||
}
|
||||
|
||||
uint8_t MCP2521_Command_Interface::read_status() {
|
||||
return hardware_handle->read(MCP2521_OP_READ_STATUS);
|
||||
}
|
||||
|
||||
uint8_t MCP2521_Command_Interface::read_rx_status() {
|
||||
return hardware_handle->read(MCP2521_OP_RX_STATUS);
|
||||
}
|
||||
|
||||
void MCP2521_Command_Interface::bit_modify(uint8_t address, uint8_t mask, uint8_t data) {
|
||||
uint8_t data_array[3] = {address, mask, data};
|
||||
hardware_handle->write(MCP2521_OP_BIT_MODIFY, data_array, 3);
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
|
||||
bool dummy_function(bool flag) {
|
||||
return !flag;
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
#include "mcp2515.hpp"
|
||||
#include "register_interface.hpp"
|
||||
@@ -1,49 +0,0 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "mcp2521.hpp"
|
||||
#include "reg.hpp"
|
||||
|
||||
void MCP2521_SPI_Interface::reset() {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = MCP2521_OP_RESET,
|
||||
.addr = 8,
|
||||
.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_SPI_Interface::read_reg(uint8_t address, uint8_t *data, size_t length) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
||||
.cmd = MCP2521_OP_READ,
|
||||
.addr = address,
|
||||
.length = 8 * length,
|
||||
.rxlength = 8 * length,
|
||||
.tx_buffer = &this->spi_tx_buffer,
|
||||
.rx_buffer = &this->spi_rx_buffer,
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 8,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
memcpy(data, this->spi_rx_buffer, length);
|
||||
}
|
||||
|
||||
uint8_t MCP2521_SPI_Interface::read_reg(uint8_t address) {
|
||||
uint8_t data;
|
||||
read_reg(address, &data, 1);
|
||||
return data;
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
#include "mcp2521.hpp"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
|
||||
MCP2521_SPI_Interface::MCP2521_SPI_Interface(
|
||||
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_SPI_Interface::MCP2521_SPI_Interface(
|
||||
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);
|
||||
}
|
||||
|
||||
void MCP2521_SPI_Interface::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_SPI_Interface::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);
|
||||
|
||||
memset(&this->spi_tx_buffer, 0, sizeof(this->spi_tx_buffer));
|
||||
memset(&this->spi_rx_buffer, 0, sizeof(this->spi_rx_buffer));
|
||||
}
|
||||
|
||||
spi_bus_config_t * MCP2521_SPI_Interface::getSPI_bus_config() {
|
||||
return this->spi_bus_config;
|
||||
}
|
||||
|
||||
MCP2521_SPI_Interface::~MCP2521_SPI_Interface() {
|
||||
deinitSPI();
|
||||
deinitPins();
|
||||
}
|
||||
|
||||
void MCP2521_SPI_Interface::initPins(gpio_num_t int_pin) {
|
||||
|
||||
}
|
||||
|
||||
void MCP2521_SPI_Interface::deinitPins() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user