Implemented Command-Level Interaction with CAN Interface
This commit is contained in:
11
can-interface/.vscode/settings.json
vendored
11
can-interface/.vscode/settings.json
vendored
@@ -13,5 +13,14 @@
|
|||||||
"idf.port": "/dev/ttyUSB0",
|
"idf.port": "/dev/ttyUSB0",
|
||||||
"idf.pythonBinPath": "/home/alexander/.espressif/python_env/idf5.3_py3.12_env/bin/python",
|
"idf.pythonBinPath": "/home/alexander/.espressif/python_env/idf5.3_py3.12_env/bin/python",
|
||||||
"idf.toolsPath": "/home/alexander/.espressif",
|
"idf.toolsPath": "/home/alexander/.espressif",
|
||||||
"idf.flashType": "UART"
|
"idf.flashType": "UART",
|
||||||
|
"files.associations": {
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"future": "cpp",
|
||||||
|
"bitset": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"regex": "cpp"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
idf_component_register(SRCS "operations.cpp"
|
idf_component_register(SRCS
|
||||||
"spi_interface_init.cpp"
|
"interface_commands.cpp"
|
||||||
"spi_interface_commands.cpp"
|
|
||||||
"register_interface.cpp"
|
|
||||||
INCLUDE_DIRS "include"
|
INCLUDE_DIRS "include"
|
||||||
REQUIRES driver)
|
REQUIRES driver
|
||||||
|
REQUIRES mcp2521_hardware_interface
|
||||||
|
)
|
||||||
@@ -1,98 +1,5 @@
|
|||||||
#pragma once
|
#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_OPMOD2 (1 << 7)
|
||||||
#define MCP2521_CANSTAT_OPMOD1 (1 << 6)
|
#define MCP2521_CANSTAT_OPMOD1 (1 << 6)
|
||||||
#define MCP2521_CANSTAT_OPMOD0 (1 << 5)
|
#define MCP2521_CANSTAT_OPMOD0 (1 << 5)
|
||||||
#define MCP2521_CANSTAT_ICOD2 (1 << 3)
|
#define MCP2521_CANSTAT_ICOD2 (1 << 2)
|
||||||
#define MCP2521_CANSTAT_ICOD1 (1 << 2)
|
#define MCP2521_CANSTAT_ICOD1 (1 << 1)
|
||||||
#define MCP2521_CANSTAT_ICOD0 (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 0x0F
|
||||||
#define MCP2521_CANCTRL_REQOP2 (1 << 7)
|
#define MCP2521_CANCTRL_REQOP2 (1 << 7)
|
||||||
@@ -119,6 +126,17 @@
|
|||||||
#define MCP2521_TXB2CTRL_TXP1 (1 << 1)
|
#define MCP2521_TXB2CTRL_TXP1 (1 << 1)
|
||||||
#define MCP2521_TXB2CTRL_TXP0 (1 << 0)
|
#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 0x60
|
||||||
#define MCP2521_RXB0CTRL_RXM1 (1 << 6)
|
#define MCP2521_RXB0CTRL_RXM1 (1 << 6)
|
||||||
#define MCP2521_RXB0CTRL_RXM0 (1 << 5)
|
#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;
|
|
||||||
}
|
|
||||||
@@ -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
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
#include "mcp2521.hpp"
|
#include "mcp2521_hardware_handle.hpp"
|
||||||
#include <stdint.h>
|
|
||||||
|
#ifdef ESP_PLATFORM
|
||||||
|
#include "mcp2521_hardware_esp.hpp"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "driver/gpio.h"
|
MCP2521_Hardware_Handle_ESP::MCP2521_Hardware_Handle_ESP(
|
||||||
#include "driver/spi_master.h"
|
|
||||||
|
|
||||||
MCP2521_SPI_Interface::MCP2521_SPI_Interface(
|
|
||||||
spi_host_device_t spi_host,
|
spi_host_device_t spi_host,
|
||||||
spi_bus_config_t *bus_config,
|
spi_bus_config_t *bus_config,
|
||||||
gpio_num_t mosi,
|
gpio_num_t mosi,
|
||||||
@@ -20,7 +19,7 @@ MCP2521_SPI_Interface::MCP2521_SPI_Interface(
|
|||||||
initSPIDevice(spi_host, cs);
|
initSPIDevice(spi_host, cs);
|
||||||
}
|
}
|
||||||
|
|
||||||
MCP2521_SPI_Interface::MCP2521_SPI_Interface(
|
MCP2521_Hardware_Handle_ESP::MCP2521_Hardware_Handle_ESP(
|
||||||
spi_host_device_t spi_host,
|
spi_host_device_t spi_host,
|
||||||
spi_bus_config_t *bus_config,
|
spi_bus_config_t *bus_config,
|
||||||
gpio_num_t cs,
|
gpio_num_t cs,
|
||||||
@@ -31,7 +30,23 @@ MCP2521_SPI_Interface::MCP2521_SPI_Interface(
|
|||||||
initSPIDevice(spi_host, cs);
|
initSPIDevice(spi_host, cs);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCP2521_SPI_Interface::initSPIBus(
|
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,
|
spi_host_device_t spi_host,
|
||||||
gpio_num_t mosi,
|
gpio_num_t mosi,
|
||||||
gpio_num_t miso,
|
gpio_num_t miso,
|
||||||
@@ -50,7 +65,7 @@ void MCP2521_SPI_Interface::initSPIBus(
|
|||||||
spi_bus_initialize(spi_host, bus_config, SPI_DMA_CH_AUTO);
|
spi_bus_initialize(spi_host, bus_config, SPI_DMA_CH_AUTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCP2521_SPI_Interface::initSPIDevice(
|
void MCP2521_Hardware_Handle_ESP::initSPIDevice(
|
||||||
spi_host_device_t spi_host,
|
spi_host_device_t spi_host,
|
||||||
gpio_num_t cs
|
gpio_num_t cs
|
||||||
) {
|
) {
|
||||||
@@ -74,26 +89,10 @@ void MCP2521_SPI_Interface::initSPIDevice(
|
|||||||
};
|
};
|
||||||
|
|
||||||
spi_bus_add_device(spi_host, &this->spi_device_config, &this->spi_device_handle);
|
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() {
|
spi_bus_config_t * MCP2521_Hardware_Handle_ESP::getSPI_bus_config() {
|
||||||
return this->spi_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() {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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;
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
idf_component_register(SRCS "hello_world_main.cpp"
|
idf_component_register(SRCS "hello_world_main.cpp"
|
||||||
REQUIRES driver
|
REQUIRES driver
|
||||||
REQUIRES mcp2521
|
REQUIRES mcp2521
|
||||||
|
REQUIRES mcp2521_hardware_interface
|
||||||
REQUIRES spi_flash
|
REQUIRES spi_flash
|
||||||
INCLUDE_DIRS "")
|
INCLUDE_DIRS "")
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
#include "driver/spi_master.h"
|
#include "driver/spi_master.h"
|
||||||
|
|
||||||
#include "mcp2521.hpp"
|
#include "mcp2521.hpp"
|
||||||
#include "reg.hpp"
|
|
||||||
|
|
||||||
#define SPI_PIN_CS0 GPIO_NUM_5
|
#define SPI_PIN_CS0 GPIO_NUM_5
|
||||||
#define SPI_PIN_SCLK GPIO_NUM_18
|
#define SPI_PIN_SCLK GPIO_NUM_18
|
||||||
@@ -32,11 +31,7 @@
|
|||||||
|
|
||||||
#define EXTERNAL_TRIGGER GPIO_NUM_26
|
#define EXTERNAL_TRIGGER GPIO_NUM_26
|
||||||
|
|
||||||
#ifdef __cplusplus
|
extern "C" void app_main() {
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void app_main() {
|
|
||||||
printf("Hello world!\n");
|
printf("Hello world!\n");
|
||||||
|
|
||||||
const gpio_num_t LED_PIN = GPIO_NUM_2;
|
const gpio_num_t LED_PIN = GPIO_NUM_2;
|
||||||
@@ -46,11 +41,11 @@ void app_main() {
|
|||||||
gpio_set_level(EXTERNAL_TRIGGER, true);
|
gpio_set_level(EXTERNAL_TRIGGER, true);
|
||||||
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
gpio_set_level(EXTERNAL_TRIGGER, false);
|
|
||||||
|
|
||||||
spi_bus_config_t spi_bus;
|
spi_bus_config_t spi_bus;
|
||||||
|
|
||||||
MCP2521_SPI_Interface mcp2521_spi(
|
MCP2521_Hardware_Handle_ESP hardware_mcp2521(
|
||||||
VSPI_HOST,
|
VSPI_HOST,
|
||||||
&spi_bus,
|
&spi_bus,
|
||||||
SPI_PIN_MOSI,
|
SPI_PIN_MOSI,
|
||||||
@@ -60,11 +55,16 @@ void app_main() {
|
|||||||
CAN_INT_PIN
|
CAN_INT_PIN
|
||||||
);
|
);
|
||||||
|
|
||||||
mcp2521_spi.reset();
|
MCP2521_Command_Interface mcp2521(&hardware_mcp2521);
|
||||||
printf("%x\n", mcp2521_spi.read_reg(MCP2521_CANCTRL));
|
|
||||||
|
|
||||||
|
gpio_set_level(EXTERNAL_TRIGGER, false);
|
||||||
|
printf("CANCTRL: %x\n", mcp2521.read_reg(MCP2521_CANCTRL));
|
||||||
|
printf("RX_STATUS: %x\n", mcp2521.read_rx_status());
|
||||||
|
printf("STATUS: %x\n", mcp2521.read_status());
|
||||||
|
|
||||||
uint8_t data[16];
|
uint8_t data[16];
|
||||||
mcp2521_spi.read_reg(MCP2521_CANSTAT, data, 16);
|
hardware_mcp2521.read(MCP2521_OP_READ, data, 16, MCP2521_RXB0CTRL);
|
||||||
|
|
||||||
for(int i=0; i<0x10; i++) {
|
for(int i=0; i<0x10; i++) {
|
||||||
printf("%x ", i);
|
printf("%x ", i);
|
||||||
@@ -79,12 +79,8 @@ void app_main() {
|
|||||||
bool flag = true;
|
bool flag = true;
|
||||||
while (true) {
|
while (true) {
|
||||||
gpio_set_level(LED_PIN, flag);
|
gpio_set_level(LED_PIN, flag);
|
||||||
flag = dummy_function(flag);
|
flag = !flag;
|
||||||
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
Binary file not shown.
Reference in New Issue
Block a user