Skip to content

Commit cad3c29

Browse files
committed
Unified can
1 parent b9df4ea commit cad3c29

File tree

8 files changed

+138
-30
lines changed

8 files changed

+138
-30
lines changed

inc/sp140/bms.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern bool bmsTwaiInitialized;
2424
// BMS functions
2525
void initBMS();
2626
void updateBMSData();
27+
bool isBMSMessage(uint32_t id);
2728
void parseBMSPacket(const twai_message_t* message);
2829
bool isBMSConnected();
2930
void printBMSData();

inc/sp140/can_bus.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
#include "driver/twai.h"
5+
#include "sp140/structs.h"
6+
7+
// Unified CAN bus management
8+
void initUnifiedCANBus();
9+
void unifiedCANBusTask(void* parameter);
10+
void processCANMessage(const twai_message_t& message);
11+
12+
// External references to ESC functions
13+
extern void processESCMessage(const twai_message_t& message);
14+
extern void processESCTasks();
15+
16+
// Task handle
17+
extern TaskHandle_t canBusTaskHandle;

inc/sp140/esc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
void initESC();
2121
void setESCThrottle(int throttlePWM);
2222
void readESCTelemetry();
23+
void processESCMessage(const twai_message_t& message);
24+
void processESCTasks();
2325
bool setupTWAI();
2426

2527
// Get the highest temperature from all ESC sensors

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ lib_deps =
5050
adafruit/Adafruit ST7735 and ST7789 Library@1.11.0
5151
adafruit/Adafruit NeoPixel@1.14.0
5252
https://github.com/rlogiacco/CircularBuffer@1.4.0
53-
https://github.com/openppg/SINE-ESC-CAN
53+
https://github.com/openppg/SINE-ESC-CAN#4ea824aedba818ee04e301a8bf46f62010e5b363
5454
lvgl/lvgl@^8.3.11
5555
lib_ignore =
5656
Adafruit SleepyDog Library

src/sp140/bms.cpp

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ void initBMS() {
6969
memset(cellVoltages, 0, sizeof(cellVoltages));
7070
}
7171

72+
/**
73+
* Check if this is a BMS message ID
74+
*/
75+
bool isBMSMessage(uint32_t id) {
76+
return (id == BMS_BASIC_INFO_1 || id == BMS_BASIC_INFO_2 || id == BMS_CYCLE_INFO ||
77+
id == BMS_TEMPERATURE ||
78+
(id >= BMS_BATTERY_ID_BASE && id <= (BMS_BATTERY_ID_BASE + 2)) ||
79+
((id >> 24) == 0x18 && ((id >> 8) & 0xFF) == 0x28 &&
80+
((id >> 16) & 0xFF) >= 0xC8 && ((id >> 16) & 0xFF) < (0xC8 + BMS_MAX_CELLS/4)));
81+
}
82+
7283
/**
7384
* Parse BMS CAN packet and update internal data
7485
*/
@@ -173,38 +184,14 @@ bool isBMSConnected() {
173184
}
174185

