Skip to content

Commit 3cf7608

Browse files
committed
feature: transmit path works (queue-less)
1 parent 73fec9f commit 3cf7608

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ So we end-up with the following features for the moment:
7171

7272
The states and transitions of the ESP32S3 CAN driver are described here: https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/twai.html
7373

74-
### How did the CAN reception work (in the old world with espressif TWAI driver?
74+
### How did the CAN reception work (in the old world with espressif TWAI driver)?
7575

7676
- During startup, the task task_LowLevelRX is started.
7777
- The task_LowLevelRX suspends in twai_receive().
@@ -96,6 +96,16 @@ canBuses[i]->watchFor() --> setRXFilter()
9696
CAN_COMMON::attachCANInterrupt() --> CAN0.setCallback() -->
9797
...tbd...
9898

99+
### Where is the espressif TWAI driver stored?
100+
101+
Source code in the espressif IDE:
102+
- C:\esp-idf-v4.4.4\components\driver\twai.c
103+
104+
Header and lib in the arduino IDE:
105+
- C:\Users\uwemi\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.13\tools\sdk\esp32s3\include\driver\include\driver\twai.h
106+
- C:\Users\uwemi\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.13\tools\sdk\esp32s3\lib\libdriver.a
107+
108+
99109
### Is useBinarySerialComm true?
100110

101111
Yes, the SavvyCAN sends 0xE7 as first two bytes. This turns-on the binary mode.

esp32_can_builtin_local.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ twai_filter_config_t twai_filters_cfg = TWAI_FILTER_CONFIG_ACCEPT_ALL();
3939
#define TWAI_ISR_ATTR
4040
#define TWAI_MALLOC_CAPS MALLOC_CAP_DEFAULT
4141
#endif //CONFIG_TWAI_ISR_IN_IRAM
42+
43+
#define TWAI_CHECK(cond, ret_val) ({ \
44+
if (!(cond)) { \
45+
return (ret_val); \
46+
} \
47+
})
48+
4249
intr_handle_t experimental_isr_handle;
4350
static twai_hal_context_t twai_context;
4451
volatile uint32_t expCounterIsr;
@@ -400,19 +407,39 @@ void ESP32CAN::disable()
400407
//twai_driver_uninstall();
401408
}
402409

410+
esp_err_t my_twai_transmit(const twai_message_t *message) {
411+
/* based on: C:\esp-idf-v4.4.4\components\driver\twai.c,
412+
but without queue. This means: If the application sends
413+
faster than the CAN is able to handle, the message will
414+
be lost. A queue would not improve this situation long term. */
415+
416+
/* Check arguments */
417+
TWAI_CHECK(message != NULL, ESP_ERR_INVALID_ARG);
418+
TWAI_CHECK((message->data_length_code <= TWAI_FRAME_MAX_DLC) || message->dlc_non_comp, ESP_ERR_INVALID_ARG);
419+
420+
/* Format frame */
421+
esp_err_t ret = ESP_FAIL;
422+
twai_hal_frame_t tx_frame;
423+
twai_hal_format_frame(message, &tx_frame);
424+
/* try to send the frame immediately */
425+
twai_hal_set_tx_buffer_and_transmit(&twai_context, &tx_frame);
426+
ret = ESP_OK;
427+
return ret;
428+
}
429+
403430
bool ESP32CAN::sendFrame(CAN_FRAME& txFrame)
404431
{
405432
twai_message_t __TX_frame;
433+
int returnCode;
406434

407435
__TX_frame.identifier = txFrame.id;
408436
__TX_frame.data_length_code = txFrame.length;
409437
__TX_frame.rtr = txFrame.rtr;
410438
__TX_frame.extd = txFrame.extended;
411439
for (int i = 0; i < 8; i++) __TX_frame.data[i] = txFrame.data.byte[i];
412440

413-
//don't wait long if the queue was full. The end user code shouldn't be sending faster
414-
//than the buffer can empty. Set a bigger TX buffer or delay sending if this is a problem.
415-
switch (twai_transmit(&__TX_frame, pdMS_TO_TICKS(4)))
441+
returnCode = my_twai_transmit(&__TX_frame);
442+
switch (returnCode)
416443
{
417444
case ESP_OK:
418445
if (debuggingMode) Serial.write('<');
@@ -424,7 +451,10 @@ bool ESP32CAN::sendFrame(CAN_FRAME& txFrame)
424451
case ESP_FAIL:
425452
case ESP_ERR_INVALID_STATE:
426453
case ESP_ERR_NOT_SUPPORTED:
427-
if (debuggingMode) Serial.write('!');
454+
if (debuggingMode) {
455+
Serial.write('!');
456+
Serial.print(returnCode);
457+
}
428458
break;
429459
}
430460

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SV1,1000,wificanStatistics1,wificanAliveCounter
2+
SV1,1000,wificanStatistics1,wificanTotalRxMessages
3+
SV1,1001,wificanStatistics2,wificanLostRxFrames
4+
SV1,567,FOCCCI01,foccci_uptime_s
5+
SV1,567,FOCCCI01,foccciCheckpoint
6+
SV1,568,foccciFast01,EVSEPresentVoltage
7+
SV1,568,foccciFast01,uCcsInlet_V
8+
SV1,569,foccciTemperatures01,TempCPU
9+
SV1,569,foccciTemperatures01,Temp1
10+
SV1,569,foccciTemperatures01,Temp2
11+
SV1,569,foccciTemperatures01,Temp3

0 commit comments

Comments
 (0)