Skip to content

Commit 427b374

Browse files
committed
Fixes to stop race conditions and crashes in esp32 TWAI driver
1 parent a09478a commit 427b374

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/esp32_can_builtin.cpp

+23-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ESP32CAN::ESP32CAN(gpio_num_t rxPin, gpio_num_t txPin) : CAN_COMMON(32)
4949
twai_general_cfg.tx_io = txPin;
5050
cyclesSinceTraffic = 0;
5151
initializedResources = false;
52+
readyForTraffic = false;
5253
twai_general_cfg.tx_queue_len = BI_TX_BUFFER_SIZE;
5354
twai_general_cfg.rx_queue_len = 6;
5455
rxBufferSize = BI_RX_BUFFER_SIZE;
@@ -69,6 +70,7 @@ ESP32CAN::ESP32CAN() : CAN_COMMON(BI_NUM_FILTERS)
6970
filters[i].configured = false;
7071
}
7172
initializedResources = false;
73+
readyForTraffic = false;
7274
cyclesSinceTraffic = 0;
7375
}
7476

@@ -109,14 +111,20 @@ void CAN_WatchDog_Builtin( void *pvParameters )
109111
void task_LowLevelRX(void *pvParameters)
110112
{
111113
ESP32CAN* espCan = (ESP32CAN*)pvParameters;
114+
112115
while (1)
113116
{
114117
twai_message_t message;
115-
if (twai_receive(&message, pdMS_TO_TICKS(100)) == ESP_OK)
118+
if (espCan->readyForTraffic)
116119
{
117-
espCan->processFrame(message);
120+
if (twai_receive(&message, pdMS_TO_TICKS(100)) == ESP_OK)
121+
{
122+
espCan->processFrame(message);
123+
}
118124
}
125+
else vTaskDelay(pdMS_TO_TICKS(100));
119126
}
127+
120128
}
121129

122130
/*
@@ -217,19 +225,22 @@ void ESP32CAN::_init()
217225

218226
if (!initializedResources)
219227
{
220-
//printf("Initializing resources for built-in CAN\n");
228+
if (debuggingMode) printf("Initializing resources for built-in CAN\n");
221229

222230
//Queue size, item size
223231
callbackQueue = xQueueCreate(16, sizeof(CAN_FRAME));
224232
rx_queue = xQueueCreate(rxBufferSize, sizeof(CAN_FRAME));
233+
if (debuggingMode) Serial.println("Created queues.");
225234

226235
//func desc stack, params, priority, handle to task
227236
xTaskCreate(&task_CAN, "CAN_RX", 8192, this, 15, NULL);
228-
//this next task implements our better filtering on top of the TWAI library. Accept all frames then filter in here VVVVV
229-
xTaskCreatePinnedToCore(&task_LowLevelRX, "CAN_LORX", 4096, this, 19, NULL, 1);
237+
if (debuggingMode) Serial.println("task rx created.");
238+
if (debuggingMode) Serial.println("task low level rx created.");
230239
xTaskCreatePinnedToCore(&CAN_WatchDog_Builtin, "CAN_WD_BI", 2048, this, 10, NULL, 1);
240+
if (debuggingMode) Serial.println("task watchdog created.");
231241
initializedResources = true;
232242
}
243+
if (debuggingMode) Serial.println("_init done");
233244
}
234245

235246
uint32_t ESP32CAN::init(uint32_t ul_baudrate)
@@ -247,9 +258,12 @@ uint32_t ESP32CAN::init(uint32_t ul_baudrate)
247258
}
248259
else
249260
{
250-
printf("Failed to reconfigure alerts");
261+
printf("Failed to reconfigure alerts");
251262
}
252263
}
264+
//this task implements our better filtering on top of the TWAI library. Accept all frames then filter in here VVVVV
265+
xTaskCreatePinnedToCore(&task_LowLevelRX, "CAN_LORX", 4096, this, 19, NULL, 1);
266+
readyForTraffic = true;
253267
return ul_baudrate;
254268
}
255269

@@ -259,6 +273,7 @@ uint32_t ESP32CAN::beginAutoSpeed()
259273

260274
_init();
261275

276+
readyForTraffic = false;
262277
twai_stop();
263278
twai_general_cfg.mode = TWAI_MODE_LISTEN_ONLY;
264279
int idx = 0;
@@ -336,10 +351,12 @@ void ESP32CAN::enable()
336351
printf("Failed to start TWAI driver\n");
337352
return;
338353
}
354+
readyForTraffic = true;
339355
}
340356

341357
void ESP32CAN::disable()
342358
{
359+
readyForTraffic = false;
343360
twai_stop();
344361
vTaskDelay(pdMS_TO_TICKS(100)); //a bit of delay here seems to fix a race condition triggered by task_LowLevelRX
345362
twai_driver_uninstall();

src/esp32_can_builtin.h

+2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ class ESP32CAN : public CAN_COMMON
9090
void setCANPins(gpio_num_t rxPin, gpio_num_t txPin);
9191

9292
friend void CAN_WatchDog_Builtin( void *pvParameters );
93+
friend void task_LowLevelRX(void *pvParameters);
9394

9495
protected:
9596
bool initializedResources;
97+
bool readyForTraffic;
9698
int cyclesSinceTraffic;
9799

98100
private:

0 commit comments

Comments
 (0)