Skip to content

Commit

Permalink
Bump pico mcp lib and configure all
Browse files Browse the repository at this point in the history
  • Loading branch information
KenwoodFox committed Sep 13, 2024
1 parent e100ddb commit f09628e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 57 deletions.
2 changes: 2 additions & 0 deletions Firmware/foxtool.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python

# Saw a lot of people using small python scripts to
# help automate the process of getting the uf2 on the rp2040

Expand Down
2 changes: 1 addition & 1 deletion Firmware/lib/pico-mcp2515
10 changes: 6 additions & 4 deletions Firmware/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ include(../pico_sdk_import.cmake)
project(${PROJECT} C CXX)
pico_sdk_init()

# We have to do this manually with the pico-mcp2515 library for.. some reason :/
add_subdirectory(../lib/pico-mcp2515 ${CMAKE_BINARY_DIR}/pico-mcp2515)

add_executable(${ProjectName}
main.cpp
console.cpp
command_dispatcher.cpp
usb_console.cpp
parser.cpp
led_task.cpp
# can_controller.cpp
can_controller.cpp
)

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
pico_mcp2515 # MCP_CAN library
cmake_git_version_tracking # https://github.com/andrew-hardin/cmake-git-version-tracking Very handy library!
)
)


# Setting these will configure USB for serial output and disable uart
Expand Down
60 changes: 15 additions & 45 deletions Firmware/src/can_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
#include "can_controller.h"
#include "pico/stdlib.h"
#include "console.h"
#include "mcp2515.h"
#include "can.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
const uint8_t CAN_QUEUE_LENGTH = 10;

static QueueHandle_t canQueue; // RTOS queue for CAN commands
static MCP2515 mcp2515; // MCP2515 CAN controller instance
static MCP2515 can0; // MCP2515 CAN controller instance
struct can_frame rx;

// Function to request version info from the MCP2515
void request_can_version(void)
void request_can_version(Command_t *cmd)
{
if (canQueue == NULL)
{
Expand All @@ -29,37 +24,13 @@ void request_can_version(void)
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);
// Initialize interface
can0.reset();
can0.setBitrate(CAN_1000KBPS, MCP_16MHZ);
can0.setNormalMode();
}

// The CAN task function
Expand All @@ -69,16 +40,15 @@ void can_task(void *pvParams)
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 communication
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");
console_printf(BROADCAST, "Failed to create CAN command queue.\n");
vTaskDelete(NULL); // Delete the task if the queue creation fails
}

Expand All @@ -94,8 +64,8 @@ void can_task(void *pvParams)
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);
uint8_t version = can0.getStatus();
console_printf(BROADCAST, "MCP2515 Version: 0x%x\n", version);
}
}

Expand Down
9 changes: 7 additions & 2 deletions Firmware/src/can_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "mcp2515/mcp2515.h"
#include "command_dispatcher.h"

enum CANCommands
{
CAN_COMMAND_GET_VERSION
};

// 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);
void request_can_version(Command_t *cmd);

#endif // CAN_CONTROLLER_H
8 changes: 5 additions & 3 deletions Firmware/src/command_dispatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "command_dispatcher.h"
#include "git.h"
#include "can_controller.h"

// Define the command mapping table
const CommandMapping_t commandTable[] = {
{"version", handleVersionCommand}, // Prints the version
{"help", handleHelpCommand}, // Prints the help
{NULL, handleUnknownCommand} // Default handler for unknown commands
{"version", handleVersionCommand}, // Prints the version
{"help", handleHelpCommand}, // Prints the help
{"canstatus", request_can_version}, // Gets the status from the canbus
{NULL, handleUnknownCommand} // Default handler for unknown commands
};

// Simple retrieve version
Expand Down
4 changes: 2 additions & 2 deletions Firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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
#include "can_controller.h" // Manages the canbus

// Global Definitions
QueueHandle_t cmdQueue = NULL;
Expand All @@ -36,7 +36,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(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 f09628e

Please sign in to comment.