Implement Multi-Bus CAN Interfaces
This commit is contained in:
@@ -1,3 +1,13 @@
|
||||
/**
|
||||
* @file mcp2521_hardware_esp.hpp
|
||||
* @author AlexanderHD27
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2024-11-16
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
#include "mcp2521_hardware_handle.hpp"
|
||||
|
||||
@@ -8,24 +18,48 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.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;
|
||||
static void handleInteruptTaskCallerFn(void *arg);
|
||||
static void IRAM_ATTR gpio_isr_can_handler(void* arg);
|
||||
|
||||
/**
|
||||
* @brief Hardware handle for MCP2521 over SPI on ESP32
|
||||
* This is should not be used if multiple MCP2521 are on the same SPI bus
|
||||
*/
|
||||
class MCP2521_HardwareHandle_ESP : public I_MCP2521_HardwareHandle {
|
||||
protected:
|
||||
void spi_transmit(spi_transaction_t *t);
|
||||
|
||||
/**
|
||||
* @brief Sempahore, that is set every time the MCP2521 triggers an interrupt by the canInterruptTaskHandle
|
||||
*/
|
||||
SemaphoreHandle_t canInterruptSemaphore = NULL;
|
||||
SemaphoreHandle_t spiMutex = NULL;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Handle to task that handles the MCP2521 interrupt
|
||||
* A Handle can be registered via the registerIntHandler method
|
||||
*/
|
||||
TaskHandle_t canInterruptTaskHandle = NULL;
|
||||
|
||||
void spi_transmit(spi_transaction_t *t);
|
||||
spi_device_handle_t spi_device_handle;
|
||||
|
||||
private:
|
||||
spi_bus_config_t * spi_bus_config;
|
||||
char spi_tmp_buffer;
|
||||
|
||||
spi_device_interface_config_t spi_device_config;
|
||||
|
||||
/**
|
||||
* @brief Sempahore, that protects the SPI bus from being accessed by multiple tasks at the same time
|
||||
*
|
||||
*/
|
||||
SemaphoreHandle_t spiMutex = NULL;
|
||||
|
||||
void * intHandlerArg = NULL;
|
||||
intHandlerFunction_t intHandler = NULL;
|
||||
|
||||
public:
|
||||
MCP2521_Hardware_Handle_ESP(
|
||||
MCP2521_HardwareHandle_ESP(
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t *bus_config,
|
||||
gpio_num_t mosi,
|
||||
@@ -35,7 +69,7 @@ public:
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
|
||||
MCP2521_Hardware_Handle_ESP(
|
||||
MCP2521_HardwareHandle_ESP(
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t *bus_config,
|
||||
gpio_num_t cs,
|
||||
@@ -59,12 +93,22 @@ public:
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
|
||||
~MCP2521_Hardware_Handle_ESP();
|
||||
~MCP2521_HardwareHandle_ESP();
|
||||
|
||||
spi_bus_config_t * getSPI_bus_config();
|
||||
|
||||
// ISR Stuff
|
||||
/**
|
||||
* @brief This function is called by the ISR, that is triggered by the MCP2521
|
||||
* This function just set the canInterruptSemaphore, that then unblocks the canInterruptTaskHandle.
|
||||
* So this function should be as short as possible, let the reset of the work be done in the canInterruptTaskHandle
|
||||
*/
|
||||
void isr_can_interrupt();
|
||||
|
||||
/**
|
||||
* This is the methode run as canInterruptTaskHandle.
|
||||
* It should continously aquire the canInterruptSemaphore and then call the intHandler
|
||||
*/
|
||||
void handleIntteruptTaskFn();
|
||||
|
||||
// Inherited from MCP2521_Hardware_Handle
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "mcp2521_hardware_esp.hpp"
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
struct spi_message_t {
|
||||
spi_transaction_t *transaction;
|
||||
QueueHandle_t queue;
|
||||
spi_device_handle_t spi_device_handle;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Hardware handle for MCP2521 over SPI in a multi-Bus-Setup on ESP32
|
||||
* This should not be created directly, use the MCP2521_HardwareHandleFactory_ESPBus instead
|
||||
*/
|
||||
class MCP2521_HardwareHandle_ESPBus : public MCP2521_HardwareHandle_ESP {
|
||||
using MCP2521_HardwareHandle_ESP::MCP2521_HardwareHandle_ESP;
|
||||
private:
|
||||
QueueHandle_t send_queue;
|
||||
QueueHandle_t receive_queue;
|
||||
|
||||
/**
|
||||
* @brief Wrapper around the spi_transmit function, that locks the spiMutex before calling spi_transmit.
|
||||
* The muxtex is shared with all other MCP2521_HardwareHandle_ESPBus instances created by the same MCP2521_HardwareHandleFactory_ESPBus
|
||||
* @overload
|
||||
*
|
||||
* @param t ESP32 SPI Transaction struct
|
||||
*/
|
||||
void spi_transmit(spi_transaction_t *t);
|
||||
public:
|
||||
MCP2521_HardwareHandle_ESPBus(
|
||||
QueueHandle_t send_queue,
|
||||
spi_host_device_t spi_host,
|
||||
spi_bus_config_t * bus_config,
|
||||
gpio_num_t cs,
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
|
||||
void initPins(
|
||||
gpio_num_t int_pin
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "mcp2521_hardware_handle.hpp"
|
||||
#include "mcp2521_hardware_esp_bus.hpp"
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
class MCP2521_HardwareHandleFactory_ESPBus {
|
||||
private:
|
||||
spi_bus_config_t bus_config;
|
||||
spi_host_device_t spi_host;
|
||||
|
||||
gpio_num_t mosi;
|
||||
gpio_num_t miso;
|
||||
gpio_num_t sclk;
|
||||
|
||||
QueueHandle_t spi_queue;
|
||||
TaskHandle_t transactionTaskHandle;
|
||||
|
||||
public:
|
||||
MCP2521_HardwareHandleFactory_ESPBus(
|
||||
spi_host_device_t spi_host,
|
||||
gpio_num_t mosi,
|
||||
gpio_num_t miso,
|
||||
gpio_num_t sclk
|
||||
);
|
||||
|
||||
void transactionTaskFn();
|
||||
|
||||
MCP2521_HardwareHandle_ESPBus create(
|
||||
gpio_num_t int_pin,
|
||||
gpio_num_t cs
|
||||
);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -2,23 +2,118 @@
|
||||
#include <cstdint>
|
||||
#include <strings.h>
|
||||
|
||||
/**
|
||||
* @brief Handler function pointer type
|
||||
*
|
||||
*/
|
||||
typedef void (*intHandlerFunction_t)(void *);
|
||||
|
||||
class MCP2521_Hardware_Handle {
|
||||
/**
|
||||
* @brief Hardware handle for MCP2521
|
||||
* @interface
|
||||
* This is an abstraction over the sepific hardware setup, eg. SPI@ESP32 or RP2040
|
||||
*
|
||||
*/
|
||||
class I_MCP2521_HardwareHandle {
|
||||
public:
|
||||
/**
|
||||
* @brief Registeres a function that is called when the MCP2521 triggers an interrupt
|
||||
*
|
||||
* @param handler This function is called when the MCP2521 triggers an interrupt
|
||||
* @param arg Arguments passed to the handler-function
|
||||
*/
|
||||
virtual void registerIntHandler(intHandlerFunction_t handler, void * arg) = 0;
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @brief justed sends a 8bit command to the MCP2521
|
||||
*
|
||||
* @param cmd command to send
|
||||
*/
|
||||
virtual void execute(uint8_t cmd) = 0;
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @brief executes a command (8 bit) with an address (8 bit)
|
||||
*
|
||||
* @param cmd 8 bit command
|
||||
* @param address 8 bit address
|
||||
*/
|
||||
virtual void execute(uint8_t cmd, uint8_t address) = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Executes a read from the MCP2521 with command, data buffer, length and address
|
||||
* @overload
|
||||
*
|
||||
* @param cmd Command to MCP2521
|
||||
* @param data Data buffer to store the read data
|
||||
* @param length How many bytes to read
|
||||
* @param address Address to read from
|
||||
*/
|
||||
virtual void read(uint8_t cmd, uint8_t *data, size_t length, uint8_t address) = 0;
|
||||
|
||||
/**
|
||||
* @brief Executes a read from the MCP2521 with command, data buffer and length
|
||||
* @overload
|
||||
*
|
||||
* @param cmd Command to MCP2521
|
||||
* @param data Data buffer to store the read data
|
||||
* @param length How many bytes to read
|
||||
*/
|
||||
virtual void read(uint8_t cmd, uint8_t *data, size_t length) = 0;
|
||||
|
||||
/**
|
||||
* @brief Executes a read from the MCP2521 with command and address, returns just one byte
|
||||
*
|
||||
* @param cmd Command to MCP2521
|
||||
* @param address Address to read from
|
||||
* @return uint8_t
|
||||
*/
|
||||
virtual uint8_t read(uint8_t cmd, uint8_t address) = 0;
|
||||
|
||||
/**
|
||||
* @brief Executes a read from the MCP2521 with command, returns just one byte
|
||||
*
|
||||
* @param cmd Command to MCP2521
|
||||
* @return uint8_t
|
||||
*/
|
||||
virtual uint8_t read(uint8_t cmd) = 0;
|
||||
|
||||
/**
|
||||
* @brief Writes data to the MCP2521 with command, data buffer, length and address
|
||||
*
|
||||
* @param cmd Command to MCP2521
|
||||
* @param data Data buffer to write
|
||||
* @param length Length of the data buffer
|
||||
* @param address Where to write the data
|
||||
*/
|
||||
virtual void write(uint8_t cmd, uint8_t *data, size_t length, uint8_t address) = 0;
|
||||
|
||||
/**
|
||||
* @brief Wirites data to the MCP2521 with command, data buffer and length
|
||||
*
|
||||
* @param cmd Command to MCP2521
|
||||
* @param data Data buffer to write
|
||||
* @param length Length of the data buffer
|
||||
*/
|
||||
virtual void write(uint8_t cmd, uint8_t *data, size_t length) = 0;
|
||||
|
||||
/**
|
||||
* @brief Writes data to the MCP2521 with command, data (on byte) and address
|
||||
*
|
||||
* @param cmd Command to MCP2521
|
||||
* @param data Data byte to write
|
||||
* @param address Where to write the data
|
||||
*/
|
||||
virtual void write(uint8_t cmd, uint8_t data, uint8_t address) = 0;
|
||||
|
||||
/**
|
||||
* @brief Writes to the MCP2521 with command and data (one byte)
|
||||
*
|
||||
* @param cmd Command to MCP2521
|
||||
* @param data Data byte to write
|
||||
*/
|
||||
virtual void write(uint8_t cmd, uint8_t data) = 0;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user