175186
/**
176-
* Update BMS data by reading from TWAI and processing messages
177-
* This function should be called regularly to check for new BMS messages
178-
* Processes only a limited number of messages per call to avoid blocking
187+
* Update BMS telemetry structure with latest parsed data
188+
* Called by unified CAN bus after BMS messages are processed
179189
*/
180190
void updateBMSData() {
181191
if (!bmsTwaiInitialized) {
182192
return;
183193
}
184194

185-
// Process only a few messages per call to avoid blocking the ESC
186-
int messagesProcessed = 0;
187-
const int maxMessagesPerCall = 10;
188-
189-
twai_message_t message;
190-
while (messagesProcessed < maxMessagesPerCall && twai_receive(&message, 0) == ESP_OK) {
191-
messagesProcessed++;
192-
193-
// Only process messages that could be from BMS
194-
uint32_t id = message.identifier;
195-
196-
// Check if this is a BMS message ID
197-
if (id == BMS_BASIC_INFO_1 || id == BMS_BASIC_INFO_2 || id == BMS_CYCLE_INFO ||
198-
id == BMS_TEMPERATURE ||
199-
(id >= BMS_BATTERY_ID_BASE && id <= (BMS_BATTERY_ID_BASE + 2)) ||
200-
((id >> 24) == 0x18 && ((id >> 8) & 0xFF) == 0x28 &&
201-
((id >> 16) & 0xFF) >= 0xC8 && ((id >> 16) & 0xFF) < (0xC8 + BMS_MAX_CELLS/4))) {
202-
203-
parseBMSPacket(&message);
204-
}
205-
// Other messages are ignored and will be available for ESC processing
206-
}
207-
208195
// Update connection status
209196
bool connected = isBMSConnected();
210197
if (connected != (bmsTelemetryData.bmsState == TelemetryState::CONNECTED)) {

src/sp140/can_bus.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "sp140/can_bus.h"
2+
#include "sp140/bms.h"
3+
#include "sp140/esc.h"
4+
#include "sp140/globals.h"
5+
#include "driver/twai.h"
6+
7+
TaskHandle_t canBusTaskHandle = NULL;
8+
9+
/**
10+
* Initialize the unified CAN bus management system
11+
* This should be called after both ESC and BMS systems are initialized
12+
*/
13+
void initUnifiedCANBus() {
14+
USBSerial.println("Initializing unified CAN bus management...");
15+
16+
// Create the unified CAN bus task with high priority
17+
xTaskCreatePinnedToCore(
18+
unifiedCANBusTask,
19+
"UnifiedCANBus",
20+
4096,
21+
NULL,
22+
6, // High priority to ensure messages are processed quickly
23+
&canBusTaskHandle,
24+
1 // Pin to core 1
25+
);
26+
27+
USBSerial.println("Unified CAN bus task created successfully");
28+
}
29+
30+
/**
31+
* Unified CAN bus task that handles all TWAI message processing
32+
* Routes messages to appropriate systems (BMS or ESC) based on message ID
33+
*/
34+
void unifiedCANBusTask(void* parameter) {
35+
twai_message_t message;
36+
const TickType_t maxWaitTime = pdMS_TO_TICKS(1); // 1ms timeout
37+
38+
USBSerial.println("Unified CAN bus task started");
39+
40+
while (true) {
41+
// Only process if TWAI is initialized
42+
if (!escTwaiInitialized) {
43+
vTaskDelay(pdMS_TO_TICKS(10));
44+
continue;
45+
}
46+
47+
// Process all available messages
48+
while (twai_receive(&message, 0) == ESP_OK) {
49+
processCANMessage(message);
50+
}
51+
52+
// Process ESC tasks (alerts, transmission, stale transfers)
53+
processESCTasks();
54+
55+
// Small delay to prevent task from consuming too much CPU
56+
vTaskDelay(pdMS_TO_TICKS(2));
57+
}
58+
}
59+
60+
/**
61+
* Process a single CAN message and route it to the appropriate system
62+
* @param message The received TWAI message
63+
*/
64+
void processCANMessage(const twai_message_t& message) {
65+
// Check if this is a BMS message
66+
if (isBMSMessage(message.identifier)) {
67+
// Route to BMS system
68+
parseBMSPacket(&message);
69+
70+
#ifdef CAN_DEBUG
71+
USBSerial.printf("BMS message processed: ID=0x%08X\n", message.identifier);
72+
#endif
73+
} else {
74+
// Route to ESC system
75+
processESCMessage(message);
76+
77+
#ifdef CAN_DEBUG
78+
USBSerial.printf("ESC message processed: ID=0x%08X\n", message.identifier);
79+
#endif
80+
}
81+
}

src/sp140/esc.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void readESCTelemetry() {
134134
}
135135
}
136136

137-
adapter.processTxRxOnce(); // Process CAN messages
137+
// Note: CAN message processing is now handled by unified CAN bus task
138138
}
139139

140140
/**
@@ -202,6 +202,22 @@ bool setupTWAI() {
202202
return true;
203203
}
204204

205+
/**
206+
* Process a single ESC message from the unified CAN bus
207+
*/
208+
void processESCMessage(const twai_message_t& message) {
209+
adapter.processRxMessage(message);
210+
}
211+
212+
/**
213+
* Process ESC tasks (alerts, transmission, stale transfers)
214+
*/
215+
void processESCTasks() {
216+
adapter.processAlerts();
217+
adapter.processTx();
218+
adapter.processStaleTransfers();
219+
}
220+
205221
/**
206222
* Debug function to dump ESC throttle response data to serial
207223
* @param res Pointer to the throttle response structure from ESC

src/sp140/sp140.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "../../inc/sp140/esc.h"
2929
#include "../../inc/sp140/lvgl/lvgl_display.h"
3030
#include "../../inc/sp140/bms.h"
31+
#include "../../inc/sp140/can_bus.h"
3132
#include "../../inc/sp140/altimeter.h"
3233
#include "../../inc/sp140/debug.h"
3334

@@ -365,9 +366,9 @@ void spiCommTask(void *pvParameters) {
365366
xQueueOverwrite(escTelemetryQueue, &escTelemetryData); // Always latest data
366367
refreshDisplay();
367368
#else
368-
// 1. Update BMS data if TWAI is initialized
369+
// 1. Update BMS telemetry structure (CAN messages processed by unified CAN bus)
369370
if (bmsTwaiInitialized) {
370-
updateBMSData(); // This function handles TWAI message processing
371+
updateBMSData(); // This function updates telemetry structure only
371372
}
372373

373374
// 2. Use BMS data if connected, otherwise use ESC data
@@ -490,6 +491,9 @@ void setup() {
490491
#ifndef SCREEN_DEBUG
491492
// Initialize BMS on shared TWAI bus (must be after ESC init)
492493
initBMS();
494+
495+
// Initialize unified CAN bus management (must be after both ESC and BMS init)
496+
initUnifiedCANBus();
493497
#endif
494498
eepromSemaphore = xSemaphoreCreateBinary();
495499
xSemaphoreGive(eepromSemaphore);

0 commit comments

Comments
 (0)