Skip to content

Commit

Permalink
Add MCP_CAN lib
Browse files Browse the repository at this point in the history
  • Loading branch information
KenwoodFox committed Sep 3, 2024
1 parent ecc2ee0 commit a555641
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "Firmware/bootloader/rp2040-radio-bootloader"]
path = Firmware/bootloader/rp2040-radio-bootloader
url = https://github.com/Kitsune-Robotics/rp2040-radio-bootloader
[submodule "Firmware/lib/pico-mcp2515"]
path = Firmware/lib/pico-mcp2515
url = https://github.com/adamczykpiotr/pico-mcp2515
1 change: 1 addition & 0 deletions Firmware/lib/pico-mcp2515
Submodule pico-mcp2515 added at 4506dd
4 changes: 4 additions & 0 deletions Firmware/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ add_executable(${ProjectName}
usb_console.c
parser.c
led_task.c
can_controller.c
)

target_include_directories(${ProjectName} PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/../lib/pico-mcp2515/include # MCP_CAN library
)

target_link_libraries(${ProjectName}
pico_stdlib # Obviously we need this one
hardware_spi # SPI library pico
FreeRTOS-Kernel-Heap4 # From FreeRTOS
pcio-mcp2515 # MCP_CAN library
cmake_git_version_tracking # https://github.com/andrew-hardin/cmake-git-version-tracking Very handy library!
)

Expand Down
105 changes: 105 additions & 0 deletions Firmware/src/can_controller.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "can_controller.h"
#include "pico/stdlib.h"

// Define SPI pins and settings for MCP2515
#define SPI_PORT spi0
#define PIN_SCK 2
#define PIN_MOSI 3
#define PIN_MISO 4
#define PIN_CS 5

// CAN control queue
#define CAN_QUEUE_LENGTH 1
#define CAN_COMMAND_GET_VERSION 1

static QueueHandle_t canQueue; // RTOS queue for CAN commands
static MCP2515 mcp2515; // MCP2515 CAN controller instance

// Function to request version info from the MCP2515
void request_can_version(void)
{
if (canQueue == NULL)
{
// Ensure the queue is initialized before using it
return;
}

// Send a version request command to the CAN task
uint8_t versionCommand = CAN_COMMAND_GET_VERSION;
xQueueSend(canQueue, &versionCommand, 0);
}

// Initialize SPI for MCP2515 communication
void setup_spi()
{
// Initialize SPI at 1MHz
spi_init(SPI_PORT, 1000 * 1000);

// Set up SPI pins
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);

// Set CS pin as output
gpio_init(PIN_CS);
gpio_set_dir(PIN_CS, GPIO_OUT);
gpio_put(PIN_CS, 1); // CS high to deselect the MCP2515
}

// Function to initialize CAN communication
void setup_can()
{
// Initialize the MCP2515 driver
mcp2515_init(&mcp2515, SPI_PORT, PIN_CS);

// Reset the MCP2515 to enter configuration mode
mcp2515_reset(&mcp2515);

// Set bit rate to 500kbps
mcp2515_set_bitrate(&mcp2515, MCP_500KBPS);

// Enable normal mode for operation
mcp2515_set_mode(&mcp2515, MCP_MODE_NORMAL);
}

// The CAN task function
void can_task(void *pvParams)
{
uint8_t canCommand; // The general command to observe
TickType_t lastWakeTime; // The last time we got a general command that wasn't idle
const TickType_t tickInterval = pdMS_TO_TICKS(10); // 10 ms interval for checking commands

// Set up SPI and CAN communication
setup_spi();
setup_can();

// Create the CAN command queue
canQueue = xQueueCreate(CAN_QUEUE_LENGTH, sizeof(uint8_t));
if (canQueue == NULL)
{
// Handle error (e.g., print an error message)
printf("Failed to create CAN command queue.\n");
vTaskDelete(NULL); // Delete the task if the queue creation fails
}

// Initialize the last wake time for accurate timing
lastWakeTime = xTaskGetTickCount();

while (true)
{
// Check for new commands in the queue
if (xQueueReceive(canQueue, &canCommand, 0) == pdPASS)
{
// Handle the received command
if (canCommand == CAN_COMMAND_GET_VERSION)
{
// Retrieve the MCP2515 version
uint8_t version = mcp2515_read_register(&mcp2515, MCP_CANSTAT);
printf("MCP2515 Version: 0x%x\n", version);
}
}

// Wait for the next cycle with a short interval to remain responsive
vTaskDelayUntil(&lastWakeTime, tickInterval);
}
}
15 changes: 15 additions & 0 deletions Firmware/src/can_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef CAN_CONTROLLER_H
#define CAN_CONTROLLER_H

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "mcp2515/mcp2515.h"

// Function to initialize and start the CAN task
void can_task(void *pvParams);

// Function to request version info from the MCP2515
void request_can_version(void);

#endif // CAN_CONTROLLER_H
2 changes: 2 additions & 0 deletions Firmware/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "usb_console.h" // USB serial console
#include "command_dispatcher.h" // Functions for dispatching commands
#include "led_task.h" // Manages LED functions
#include "can_controller.h" // Manages the canbus

// Global Definitions
QueueHandle_t cmdQueue = NULL;
Expand All @@ -38,6 +39,7 @@ int main()

// FreeRTOS Create tasks
xTaskCreate(led_task, "LED_Task", 256, NULL, 1, NULL);
xTaskCreate(can_task, "CAN_Task", 512, NULL, 1, NULL);
xTaskCreate(commandDispatcherTask, "CmdDispatcher", 512, NULL, 1, NULL);
xTaskCreate(usb_console, "USB_Console", 1024, NULL, 1, NULL);

Expand Down

0 comments on commit a555641

Please sign in to comment.