initial commit

This commit is contained in:
valentineautos
2025-12-05 09:22:55 +00:00
parent 46949642a2
commit 16ffc2ab10
4033 changed files with 980542 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
set(SRCS "")
list(APPEND SRCS
"src/CANBus_Driver.cpp"
)
set(INCLUDE_DIRS "")
list(APPEND INCLUDE_DIRS "include")
foreach(SRC_DIR IN LISTS SRC_DIRS)
file(GLOB_RECURSE SRC ${SRC_DIR}/*.c)
list(APPEND SRCS ${SRC})
list(APPEND INCLUDE_DIRS ${SRC_DIR}/include)
endforeach()
idf_component_register(
SRCS ${SRCS}
INCLUDE_DIRS ${INCLUDE_DIRS}
REQUIRES driver
PRIV_REQUIRES esp_timer fatfs esp_psram esp_mm
)
target_compile_options(
${COMPONENT_LIB} PRIVATE
-Wno-format
-Wno-int-conversion
-Wno-incompatible-pointer-types
-Wunused-function
-Wno-unused-variable
-Wno-unused-function
-Wno-overflow
-Wno-unused-but-set-variable
-Wno-discarded-qualifiers)

View File

@@ -0,0 +1,23 @@
#ifdef __cplusplus
extern "C" {
#endif
#include "driver/twai.h"
#define CAN_TX_GPIO (gpio_num_t)21
#define CAN_RX_GPIO (gpio_num_t)22
#define CANBUS_SPEED 500000 // 500kbps
#define CAN_QUEUE_LENGTH 32
#define CAN_QUEUE_ITEM_SIZE sizeof(twai_message_t)
#define TAG "TWAI"
extern bool receiving_data;
extern void (*can_message_handler)(twai_message_t *message);
void canbus_init(void);
void start_can_tasks(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,59 @@
#include "CANBus_Driver.h"
QueueHandle_t canMsgQueue;
void receive_can_task(void *arg) {
while (1) {
twai_message_t message;
esp_err_t err = twai_receive(&message, pdMS_TO_TICKS(5)); // lower timeout for faster response
if (err == ESP_OK) {
receiving_data = true;
if (xQueueSend(canMsgQueue, &message, 0) != pdPASS) {
}
vTaskDelay(pdMS_TO_TICKS(1));
// No delay after successful receive
} else if (err == ESP_ERR_TIMEOUT) {
receiving_data = false;
// Minimal delay when idle
vTaskDelay(pdMS_TO_TICKS(1));
} else {
receiving_data = false;
vTaskDelay(pdMS_TO_TICKS(5));
}
}
}
void process_can_queue_task(void *arg) {
twai_message_t message;
while (1) {
if (xQueueReceive(canMsgQueue, &message, pdMS_TO_TICKS(1)) == pdPASS) {
if (can_message_handler) {
can_message_handler(&message);
}
}
vTaskDelay(pdMS_TO_TICKS(1));
}
}
void canbus_init(void) {
// Configure TWAI (CAN)
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(CAN_TX_GPIO, CAN_RX_GPIO, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); // Accept all IDs
// Install and start TWAI driver
twai_driver_install(&g_config, &t_config, &f_config);
twai_start();
// setup TWAI
}
void start_can_tasks(void) {
canMsgQueue = xQueueCreate(CAN_QUEUE_LENGTH, CAN_QUEUE_ITEM_SIZE);
if (canMsgQueue == NULL) {
while (1) vTaskDelay(1000);
}
xTaskCreatePinnedToCore(receive_can_task, "Receive_CAN_Task", 4096, NULL, 2, NULL, 1);
xTaskCreatePinnedToCore(process_can_queue_task, "Process_CAN_Queue_Task", 4096, NULL, 2, NULL, 1);
}