@@ -49,6 +49,7 @@ ESP32CAN::ESP32CAN(gpio_num_t rxPin, gpio_num_t txPin) : CAN_COMMON(32)
49
49
twai_general_cfg.tx_io = txPin;
50
50
cyclesSinceTraffic = 0 ;
51
51
initializedResources = false ;
52
+ readyForTraffic = false ;
52
53
twai_general_cfg.tx_queue_len = BI_TX_BUFFER_SIZE;
53
54
twai_general_cfg.rx_queue_len = 6 ;
54
55
rxBufferSize = BI_RX_BUFFER_SIZE;
@@ -69,6 +70,7 @@ ESP32CAN::ESP32CAN() : CAN_COMMON(BI_NUM_FILTERS)
69
70
filters[i].configured = false ;
70
71
}
71
72
initializedResources = false ;
73
+ readyForTraffic = false ;
72
74
cyclesSinceTraffic = 0 ;
73
75
}
74
76
@@ -109,14 +111,20 @@ void CAN_WatchDog_Builtin( void *pvParameters )
109
111
void task_LowLevelRX (void *pvParameters)
110
112
{
111
113
ESP32CAN* espCan = (ESP32CAN*)pvParameters;
114
+
112
115
while (1 )
113
116
{
114
117
twai_message_t message;
115
- if (twai_receive (&message, pdMS_TO_TICKS ( 100 )) == ESP_OK )
118
+ if (espCan-> readyForTraffic )
116
119
{
117
- espCan->processFrame (message);
120
+ if (twai_receive (&message, pdMS_TO_TICKS (100 )) == ESP_OK)
121
+ {
122
+ espCan->processFrame (message);
123
+ }
118
124
}
125
+ else vTaskDelay (pdMS_TO_TICKS (100 ));
119
126
}
127
+
120
128
}
121
129
122
130
/*
@@ -217,19 +225,22 @@ void ESP32CAN::_init()
217
225
218
226
if (!initializedResources)
219
227
{
220
- // printf("Initializing resources for built-in CAN\n");
228
+ if (debuggingMode) printf (" Initializing resources for built-in CAN\n " );
221
229
222
230
// Queue size, item size
223
231
callbackQueue = xQueueCreate (16 , sizeof (CAN_FRAME));
224
232
rx_queue = xQueueCreate (rxBufferSize, sizeof (CAN_FRAME));
233
+ if (debuggingMode) Serial.println (" Created queues." );
225
234
226
235
// func desc stack, params, priority, handle to task
227
236
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. " );
230
239
xTaskCreatePinnedToCore (&CAN_WatchDog_Builtin, " CAN_WD_BI" , 2048 , this , 10 , NULL , 1 );
240
+ if (debuggingMode) Serial.println (" task watchdog created." );
231
241
initializedResources = true ;
232
242
}
243
+ if (debuggingMode) Serial.println (" _init done" );
233
244
}
234
245
235
246
uint32_t ESP32CAN::init (uint32_t ul_baudrate)
@@ -247,9 +258,12 @@ uint32_t ESP32CAN::init(uint32_t ul_baudrate)
247
258
}
248
259
else
249
260
{
250
- printf (" Failed to reconfigure alerts" );
261
+ printf (" Failed to reconfigure alerts" );
251
262
}
252
263
}
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 ;
253
267
return ul_baudrate;
254
268
}
255
269
@@ -259,6 +273,7 @@ uint32_t ESP32CAN::beginAutoSpeed()
259
273
260
274
_init ();
261
275
276
+ readyForTraffic = false ;
262
277
twai_stop ();
263
278
twai_general_cfg.mode = TWAI_MODE_LISTEN_ONLY;
264
279
int idx = 0 ;
@@ -336,10 +351,12 @@ void ESP32CAN::enable()
336
351
printf (" Failed to start TWAI driver\n " );
337
352
return ;
338
353
}
354
+ readyForTraffic = true ;
339
355
}
340
356
341
357
void ESP32CAN::disable ()
342
358
{
359
+ readyForTraffic = false ;
343
360
twai_stop ();
344
361
vTaskDelay (pdMS_TO_TICKS (100 )); // a bit of delay here seems to fix a race condition triggered by task_LowLevelRX
345
362
twai_driver_uninstall ();
0 commit comments