From 3cf76087a3c3f511d5bbda5d35941817cf404bfb Mon Sep 17 00:00:00 2001 From: uhi22 Date: Thu, 19 Oct 2023 10:38:53 +0200 Subject: [PATCH] feature: transmit path works (queue-less) --- README.md | 12 ++++++- esp32_can_builtin_local.cpp | 38 +++++++++++++++++++--- savvycan_signalview_wifican_and_foccci.sdf | 11 +++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 savvycan_signalview_wifican_and_foccci.sdf diff --git a/README.md b/README.md index 6a520aa..fdf27b6 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ So we end-up with the following features for the moment: 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 -### How did the CAN reception work (in the old world with espressif TWAI driver? +### How did the CAN reception work (in the old world with espressif TWAI driver)? - During startup, the task task_LowLevelRX is started. - The task_LowLevelRX suspends in twai_receive(). @@ -96,6 +96,16 @@ canBuses[i]->watchFor() --> setRXFilter() CAN_COMMON::attachCANInterrupt() --> CAN0.setCallback() --> ...tbd... +### Where is the espressif TWAI driver stored? + +Source code in the espressif IDE: +- C:\esp-idf-v4.4.4\components\driver\twai.c + +Header and lib in the arduino IDE: +- C:\Users\uwemi\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.13\tools\sdk\esp32s3\include\driver\include\driver\twai.h +- C:\Users\uwemi\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.13\tools\sdk\esp32s3\lib\libdriver.a + + ### Is useBinarySerialComm true? Yes, the SavvyCAN sends 0xE7 as first two bytes. This turns-on the binary mode. diff --git a/esp32_can_builtin_local.cpp b/esp32_can_builtin_local.cpp index 24e107e..16c5887 100644 --- a/esp32_can_builtin_local.cpp +++ b/esp32_can_builtin_local.cpp @@ -39,6 +39,13 @@ twai_filter_config_t twai_filters_cfg = TWAI_FILTER_CONFIG_ACCEPT_ALL(); #define TWAI_ISR_ATTR #define TWAI_MALLOC_CAPS MALLOC_CAP_DEFAULT #endif //CONFIG_TWAI_ISR_IN_IRAM + +#define TWAI_CHECK(cond, ret_val) ({ \ + if (!(cond)) { \ + return (ret_val); \ + } \ +}) + intr_handle_t experimental_isr_handle; static twai_hal_context_t twai_context; volatile uint32_t expCounterIsr; @@ -400,9 +407,30 @@ void ESP32CAN::disable() //twai_driver_uninstall(); } +esp_err_t my_twai_transmit(const twai_message_t *message) { + /* based on: C:\esp-idf-v4.4.4\components\driver\twai.c, + but without queue. This means: If the application sends + faster than the CAN is able to handle, the message will + be lost. A queue would not improve this situation long term. */ + + /* Check arguments */ + TWAI_CHECK(message != NULL, ESP_ERR_INVALID_ARG); + TWAI_CHECK((message->data_length_code <= TWAI_FRAME_MAX_DLC) || message->dlc_non_comp, ESP_ERR_INVALID_ARG); + + /* Format frame */ + esp_err_t ret = ESP_FAIL; + twai_hal_frame_t tx_frame; + twai_hal_format_frame(message, &tx_frame); + /* try to send the frame immediately */ + twai_hal_set_tx_buffer_and_transmit(&twai_context, &tx_frame); + ret = ESP_OK; + return ret; +} + bool ESP32CAN::sendFrame(CAN_FRAME& txFrame) { twai_message_t __TX_frame; + int returnCode; __TX_frame.identifier = txFrame.id; __TX_frame.data_length_code = txFrame.length; @@ -410,9 +438,8 @@ bool ESP32CAN::sendFrame(CAN_FRAME& txFrame) __TX_frame.extd = txFrame.extended; for (int i = 0; i < 8; i++) __TX_frame.data[i] = txFrame.data.byte[i]; - //don't wait long if the queue was full. The end user code shouldn't be sending faster - //than the buffer can empty. Set a bigger TX buffer or delay sending if this is a problem. - switch (twai_transmit(&__TX_frame, pdMS_TO_TICKS(4))) + returnCode = my_twai_transmit(&__TX_frame); + switch (returnCode) { case ESP_OK: if (debuggingMode) Serial.write('<'); @@ -424,7 +451,10 @@ bool ESP32CAN::sendFrame(CAN_FRAME& txFrame) case ESP_FAIL: case ESP_ERR_INVALID_STATE: case ESP_ERR_NOT_SUPPORTED: - if (debuggingMode) Serial.write('!'); + if (debuggingMode) { + Serial.write('!'); + Serial.print(returnCode); + } break; } diff --git a/savvycan_signalview_wifican_and_foccci.sdf b/savvycan_signalview_wifican_and_foccci.sdf new file mode 100644 index 0000000..bb44d2e --- /dev/null +++ b/savvycan_signalview_wifican_and_foccci.sdf @@ -0,0 +1,11 @@ +SV1,1000,wificanStatistics1,wificanAliveCounter +SV1,1000,wificanStatistics1,wificanTotalRxMessages +SV1,1001,wificanStatistics2,wificanLostRxFrames +SV1,567,FOCCCI01,foccci_uptime_s +SV1,567,FOCCCI01,foccciCheckpoint +SV1,568,foccciFast01,EVSEPresentVoltage +SV1,568,foccciFast01,uCcsInlet_V +SV1,569,foccciTemperatures01,TempCPU +SV1,569,foccciTemperatures01,Temp1 +SV1,569,foccciTemperatures01,Temp2 +SV1,569,foccciTemperatures01,Temp3