From f09628eb37099a1ac897b09decd393eda9e79afe Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Fri, 13 Sep 2024 17:10:41 -0400 Subject: [PATCH] Bump pico mcp lib and configure all --- Firmware/foxtool.py | 2 + Firmware/lib/pico-mcp2515 | 2 +- Firmware/src/CMakeLists.txt | 10 +++-- Firmware/src/can_controller.cpp | 60 ++++++++--------------------- Firmware/src/can_controller.h | 9 ++++- Firmware/src/command_dispatcher.cpp | 8 ++-- Firmware/src/main.cpp | 4 +- 7 files changed, 38 insertions(+), 57 deletions(-) mode change 100644 => 100755 Firmware/foxtool.py diff --git a/Firmware/foxtool.py b/Firmware/foxtool.py old mode 100644 new mode 100755 index ed6e73a..687567c --- a/Firmware/foxtool.py +++ b/Firmware/foxtool.py @@ -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 diff --git a/Firmware/lib/pico-mcp2515 b/Firmware/lib/pico-mcp2515 index 4506dd0..d8e8e0e 160000 --- a/Firmware/lib/pico-mcp2515 +++ b/Firmware/lib/pico-mcp2515 @@ -1 +1 @@ -Subproject commit 4506dd0659b37b35eaf411de2d93669da471f6c4 +Subproject commit d8e8e0e8f1e1c7ac835c574341a8a2a31edf07d6 diff --git a/Firmware/src/CMakeLists.txt b/Firmware/src/CMakeLists.txt index d9404f6..d6c8639 100644 --- a/Firmware/src/CMakeLists.txt +++ b/Firmware/src/CMakeLists.txt @@ -9,6 +9,9 @@ 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 @@ -16,21 +19,20 @@ add_executable(${ProjectName} 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 diff --git a/Firmware/src/can_controller.cpp b/Firmware/src/can_controller.cpp index fc92c0b..4895a3c 100644 --- a/Firmware/src/can_controller.cpp +++ b/Firmware/src/can_controller.cpp @@ -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) { @@ -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 @@ -69,8 +40,7 @@ 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 @@ -78,7 +48,7 @@ void can_task(void *pvParams) 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 } @@ -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); } } diff --git a/Firmware/src/can_controller.h b/Firmware/src/can_controller.h index 3e052f0..3ac4aa8 100644 --- a/Firmware/src/can_controller.h +++ b/Firmware/src/can_controller.h @@ -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 diff --git a/Firmware/src/command_dispatcher.cpp b/Firmware/src/command_dispatcher.cpp index f2fb056..9e74b5c 100644 --- a/Firmware/src/command_dispatcher.cpp +++ b/Firmware/src/command_dispatcher.cpp @@ -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 diff --git a/Firmware/src/main.cpp b/Firmware/src/main.cpp index bf4ce93..4a1a832 100644 --- a/Firmware/src/main.cpp +++ b/Firmware/src/main.cpp @@ -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; @@ -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);