Got MCP2521 to work
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
idf_component_register(SRCS "esp_implementation_init.cpp"
|
||||
"esp_implementation_cmd.cpp"
|
||||
"esp_implementation_int.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES driver)
|
||||
@@ -1,8 +1,17 @@
|
||||
#include "mcp2521_hardware_handle.hpp"
|
||||
|
||||
|
||||
const uint8_t null_buffer[32] = {0};
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "mcp2521_hardware_esp.hpp"
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::spi_transmit(spi_transaction_t *t) {
|
||||
xSemaphoreTake(spiMutex, portMAX_DELAY);
|
||||
spi_device_transmit(this->spi_device_handle, t);
|
||||
xSemaphoreGive(spiMutex);
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::execute(uint8_t cmd) {
|
||||
spi_transaction_ext_t t = {
|
||||
.base = {
|
||||
@@ -19,7 +28,7 @@ void MCP2521_Hardware_Handle_ESP::execute(uint8_t cmd) {
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::execute(uint8_t cmd, uint8_t address) {
|
||||
@@ -38,7 +47,7 @@ void MCP2521_Hardware_Handle_ESP::execute(uint8_t cmd, uint8_t address) {
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t *data, size_t length, uint8_t address) {
|
||||
@@ -49,15 +58,15 @@ void MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t *data, size_t length
|
||||
.addr = address,
|
||||
.length = 8*length,
|
||||
.rxlength = 8*length,
|
||||
.tx_buffer = NULL,
|
||||
.tx_buffer = null_buffer,
|
||||
.rx_buffer = data
|
||||
},
|
||||
.command_bits = 8,
|
||||
.address_bits = 0,
|
||||
.address_bits = 8,
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t *data, size_t length) {
|
||||
@@ -76,7 +85,7 @@ void MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t *data, size_t length
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
uint8_t MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t address) {
|
||||
@@ -88,7 +97,7 @@ uint8_t MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t address) {
|
||||
.addr = address,
|
||||
.length = 8,
|
||||
.rxlength = 8,
|
||||
.tx_buffer = NULL,
|
||||
.tx_buffer = null_buffer,
|
||||
.rx_buffer = &result
|
||||
},
|
||||
.command_bits = 8,
|
||||
@@ -96,7 +105,7 @@ uint8_t MCP2521_Hardware_Handle_ESP::read(uint8_t cmd, uint8_t address) {
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -109,7 +118,7 @@ uint8_t MCP2521_Hardware_Handle_ESP::read(uint8_t cmd) {
|
||||
.addr = 0,
|
||||
.length = 8,
|
||||
.rxlength = 8,
|
||||
.tx_buffer = NULL,
|
||||
.tx_buffer = null_buffer,
|
||||
.rx_buffer = &result
|
||||
},
|
||||
.command_bits = 8,
|
||||
@@ -117,7 +126,7 @@ uint8_t MCP2521_Hardware_Handle_ESP::read(uint8_t cmd) {
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -138,7 +147,7 @@ void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t *data, size_t lengt
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t *data, size_t length) {
|
||||
@@ -157,7 +166,7 @@ void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t *data, size_t lengt
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t data, uint8_t address) {
|
||||
@@ -176,7 +185,7 @@ void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t data, uint8_t addre
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t data) {
|
||||
@@ -195,7 +204,7 @@ void MCP2521_Hardware_Handle_ESP::write(uint8_t cmd, uint8_t data) {
|
||||
.dummy_bits = 0,
|
||||
};
|
||||
|
||||
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
||||
spi_transmit((spi_transaction_t*)(&t));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "mcp2521_hardware_esp.hpp"
|
||||
#include "driver/gpio.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
MCP2521_Hardware_Handle_ESP::MCP2521_Hardware_Handle_ESP(
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t *bus_config,
|
||||
@@ -34,18 +38,6 @@ 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,
|
||||
@@ -89,6 +81,8 @@ void MCP2521_Hardware_Handle_ESP::initSPIDevice(
|
||||
};
|
||||
|
||||
spi_bus_add_device(spi_host, &this->spi_device_config, &this->spi_device_handle);
|
||||
|
||||
spiMutex = xSemaphoreCreateMutex();
|
||||
}
|
||||
|
||||
spi_bus_config_t * MCP2521_Hardware_Handle_ESP::getSPI_bus_config() {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "mcp2521_hardware_handle.hpp"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "mcp2521_hardware_esp.hpp"
|
||||
|
||||
static void IRAM_ATTR gpio_isr_can_handler(void* arg) {
|
||||
MCP2521_Hardware_Handle_ESP * handle = (MCP2521_Hardware_Handle_ESP *)arg;
|
||||
handle->isr_can_interrupt();
|
||||
}
|
||||
|
||||
static void handleInteruptTaskCallerFn(void *arg) {
|
||||
MCP2521_Hardware_Handle_ESP * handle = (MCP2521_Hardware_Handle_ESP *)arg;
|
||||
handle->handleIntteruptTaskFn();
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::initPins(
|
||||
gpio_num_t int_pin
|
||||
) {
|
||||
canInterruptSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
gpio_config_t io_conf;
|
||||
io_conf.intr_type = GPIO_INTR_NEGEDGE;
|
||||
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);
|
||||
|
||||
gpio_install_isr_service(0);
|
||||
gpio_isr_handler_add(int_pin, gpio_isr_can_handler, this);
|
||||
|
||||
xTaskCreate(
|
||||
(TaskFunction_t)&handleInteruptTaskCallerFn,
|
||||
"canInterruptTask",
|
||||
2048,
|
||||
this,
|
||||
5,
|
||||
&canInterruptTaskHandle
|
||||
);
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::handleIntteruptTaskFn() {
|
||||
while(true) {
|
||||
xSemaphoreTake(canInterruptSemaphore, portMAX_DELAY);
|
||||
intHandler(intHandlerArg);
|
||||
}
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::isr_can_interrupt() {
|
||||
BaseType_t wokenTask = pdFALSE;
|
||||
xSemaphoreGiveFromISR(canInterruptSemaphore, &wokenTask);
|
||||
|
||||
if(wokenTask) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
}
|
||||
|
||||
void MCP2521_Hardware_Handle_ESP::registerIntHandler(intHandlerFunction_t handler, void * arg) {
|
||||
intHandlerArg = arg;
|
||||
intHandler = handler;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
class MCP2521_Hardware_Handle_ESP : public MCP2521_Hardware_Handle {
|
||||
char spi_tmp_buffer;
|
||||
@@ -13,6 +15,15 @@ class MCP2521_Hardware_Handle_ESP : public MCP2521_Hardware_Handle {
|
||||
spi_device_interface_config_t spi_device_config;
|
||||
spi_device_handle_t spi_device_handle;
|
||||
|
||||
SemaphoreHandle_t canInterruptSemaphore = NULL;
|
||||
SemaphoreHandle_t spiMutex = NULL;
|
||||
TaskHandle_t canInterruptTaskHandle = NULL;
|
||||
|
||||
void spi_transmit(spi_transaction_t *t);
|
||||
|
||||
void * intHandlerArg = NULL;
|
||||
intHandlerFunction_t intHandler = NULL;
|
||||
|
||||
public:
|
||||
MCP2521_Hardware_Handle_ESP(
|
||||
spi_host_device_t spi_host,
|
||||
@@ -52,6 +63,10 @@ public:
|
||||
|
||||
spi_bus_config_t * getSPI_bus_config();
|
||||
|
||||
// ISR Stuff
|
||||
void isr_can_interrupt();
|
||||
void handleIntteruptTaskFn();
|
||||
|
||||
// Inherited from MCP2521_Hardware_Handle
|
||||
void execute(uint8_t cmd);
|
||||
void execute(uint8_t cmd, uint8_t address);
|
||||
@@ -65,6 +80,8 @@ public:
|
||||
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);
|
||||
|
||||
void registerIntHandler(intHandlerFunction_t handler, void * arg);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
#include <cstdint>
|
||||
#include <strings.h>
|
||||
|
||||
typedef void (*intHandlerFunction_t)(void *);
|
||||
|
||||
class MCP2521_Hardware_Handle {
|
||||
public:
|
||||
virtual void registerIntHandler(intHandlerFunction_t handler, void * arg) = 0;
|
||||
|
||||
virtual void execute(uint8_t cmd) = 0;
|
||||
virtual void execute(uint8_t cmd, uint8_t address) = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user